cuda programming learning - the first cuda program! Dry goods to (1)

Foreword:

Recently, when I was doing 3D reconstruction, especially the multi-view synthesis work in Nerf, I realized that the programming calculation of cuda can greatly improve the calculation of rendering. The most obvious examples are articles such as Instant-ngp and Plenoxels, so I will learn about Cuda later. At the same time, this new hole was opened.
Because the author is also a novice in cuda, if you have any questions, you can point them out in the comment area, and learn and make progress together!

Nvidia Cuda Official Getting Started Information

Operating environment:

Windows 10, Visual Studio 2019, graphics card 3050Ti
(you can configure the environment according to your own laptop, or if you don’t have a GPU, you can also rent a cloud server to learn)

(1) cuda program

Step analysis:
1: Call the graphics card device
2: Allocate video memory space, that is, GPU space
3: Allocate CPU space
4: Copy the data on the CPU to the allocated GPU space
5: Perform calculations on the GPU and execute the kernel function kernel
6: Copy the calculation result from the GPU to the CPU space
7: Output the copied data on the CPU
8: Complete the work, release the cpu and gpu space

#include<stdint.h>
#include<cuda.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>

//定义核函数, 在host调用, device上计算, 该函数作用为把空间幅值为1
__global__ void kernel(float* a)
{
    
    
	a[threadIdx.x] = 1;

}
int main(int argc, char** argv)
{
    
    
	// 设置使用的显卡设备  cpu用 host表示  gpu用device表示
	cudaSetDevice(0);
	//分配显存空间  dx  表示 device 上的空间x
	float* dx;
	cudaMalloc((void**)&dx, 16 * sizeof(float));
	//分配cpu内存空间
	float hx[16] = {
    
     0 };
	//把cpu上的数据拷贝到gpu device 上
	cudaMemcpy(dx, hx, 16 * sizeof(float), cudaMemcpyHostToDevice);
	kernel << <1, 16 >> > (dx);
	//把gpu上的数据,计算结果,拷贝到cpu host 上
	cudaMemcpy(hx, dx, 16 * sizeof(float), cudaMemcpyDeviceToHost);
	for (int i = 0; i < 16; i++)
	{
    
    
		printf("%f \n", hx[i]);
	}
	//释放资源 分别释放 显存和内存空间
	cudaFree(dx);
	free(hx);
//	cudaDeviceReset();
	return 0;
}


























Guess you like

Origin blog.csdn.net/qq_40514113/article/details/130752967
Recommended