-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
40 lines (33 loc) · 995 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
#ifdef USE_OPENMP
#include "omp.h"
#endif
#include "stdio.h" // import printf
#include "stdlib.h" // import size_t
float myadder(float x, float y) {
return x + y;
}
void add_array_f(size_t row_count, size_t column_count, float *a, float *b, float *result) {
size_t idx, i, j;
#ifdef USE_OPENMP
#pragma omp parallel for private(j) collapse(2)
#endif
for(i = 0; i < row_count; i++) {
for(j = 0; j < column_count; j++) {
idx = column_count * i + j;
result[idx] = a[idx] + b[idx];
}
}
}
void add_array_i(size_t row_count, size_t column_count, int *a, int *b, int *result) {
size_t idx;
// collapse(2) needed to parallelize both loops
#ifdef use_openmp
#pragma omp parallel for collapse(2)
#endif
for(size_t i = 0; i < row_count; i++) {
for(size_t j = 0; j < column_count; j++) {
idx = column_count * i + j;
result[idx] = a[idx] + b[idx];
}
}
}