参照数学坐标系,grid的规格是 ( 4 , 3 ) (4,3) (4,3) ,block的规格是 ( 3 , 2 ) (3,2) (3,2)
对于CUDA编程模型,本质上还是要掌握并行编程思想。每一个矩阵元素运算,都是由一条线程执行。我们要做的就是找到线程坐标位置及其对应的矩阵元素,然后执行计算逻辑。
下面是一个二维矩阵相加示例:
cudastart.h
#ifndef CUDASTART_H
#define CUDASTART_H
#define CHECK(call)\
{
\
const cudaError_t error=call;\
if(error!=cudaSuccess)\
{
\
printf("ERROR: %s:%d,",__FILE__,__LINE__);\
printf("code:%d,reason:%s\n",error,cudaGetErrorString(error));\
exit(1);\
}\
}
#include <time.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <sys/time.h>
#endif
double cpuSecond()
{
struct timeval tp;
gettimeofday(&tp,NULL);
return((double)tp.tv_sec+(double)tp.tv_usec*1e-6);
}
void initialData(float* ip,int size)
{
time_t t