Busy today for 3 hours, unexpectedly was a distress of cuda small routine to trapped, originally was referring to Rachal Zhang's Cuda learning notes to a imitation, the results of their own confused, and finally to figure out some.
RZ great God to Cuda about Kernel,memory's introduction still pretty clear, after reading decided to write a two-dimensional array of addition. If it is the addition of C + +, it is simple, with c[i][j] = A[i][j] +b[i][j] can.
1 void Cppmatadd (int a[m][n],int b[m][n],int c[m][n]) {2for ( int i=0; i<m;i++)3for (int j=0 ; j<n;j++)4 c[i][j] = a[i][j] + b[i][j]; 5 }
1 intMain ()2 {3 intA[m][n] = {1,2,3,4,5,6,7,8,9,Ten, One, A};4 intB[m][n] = {1,2,3,4,5,6,7,8,9,Ten, One, A};5 intC[m][n];6 Cppmatadd (a,b,c);7std::cout<<c[0][0];8}
Run the above code, you can implement the two-dimensional matrix (that is, arrays) of the addition operation.
But Cuda computing is implemented on the GPU, to divide the dedicated memory area to the GPU to do the operation, the result is that we have to partition the host memory, the device memory for the CPU, GPU access.
For a one-dimensional situation, we set the host variable, the device variable can be. Specifically, you can find the RZ blog.
But the two-dimensional situation is troublesome, the first I also set out the host variables, device variables, one-to-the-other allocation of memory, copy data, GPU operations, the final test results. But found how debugging results are not correct, the main reason is that the C + + two-dimensional array is actually a pointer to a one-dimensional array, so, can not follow the pattern of an array to copy data, the result of a matching method is a lot of trouble, in fact, or revert to a one-dimensional array of methods to do the addition operation, the code Specifically do not want to repeat, the code capacity is limited, slowly come, today is the pointer to more clearly.
/*--------------------------------------------* date:2015-3-18 * Author: Lee Geun * filename:.cpp * Description:cuda two-dimensional array plus Law------------------------------------------------*/#include"cuda_runtime.h"#include"Device_launch_parameters.h"#include<iostream>#include<stdio.h>Static Const intM =4;Static Const intN =3;//the kernel of matrix addition__global__voidAddmat (int**a,int**b,int**b) { inti = blockidx.x * blockdim.x +threadidx.x; intj = blockidx.y * blockdim.y +threadidx.y; if(I < M && J <N) C[i][j]= A[i][j] +b[i][j];}intMain () {int**a = (int* *) malloc (m*sizeof(int*));//Host Memory int**b = (int* *) malloc (m*sizeof(int*));//Host Memory int**c = (int* *) malloc (m*sizeof(int*));//Host Memory int*dataa = (int*) malloc (m*n*sizeof(int));//Host Memory Data int*datab = (int*) malloc (m*n*sizeof(int));//Host Memory Data int*datac = (int*) malloc (m*n*sizeof(int));//Host Memory Data int**dev_a;//Device Memory int**dev_b;//Device Memory int**dev_c;//Device Memory int*dev_dataa;//Device Memory Data int*dev_datab;//Device Memory Data int*dev_datac;//Device Memory DataCudamalloc ((void* *) (&dev_a), m*sizeof(int*)); Cudamalloc ((void* *) (&DEV_DATAA), m*n*sizeof(int)); Cudamalloc ((void* *) (&dev_b), m*sizeof(int*)); Cudamalloc ((void* *) (&dev_datab), m*n*sizeof(int)); Cudamalloc ((void* *) (&dev_c), m*sizeof(int*)); Cudamalloc ((void* *) (&DEV_DATAC), m*n*sizeof(int)); for(intI=0; i<m*n;i++) {Dataa[i]=i; Datab[i]= i+1; Datac[i]=0; } cudamemcpy ((void*) (DEV_DATAA), (void*) (DATAA), m*n*sizeof(int*), Cudamemcpyhosttodevice); cudamemcpy ((void*) (Dev_datab), (void*) (Datab), m*n*sizeof(int*), Cudamemcpyhosttodevice); for(intI=0; i<m;i++) {A[i]= Dev_dataa + Ni; B[i]= Dev_datab + Ni; C[i]= Dev_datac + Ni; } cudamemcpy ((void*) (DEV_A), (void*) (A), m*sizeof(int*), Cudamemcpyhosttodevice); cudamemcpy ((void*) (Dev_b), (void*) (B), m*sizeof(int*), Cudamemcpyhosttodevice); cudamemcpy ((void*) (Dev_c), (void*) (C), m*sizeof(int*), Cudamemcpyhosttodevice); DIM3 Threadperblock ( -, -); Dim3 numblocks (N+threadperblock.x-1)/(threadperblock.x), (m+threadperblock.y-1)/(THREADPERBLOCK.Y)); Addmat<<<numBlocks,threadPerBlock>>>(Dev_a,dev_b,dev_c); cudamemcpy ((void*) (DATAC), (void*) (DEV_DATAC), m*n*sizeof(int), cudamemcpydevicetohost); for(intI=0; i<m*n;i++) Std::cout<<dataC[i]<<" "; Cudafree ((void*) DEV_DATAC); Cudafree ((void*) Dev_c); Free (C); Free (DATAC); Cudafree ((void*) dev_datab); Cudafree ((void*) dev_b); Free (B); Free (datab); Cudafree ((void*) dev_dataa); Cudafree ((void*) dev_a); Free (A); Free (DATAA); GetChar ();}
Blog recovery update, slowly accumulate it
One of Cuda learning: two-dimensional matrix addition