在Google Colab上执行CUDA程序时如何链接库?

Sai*_*aga 4 cuda libraries google-colaboratory

我正在尝试使用 Google Colab 上的 cuRAND 库运行 CUDA 程序来生成随机数,但遇到链接器问题。

我知道,我们可以在使用 nvcc 编译时使用 -lcurand 来解决这个问题,但据我所知,我们无法访问 colab 中的终端

我用它来生成 2*N 随机数。

#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);
Run Code Online (Sandbox Code Playgroud)

错误:

/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
Run Code Online (Sandbox Code Playgroud)

Rob*_*lla 9

这是一种可能的方法:

  1. 确保您的Colab会话具有 GPU:

    只需在笔记本设置的加速器下拉列表中选择“GPU”(通过“编辑”菜单或 cmd/ctrl-shift-P 的命令选项板)。

  2. 安装nvcc4jupyter 插件

    !pip install git+git://github.com/andreinechaev/nvcc4jupyter.git
    
    Run Code Online (Sandbox Code Playgroud)
  3. 加载插件:

    %load_ext nvcc_plugin
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将所需的代码放入单元格中,并传递文件名:

    %%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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    (您的代码已损坏/不完整,因此我使用curand 文档中的示例代码)。

    注意单元格输出:

    'File written in /content/src/my_curand.cu'
    
    Run Code Online (Sandbox Code Playgroud)
  5. 编译代码:

    !nvcc -o /content/src/my_curand /content/src/my_curand.cu -lcurand
    
    Run Code Online (Sandbox Code Playgroud)
  6. 运行代码

    !/content/src/my_curand
    
    Run Code Online (Sandbox Code Playgroud)

    注意单元格输出:

    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
    
    Run Code Online (Sandbox Code Playgroud)