-
Notifications
You must be signed in to change notification settings - Fork 3
/
openmp_if.c
77 lines (65 loc) · 1.54 KB
/
openmp_if.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
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
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define THRESHOLD 200
void test_parallel(int *A, int *B, int *C, int NT, int NE)
{
int i = 0;
int parallel = 1;
double start = omp_get_wtime();
#pragma omp parallel if (NE > THRESHOLD)
{
if (omp_in_parallel())
{
#pragma omp for
for( i = 0; i < NE; i++) {
C[i] = A[i] * B[i];
}
#pragma omp single
printf("parallel \n");
}
else
{
for( i = 0; i < NE; i++) {
C[i] = A[i] * B[i];
}
parallel = 0;
}
}
double end = omp_get_wtime();
if(1 == parallel) {
printf("The program was run in parallel with walltime %f seconds\n ", end - start);
}
else {
printf("The program was run in serial with walltime %f seconds\n ", end - start);
}
}
int main(int argc, char* argv[] )
{
int NT, NE;
int *a = NULL;
int *b = NULL;
int *c = NULL;
if (argc != 3) {
printf(" %s Usage: ./openmp_if 4 10 \n", argv[0]);
return -1;
}
else {
NT = atoi(argv[1]);
NE = atoi(argv[2]);
}
omp_set_num_threads(NT);
a = malloc(NE * sizeof(int));
b = malloc(NE * sizeof(int));
c = malloc(NE * sizeof(int));
srand(time(NULL));
int i = 0;
for( i = 0; i < NE; i++) {
a[i] = rand() % 10;
b[i] = rand() % 10;
c[i] = 0;
}
test_parallel(a, b, c, NT, NE);
}