////////////////////////////////////////////////////////////////////////////
//
// 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. HTML5 Canvas 实现的9个 Loading 效果

    Sonic.js 是一个很小的 JavaScript 类,用于创建基于 HTML5 画布的加载图像.更强大的是 Sonic.js 还提供了基于现成的例子的创建工具,可以帮助你实现更多自定义的(Load ...

  2. 原生andriod浏览器回退后dom(click)事件全体失效问题探究

    问题描述 今天同事遇到一个神一样的BUG: 在原生浏览器下,为dom元素绑定一个click事件,其中有个a标签外链,点击a后进入其他页面,点击浏览器后退后,页面点击事件全体失效! 我于是用ios测了下 ...

  3. [deviceone开发]-拼图小游戏

    一.简介 九宫格小游戏,可从本地图库载入一张图片,填充到9个ImageView,另涉及Timer计时.图库控件. 每个格子都是相同的控件,动态添加到首页中的,在初始化后,响应touch事件,之后通过多 ...

  4. 学习zepto.js(Hello World)

    Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jquery有着类似的api. 如果你会用jquery,那么你也会用zepto. 昨天听说了zepto.js,正好最近也比较闲 ...

  5. iOS UIView设置圆角

    UIView设置圆角 1.比较简单的情况,UIView四个角都是圆角: UIView *aView = [[UIView alloc] init]; aView.frame = CGRectMake( ...

  6. Javascript 中的window.parent ,window.top,window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  7. filezilla sftp 文件和linux 文件不同步的问题

    删除掉以前的链接,重新建立链接就好了

  8. Android时区及语言代码

    1. 设置默认时区   PRODUCT_PROPERTY_OVERRIDES += \         persist.sys.timezone=Asia/Shanghai\ 注:搜索“persist ...

  9. Android - ADB 的使用

    一.什么是ADB? 1.ADB全称Android Debug Bridge, 是android sdk里的一个工具,用这个工具可以直接操作管理android模拟器或者真实的andriod设备 2.AD ...

  10. Android 生成xml文件

    生成XML文件备份短信,其格式为: <?xml version="1.0" encoding="UTF-8" standalone="true& ...