How to link the libraries when executing CUDA program on Google Colab?

故事扮演 提交于 2021-02-08 09:48:20

问题


I'm trying to run CUDA program to generate random numbers by using cuRAND library on Google Colab but I am getting a linker issue.

I know,we can fix this by using -lcurand while compiling with nvcc, but as far as I know, we cannot access terminal in colab.

I'm using this to generate 2*N random numbers.

#include <curand_kernel.h>

int status;
curandGenerator_t gen;
status = curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MRG32K3A);
status |= curandSetPseudoRandomGeneratorSeed(gen, 4294967296ULL^time(NULL));
status |= curandGenerateUniform(gen, randomnums, (2*N));
status |= curandDestroyGenerator(gen);

Error:

/tmp/tmpxft_000006b3_00000000-10_11f5cb12-9471-4d0d-9dcb-659af6ee1dae.o: In function `main':
tmpxft_000006b3_00000000-5_11f5cb12-9471-4d0d-9dcb-659af6ee1dae.cudafe1.cpp:(.text+0xb0): undefined reference to `curandCreateGenerator'
tmpxft_000006b3_00000000-5_11f5cb12-9471-4d0d-9dcb-659af6ee1dae.cudafe1.cpp:(.text+0xdc): undefined reference to `curandSetPseudoRandomGeneratorSeed'
tmpxft_000006b3_00000000-5_11f5cb12-9471-4d0d-9dcb-659af6ee1dae.cudafe1.cpp:(.text+0xfa): undefined reference to `curandGenerateUniform'
tmpxft_000006b3_00000000-5_11f5cb12-9471-4d0d-9dcb-659af6ee1dae.cudafe1.cpp:(.text+0x109): undefined reference to `curandDestroyGenerator'
collect2: error: ld returned 1 exit status

回答1:


Here is one possible method:

  1. Make sure your colab session has a GPU:

    Simply select "GPU" in the Accelerator drop-down in Notebook Settings (either through the Edit menu or the command palette at cmd/ctrl-shift-P).

  2. Install the nvcc4jupyter plugin:

    !pip install git+git://github.com/andreinechaev/nvcc4jupyter.git
    
  3. Load the plugin:

    %load_ext nvcc_plugin
    
  4. Put the desired code in a cell, passing a filename:

    %%cuda --name my_curand.cu 
    /*
     * This program uses the host CURAND API to generate 100 
     * pseudorandom floats.
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <cuda.h>
    #include <curand.h>
    
    #define CUDA_CALL(x) do { if((x)!=cudaSuccess) { \
        printf("Error at %s:%d\n",__FILE__,__LINE__);\
        return EXIT_FAILURE;}} while(0)
    #define CURAND_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) { \
        printf("Error at %s:%d\n",__FILE__,__LINE__);\
        return EXIT_FAILURE;}} while(0)
    
    int main(int argc, char *argv[])
    {
        size_t n = 100;
        size_t i;
        curandGenerator_t gen;
        float *devData, *hostData;
    
        /* Allocate n floats on host */
        hostData = (float *)calloc(n, sizeof(float));
    
        /* Allocate n floats on device */
        CUDA_CALL(cudaMalloc((void **)&devData, n*sizeof(float)));
    
        /* Create pseudo-random number generator */
        CURAND_CALL(curandCreateGenerator(&gen, 
                    CURAND_RNG_PSEUDO_DEFAULT));
    
        /* Set seed */
        CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 
                    1234ULL));
    
        /* Generate n floats on device */
        CURAND_CALL(curandGenerateUniform(gen, devData, n));
    
        /* Copy device memory to host */
        CUDA_CALL(cudaMemcpy(hostData, devData, n * sizeof(float),
            cudaMemcpyDeviceToHost));
    
        /* Show result */
        for(i = 0; i < n; i++) {
            printf("%1.4f ", hostData[i]);
        }
        printf("\n");
    
        /* Cleanup */
        CURAND_CALL(curandDestroyGenerator(gen));
        CUDA_CALL(cudaFree(devData));
        free(hostData);    
        return EXIT_SUCCESS;
    }
    

    (your code was broken/incomplete, so I'm using the example code from the curand docs).

    Note the cell output:

    'File written in /content/src/my_curand.cu'
    
  5. Compile the code:

    !nvcc -o /content/src/my_curand /content/src/my_curand.cu -lcurand
    
  6. Run the code

    !/content/src/my_curand
    

    Note the cell output:

    0.1455 0.8202 0.5504 0.2948 0.9147 0.8690 0.3219 0.7829 0.0113 0.2855 0.7816 0.2338 0.6791 0.2824 0.6299 0.1212 0.4333 0.3831 0.5136 0.2987 0.4166 0.0345 0.0494 0.0467 0.6166 0.6480 0.8685 0.4012 0.0631 0.4972 0.6809 0.9350 0.0704 0.0458 0.1324 0.3785 0.6457 0.9930 0.9952 0.7677 0.3217 0.8210 0.2765 0.2691 0.4579 0.1969 0.9555 0.8739 0.7996 0.3810 0.6662 0.3153 0.9428 0.5006 0.3369 0.1490 0.8637 0.6191 0.6820 0.4573 0.9261 0.5650 0.7117 0.8252 0.8755 0.2216 0.2958 0.4046 0.3896 0.7335 0.7301 0.8154 0.0913 0.0866 0.6974 0.1811 0.5834 0.9255 0.9029 0.0413 0.9522 0.5507 0.7237 0.3976 0.7519 0.4398 0.4638 0.6094 0.7358 0.3272 0.6961 0.4893 0.9698 0.0456 0.2025 0.9491 0.1516 0.0424 0.6149 0.5638
    


来源:https://stackoverflow.com/questions/56854243/how-to-link-the-libraries-when-executing-cuda-program-on-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!