在Google Colab上执行CUDA程序时如何链接库? [英] How to link the libraries when executing CUDA program on Google Colab?

查看:75
本文介绍了在Google Colab上执行CUDA程序时如何链接库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用Google Colab上的cuRAND库来运行CUDA程序以生成随机数,但是出现链接器问题.

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.

我知道,我们可以在使用nvcc进行编译时通过使用-lcurand来解决此问题,但据我所知,我们无法在colab中访问终端.

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.

我正在使用它生成2 * N个随机数.

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);

错误:

/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

推荐答案

以下是一种可能的方法:

Here is one possible method:

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

只需在笔记本设置的Accelerator下拉列表中选择"GPU"(通过编辑"菜单或cmd/ctrl-shift-P上的命令面板).

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).

安装 nvcc4jupyter插件:

!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

  • 加载插件:

  • Load the plugin:

    %load_ext nvcc_plugin
    

  • 将所需的代码放入单元格中,并传递文件名:

  • 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).

    注意单元格输出:

    'File written in /content/src/my_curand.cu'
    

  • 编译代码:

  • Compile the code:

    !nvcc -o /content/src/my_curand /content/src/my_curand.cu -lcurand
    

  • 运行代码

  • Run the code

    !/content/src/my_curand
    

    注意单元格输出:

    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
    

  • 这篇关于在Google Colab上执行CUDA程序时如何链接库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆