C++和CUDA混合编程

部署运行你感兴趣的模型镜像

最近心血来潮,想用CUDA实现加速光流计算,简单研究了一下CUDA编程,自然地想到了CPP和CUDA的混合编程。
一般来说,CPP可通过g++、clang、MSVC等编译器编译,CUDA则使用NVIDIA的nvcc编译。
CUDA编程其实很大程度兼容C++,所以其实可以直接使用nvcc作为CPP文件的编译器。
下面举一个简单的例子来说明。

kernel.cu

#include <cuda_runtime.h>
#include <iostream>

__global__ void addKernel(int* c, const int* a, const int* b, int size) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < size) {
        c[i] = a[i] + b[i];
    }
}

void addWithCuda(int* c, const int* a, const int* b, int size) {
    int* dev_a = nullptr;
    int* dev_b = nullptr;
    int* dev_c = nullptr;

    cudaMalloc((void**)&dev_a, size * sizeof(int));
    cudaMalloc((void**)&dev_b, size * sizeof(int));
    cudaMalloc((void**)&dev_c, size * sizeof(int));

    cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);

    addKernel<<<1, size>>>(dev_c, dev_a, dev_b, size);

    cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_c);
}
#include <iostream>

void addWithCuda(int *c, const int *a, const int *b, int size);

int main()
{
    const int size = 5;
    int a[size] = {1, 2, 3, 4, 5};
    int b[size] = {10, 20, 30, 40, 50};
    int c[size] = {0};

    addWithCuda(c, a, b, size);

    std::cout << "Result: ";
    for (int i = 0; i < size; ++i)
    {
        std::cout << c[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

或者在同一个CUDA文件中编写两种代码:

#include <cuda_runtime.h>
#include <iostream>

__global__ void addKernel(int* c, const int* a, const int* b, int size) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < size) {
        c[i] = a[i] + b[i];
    }
}

void addWithCuda(int* c, const int* a, const int* b, int size) {
    int* dev_a = nullptr;
    int* dev_b = nullptr;
    int* dev_c = nullptr;

    cudaMalloc((void**)&dev_a, size * sizeof(int));
    cudaMalloc((void**)&dev_b, size * sizeof(int));
    cudaMalloc((void**)&dev_c, size * sizeof(int));

    cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);

    addKernel<<<1, size>>>(dev_c, dev_a, dev_b, size);

    cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_c);
}

int main()
{
    const int size = 5;
    int a[size] = {1, 2, 3, 4, 5};
    int b[size] = {10, 20, 30, 40, 50};
    int c[size] = {0};

    addWithCuda(c, a, b, size);

    std::cout << "Result: ";
    for (int i = 0; i < size; ++i)
    {
        std::cout << c[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

如果是前者,需要编译main.cppkernel.cu,那么编译指令是:

nvcc main.cpp kernel.cu -o main.exe -lcudart -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\lib\x64"

后者直接编译kernel.cu

nvcc kernel.cu -o main.exe -lcudart -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\lib\x64"

最后都会生成可执行文件kernel.exe
实测nvcc应该可以支持C++ 17的标准,需要版本大于9.0。

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值