I have a stack. Each time I pop one element from it, handle this element and determine whether the element should be pushed back to the stack according to some results.
The code is like the following. I used the OpenMP task construct to achieve parallelism because the handling processes of different elements are independent. But I got error: expected ‘#pragma omp’ clause before ‘{’ token
for both the lines of #pragma omp parallel
and #pragma omp single
. I have no idea about the reasons. And I am also curious about the correctness of my usage of the OpenMP task construct.
int processing_ele;
omp_set_num_threads(2);
#pragma omp parallel {
#pragma omp single {
while (!stack_ele.empty()) {
// pop
int processing_ele = stack_edge_id.top();
stack_ele.pop();
// handle
#pragma omp task
Test(processing_ele);
if (global_res[processing_ele].need_to_push) {
stack_ele.push(processing_elec);
}
}
}
}
In OpenMP specification you can read
The syntax of an OpenMP directive is as follows:
#pragma omp directive-name [clause[ [,] clause] ... ] new-line
Please observe the new line at the end of the directive, so {
have to be in a new line:
#pragma omp parallel
{
#pragma omp single
{
or
#pragma omp parallel
#pragma omp single
{