-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
42 lines (38 loc) · 836 Bytes
/
test.c
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
#include <math.h>
#include <stdio.h>
#include <omp.h>
#define ENDOFLOOP 2000000
#define TRUE 1
#define FALSE 0
//int x;
//#pragma omp threadprivate(x)
short cost(int x)
{
int i, n_iter = 20;
double dcost = 0;
for(i = 0; i < n_iter; i++)
dcost += pow(sin((double) x),2) + pow(cos((double) x),2);
return (short) (dcost / n_iter + 0.1);
}
/*
* To run with OpenMP, run ./test p
* Otherwise will run sequentially
*/
void main(int argc, char *argv[]) {
int i;
if(argc == 2 && !strcmp(argv[1], "p")) {
#pragma omp parallel
{
//x = omp_get_thread_num();
#pragma omp for
for(i = 0; i < ENDOFLOOP ; i++) {
cost(i);
//printf("Running thread %d\n", tid);
}
}
} else {
for(i = 0; i < ENDOFLOOP ; i++) {
cost(i);
}
}
}