forked from csc-training/summerschool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.cpp
42 lines (35 loc) · 836 Bytes
/
tasks.cpp
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 <cstdio>
#include <omp.h>
int main(void)
{
int array[4] = {0, 0, 0, 0};
int tid;
printf("Array at the beginning: ");
for (int i=0; i < 4; i++) {
printf("%d ", array[i]);
}
printf("\n");
// TODO: launch threads and create tasks so that there
// one task per loop iteration
#pragma omp parallel private(tid)
#pragma omp single
{
tid = omp_get_thread_num();
printf("Tasks created by thread: %d\n", tid);
for (int i=0; i < 4; i++) {
#pragma omp task if (i<3)
{
tid = omp_get_thread_num();
printf("Task %d executed by thread %d\n", i, tid);
array[i] += tid;
}
}
}
// TODO end
printf("Array at the end: ");
for (int i=0; i < 4; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}