1 // RUN: %libomp-cxx-compile-and-run
10 The test emulates code generation needed for reduction with task modifier on
13 Note: tasks could just use in_reduction clause, but compiler does not accept
14 this because of bug: it mistakenly requires reduction item to be shared, which
15 is only true for reduction on worksharing and wrong for task reductions.
18 //------------------------------------------------
19 // OpenMP runtime library routines
23 extern void *__kmpc_task_reduction_get_th_data(int gtid
, void *tg
, void *item
);
24 // extern void* __kmpc_task_reduction_modifier_init(void *loc, int gtid, int
25 // is_ws, int num, void* data);
26 extern void *__kmpc_taskred_modifier_init(void *loc
, int gtid
, int is_ws
,
28 extern void __kmpc_task_reduction_modifier_fini(void *loc
, int gtid
, int is_ws
);
29 extern int __kmpc_global_thread_num(void *);
34 //------------------------------------------------
35 // Compiler-generated code
37 typedef struct red_input
{
38 void *reduce_shar
; /**< shared between tasks item to reduce into */
39 void *reduce_orig
; /**< original reduction item used for initialization */
40 size_t reduce_size
; /**< size of data item in bytes */
41 // three compiler-generated routines (init, fini are optional):
42 void *reduce_init
; /**< data initialization routine (single parameter) */
43 void *reduce_fini
; /**< data finalization routine */
44 void *reduce_comb
; /**< data combiner routine */
45 unsigned flags
; /**< flags for additional info from compiler */
48 void i_comb(void *lhs
, void *rhs
) { *(int *)lhs
+= *(int *)rhs
; }
52 int *p_var_orig
= &var
;
54 omp_set_num_threads(NT
);
55 // #pragma omp parallel reduction(task,+:var)
56 #pragma omp parallel reduction(+ : var) shared(p_var_orig)
58 int gtid
= __kmpc_global_thread_num(NULL
);
59 void *tg
; // pointer to taskgroup (optional)
61 r_var
.reduce_shar
= &var
;
63 p_var_orig
; // not used in this test but illustrates codegen
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_taskred_modifier_init(
71 0, // 1 - worksharing construct, 0 - parallel
72 1, // number of reduction objects
73 &r_var
// related data
76 #pragma omp task /*in_reduction(+:var)*/ shared(var)
78 int gtid
= __kmpc_global_thread_num(NULL
);
79 int *p_var
= (int *)__kmpc_task_reduction_get_th_data(gtid
, tg
, &var
);
82 if (omp_get_thread_num() > 0) {
83 #pragma omp task /*in_reduction(+:var)*/ shared(var)
85 int gtid
= __kmpc_global_thread_num(NULL
);
86 int *p_var
= (int *)__kmpc_task_reduction_get_th_data(gtid
, tg
, &var
);
90 __kmpc_task_reduction_modifier_fini(NULL
, gtid
, 0);
92 if (var
== INIT
+ NT
* 3 - 1) {
96 printf("failed: var = %d (!= %d)\n", var
, INIT
+ NT
* 3 - 1);