cuda中thread id
////////////////////////////////////////////////////////////////////////////
//
// Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
//
// Please refer to the NVIDIA end user license agreement (EULA) associated
// with this source code for terms and conditions that govern your use of
// this software. Any use, reproduction, disclosure, or distribution of
// this software and related documentation outside the terms of the EULA
// is strictly prohibited.
//
//////////////////////////////////////////////////////////////////////////// //
// This sample illustrates the usage of CUDA events for both GPU timing and
// overlapping CPU and GPU execution. Events are inserted into a stream
// of CUDA calls. Since CUDA stream calls are asynchronous, the CPU can
// perform computations while GPU is executing (including DMA memcopies
// between the host and device). CPU can query CUDA events to determine
// whether GPU has completed tasks.
// // includes, system
#include <stdio.h> // includes CUDA Runtime
#include <cuda_runtime.h> // includes, project
#include <helper_cuda.h>
#include <helper_functions.h> // helper utility functions __global__ void increment_kernel(int *g_data, int inc_value)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;// thread id 计算分三级:thread, block .grid .
g_data[idx] = g_data[idx] + inc_value; //每一个线程,把对应的操作数增加一个常数
} bool correct_output(int *data, const int n, const int x)
{
for (int i = ; i < n; i++)
if (data[i] != x)
{
printf("Error! data[%d] = %d, ref = %d\n", i, data[i], x);
return false;
} return true;
} int main(int argc, char *argv[])
{
int devID;
cudaDeviceProp deviceProps; printf("[%s] - Starting...\n", argv[]); // This will pick the best possible CUDA capable device
devID = findCudaDevice(argc, (const char **)argv); // get device name
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
printf("CUDA device [%s]\n", deviceProps.name); int n = * * ;
int nbytes = n * sizeof(int);
int value = ; // allocate host memory
int *a = ;
checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
memset(a, , nbytes); // allocate device memory
int *d_a=;
checkCudaErrors(cudaMalloc((void **)&d_a, nbytes));
checkCudaErrors(cudaMemset(d_a, , nbytes)); // set kernel launch configuration
dim3 threads = dim3(, );//每个block1024个threads,一维
dim3 blocks = dim3(n / threads.x, );//block数量, // create cuda event handles
cudaEvent_t start, stop;//运算计时
checkCudaErrors(cudaEventCreate(&start));
checkCudaErrors(cudaEventCreate(&stop)); StopWatchInterface *timer = NULL;
sdkCreateTimer(&timer);
sdkResetTimer(&timer); checkCudaErrors(cudaDeviceSynchronize());
float gpu_time = 0.0f;
printf("a=%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",a[n--],a[n--],a[n--],a[n--],a[n--],a[n--],a[n--],a[n--],a[n--]);
// asynchronously issue work to the GPU (all to stream 0)
sdkStartTimer(&timer);
cudaEventRecord(start, );
cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, );//把host中变量a复制到device中的变量d_a
increment_kernel<<<blocks, threads, , >>>(d_a, value);//device执行
cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, );//device结果复制到host
cudaEventRecord(stop, );
sdkStopTimer(&timer); // have CPU do some work while waiting for stage 1 to finish
unsigned long int counter=; while (cudaEventQuery(stop) == cudaErrorNotReady)
{
counter++;
} checkCudaErrors(cudaEventElapsedTime(&gpu_time, start, stop)); // print the cpu and gpu times
printf("time spent executing by the GPU: %.2f\n", gpu_time);
printf("time spent by CPU in CUDA calls: %.2f\n", sdkGetTimerValue(&timer));
printf("CPU executed %lu iterations while waiting for GPU to finish\n", counter);
printf("a=%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",a[n--],a[n--],a[n--],a[n--],a[n--],a[n--],a[n--],a[],a[]); // check the output for correctness
bool bFinalResults = correct_output(a, n, value); // release resources
checkCudaErrors(cudaEventDestroy(start));
checkCudaErrors(cudaEventDestroy(stop));
checkCudaErrors(cudaFreeHost(a));
checkCudaErrors(cudaFree(d_a)); exit(bFinalResults ? EXIT_SUCCESS : EXIT_FAILURE);
}
一个grid包含多个blocks,这些blocks的组织方式可以是一维,二维或者三维。任何一个block包含有多个Threads,这些Threads的组织方式也可以是一维,二维或者三维。举例来讲:比如上图中,任何一个block中有10个Thread,那么,Block(0,0)的第一个Thread的ThreadIdx是0,Block(1,0)的第一个Thread的ThreadIdx是11;Block(2,0)的第一个Thread的ThreadIdx是21,......,依此类推,

cuda中thread id的更多相关文章
- CUDA中并行规约(Parallel Reduction)的优化
转自: http://hackecho.com/2013/04/cuda-parallel-reduction/ Parallel Reduction是NVIDIA-CUDA自带的例子,也几乎是所有C ...
- CUDA中确定你显卡的thread和block数
CUDA中确定你显卡的thread和block数 在进行并行计算时, 你的显卡所支持创建的thread数与block数是有限制的, 因此, 需要自己提前确定够用, 再进行计算, 否则, 你需要改进你的 ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- thread::id
线程标识符id可以通过thread::get_id()获得,若thread obejct没有和任何线程关联则返回一个NULL的std::thread::id表示没有任何线程.当前线程若想获得自己的id ...
- CUDA中使用多维数组
今天想起一个问题,看到的绝大多数CUDA代码都是使用的一维数组,是否可以在CUDA中使用一维数组,这是一个问题,想了各种问题,各种被77的错误状态码和段错误折磨,最后发现有一个cudaMallocMa ...
- Android Framework中Thread类
Thread类是Android为线程操作而做的一个封装.代码在Thread.cpp中,其中还封装了一些与线程同步相关的类. Thread类 Thread类的构造函数中的有一个canCallJava T ...
- 详解C#中 Thread,Task,Async/Await,IAsyncResult的那些事儿
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- 删除数据表中除id外其他字段相同的冗余信息
删除一个信息表中除id外其他字段都相同的冗余信息,如下 id name addr 1 a b 2 a b 3 b c 删除这个表中的冗余信息 即应该是 id name addr 1 a b 3 b c ...
随机推荐
- 调用MyFocus库,简单实现二十几种轮播效果
一.首先点击这里下载myFocus库文件,标准文件库就行了,很小仅仅1.4M. myFocus库有以下的好处: a . 文件小巧却高效强大,能够实现二十几种轮播的效果. b . 极其简单的使用,只需要 ...
- easyui加载datagrid时随着窗体大小改变而改变
function initTable() { $('#tt').datagrid({ width: $(document).width() - 20, heig ...
- 设置css通用字体
font-family: "Helvetica Neue","Arial","PingFang SC","Hiragino San ...
- js资源加载优化
互联网应用或者访问量大的应用,对js的加载优化是不可少的.下面记录几种优化方法 CDN + 浏览器缓存 CDN(content delivery network)内容分发网络, 最传统的优化方式.其 ...
- SharePoint 2010 系统账户没完全控制权限了
网上下载了一个试用版的wsp包,安装部署后感觉不好就卸载掉了.坑爹的事情发生了,系统账户登录网站集竟然没完全控制权限了.连添加列表项的权限都没有了. 去管理中心查看,网站集管理员,没发现问题. 更坑爹 ...
- VSTO开发,转帖
http://www.cnblogs.com/oneivan/p/4243574.html
- Hadoop 2.5.1编译
1.环境安装 gcc.gcc-c++.make.cmake.svn yum install lzo-devel zlib-devel gcc gcc-c++ make cmake autoconf a ...
- linux集群运维工具:pssh
由于需要安装hadoop集群,有10台机器需要安装,一开始打算用SCP复制,后来觉得不可接受(实际现场可能数倍的机器集群,就是10台也不想干).后来在网上找了,发现了clustershell和pssh ...
- clang 搭建和编译boost 和zero ICE库 (Ubuntu10 64)
相关介绍资料如下: Boost编译http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz/downlo ...
- ZeroMq安装包的生成【ubuntu10】
生成方法添加源sudo add-apt-repository ppa:chris-lea/zeromqsudo add-apt-repository ppa:chris-lea/libpgmsudo ...