简体   繁体   English

使用Thrust在Cuda中划分线程的作业

[英]Dividing jobs for threads in Cuda using Thrust

I have a testing code that needs to update keys inside a device_vector of a class. 我有一个测试代码,需要更新类的device_vector中的键。 Therefore, how do I divide portions of the work to especific threads? 因此,如何将工作的各个部分划分为特定的线程?

Example of the code without the division: 不进行除法的代码示例:

__global__ void UpdateKeys(Request* vector, int size, int seed, int qt_threads){
   curandState_t state;
   curand_init(seed, threadIdx.x, 0, &state);
   int id = blockIdx.x * blockDim.x + threadIdx.x;
   if(id < size){
       vector[i].key_ = (curand(&state % 100) / 100;
   }
}

That vector is passed as a thrust::device_vector. 该矢量作为推力:: device_vector传递。

Examples of what I want: 我想要的例子:

1000 keys and 2000 threads: use only 1000 and give a key to each one. 1000个密钥和2000个线程:仅使用1000,并分别给每个密钥。
1000 keys and 1000 threads: use it all. 1000个键和1000个线程:全部使用。
1 key and 100 threads: use 1 thread. 1个键和100个线程:使用1个线程。
500 keys and 250 threads: each thread take care of 2. 500个键和250个线程:每个线程负责2个。
240 keys and 80 threads: each thread take care of 3. 240个键和80个线程:每个线程需要处理3个。

If you modify your basic kernel structure like this: 如果您像这样修改基本内核结构:

__global__ void UpdateKeys(Request* vector, int size, int seed, int qt_threads){
   curandState_t state;
   curand_init(seed, threadIdx.x, 0, &state);
   int id = blockIdx.x * blockDim.x + threadIdx.x;
   int gid = blockDim.x * gridDim.x;
   for(; id < size; id += gid){
       vector[id].key_ = (curand(&state) % 100) / 100;
   }
}

then it should be possible for any legal one dimensional block size (and number of one dimensional blocks) to process as many or as few inputs as you choose to provide via the size parameter. 那么任何合法的一维数据块大小(和一维数据块的数量)都应该有可能处理您通过size参数选择提供的任意数量的输入。 If you run more threads than keys, some threads will do nothing. 如果运行的线程多于键,则某些线程将不起作用。 If you run less threads than keys, some threads will process multiple keys. 如果运行的线程少于键,则某些线程将处理多个键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM