1// RUN: %libomp-cxx-compile-and-run
2
3#include <stdio.h>
4#include <omp.h>
5
6#define NT 4
7#define INIT 10
8
9/*
10The test emulates code generation needed for reduction with task modifier on
11parallel construct.
12
13Note: tasks could just use in_reduction clause, but compiler does not accept
14this because of bug: it mistakenly requires reduction item to be shared, which
15is only true for reduction on worksharing and wrong for task reductions.
16*/
17
18//------------------------------------------------
19// OpenMP runtime library routines
20#ifdef __cplusplus
21extern "C" {
22#endif
23extern void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void *item);
24extern void *__kmpc_task_reduction_modifier_init(void *loc, int gtid, int is_ws,
25 int num, void *data);
26extern void __kmpc_task_reduction_modifier_fini(void *loc, int gtid, int is_ws);
27extern int __kmpc_global_thread_num(void *);
28#ifdef __cplusplus
29}
30#endif
31
32//------------------------------------------------
33// Compiler-generated code
34
35typedef struct red_input {
36 void *reduce_shar; /**< shared between tasks item to reduce into */
37 size_t reduce_size; /**< size of data item in bytes */
38 // three compiler-generated routines (init, fini are optional):
39 void *reduce_init; /**< data initialization routine (single parameter) */
40 void *reduce_fini; /**< data finalization routine */
41 void *reduce_comb; /**< data combiner routine */
42 unsigned flags; /**< flags for additional info from compiler */
43} red_input_t;
44
45void i_comb(void *lhs, void *rhs) { *(int *)lhs += *(int *)rhs; }
46
47int main() {
48 int var = INIT;
49 int i;
50 omp_set_dynamic(0);
51 omp_set_num_threads(NT);
52#pragma omp parallel private(i)
53// #pragma omp for reduction(task,+:var)
54#pragma omp for reduction(+ : var)
55 for (i = 0; i < NT; ++i) // single iteration per thread
56 {
57 // generated code, which actually should be placed before
58 // loop iterations distribution, but placed here just to show the idea,
59 // and to keep correctness the loop count is equal to number of threads
60 int gtid = __kmpc_global_thread_num(NULL);
61 void *tg; // pointer to taskgroup (optional)
62 red_input_t r_var;
63 r_var.reduce_shar = &var;
64 r_var.reduce_size = sizeof(var);
65 r_var.reduce_init = NULL;
66 r_var.reduce_fini = NULL;
67 r_var.reduce_comb = (void *)&i_comb;
68 tg = __kmpc_task_reduction_modifier_init(
69 NULL, // ident_t loc;
70 gtid,
71 is_ws: 1, // 1 - worksharing construct, 0 - parallel
72 num: 1, // number of reduction objects
73 data: &r_var // related data
74 );
75 // end of generated code
76 var++;
77#pragma omp task /*in_reduction(+:var)*/ shared(var)
78 {
79 // emulate task reduction here because of compiler bug:
80 // it mistakenly declines to accept in_reduction because var is private
81 // outside.
82 int gtid = __kmpc_global_thread_num(NULL);
83 int *p_var = (int *)__kmpc_task_reduction_get_th_data(gtid, tg, item: &var);
84 *p_var += 1;
85 }
86 if (omp_get_thread_num() > 0) {
87#pragma omp task /*in_reduction(+:var)*/ shared(var)
88 {
89 int gtid = __kmpc_global_thread_num(NULL);
90 int *p_var = (int *)__kmpc_task_reduction_get_th_data(gtid, tg, item: &var);
91 *p_var += 1;
92 }
93 }
94 // generated code, which actually should be placed after loop completion
95 // but before barrier and before loop reduction. It placed here just to show
96 // the idea,
97 // and to keep correctness the loop count is equal to number of threads
98 __kmpc_task_reduction_modifier_fini(NULL, gtid, is_ws: 1);
99 // end of generated code
100 }
101 if (var == INIT + NT * 3 - 1) {
102 printf(format: "passed\n");
103 return 0;
104 } else {
105 printf(format: "failed: var = %d (!= %d)\n", var, INIT + NT * 3 - 1);
106 return 1;
107 }
108}
109

source code of openmp/runtime/test/tasking/kmp_task_modifier_simple_ws_old.cpp