CUDA_Programming
所属分类:GPU/显卡
开发工具:Cuda
文件大小:0KB
下载次数:0
上传日期:2023-03-23 04:09:52
上 传 者:
sh-1993
说明: 学习CUDA编程,
(learning CUDA programming,)
文件列表:
atomic_mulB_mulT.cu (908, 2023-03-22)
cuda_01.cu (1541, 2023-03-22)
mem_in_same_B.cu (998, 2023-03-22)
mulB_mulT.cu (1248, 2023-03-22)
mulB_sinT.cu (1028, 2023-03-22)
mulT_sinB.cu (1035, 2023-03-22)
# CUDA Programming
## Install CUDA and compile
* [Install reference](https://shuuutin-cg.medium.com/ubuntu18-04%E5%AE%89%E8%A3%9Dcuda%E4%BB%A5%E5%8F%8A%E6%94%B9%E8%AE%8Acuda%E7%89%88%E6%9C%AC-b8ac917f880f
)
* Compile Nvcc
```shell
# Example: after we write a test.cu we can use following command to compile and run
$ nvcc test.cu
$ ./a.out
```
## Hello World
```c
#include
// Run on GPU(when we see __global__ it means it run on GPU)
// __global__ can be called by CPU, but __device__ only can be called by __global__ or __device__
__global__ void kernel()
{
printf("Hello World!");
}
// Run on CPU
int main()
{
// <<>>
// * kernel function has variable_1 thread blocks
// * In each thread blocks has variable_2 threads
kernel<<<1, 1>>>();
return 0;
}
```
> * Size: `Grid`(huge) > `Block` > `Thread`(small)
> * Execution unit is `Block`.
## Data transmission between CPU and GPU
```c
#include
__global__ void add(int a, int b, int *c)
{
*c = a + b;
}
int main()
{
int cpu_c;
int *gpu_c;
// melloc on GPU
// cudaError_t cudaMalloc (void **devPtr, size_t size)
cudaMalloc((void**)&gpu_c, sizeof(int));
// calculate on GPU
add<<<1, 1>>>(1, 2, gpu_c);
// transmision the data from GPU to CPU
// cudaError_t cudaMemcpy (void *dst, const void *src, size_t count, cudaMemcpyKind kind)
cudaMemcpy(&cpu_c, gpu_c, sizeof(int), cudaMemcpyDeviceToHost);
// cudaError_t cudaFree (void* devPtr)
cudaFree(gpu_c);
return 0;
}
```
> Flow: cudaMelloc() -> cudaMemcpy() -> cudaFree()
近期下载者:
相关文件:
收藏者: