-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (60 loc) · 1.73 KB
/
main.cpp
File metadata and controls
79 lines (60 loc) · 1.73 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
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <omp.h>
#define N 10000
#define CHUNKSIZE 100
using namespace std;
int main(int argc, char *argv[])
{
omp_set_num_threads(137);
#pragma omp parallel for
for (unsigned int i = 0; i < 10; i++) {
#pragma omp critical
cout << "Hello from thread #" << omp_get_thread_num() << " (nthreads=" << omp_get_num_threads() << ")"<< endl;
}
// example from https://wvuhpc.github.io/2018-Lesson_4/03-openmp/index.html
double a[N], b[N], c[N], d[N];
for (unsigned int i = 0; i < N; i++) {
a[i] = i;
b[i] = i * N;
}
unsigned int chunk = CHUNKSIZE;
//for loop pattern
// #pragma omp parallel for schedule(dynamic,chunk)
// for (unsigned int i = 0; i < N; i++) {
// #pragma omp critical
// c[i] = a[i] + b[i];
// }
//sections pattern
#pragma omp parallel
{
#pragma omp sections nowait
{
#pragma omp section
{
printf("Thread: %d working on + section\n", omp_get_thread_num());
for (unsigned int i = 0; i < N; i++) {
#pragma omp critical
c[i] = a[i] + b[i];
}
}
#pragma omp section
{
printf("Thread: %d working on * section\n", omp_get_thread_num());
for(unsigned int i = 0; i < N; i++) {
#pragma omp critical
d[i] = a[i] * b[i];
}
}
}
}
cout << "Block 1:" << endl;
for (unsigned int i = 0; i < 10; i++){
printf("%17.1f %17.1f %17.1f\n", a[i], b[i], c[i]);
}
cout << endl;
cout << "Block 2:" << endl;
for (int i = N-10; i < N; i++){
printf("%17.1f %17.1f %17.1f\n", a[i], b[i], c[i]);
}
cout << endl;
}