c++ - 如何在通过 nvcc 传递到 VS2015 的文件上启用 OMP?

标签 c++ cuda openmp

标题几乎概括了它。我想在必须首先通过 nvcc 的文件中为主机代码启用 openMP。如果下面是“test.cu”

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

int main()
{
#pragma omp parallel
    {
        std::cout << "Hi " << omp_get_thread_num()+1 << "/" << omp_get_num_threads() << "!\n";
    }
    return 0;
}

我在 Ubuntu 16.04 上使用 CUDA 8 (nvcc 8.0.44) 和 g++ 5.4.0 (nvcc -O2 test.cu -o testCu -std=c++11 -Xcompiler -fopenmp && ./testCu) 我得到(如预期的那样):

Hi 2/3!
Hi 1/3!
Hi 3/3!

但是,使用 VS2015 我得到:

Hi 0/1!

而且,是的,我启用了 /openmp(如果我将名称更改为 test.cpp 并将编译器更改为 cl,它会正常工作)。

最佳答案

正如 talonmies 指出的那样和 Robert Crovella ,我们必须通过 nvcc 显式地将 /openmp 标志传递给 cl。使用命令行时,这很简单。使用 Visual Studio 时,需要在属性页中添加 cl 的所有参数。打开项目或特定 .cu 文件的属性。配置属性 > CUDA C/C++ > 主机。将命令行选项 (/openmp) 添加到附加编译器选项字段。

进行健全性检查并查看 CUDA C/C++ > 命令行表明它没有出现,但是通过检查输出窗口我们看到有两个 -Xcompiler 传递的参数,一个是出现在 CUDA C/C++ > 命令行中的参数(例如 -Xcompiler "/EHsc/W3/nologo/O2/FS/Zi/MD "),另一个是手动添加的命令(例如 -Xcompiler/openmp)。

关于c++ - 如何在通过 nvcc 传递到 VS2015 的文件上启用 OMP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40999104/

相关文章:

c++ - 进度条颜色

c++ - 访问 CUDA 中的共享内存

c - 使用循环平铺和 OpenMP 进行优化

c++ - OpenMP 与 boost::directory_iterator 并行

c++ - 将业务逻辑迁移到服务 : alternatives to Thrift

C++ 标准 : end of lifetime

c++ - 为什么这是内存复制错误 - Insure++ 误报?

cuda - 让 CUB blockradixsort 完全在片上进行?

cuda - GPU MD5/SHA1 哈希器

C++ OpenMP exit while loop inside a parallel for