一段代碼搞懂 gpu memory
GPU 的 memory 分爲 三種,io速度從快到慢排序爲:
- local memory
- shared memory
- global memory
其中 shared memory 的io 速度是遠快於 global memory 的。
這三種 memory 的訪問性質是:
- local memory: 線程私有,只能本線程訪問
- shared memory: 線程塊(thread block) 共享, 同一個線程塊中的線程可以訪問。
- global memory: 所有線程都可訪問。
那麼在編程的過程中,這三種 memory 是從什麼地方體現出來的呢?
#include <stdio.h>
__global__ void memory_demo(float* array)
{
// array 指針是在 local memory 上的,但是它指向的 memory 是 global memory
// i, index 都是 local variable,每個 線程 私有。
int i, index = threadIdx.x;
// __shared__ variable 對 block 中的 線程可見
// 並 和 thread block 有相同的 生命週期。
__shared__ float sh_arr[128];
// 將 global memory 的值 拷貝到 shared memory 上。
sh_arr[index] = array[index];
// barrier here
__syncthreads();
// 之後對 shared memory 的 IO 要快的多
// do something
}
int main()
{
float h_arr[128];
float *d_arr;
// cudaMalloc 分配的 memory 是在 global memory 上的。
cudaMalloc((void **)&d_arr, sizeof(float)*128);
cudaMemcpy((void*) d_arr, (void*) h_arr, sizeof(float)*128, cudaMemcpyHostToDevice);
// 啓動 kernel
memory_demo<<<1, 128>>>(d_arr);
// .. do other stuff
}