0

I am trying to run a simple Hello World program with OpenMP directives on Google Colab using OpenMP library and CUDA. I have followed this tutorial but I am getting an error even if I am trying to include %%cu in my code. This is my code-

%%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;
}

And this is the error I am getting-

/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

Without OpenMP directives, a simple Hello World program is running perfectly as can be seen below-

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

Output-

Welcome To GeeksforGeeks
talonmies
  • 70,661
  • 34
  • 192
  • 269
PeakyBlinder
  • 1,059
  • 1
  • 14
  • 35

1 Answers1

2

There are two problems here:

  1. nvcc doesn't enable or natively support OpenMP compilation. This has to be enabled by additional command line arguments passed through to the host compiler (gcc by default)
  2. The standard Google Colab/Jupyter notebook plugin for nvcc doesn't allow passing of extra compilation arguments, meaning that even if you solve the first issue, it doesn't help in Colab or Jupyter.

You can solve the first problem as described here, and you can solve the second as described here and here.

Combining these in Colab got me this:

enter image description here

and then this:

enter image description here

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • I am able to write the content to the file as shown in Image 1. But the second Image is giving me an error. `nvcc fatal : Value 'sm_75' is not defined for option 'gpu-architecture' /bin/bash: /content/src/omp_cuda: No such file or directory` – PeakyBlinder Feb 27 '21 at 08:53
  • In that case you must be using a rather old CUDA toolkit. Change it to whatever GPU your instance has, or just remove it – talonmies Feb 27 '21 at 09:37
  • I am really curious to know why this was downvoted. It is a community wiki entry -- it is there to be edited if something is incorrect. I earn no reputation either way. – talonmies Feb 28 '21 at 08:19