简体   繁体   中英

How to process return in OpenMP parallel code?

My requirement is like this: every thread allocates memory itself, then processes it:

typedef struct
{
    ......
}A;

A *p[N];

#pragma omp parallel
{
    #pragma omp for
    for (int i = 0; i < N; i++) {
        p[i] = (A*)calloc(sizeof(*p[i]), N);
        if (NULL == p[i]) {
            return;
        }
        ......          
    }
}

But the compiler will complain:

error: invalid exit from OpenMP structured block
     return;

So except put the allocating memory code out of the #pragma omp parallel :

for (int i = 0; i < N; i++) {
    p[i] = (A*)calloc(sizeof(*p[i]), N);
    if (NULL == p[i]) {
        return;
    }       
}
#pragma omp parallel
{
    #pragma omp for
    ......
}

Is there any better method?

You're looking for this, I think:

#pragma omp parallel
{
    #pragma omp for
    for (int i = 0; i < N; i++) {
        p[i] = (A*)calloc(sizeof(*p[i]), N);
        if (NULL == p[i]) {
            #pragma omp cancel for
        }
        ......          
    }
}

But you'll need to set the environment variable OMP_CANCELLATION to true for this to work.

You should try to avoid doing this, though, because cancellation is expensive.

You could try this

omp_set_dynamic(0); //Explicitly turn off dynamic threads
bool cancel = false;    

#pragma omp parallel for schedule(static)
for (int i = 0; i < N; i++) {
    p[i] = (A*)calloc(sizeof(*p[i]),N);
    if (NULL == p[i]) cancel = true;
}
if(cancel) return;
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; i++) {
    ......   
}

This could allocate the memory local to each core/node. I turned off dynamic adjusting the number of threads and used schedule(static) to make sure the threads in the second for loop access the same memory allocated in the first for loop.

I don't know if this solution would be any better. According to this comment it could be worse. It could make a big difference if you have a multi-socket (NUMA) system or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM