【问题标题】:Can I use `omp_get_thread_num()` on the GPU?我可以在 GPU 上使用 `omp_get_thread_num()` 吗?
【发布时间】:2017-12-23 19:30:07
【问题描述】:

我有在 CPU 上运行的 OpenMP 代码,通过让每个线程管理由线程 ID 号寻址的内存,可通过omp_get_thread_num() 访问。这在 CPU 上运行良好,但它可以在 GPU 上运行吗?

MWE 是:

#include <iostream>
#include <omp.h>

int main(){
  const int SIZE = 400000;

  int *m;
  m = new int[SIZE];

  #pragma omp target
  {
    #pragma omp parallel for
    for(int i=0;i<SIZE;i++)
      m[i] = omp_get_thread_num();
  }

  for(int i=0;i<SIZE;i++)
    std::cout<<m[i]<<"\n";
}

【问题讨论】:

    标签: g++ gpu openmp pgi offloading


    【解决方案1】:

    使用 GCC 在 GPU 上运行良好。您需要映射m,例如像这样

    #pragma omp target map(tofrom:m[0:SIZE])
    

    我是这样编译的

    g++ -O3 -Wall -fopenmp -fno-stack-protector so.cpp
    

    您可以在此处查看不卸载系统的示例

    http://coliru.stacked-crooked.com/a/1e756410d6e2db61

    我在工作之前用来找出团队和线程数的方法是这样的:

    #pragma omp target teams defaultmap(tofrom:scalar)
    {
        nteams = omp_get_num_teams();
        #pragma omp parallel
        #pragma omp single
        nthreads = omp_get_num_threads();
    }
    

    在我的 GCC 7.2、Ubuntu 17.10 和 gcc-offload-nvptx 和 GTX 1060 的系统上,我得到 nteams = 30nthreads = 8。请参阅this answer,我在其中使用线程和团队对目标区域进行自定义缩减。使用-offload=disablenteams = 1nthreads = 8(4 核/8 硬件线程 CPU)。


    我在编译选项中添加了-fopt-info,但我只收到消息

    note: basic block vectorized
    

    【讨论】:

      【解决方案2】:

      答案似乎是

      使用 PGI 编译:

      pgc++ -fast -mp -ta=tesla,pinned,cc60 -Minfo=all test2.cpp
      

      给予:

      13, Parallel region activated
          Parallel loop activated with static block schedule
          Loop not vectorized/parallelized: contains call
      14, Parallel region terminated
      

      而使用 GCC 编译

      g++ -O3 test2.cpp -fopenmp -fopt-info
      

      给予

      test2.cpp:17: note: not vectorized: loop contains function calls or data references that cannot be analyzed
      test2.cpp:17: note: bad data references.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 2018-01-21
        • 2019-06-26
        • 2021-05-04
        • 2020-10-03
        • 1970-01-01
        相关资源
        最近更新 更多