测试cuda的unified memory和cudaMemcpy的if判断

来源:互联网 发布:淘宝助理 同步宝贝 编辑:程序博客网 时间:2024/04/30 11:48
//
//#include<iostream>
//#include<cuda.h>
//#include<cuda_runtime.h>
//#include<time.h>
//using namespace std;
//
//const int N=1001569;
//const int threadPerBlock=256;
//
//
//__global__ void aplusb(int *a,int *b,float *c)
//{
//const int tid=threadIdx.x+blockIdx.x*blockDim.x;
//if(tid<N)
//{
//c[tid]=a[tid]/b[tid];
//}
//}
//
//int main()
//{
//int block;//块的个数
//if(N%threadPerBlock==0)
//block=N/threadPerBlock;
//else
//block=N/threadPerBlock+1;
//int *a,*b;//unified memory的
//float *c;
////int *da,*db,*dc;
////int ha[N],hb[N],hc[N];
//int *da,*db;
//float *dc;
//int ha[N],hb[N];
//float hc[N];
//cudaMallocManaged(&a,N*sizeof(int));
//cudaMallocManaged(&b,N*sizeof(int));
//cudaMallocManaged(&c,N*sizeof(float));
//for(int i=0;i<N;i++)
//{
//a[i]=rand();
//b[i]=rand()+1;
//ha[i]=a[i];
//hb[i]=b[i];
//}
//
//
//
//
//cudaEvent_t start,stop;//事件
//float time_unified,time_gpu,time_cpu;//测试时间
//
//cudaEventCreate(&start);
//cudaEventCreate(&stop);
//cudaEventRecord(start,0);
//
//for(int i=0;i<10;i++)
//aplusb<<<block,threadPerBlock>>>(a,b,c);
//cudaEventRecord(stop,0);
//cudaEventSynchronize(stop);
//cudaEventElapsedTime(&time_unified,start,stop);
//
//
//
//cudaMalloc((void**)&da,N*sizeof(int));//申请空间
//cudaMalloc((void**)&db,N*sizeof(int));
//cudaMalloc((void**)&dc,N*sizeof(float));
//
//
//
//
////for(int i=0;i<N;i++)//考虑到每次循环的数据会变化,所以每次都要重新传数据
////{
////ha[i]=i;
////hb[i]=2*i;
////}
//cudaMemcpy(da,ha,N*sizeof(int),cudaMemcpyHostToDevice);
//cudaMemcpy(db,hb,N*sizeof(int),cudaMemcpyHostToDevice);
//cudaEventCreate(&start);
//cudaEventCreate(&stop);
//cudaEventRecord(start,0);
//for(int j=0;j<10;j++)
//{
//aplusb<<<block,threadPerBlock>>>(da,db,dc);
//}
//cudaEventRecord(stop,0);
//cudaEventSynchronize(stop);
//cudaEventElapsedTime(&time_gpu,start,stop);
//cudaMemcpy(hc,dc,N*sizeof(float),cudaMemcpyDeviceToHost);
//
//
//
//for(int i=0;i<N;i++)//检测unified与gpu运行的结果是否相同
//{
//if(c[i]!=hc[i])
//cout<<"error";
//}
//
//clock_t start1,stop1;//运行CPU
//start1=clock();
//for(int j=0;j<10;j++)
//for(int i=0;i<N;i++)
//hc[i]=ha[i]/hb[i];
//stop1=clock();
//time_cpu=stop1-start1;
//
//for(int i=0;i<N;i++)//检测unified与cpu运行的结果是否相同
//{
//if(c[i]!=hc[i])
//cout<<"error";
//}
//
//cout<<N<<"  unified.."<<time_unified<<"  gpu.."<<time_gpu<<"  cpu.."<<time_cpu<<endl;
//return 0;
//}
0 0
原创粉丝点击