Основы программирования CUDA

C++

Описание окружающей среды

  • CUDA:cuda-11
  • driver:460.67
  • os:5.10.18-1-MANJARO
  • CMAKE:3.19.5

Структура каталогов следующая:

├── CMakeLists.txt
├── include
│   └── sumMatrix.h
├── main.cu
└── src
    ├── CMakeLists.txt
    └── sumMatrix.cu

2 directories, 5 files

cuda функция

Для простоты CUDA реализует добавление двумерной матрицы, заголовочный файл (include/sumMatrix.h):

#ifndef SUM_MATRIX_CU_H
#define SUM_MATRIX_CU_H
#include <cuda_runtime.h>
__global__ void sumMatrix(float *a, float *b, int nx, int ny);
#endif

реализация исходного файлаsrc/sumMatrix.cu:

#include "sumMatrix.h"
__global__ void sumMatrix(float *a, float *b, int nx, int ny) {
    int idx = threadIdx.x + blockDim.x * blockIdx.x;
    int idy = threadIdx.x + blockDim.y * blockIdx.y;
    int index = idy * nx + idx;
    // printf("==> (%d,%d) threadidx:%d index:%d Current x:%.2f,y:%.2f\n",idx,idy,threadIdx.x,index,a[index],b[index]);
    if (index < nx && index < ny)
      a[index] = a[index] + b[index];
  }

Реализация основной функции:main.cu

#include <cstdlib>
#include "sumMatrix.h"
#include <stdio.h>
void initData(float *f, int size, float value) {
  for (int i = 0; i < size; i++)
    *(f + i) = value;
}

void check_data(float *a, int n) {
  for (int i = 0; i < n; i++)
    printf("Current :%.3f\n", *(a + i));
}
int main() {
  int dev = 0;
  cudaDeviceProp deviceProp;
  cudaGetDeviceProperties(&deviceProp, dev);

  int nx = 1 << 5;
  int ny = 1 << 5;

  int nxy = nx * ny;
  int nBytes = nxy * sizeof(float);

  float *h_a, *h_b, *hostRef, *gpuRef;

  h_a = (float *)malloc(nBytes);
  h_b = (float *)malloc(nBytes);

  hostRef = (float *)malloc(nBytes);
  gpuRef = (float *)malloc(nBytes);

  initData(h_a, nx, 1.0f);
  initData(h_b, ny, 2.0f);

  memset(hostRef, 0, nBytes);
  memset(gpuRef, 0, nBytes);

  float *d_a, *d_b;
  cudaMalloc((void **)&d_a, nBytes);
  cudaMalloc((void **)&d_b, nBytes);

  cudaMemcpy(d_a, h_a, nBytes, cudaMemcpyHostToDevice);
  cudaMemcpy(d_b, h_b, nBytes, cudaMemcpyHostToDevice);

  int dimx = 32;
  int dimy = 32;
  dim3 block(dimx, dimy);
  dim3 grid((nx + block.x - 1) / block.x, (ny + block.y - 1) / block.y);
  printf("grid:(%d,%d),Block:(%d,%d)",grid.x,grid.y,block.x,block.y);

  sumMatrix<<<grid, block>>>(d_a, d_b, nx, ny);
  cudaMemcpy(gpuRef, d_a, nBytes, cudaMemcpyDeviceToHost);
  //check_data(gpuRef, 10);

  cudaFree(d_a);
  cudaFree(d_b);
  free(h_a);
  free(h_b);
}

Файл проекта CMakeLists:CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
# 开启语言支持
project(matrix_demo LANGUAGES CXX CUDA)
if(CUDA_ENABLED)
    enable_language(CUDA)
endif()
# 设置cuda架构,本机GTX980,cc52,另有一台RTX3090机器,这里设置两个arch
set(CMAKE_CUDA_ARCHITECTURES 52 80)
# 编译库所在路径
add_subdirectory(src)
# 调用函数需要的头文件
include_directories(include)
add_executable(main main.cu)
# 链接编译后生成的库
target_link_libraries(main matrix)

файл конфигурации библиотекиsrc/CMakeLists.txt:

include_directories(${CMAKE_SOURCE_DIR}/include)
file(GLOB CUDA_SRC ${CMAKE_SOURCE_DIR}/src/*.cu)
add_library(matrix ${CUDA_SRC})

скомпилировать и запустить

  • mkdir build&&cmake ..
-- The CXX compiler identification is GNU 10.2.0
-- The CUDA compiler identification is NVIDIA 11.0.221
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /opt/cuda/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bleedingfight/test/cudnn/build
  • компилироватьmake -j16
[ 25%] Building CUDA object src/CMakeFiles/matrix.dir/sumMatrix.cu.o
[ 50%] Linking CUDA static library libmatrix.a
[ 50%] Built target matrix
Scanning dependencies of target main
[ 75%] Building CUDA object CMakeFiles/main.dir/main.cu.o
[100%] Linking CUDA executable main
[100%] Built target main

  • бегать:./main
grid:(1,1),Block:(32,32)