美文网首页
cuda学习笔记1

cuda学习笔记1

作者: FOFI | 来源:发表于2019-04-30 21:44 被阅读0次
    本节目标:
    • 为主机(Host)编写代码和为设备(Device)编写代码的区别
    • 从主机运行设备代码
    • 如何在支持CUDA的设备上使用设备内存
    • 如何查询系统中支持CUDA的设备信息

    1. 在GPU设备上执行的函数称为核函数,NVIDIA工具使用CUDA C编写核函数,CUDA C为标准C增加 global 修饰符,这个修饰符将告诉编译器,函数应该编译为设备而不是主机运行。具有该修饰符的函数将作为核函数交给CUDA编译器编译。
    2.从主机运行设备代码,如下所示:
    cudaError_t err = cudaSuccess;
        ////参数传递
        int c;
        int *dev_c;
        err = cudaMalloc((void**)&dev_c, sizeof(int));
        add_kernel << <1, 1 >> > (2, 7, dev_c); ////核函数处理
        if (err != cudaSuccess)
        {
            fprintf(stderr, "Failed to allocate device value dev_c (error code %s)!\n", cudaGetErrorString(err));
            exit(EXIT_FAILURE);
        }
    
        err = cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost); ////主机代码中,不能对cudaMalloc()返回的指针进行读取,写入内存操作(即不可修改),但可以作参数传递,执行算术运算。
        if (err != cudaSuccess)
        {
            fprintf(stderr, "Failed to copy device value dev_c to c (error code %s)!\n", cudaGetErrorString(err));
            exit(EXIT_FAILURE);
        }
    
        printf("2+7=%d\n", c);
        cudaFree(dev_c); ////不可使用free(),因为在主机代码中不可修改该内存的值  ,  总的来说,主机指针只能访问主机代码的内存,设备指针只能访问设备代码的内存
    
    
    从主机运行设备代码,以下几点值得注意:
    1.可以像调用C函数那样将参数传递给核函数
    2.当设备执行任何有用操作时,都需要分配内存,例如将计算值返回主机。
    3.使用设备内存:关于cudaMalloc()分配到的指针dev_c.
    可以将dev_c传递给设备上执行的函数。可以在设备代码中使用dev_c进行内存读/写操作(修改该内存的值)。可以可以将dev_c传递给主机上执行的函数,但是不能在主机代码中对dev_c进行内存读/写操作。因为dev_c申请的是设备内存,不能在主机代码中用主机指针访问设备内存,反过来也一样,不能用设备指针访问主机内存。
    然后可以利用cudaMencpy()在主机代码中访问设备内存,设备指针用完后用cudaFree()释放,不可以直接Free(),原因同上,不能在主机代码中直接访问设备内存。
    4.查询系统中支持CUDA的设备信息
    ////查询设备
        cudaDeviceProp prop;
        int count;
        err = cudaGetDeviceCount(&count);
        for (int i = 0; i < count; i++)
        {
            err = cudaGetDeviceProperties(&prop, i);
    
            if (err != cudaSuccess)
            {
                fprintf(stderr, "Failed to get device %d properties (error code %s)!\n", i, cudaGetErrorString(err));
                exit(EXIT_FAILURE);
            }
    
            printf("Name: %s\n", prop.name);////这里打印设备信息,具体包含哪些信息查看《NVIDIA CUDA Programming Guide》  
            printf("Compute capability: %d . %d\n", prop.major,prop.minor);
            printf("Total global Mem:%lld\n", prop.totalGlobalMem);
            printf("Total constant Mem:%ld\n", prop.totalConstMem);
    
        }
    
    完整代码:
    
    #include "cuda_runtime.h"
    #include "device_launch_parameters.h"
    
    #include <stdio.h>
    #include <helper_cuda.h>
    using namespace std;
    
    
    
    
    __global__ void add_kernel(int a,int b, int *c) {
        *c = a + b;
    }
    
    int main(void)
    {
        cudaError_t err = cudaSuccess;
        ////参数传递
        int c;
        int *dev_c;
        err = cudaMalloc((void**)&dev_c, sizeof(int));
        add_kernel << <1, 1 >> > (2, 7, dev_c); ////核函数处理
        if (err != cudaSuccess)
        {
            fprintf(stderr, "Failed to allocate device value dev_c (error code %s)!\n", cudaGetErrorString(err));
            exit(EXIT_FAILURE);
        }
    
        err = cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost); ////主机代码中,不能对cudaMalloc()返回的指针进行读取,写入内存操作(即不可修改),但可以作参数传递,执行算术运算。
        if (err != cudaSuccess)
        {
            fprintf(stderr, "Failed to copy device value dev_c to c (error code %s)!\n", cudaGetErrorString(err));
            exit(EXIT_FAILURE);
        }
    
        printf("2+7=%d\n", c);
        cudaFree(dev_c); ////不可使用free(),因为在主机代码中不可修改该内存的值  ,  总的来说,主机指针只能访问主机代码的内存,设备指针只能访问设备代码的内存
    
        ////查询设备
        cudaDeviceProp prop;
        int count;
        err = cudaGetDeviceCount(&count);
        for (int i = 0; i < count; i++)
        {
            err = cudaGetDeviceProperties(&prop, i);
    
            if (err != cudaSuccess)
            {
                fprintf(stderr, "Failed to get device %d properties (error code %s)!\n", i, cudaGetErrorString(err));
                exit(EXIT_FAILURE);
            }
    
            printf("Name: %s\n", prop.name);////这里打印设备信息,具体包含哪些信息查看《NVIDIA CUDA Programming Guide》  
            printf("Compute capability: %d . %d\n", prop.major,prop.minor);
            printf("Total global Mem:%lld\n", prop.totalGlobalMem);
            printf("Total constant Mem:%ld\n", prop.totalConstMem);
    
        }
        return 0;   
        
    }
    
    

    相关文章

      网友评论

          本文标题:cuda学习笔记1

          本文链接:https://www.haomeiwen.com/subject/dxybnqtx.html