////////////////////////////////////////////////////////////////////////////
//
// 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的更多相关文章

  1. CUDA中并行规约(Parallel Reduction)的优化

    转自: http://hackecho.com/2013/04/cuda-parallel-reduction/ Parallel Reduction是NVIDIA-CUDA自带的例子,也几乎是所有C ...

  2. CUDA中确定你显卡的thread和block数

    CUDA中确定你显卡的thread和block数 在进行并行计算时, 你的显卡所支持创建的thread数与block数是有限制的, 因此, 需要自己提前确定够用, 再进行计算, 否则, 你需要改进你的 ...

  3. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  4. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  5. thread::id

    线程标识符id可以通过thread::get_id()获得,若thread obejct没有和任何线程关联则返回一个NULL的std::thread::id表示没有任何线程.当前线程若想获得自己的id ...

  6. CUDA中使用多维数组

    今天想起一个问题,看到的绝大多数CUDA代码都是使用的一维数组,是否可以在CUDA中使用一维数组,这是一个问题,想了各种问题,各种被77的错误状态码和段错误折磨,最后发现有一个cudaMallocMa ...

  7. Android Framework中Thread类

    Thread类是Android为线程操作而做的一个封装.代码在Thread.cpp中,其中还封装了一些与线程同步相关的类. Thread类 Thread类的构造函数中的有一个canCallJava T ...

  8. 详解C#中 Thread,Task,Async/Await,IAsyncResult的那些事儿

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  9. 删除数据表中除id外其他字段相同的冗余信息

    删除一个信息表中除id外其他字段都相同的冗余信息,如下 id name addr 1 a b 2 a b 3 b c 删除这个表中的冗余信息 即应该是 id name addr 1 a b 3 b c ...

随机推荐

  1. 3种不同的ContextMenu右键菜单演示

    简单使用的右键菜单,希望能帮助大家.下面是截图和实例代码 实例预览 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...

  2. jQuery cxDialog 对话框

    cxDialog 是基于 jQuery 的对话框插件,支持自定义外观样式,同时兼容 Zepto,方便在移动端使用. 版本: jQuery v1.7+ | Zepto v1.0+ jQuery cxDi ...

  3. C# AOP框架入门

    AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.Spring框架用的核心技术就是AOP,是函数式编程的一 ...

  4. CSS3 使用自定义字体

    CSS3 @font-face 规则 在 CSS3 之前,web 设计师必须使用已在用户计算机上安装好的字体.通过 CSS3,web 设计师可以使用他们喜欢的任意字体.当您您找到或购买到希望使用的字体 ...

  5. AdaBoost

    一直想写Adaboost来着,但迟迟未能动笔.其算法思想虽然简单"听取多人意见,最后综合决策",但一般书上对其算法的流程描述实在是过于晦涩.昨日11月1日下午,邹博在我组织的机器学 ...

  6. YYText-显示富文本

    github地址: https://github.com/ibireme/YYText CocoaPods安装: pod 'YYText' 1.YYLabel使用注意 private lazy var ...

  7. Android不同屏幕适配

    1.尽量使用线性布局(LinearLayout)和相对布局(RelativeLayout),不要使用绝对布局. 2.尽量使用dip和sp,不要使用px. 3.为不同的分辨率提供不同的布局文件和图片.  ...

  8. tomcat <context path>的意义及作用

    context path 是在tomcat 要支持多个应用时对每个应用的docBase做区别时的区分符. 打个比方假如你有两个请求:一个为 http:localhost:8080/test1/hell ...

  9. iOS中UINavigationController控制器使用详解

    一.概述 UINavigationController用来管理视图控制器,在多视图控制器中常用.它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹 ...

  10. Objective—C基础学习总结

        1. (1)面向过程:一种以事件为中心的编程思想         (2)面向对象:一种以对象为中心的编程思想        2.get和set是用来访问和修改对象里的属性值           ...