-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_flush.cpp
66 lines (57 loc) · 1.67 KB
/
base_flush.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*!
* \brief Records the basic usage of flush.
* \operation flush: It makes a thread's temporary
* view of memory consistent with memory, and enforces
* an order on the memory operations of the variables.
*/
#include <omp.h>
#include <stdio.h>
void process(int *data, int len) {
for (int i = 0; i < len; i++) {
data[i] += 1;
}
}
int main() {
int len = 100;
int *data = new int[len];
int flag = 0;
#pragma omp parallel sections num_threads(2)
{
#pragma omp section
{
printf("<%d> Initialize data: \n", omp_get_thread_num());
for (int i = 0; i < len; i++)
data[i] = 1;
// flush also means refresh.
// If var is not specified, all memory is flushed.
// After executing this command, the memory of data will be flushed
// and synchronized with the real memory. Then all of the threads will
// see the same data.
// Note: Not executing this sentence will also synchronize,
// but it may not be in time
#pragma omp flush
flag = 1;
#pragma omp flush(flag)
// You can do more work here.
printf("Finish section 0.\n");
}
#pragma omp section
{
// Check the flag.
while (!flag) {
#pragma omp flush(flag)
}
// If the flag has been synchronized, the data has been initialized.
// But it may not be synchronized, so emphasize it.
#pragma omp flush
// Process data from thread 0.
printf("<%d> Process data: \n", omp_get_thread_num());
process(data, len);
for (int i = 0; i < len; i++) {
printf("%d, ", data[i]);
}
printf("Finish section 1.\n");
}
}
delete data;
}