forked from OpenMP/Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_cancellation.2.c
More file actions
60 lines (58 loc) · 1.47 KB
/
Example_cancellation.2.c
File metadata and controls
60 lines (58 loc) · 1.47 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
/*
* @@name: cancellation.2c
* @@type: C
* @@compilable: yes
* @@linkable: no
* @@expect: success
*/
#include <stddef.h>
typedef struct binary_tree_s {
int value;
struct binary_tree_s *left, *right;
} binary_tree_t;
binary_tree_t *search_tree(binary_tree_t *tree, int value, int level) {
binary_tree_t *found = NULL;
if (tree) {
if (tree->value == value) {
found = tree;
}
else {
#pragma omp task shared(found) if(level < 10)
{
binary_tree_t *found_left = NULL;
found_left = search_tree(tree->left, value, level + 1);
if (found_left) {
#pragma omp atomic write
found = found_left;
#pragma omp cancel taskgroup
}
}
#pragma omp task shared(found) if(level < 10)
{
binary_tree_t *found_right = NULL;
found_right = search_tree(tree->right, value, level + 1);
if (found_right) {
#pragma omp atomic write
found = found_right;
#pragma omp cancel taskgroup
}
}
#pragma omp taskwait
}
}
return found;
}
binary_tree_t *search_tree_parallel(binary_tree_t *tree, int value) {
binary_tree_t *found = NULL;
#pragma omp parallel shared(found, tree, value)
{
#pragma omp master
{
#pragma omp taskgroup
{
found = search_tree(tree, value, 0);
}
}
}
return found;
}