cuda中thread id

  1 ////////////////////////////////////////////////////////////////////////////
  2 //
  3 // Copyright 1993-2015 NVIDIA Corporation.  All rights reserved.
  4 //
  5 // Please refer to the NVIDIA end user license agreement (EULA) associated
  6 // with this source code for terms and conditions that govern your use of
  7 // this software. Any use, reproduction, disclosure, or distribution of
  8 // this software and related documentation outside the terms of the EULA
  9 // is strictly prohibited.
 10 //
 11 ////////////////////////////////////////////////////////////////////////////
 12
 13 //
 14 // This sample illustrates the usage of CUDA events for both GPU timing and
 15 // overlapping CPU and GPU execution.  Events are inserted into a stream
 16 // of CUDA calls.  Since CUDA stream calls are asynchronous, the CPU can
 17 // perform computations while GPU is executing (including DMA memcopies
 18 // between the host and device).  CPU can query CUDA events to determine
 19 // whether GPU has completed tasks.
 20 //
 21
 22 // includes, system
 23 #include <stdio.h>
 24
 25 // includes CUDA Runtime
 26 #include <cuda_runtime.h>
 27
 28 // includes, project
 29 #include <helper_cuda.h>
 30 #include <helper_functions.h> // helper utility functions
 31
 32 __global__ void increment_kernel(int *g_data, int inc_value)
 33 {
 34     int idx = blockIdx.x * blockDim.x + threadIdx.x;// thread id 计算分三级:thread, block .grid .
 35     g_data[idx] = g_data[idx] + inc_value; //每一个线程,把对应的操作数增加一个常数
 36 }
 37
 38 bool correct_output(int *data, const int n, const int x)
 39 {
 40     for (int i = 0; i < n; i++)
 41         if (data[i] != x)
 42         {
 43             printf("Error! data[%d] = %d, ref = %d\n", i, data[i], x);
 44             return false;
 45         }
 46
 47     return true;
 48 }
 49
 50 int main(int argc, char *argv[])
 51 {
 52     int devID;
 53     cudaDeviceProp deviceProps;
 54
 55     printf("[%s] - Starting...\n", argv[0]);
 56
 57     // This will pick the best possible CUDA capable device
 58     devID = findCudaDevice(argc, (const char **)argv);
 59
 60     // get device name
 61     checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
 62     printf("CUDA device [%s]\n", deviceProps.name);
 63
 64     int n = 16 * 1024 * 1024;
 65     int nbytes = n * sizeof(int);
 66     int value = 26;
 67
 68     // allocate host memory
 69     int *a = 0;
 70     checkCudaErrors(cudaMallocHost((void **)&a, nbytes));
 71     memset(a, 0, nbytes);
 72
 73     // allocate device memory
 74     int *d_a=0;
 75     checkCudaErrors(cudaMalloc((void **)&d_a, nbytes));
 76     checkCudaErrors(cudaMemset(d_a, 255, nbytes));
 77
 78     // set kernel launch configuration
 79     dim3 threads = dim3(1024, 1);//每个block1024个threads,一维
 80     dim3 blocks  = dim3(n / threads.x, 1);//block数量,
 81
 82     // create cuda event handles
 83     cudaEvent_t start, stop;//运算计时
 84     checkCudaErrors(cudaEventCreate(&start));
 85     checkCudaErrors(cudaEventCreate(&stop));
 86
 87     StopWatchInterface *timer = NULL;
 88     sdkCreateTimer(&timer);
 89     sdkResetTimer(&timer);
 90
 91     checkCudaErrors(cudaDeviceSynchronize());
 92     float gpu_time = 0.0f;
 93     printf("a=%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",a[n-1-0],a[n-1-1],a[n-1-2],a[n-1-3],a[n-1-4],a[n-1-5],a[n-1-6],a[n-1-7],a[n-1-8]);
 94     // asynchronously issue work to the GPU (all to stream 0)
 95     sdkStartTimer(&timer);
 96     cudaEventRecord(start, 0);
 97     cudaMemcpyAsync(d_a, a, nbytes, cudaMemcpyHostToDevice, 0);//把host中变量a复制到device中的变量d_a
 98     increment_kernel<<<blocks, threads, 0, 0>>>(d_a, value);//device执行
 99     cudaMemcpyAsync(a, d_a, nbytes, cudaMemcpyDeviceToHost, 0);//device结果复制到host
100     cudaEventRecord(stop, 0);
101     sdkStopTimer(&timer);
102
103     // have CPU do some work while waiting for stage 1 to finish
104     unsigned long int counter=0;
105
106     while (cudaEventQuery(stop) == cudaErrorNotReady)
107     {
108         counter++;
109     }
110
111     checkCudaErrors(cudaEventElapsedTime(&gpu_time, start, stop));
112
113     // print the cpu and gpu times
114     printf("time spent executing by the GPU: %.2f\n", gpu_time);
115     printf("time spent by CPU in CUDA calls: %.2f\n", sdkGetTimerValue(&timer));
116     printf("CPU executed %lu iterations while waiting for GPU to finish\n", counter);
117     printf("a=%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\n",a[n-1-0],a[n-1-1],a[n-1-2],a[n-1-3],a[n-1-4],a[n-1-5],a[n-1-6],a[7],a[8]);
118
119     // check the output for correctness
120     bool bFinalResults = correct_output(a, n, value);
121
122     // release resources
123     checkCudaErrors(cudaEventDestroy(start));
124     checkCudaErrors(cudaEventDestroy(stop));
125     checkCudaErrors(cudaFreeHost(a));
126     checkCudaErrors(cudaFree(d_a));
127
128     exit(bFinalResults ? EXIT_SUCCESS : EXIT_FAILURE);
129 }

一个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,......,依此类推,

时间: 2024-07-30 10:13:15

cuda中thread id的相关文章

Thread ID vs Pthread Handle(pthread_t)

在很多线程实现的案例中,pthread_t作为抽象类型,被指定为长度为4的整行作为线程ID. 在一些iSeries服务器的线程实现上,线程ID是一个64位的整型数值和pthread_t是一个抽象 的结构体包含数值和一些其他的参数.抽象化允许进程容纳上千个线程. 如果不允许移植,不允许进程访问pthread_t的内部数据和尺寸,例如对比线程ID,对于可移植的对比, 使用pthread_equal函数.文档通常描述pthread_t作为线程句柄,避免因为使用一个整型表示线程ID 而混淆概念, Thr

CUDA __shared__ thread、block、grid之间的一维关系 (例子chapter5 dot点积(GPU高性能编程))

chapter5里重要的例子是dot,来解释一个block内多个thread的共享内存和同步. __shared__共享内存:“对于在GPU上启动的每个线程块,cuda c编译器都将创建该变量的一个副本.线程块中的每个线程都共享这块内存,并和其他线程块无关,这使一个线程块中多个线程能够在计算上进行通信和协作” __syncthreads():确保线程块中的每个线程都执行完__syncthreads()前面的语句后,在往下执行. 例子是Grid->一维Block->一维Thread: 通过实例代

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

说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部分可以同时执行:对于比较耗时的操作(例如io,数据库操作),或者等待响应(如WCF通信)的操作,可以单独开启后台线程来执行,这样主线程就不会阻塞,可以继续往下执行:等到后台线程执行完毕,再通知主线程,然后做出对应操作! 在C#中开启新线程比较简单 static void Main(string[]

雪花算法中机器id保证全局唯一

关于分布式id的生成系统, 美团技术团队之前已经有写过一篇相关的文章, 详见 Leaf——美团点评分布式ID生成系统 通常在生产中会用Twitter开源的雪花算法来生成分布式主键 雪花算法中的核心就是机器id和数据中心id, 通常来说数据中心id可以在配置文件中配置, 通常一个服务集群可以共用一个配置文件, 而机器id如果也放在配置文件中维护的话, 每个应用就需要一个独立的配置, 难免也会出现机器id重复的问题 解决方案: 1. 通过启动参数去指定机器id, 但是这种方式也会有出错的可能性 2.

CUDA中使用多维数组

今天想起一个问题,看到的绝大多数CUDA代码都是使用的一维数组,是否可以在CUDA中使用一维数组,这是一个问题,想了各种问题,各种被77的错误状态码和段错误折磨,最后发现有一个cudaMallocManaged函数,这个函数可以很好的组织多维数组的多重指针的形式 ,后来发现,这个问题之前在Stack Overflow中就有很好的解决.先贴一下我自己的代码实现: 1 #include "cuda_runtime.h" 2 #include "device_launch_para

关于void*类型的用法(相当于OC中的id类型)

关于void*类型的用法(相当于OC中的id类型) 1.C++语言在对于void* 类型的使用很特别,因为void* 可以间接引用任何其他数据类型的指针,比如int*.float*甚至抽象数据类型的指针,而且可以从void* 强制转换为任何其他数据类型的指针,所以使用起来有时候会比较危险.如果开始将一个void*的指针间接引用一个float*的指针,然后将这个void*指针强制转化为一个int*类型的指针,编译器不会给出错误甚至警告,但是输出的数据却匪夷所思,如果再强制转换会float*则不会出

CSS中的id选择器和class选择器简单介绍

<!-- CSS中选择器 CSS有两种选择器id和class,总之如果说你想在HTML元素中设置CSS属性, 你要在元素中设置id和class选择器.那么我们现在来一个一个的介绍这两中选择器 id选择器: HTML中的元素属性用id来设置id选择器,CSS中的id选择器是用"#"来定义的 比如: #para1 { text-align:center; color:red; } 这样就定义了一个选择器,什么是选择器,在网上查的是说需要改变的HTML元素,很正确,在一开始我们的内部样

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

删除一个信息表中除id外其他字段都相同的冗余信息,如下 id name addr 1 a b 2 a b 3 b c 删除这个表中的冗余信息 即应该是 id name addr 1 a b 3 b c 设table为t Sql:delete from tableName where id not in (select min(id) from tableName group by name, addr…)

CUDA 中 单精度浮点操作和 双精度浮点操作

在CUDA 中如果不指明是 单精度, CUDA会调用双精度实现.    (血泪呀!!!) 如果要使用单精度,要指明, 即使用 形如__fmul_rn(x,y)的函数. 详见链接!! http://stackoverflow.com/questions/14406364/different-results-for-cuda-addition-on-host-and-on-gpu?rq=1