测试原子操作不同的变量

来源:互联网 发布:模具cnc编程 是什么 编辑:程序博客网 时间:2024/05/01 12:14
const int N=1000000;
__global__ void test(int x,int y,int z)
{

const int tid=threadIdx.x;

if(tid==0)
for(int i=0;i<N;i++)
{ atomicAdd(&x,1);
__syncthreads();}
//x++;
/*for(int i=0;i<N;i++)
x++;*/
if(tid==1)
for(int i=0;i<N;i++)
{ atomicAdd(&x,1);
__syncthreads();}
/*for(int i=0;i<N;i++)
y++;*/
if(tid==2)
for(int i=0;i<N;i++)
{atomicAdd(&x,1);
__syncthreads();}
/*for(int i=0;i<N;i++)
z++;*/
// if(tid==0)
// atomicAdd(&x,1);
//__syncthreads();
//if(tid==1)
// atomicAdd(&x,1);
//__syncthreads();
//if(tid==2)
// atomicAdd(&x,1);
//__syncthreads();
}
int main(void)
{
clock_t h_start,h_elapsed;
int x=0,y=0,z=0;
h_start=clock();

cout<<"a"<<endl;
//for(int i=0;i<10;i++)
//{
test<<<1,3>>>(x,y,z);
cudaDeviceSynchronize();
//}
h_elapsed=clock()-h_start;
cout<<h_elapsed<<endl;
return 0;

}



通过运行可以得出,原子操作是完全的串行

0 0