-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththFramework.c
More file actions
368 lines (311 loc) · 5.19 KB
/
thFramework.c
File metadata and controls
368 lines (311 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "thFramework.h"
#define DEZ 10
int aleatorio (int range, int teste)
{
int x;
if (teste)
{
while (range < 2)
{
printf("função random: o range %d passado é inválido, por favor digite um valor < 2 para range:\n", range);
scanf("%d", &range);
}
}
else if (range < 2)
{
range = DEZ;
}
while (x >= (RAND_MAX - (RAND_MAX % range)))
{
srand(time(NULL));
x = rand();
}
x %= range;
return x;
}
void *alocaVetor (size_t tam, size_t size)
{
void *vetor;
if (!(vetor = calloc(tam, size)))
{
printf("função alocaVetor: erro ao alocar memória do vetor com calloc\n");
exit(-1);
}
return vetor;
}
void **alocaMatriz (size_t n, size_t m, size_t size)
{
int i;
void **matriz;
if (!(matriz = calloc(n, sizeof(void*))))
{
printf("função alocaMatriz: erro ao alocar espaço das linhas na memória\n");
exit(-1);
}
for (i = 0; i < n; ++i)
{
if (!(matriz[i] = calloc(m, size)))
{
printf("função alocaMatriz: erro ao alocar espaço da coluna %d na memória\n", i);
exit(-1);
}
}
return matriz;
}
void barreira (int qtdThreads)
{
static int threadsCount = 0;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t condition;
pthread_mutex_lock(&lock);
threadsCount++;
if (threadsCount < qtdThreads)
{
// printf("Aguarde a largada...\n");
pthread_cond_wait(&condition, &lock); // libera o lock (momentaneamente) e espera por um sinal para continuar
}
else
{
// printf("É dada a largada\n");
threadsCount = 0;
pthread_cond_broadcast(&condition); // libera todas as threads que estejam esperando um sinal para sair do wait
}
pthread_mutex_unlock(&lock);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&condition);
}
int calcNucleosProcess (void)
{
return sysconf(_SC_NPROCESSORS_ONLN);
}
int calcTamNumeros (int *numeros, int qtdNumeros)
{
int i, num, tam = 0;
for (i = 0; i < qtdNumeros; ++i)
{
num = numeros[i];
while (num)
{
tam++;
num /= 10;
}
}
return tam;
}
double calculaPi (int qtdParcelasPrecisao)
{
int i;
double pi = 0;
if (qtdParcelasPrecisao < 0)
{
qtdParcelasPrecisao = DEZ;
}
for (i = 0; i < qtdParcelasPrecisao; ++i)
{
pi += pow(-1, i)/(2*i+1);
}
pi *= 4;
return pi;
}
void comoUsar(char* progName, char* argumentos)
{
fprintf(stderr, "como usar: ./%s %s\n", progName, argumentos);
exit(0);
}
int defineQtdThreads (int n)
{
if (n > 0)
{
return n;
}
else
{
return calcNucleosProcess();
}
}
double getMaiorDouble (double n1, double n2)
{
if (n1 > n2)
{
return n1;
}
else
{
return n2;
}
}
float getMaiorFloat (float n1, float n2)
{
if (n1 > n2)
{
return n1;
}
else
{
return n2;
}
}
int getMaiorInteiro (int n1, int n2)
{
if (n1 > n2)
{
return n1;
}
else
{
return n2;
}
}
void imprimeVetorDouble (double *vetor, int tam)
{
int i;
for (i = 0; i < tam; ++i)
{
printf("%e\t", vetor[i]);
}
printf("\n");
}
void imprimeVetorFloat (float *vetor, int tam)
{
int i;
for (i = 0; i < tam; ++i)
{
printf("%f\t", vetor[i]);
}
printf("\n");
}
void imprimeVetorInteiro (int *vetor, int tam)
{
int i;
for (i = 0; i < tam; ++i)
{
printf("%d\t", vetor[i]);
}
printf("\n");
}
void iniVetorDouble (double *vetor, int tam, double valorInicial)
{
int i;
for (i = 0; i < tam; ++i)
{
vetor[i] = valorInicial;
}
}
void iniVetorFloat (float *vetor, int tam, float valorInicial)
{
int i;
for (i = 0; i < tam; ++i)
{
vetor[i] = valorInicial;
}
}
void iniVetorInteiro (int *vetor, int tam, int valorInicial)
{
int i;
for (i = 0; i < tam; ++i)
{
vetor[i] = valorInicial;
}
}
void liberaMemo (void *ptr)
{
if (ptr)
{
free(ptr);
ptr = NULL;
}
}
void printaMatrizDouble (double **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; ++j)
{
printf("%e\t", matriz[i][j]);
}
printf("\n");
}
}
void printaMatrizFloat (float **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; ++j)
{
printf("%f\t", matriz[i][j]);
}
printf("\n");
}
}
void printaMatrizInteiro (int **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; ++j)
{
printf("%d\t", matriz[i][j]);
}
printf("\n");
}
}
void setMatrizDouble (double **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
scanf("%lf", &matriz[i][j]);
}
}
}
void setMatrizFloat (float **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
scanf("%f", &matriz[i][j]);
}
}
}
void setMatrizInteiro (int **matriz, int n, int m)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
scanf("%d", &matriz[i][j]);
}
}
}
void threadCreate (pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void*), void *arg)
{
int id;
if ((id = pthread_create(thread, attr, *start_routine, arg)))
{
printf("--ERRO: pthread_create()\n");
printf("error number %d\n", id);
exit(-1);
}
}
void threadJoin (int qtdThreads, pthread_t *threads)
{
int i, erro;
for (i = 0; i < qtdThreads; ++i)
{
// printf("thread%d\n", i);
if ((erro = pthread_join(threads[i], NULL)))
{
printf("--ERRO: pthread_join()\n");
printf("error number %d\n", erro);
exit(-1);
}
}
}