-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathompTest.c
More file actions
69 lines (63 loc) · 1.77 KB
/
Copy pathompTest.c
File metadata and controls
69 lines (63 loc) · 1.77 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
#include <omp.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "ompSenseBarr.h"
#include "ompTournamentBarr.h" //wendy
int main(int argc, char **argv)
{
int i, j;
long x = 1;
//allows explicitely setting number of threads below
omp_set_dynamic(0);
//Error checking and set num threads from cmd line argument
if (argc != 3){
fprintf(stderr, "Error. Usage: ompTest [num-threads] [barrier-type]\n"
" [barrier-type]: 0 = no barrier, 1 = sense-reversal barrier, 2 = tournament barrier\n");
exit(1);
}
int numT = atoi(argv[1]);
int barrType = atoi(argv[2]);
if (numT < 1 || numT > 200){
fprintf(stderr, "Error: number of threads must be between 1 and 200.\n");
exit(1);
}
omp_set_num_threads(numT);
//seed random number
srand(time(NULL));
#pragma omp parallel shared(x) private(i,j)
{
int tid = omp_get_thread_num();
int maxInd = (rand() % 3 + 1) * tid * 100;
printf("random quantity work loop starting for thread %d...\n", tid);
//start work
for (i = 1; i <= maxInd; ++i)
{
long y = 1;
for (j= 0; j < maxInd; j++)
{
y = y * 40000;
y = y / 40000;
y++;
}
#pragma omp atomic
x += i;
if (omp_get_thread_num() == 0 && i == 1)
printf("Number of threads = %d\n", omp_get_num_threads());
} //end work
printf("Work completed for thread: %d\n", tid);
//Print the final value of the shared variable, according to each thread
long fin_val;
if (barrType == 1){
ompSenseBarrier();
}
if (barrType == 2){
ompTournamentBarrier();
}
#pragma omp critical
fin_val = x;
printf("final value of shared variable x according to thread %d: %ld\n", tid, fin_val);
}
printf("final value of shared variable x: %ld\n", x);
return 0;
}