将C / C ++ for循环转换为CUDA [英] Converting C/C++ for loops into CUDA

查看:1243
本文介绍了将C / C ++ for循环转换为CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何将基本的C / C ++循环转换为CUDA内核。让我们简单说一下:

  for(int i = 0; i  for j = 0; j  
...使用i和j的代码....
}
pre>

每一个i都需要计算MAXj元素。
这对于一些人可能是非常基本的,但我真的在这里挣扎。
假设Maxj是大约一百万,MAXj = 1000000;并且有我们想要所有线程工作的地方。
我已经成功只有内循环:

  int tid = threadIdx.x + blockDim.x * blockIdx .x + blockDim.x * gridDim.x * blockIdx.y; 

使用2d块,我如何并行化这种循环?它们在C中非常常见,学习如何做到这一点非常有用。

解决方案

2D循环的种类是通过使用1D块和Grids

  dim3块(MAXj,1); 
dim3 grid(MAXi,1);

kernel<<< grids,blocks,1>>()

__global__ kernel()
{
int i = blockIdx.x;
int j = threadIdx.x;

...使用i和j的代码。

}

如果MAXj和MAXi非常大,内循环被分为线程,外循环被分成块(2D块)



值,那么您需要将其划分为小组并进行计算。代码与此线程中发布的代码非常相似


I would like to understand how to convert basic C/C++ loops to a CUDA kernel. Let's put it simple:

for (int i=0;i < MAXi;i++)
   for(int j=0;j< MAXj;j++){

       ...code that uses i and j....
   }

Every single i would need to compute MAXj elements. It could be very basic for some people but I am really struggling here. Let's say that Maxj is around a million, MAXj=1000000; and there is where we want all threads work. I have been successful with only the inner loop:

int tid=threadIdx.x + blockDim.x*blockIdx.x + blockDim.x*gridDim.x*blockIdx.y;

using 2d blocks, how can I parallelize this kind of loops? They are very common in C and it would be very useful to learn how to do it.

解决方案

one best way to divide these kinds of 2D loops is by using 1D blocks and Grids

dim3 blocks(MAXj, 1);
dim3 grids(MAXi, 1);

kernel<<<grids, blocks, 1>>>()

__global__ kernel()
{
   int i = blockIdx.x;
   int j = threadIdx.x;

   ...code that uses i and j....

}

The inner loop is been divided into threads and outer loop is divided into blocks (2D blocks)

if MAXj and MAXi are very large values, then you need to divide it into small groups and compute it. The code is quite similar to the one posted in this thread.

这篇关于将C / C ++ for循环转换为CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆