一个相对简单的CUDA编程Demo(Hello World):
1.CUDA编程首先呢是分配thread以及block
const int block_num=(Matrix_Size+Thread_Num-1)/Thread_Num;
2.然后是两个基本的函数:
void printDeviceProp(const cudaDeviceProp &prop)
printf("Device Name : %s.\n", prop.name);
printf("totalGlobalMem : %d.\n", prop.totalGlobalMem);
printf("sharedMemPerBlock : %d.\n", prop.sharedMemPerBlock);
printf("regsPerBlock : %d.\n", prop.regsPerBlock);
printf("warpSize : %d.\n", prop.warpSize);
printf("memPitch : %d.\n", prop.memPitch);
printf("maxThreadsPerBlock : %d.\n", prop.maxThreadsPerBlock);
printf("maxThreadsDim[0 - 2] : %d %d %d.\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
printf("maxGridSize[0 - 2] : %d %d %d.\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
printf("totalConstMem : %d.\n", prop.totalConstMem);
printf("major.minor : %d.%d.\n", prop.major, prop.minor);
printf("clockRate : %d.\n", prop.clockRate);
printf("textureAlignment : %d.\n", prop.textureAlignment);
printf("deviceOverlap : %d.\n", prop.deviceOverlap);
printf("multiProcessorCount : %d.\n", prop.multiProcessorCount);
cudaGetDeviceCount(&count);
fprintf(stderr,"three is no device.\n");
cudaGetDeviceProperties(&prop,i);
if(cudaGetDeviceProperties(&prop,i)==cudaSuccess){
if(prop.major>=1){break;}
fprintf(stderr, "There is no device supporting CUDA 1.x.\n");
3.接着随机生成两个矩阵
void matGen(float* a, int n)
a[i*n+j]=(float)rand()/(float)RAND_MAX+1.00;
4.并行矩阵乘法函数,最主要的一部分
__global__ static void matMultCuda(const float* a,const float* b,float* c,int n,clock_t* time)
const int tid=threadIdx.x;
const int bid=blockIdx.x;
const int idx = bid * Thread_Num + tid;
const int column = idx % n;
if(tid==0) time[bid]=clock();
if(row < n && column < n)
t=t+a[row*n+i]+a[i*n+column];
if (tid == 0) time[bid + block_num] = clock();
5.运算完后打印出矩阵
void printMatrix(const float *A, const int n) {
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%.2f" ,A[i*n+j]);
6.最后我们来看一下主函数
if(!InitCUDA()) return 0;
a=(float*)malloc(sizeof(float)*n*n);
b=(float*)malloc(sizeof(float)* n*n);
c=(float*)malloc(sizeof(float)* n*n);
float *cuda_a,*cuda_b,*cuda_c;
cudaMalloc((void**)&cuda_a, sizeof(float)* n*n);
cudaMalloc((void**)&cuda_b, sizeof(float)* n*n);
cudaMalloc((void**)&cuda_c, sizeof(float)* n*n);
cudaMalloc((void**)&time, sizeof(clock_t)* block_num*2);
cudaMemcpy(a,cuda_a,sizeof(float)* n*n,cudaMemcpyHostToDevice);
cudaMemcpy(b,cuda_b,sizeof(float)* n*n,cudaMemcpyHostToDevice);
matMultCuda<<<block_num, Thread_Num, 0>>>(cuda_a,cuda_b,cuda_c,n,time);
clock_t time_use[block_num*2];
cudaMemcpy(c,cuda_c,sizeof(float)* n*n,cudaMemcpyDeviceToHost);
cudaMemcpy(&time_use, time, sizeof(clock_t)* block_num * 2, cudaMemcpyDeviceToHost);
clock_t min_start, max_end;
max_end = time_use[block_num];
for (int i = 1; i < block_num; i++)
if (min_start > time_use[i]) min_start = time_use[i];
if (max_end < time_use[i + block_num]) max_end = time_use[i + block_num];
clock_t final_time = max_end - min_start;
printf("gputime: %d\n", final_time);
最后感谢大家的观看啦,一起学习一起进步鸭!