CUDA 源文件中的 OpenMP 代码未在 Google Colab 上编译

OpenMP code in CUDA source file not compiling on Google Colab

我正在尝试 运行 使用 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

这里有两个问题:

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

您可以解决第一个问题 here, and you can solve the second as described and

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

然后是这个: