【问题标题】:OpenMP code in CUDA source file not compiling on Google ColabCUDA 源文件中的 OpenMP 代码未在 Google Colab 上编译
【发布时间】:2021-05-29 11:22:54
【问题描述】:

我正在尝试使用 OpenMP 库和 CUDA 在 Google Colab 上运行带有 OpenMP 指令的简单 Hello World 程序。我已按照this 教程进行操作,但即使我尝试在我的代码中包含%%cu,我也会收到错误消息。这是我的代码-

%%cu
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>

/* Main Program */
int main(int argc , char **argv)
{
    int             Threadid, Noofthreads;

        printf("\n\t\t---------------------------------------------------------------------------");
        printf("\n\t\t Objective : OpenMP program to print \"Hello World\" using OpenMP PARALLEL directives\n ");
        printf("\n\t\t..........................................................................\n");
 
    /* Set the number of threads */
    /* omp_set_num_threads(4); */ 
    /* OpenMP Parallel Construct : Fork a team of threads */

    #pragma omp parallel private(Threadid)
    {
        /* Obtain the thread id */
        Threadid = omp_get_thread_num();
        printf("\n\t\t Hello World is being printed by the thread : %d\n", Threadid);
    
        /* Master Thread Has Its Threadid 0 */
        if (Threadid == 0) {
            Noofthreads = omp_get_num_threads();
            printf("\n\t\t Master thread printing total number of threads for this execution are : %d\n", Noofthreads);
        }
    }/* All thread join Master thread */
    return 0;
}

这是我得到的错误-

/tmp/tmpxft_00003eb7_00000000-10_15fcc2da-f354-487a-8206-ea228a09c770.o: In function `main':
tmpxft_00003eb7_00000000-5_15fcc2da-f354-487a-8206-ea228a09c770.cudafe1.cpp:(.text+0x54): undefined reference to `omp_get_thread_num'
tmpxft_00003eb7_00000000-5_15fcc2da-f354-487a-8206-ea228a09c770.cudafe1.cpp:(.text+0x78): undefined reference to `omp_get_num_threads'
collect2: error: ld returned 1 exit status

没有 OpenMP 指令,一个简单的 Hello World 程序可以完美运行,如下所示-

%%cu 
#include <iostream> 
int main() 
{ 
    std::cout << "Welcome To GeeksforGeeks\n"; 
    return 0; 
} 

输出-

Welcome To GeeksforGeeks

【问题讨论】:

    标签: jupyter-notebook cuda openmp google-colaboratory nvcc


    【解决方案1】:

    这里有两个问题:

    1. nvcc 不启用或原生支持 OpenMP 编译。这必须通过传递给主机编译器的附加命令行参数来启用(默认为 gcc)
    2. 用于 nvcc 的标准 Google Colab/Jupyter notebook 插件不允许传递额外的编译参数,这意味着即使您解决了第一个问题,它在 Colab 或 Jupyter 中也无济于事。

    您可以按照here 的描述解决第一个问题,您可以按照herehere 的描述解决第二个问题。

    在 Colab 中结合这些让我得到了这个:

    然后是这个:

    【讨论】:

    • 我可以将内容写入文件,如图 1 所示。但是第二张图片给了我一个错误。 nvcc fatal : Value 'sm_75' is not defined for option 'gpu-architecture' /bin/bash: /content/src/omp_cuda: No such file or directory
    • 在这种情况下,您必须使用相当旧的 CUDA 工具包。将其更改为您的实例具有的任何 GPU,或者将其删除
    • 我真的很想知道为什么这被否决了。这是一个社区 wiki 条目——如果有什么不正确的地方可以编辑。无论哪种方式,我都没有声望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2014-03-23
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多