<small id='FSaDs'></small><noframes id='FSaDs'>

      <tfoot id='FSaDs'></tfoot>

          <bdo id='FSaDs'></bdo><ul id='FSaDs'></ul>

        <legend id='FSaDs'><style id='FSaDs'><dir id='FSaDs'><q id='FSaDs'></q></dir></style></legend>
      1. <i id='FSaDs'><tr id='FSaDs'><dt id='FSaDs'><q id='FSaDs'><span id='FSaDs'><b id='FSaDs'><form id='FSaDs'><ins id='FSaDs'></ins><ul id='FSaDs'></ul><sub id='FSaDs'></sub></form><legend id='FSaDs'></legend><bdo id='FSaDs'><pre id='FSaDs'><center id='FSaDs'></center></pre></bdo></b><th id='FSaDs'></th></span></q></dt></tr></i><div id='FSaDs'><tfoot id='FSaDs'></tfoot><dl id='FSaDs'><fieldset id='FSaDs'></fieldset></dl></div>

        CUDA如何获取网格、块、线程大小和并行化非方阵计算

        时间:2023-12-03

      2. <i id='L4L9k'><tr id='L4L9k'><dt id='L4L9k'><q id='L4L9k'><span id='L4L9k'><b id='L4L9k'><form id='L4L9k'><ins id='L4L9k'></ins><ul id='L4L9k'></ul><sub id='L4L9k'></sub></form><legend id='L4L9k'></legend><bdo id='L4L9k'><pre id='L4L9k'><center id='L4L9k'></center></pre></bdo></b><th id='L4L9k'></th></span></q></dt></tr></i><div id='L4L9k'><tfoot id='L4L9k'></tfoot><dl id='L4L9k'><fieldset id='L4L9k'></fieldset></dl></div>

        <small id='L4L9k'></small><noframes id='L4L9k'>

          <bdo id='L4L9k'></bdo><ul id='L4L9k'></ul>

              <legend id='L4L9k'><style id='L4L9k'><dir id='L4L9k'><q id='L4L9k'></q></dir></style></legend>
            • <tfoot id='L4L9k'></tfoot>
                  <tbody id='L4L9k'></tbody>
                1. 本文介绍了CUDA如何获取网格、块、线程大小和并行化非方阵计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 CUDA 的新手,需要帮助理解一些事情.我需要帮助并行化这两个 for 循环.具体如何设置dimBlock 和dimGrid 使其运行得更快.我知道这看起来像 sdk 中的向量添加示例,但该示例仅适用于方阵,当我尝试修改 128 x 1024 矩阵的代码时,它无法正常工作.

                  I am new to CUDA and need help understanding some things. I need help parallelizing these two for loops. Specifically how to setup the dimBlock and dimGrid to make this run faster. I know this looks like the vector add example in the sdk but that example is only for square matrices and when I try to modify that code for my 128 x 1024 matrix it doesn't work properly.

                  __global__ void mAdd(float* A, float* B, float* C)
                  {
                      for(int i = 0; i < 128; i++)
                      {
                          for(int j = 0; j < 1024; j++)
                          {
                              C[i * 1024 + j] = A[i * 1024 + j] + B[i * 1024 + j];
                          }
                      }
                  }
                  

                  这段代码是一个更大循环的一部分,是代码中最简单的部分,所以我决定尝试并行化 thia 并同时学习 CUDA.我已经阅读了指南,但仍然不明白如何获得正确的编号.网格/块/线程去并有效地使用它们.

                  This code is part of a larger loop and is the simplest portion of the code, so I decided to try to paralleize thia and learn CUDA at same time. I have read the guides but still do not understand how to get the proper no. of grids/block/threads going and use them effectively.

                  推荐答案

                  正如您所写,该内核是完全串行的.为执行它而启动的每个线程都将执行相同的工作.

                  As you have written it, that kernel is completely serial. Every thread launched to execute it is going to performing the same work.

                  CUDA(以及 OpenCL 和其他类似的单程序、多数据"类型的编程模型)背后的主要思想是您进行数据并行"操作 - 因此,必须执行许多相同的、很大程度上独立的操作次 - 并编写一个执行该操作的内核.然后启动大量(半)自治线程以跨输入数据集执行该操作.

                  The main idea behind CUDA (and OpenCL and other similar "single program, multiple data" type programming models) is that you take a "data parallel" operation - so one where the same, largely independent, operation must be performed many times - and write a kernel which performs that operation. A large number of (semi)autonomous threads are then launched to perform that operation across the input data set.

                  在您的数组加法示例中,数据并行操作是

                  In your array addition example, the data parallel operation is

                  C[k] = A[k] + B[k];
                  

                  对于0到128 * 1024之间的所有k.每个加法操作是完全独立的,没有顺序要求,因此可以由不同的线程执行.要在 CUDA 中表达这一点,可以这样编写内核:

                  for all k between 0 and 128 * 1024. Each addition operation is completely independent and has no ordering requirements, and therefore can be performed by a different thread. To express this in CUDA, one might write the kernel like this:

                  __global__ void mAdd(float* A, float* B, float* C, int n)
                  {
                      int k = threadIdx.x + blockIdx.x * blockDim.x;
                  
                      if (k < n)
                          C[k] = A[k] + B[k];
                  }
                  

                  [免责声明:代码在浏览器中编写,未经测试,使用风险自负]

                  [disclaimer: code written in browser, not tested, use at own risk]

                  在这里,串行代码的内循环和外循环被每个操作的一个 CUDA 线程替换,并且我在代码中添加了限制检查,以便在启动的线程多于所需操作的情况下,不会出现缓冲区溢出发生.如果内核是这样启动的:

                  Here, the inner and outer loop from the serial code are replaced by one CUDA thread per operation, and I have added a limit check in the code so that in cases where more threads are launched than required operations, no buffer overflow can occur. If the kernel is then launched like this:

                  const int n = 128 * 1024;
                  int blocksize = 512; // value usually chosen by tuning and hardware constraints
                  int nblocks = n / blocksize; // value determine by block size and total work
                  
                  madd<<<nblocks,blocksize>>>mAdd(A,B,C,n);
                  

                  然后 256 个块,每个块包含 512 个线程,将被启动到 GPU 硬件上以并行执行阵列加法操作.请注意,如果输入数据大小不能表示为块大小的整数倍,则需要向上取整块数以覆盖完整的输入数据集.

                  Then 256 blocks, each containing 512 threads will be launched onto the GPU hardware to perform the array addition operation in parallel. Note that if the input data size was not expressible as a nice round multiple of the block size, the number of blocks would need to be rounded up to cover the full input data set.

                  以上所有内容都是对 CUDA 范式的一个非常简单的操作的极大简化概述,但也许它提供了足够的洞察力让您继续自己.CUDA 现在相当成熟,网上有很多好的免费教育材料,您可能可以用来进一步阐明我在这个答案中掩盖的编程模型的许多方面.

                  All of the above is a hugely simplified overview of the CUDA paradigm for a very trivial operation, but perhaps it gives enough insight for you to continue yourself. CUDA is rather mature these days and there is a lot of good, free educational material floating around the web you can probably use to further illuminate many of the aspects of the programming model I have glossed over in this answer.

                  这篇关于CUDA如何获取网格、块、线程大小和并行化非方阵计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我可以将 Visual Studio 2010 的 C++ 编译器与 Visual Studio 2008 的 C++ 下一篇:无法找到调试信息或与 Visual Studio 的不匹配

                  相关文章

                  <tfoot id='Xp23l'></tfoot>

                      • <bdo id='Xp23l'></bdo><ul id='Xp23l'></ul>

                      <small id='Xp23l'></small><noframes id='Xp23l'>

                    1. <legend id='Xp23l'><style id='Xp23l'><dir id='Xp23l'><q id='Xp23l'></q></dir></style></legend>
                      <i id='Xp23l'><tr id='Xp23l'><dt id='Xp23l'><q id='Xp23l'><span id='Xp23l'><b id='Xp23l'><form id='Xp23l'><ins id='Xp23l'></ins><ul id='Xp23l'></ul><sub id='Xp23l'></sub></form><legend id='Xp23l'></legend><bdo id='Xp23l'><pre id='Xp23l'><center id='Xp23l'></center></pre></bdo></b><th id='Xp23l'></th></span></q></dt></tr></i><div id='Xp23l'><tfoot id='Xp23l'></tfoot><dl id='Xp23l'><fieldset id='Xp23l'></fieldset></dl></div>