OpenMP and Thread Local Storage identifier with icc

霸气de小男生 提交于 2019-12-02 05:36:03

问题


This is a simple test code:

#include <stdlib.h>

__thread int a = 0;

int main() {

    #pragma omp parallel default(none)
    {
        a = 1;
    }

    return 0;
}

gcc compiles this without any problems with -fopenmp, but icc (ICC) 12.0.2 20110112 with -openmp complains with

test.c(7): error: "a" must be specified in a variable list at enclosing OpenMP parallel pragma #pragma omp parallel default(none)

I have no clue which paradigm (i.e. shared, private, threadprivate) applies to this type of variables. Which one is the correct one to use?

I get the expected behaviour when calling a function that accesses that thread local variable, but I have trouble accessing it from within an explicit parallel section.

Edit:

My best solution so far is to return a pointer to the variable through a function

static inline int * get_a() { return &a; }

回答1:


__thread is roughly analogous to the effect that the threadprivate OpenMP directive has. To a great extent (read as when no C++ objects are involved), both are often implemented using the same underlying compiler mechanism and therefore are compatible but this is not guaranteed to always work. Of course, the real world is far from ideal and we have to sometimes sacrifice portability for just having things working within the given development constraints.

threadprivate is a directive and not a clause, therefore you have to do something like:

#include "header_providing_a.h"

#pragma omp threadprivate(a)

void parallel_using_a()
{
   #pragma omp parallel default(none) ...
     ... use 'a' here
}

GCC (at least version 4.7.1) treats __thread as implicit threadprivate declaration and you don't have to do anything.



来源:https://stackoverflow.com/questions/19980937/openmp-and-thread-local-storage-identifier-with-icc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!