【问题标题】:cuda triple riemann sumcuda 三重黎曼和
【发布时间】:2012-09-25 11:41:55
【问题描述】:

我正在尝试通过 cuda 执行三重黎曼和。我正在尝试将多维网格迭代器用于我的 sum 迭代器以避免嵌套循环。我使用的是 2.0 telsa 卡,因此无法使用嵌套内核。

对于我需要的每个 x,y,z 变量,我似乎没有得到完整的 0 -> N 次迭代。

__global__ void test(){
uint xIteration = blockDim.x * blockIdx.x + threadIdx.x;
uint yIteration = blockDim.y * blockIdx.y + threadIdx.y;
uint zIteration = blockDim.z * blockIdx.z + threadIdx.z;
printf("x: %d * %d + %d = %d\n y: %d * %d + %d = %d\n z: %d * %d + %d = %d\n", blockDim.x, blockIdx.x, threadIdx.x, xIteration, blockDim.y, blockIdx.y, threadIdx.y, yIteration,  blockDim.z, blockIdx.z, threadIdx.z, zIteration);
}

---- 由-----调用

int totalIterations = 128; // N value for single sum (i = 0; i < N)
dim3 threadsPerBlock(8,8,8);
dim3 blocksPerGrid((totalIterations + threadsPerBlock.x - 1) / threadsPerBlock.x, 
                   (totalIterations + threadsPerBlock.y - 1) / threadsPerBlock.y, 
                   (totalIterations + threadsPerBlock.z - 1) / threadsPerBlock.z);
test<<<blocksPerGrid, threadsPerBlock>>>();

---- 输出-----

x   y   z
...
7 4 0
7 4 1
7 4 2
7 4 3
7 4 4
7 4 5
7 4 6
7 4 7
7 5 0
7 5 1
7 5 2
7 5 3
7 5 4
7 5 5
7 5 6
7 5 7
7 6 0
7 6 1
7 6 2
7 6 3
7 6 4
7 6 5
7 6 6
7 6 7
7 7 0
7 7 1
7 7 2
7 7 3
7 7 4
7 7 5
7 7 6
7 7 7
...

输出被截断,我现在得到每个排列,对于 0

另外,如果我将 totalIterations > 128,例如 1024,程序以 6 的 cudaError 代码终止,我理解这是启动计时器到期。内核除了打印什么都不做,所以我不明白为什么它会超时。在辅助设备上运行它似乎暂时消除了这个问题。我们正在使用其中一个 teslas 来运行 X,但邮件中的 geforce 将成为新的显示设备,以释放两个 teslas 进行计算。

printf(...) 将被要求和的函数的执行所取代。

思路是替换序列码版本

for (int i = 0...)
    for (int j = 0 ..)
       for (int k = 0...)

我也不确定如何存储函数值,因为创建一个潜在的巨大(数百万 x 数百万 x 数百万)3D 数组然后减少它似乎没有内存效率,而是以某种方式将函数值连接成一些一种共享变量。

---- 设备信息(我们有 2 个这些卡,两者的输出相同----

Device 1: "Tesla C2050"
  CUDA Driver Version / Runtime Version          5.0 / 5.0
  CUDA Capability Major/Minor version number:    2.0
  Total amount of global memory:                 2687 MBytes (2817982464 bytes)
  (14) Multiprocessors x ( 32) CUDA Cores/MP:    448 CUDA Cores
  GPU Clock rate:                                1147 MHz (1.15 GHz)
  Memory Clock rate:                             1500 Mhz
  Memory Bus Width:                              384-bit
  L2 Cache Size:                                 786432 bytes
  Max Texture Dimension Size (x,y,z)             1D=(65536), 2D=(65536,65535), 3D=(2048,2048,2048)
  Max Layered Texture Size (dim) x layers        1D=(16384) x 2048, 2D=(16384,16384) x 2048
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 32768
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1536
  Maximum number of threads per block:           1024
  Maximum sizes of each dimension of a block:    1024 x 1024 x 64
  Maximum sizes of each dimension of a grid:     65535 x 65535 x 65535
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and execution:                 Yes with 2 copy engine(s)
  Run time limit on kernels:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Concurrent kernel execution:                   Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support enabled:                Yes
  Device is using TCC driver mode:               No
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Bus ID / PCI location ID:           132 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

【问题讨论】:

  • 第一件事是第一:你期望的输出是什么,你得到的输出是什么? totalIterations 的值是多少?这是否意味着每个维度的总数,或总体总数(XYZ 次迭代)?关于减少,你是对的 - 你会想要在运行中减少,而不是存储到内存然后减少。共享和全局临时存储的组合将是您的最佳选择。但首先你需要回答以上问题...
  • totalIterations 是单个维度(当前 X、Y、Z 的大小都相同)。我期望得到从 0 到 totalIteration 的 xIteration、yIteration 和 zIteration 的每个整数值。每次执行我都会得到每个迭代器的不同值,但我从来没有得到一组对应于 x、y、z 的每个排列的值。期望为 totalIterations = 2;具有 x、y、z 值的线程。一个线程的迭代器值将是 0,0,0,然后是 1,0,0,然后是 1,1,0、1,0,1 等,直到执行每个排列。
  • 当需要更多详细信息时,最好将该详细信息添加到问题中(单击“编辑”)。您能否在问题中发布具体的示例输入、预期输出、实际输出?
  • 对不起,这是我第一次发布查询。添加了详细信息。目前没有要求和的函数的“输入”或“输出”,因为我只是想证明我得到了每个排列。
  • 当我运行代码时,它工作正常。如果我将 3D 索引线性化为单个数字,我会得到 totalIterations*totalIterations*totalIterations 唯一值输出。你确定你的表中缺少行吗?尝试对输出进行排序以确保没有重复。我想你会发现你错了。 printf 不是免费的; 10 亿次 printf 调用可能会超过看门狗定时器。网格的 z 维度在大小上比其他维度更受限制,并且某些设备不支持 3D 网格。请务必查询您的设备属性以确保您正在启动合法网格。

标签: cuda


【解决方案1】:

我认为正如已经提到的那样,在设备代码中使用 printf 来验证 (x,y,z) 数组中的每个元素是否已被线程触及对于较大的 x,y,z 值是不明智的。

我根据您的代码创建了以下代码,以证明每个元素 x,y,z 都会被线程触及:

#include <stdio.h>
#define DATAVAL 1
#define cudaCheckErrors(msg) \
    do { \
        cudaError_t __err = cudaGetLastError(); \
        if (__err != cudaSuccess) { \
            fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
                msg, cudaGetErrorString(__err), \
                __FILE__, __LINE__); \
            fprintf(stderr, "*** FAILED - ABORTING\n"); \
            exit(1); \
        } \
    } while (0)

__global__ void test(int *data, int dim){
  uint xIteration = blockDim.x * blockIdx.x + threadIdx.x;
  uint yIteration = blockDim.y * blockIdx.y + threadIdx.y;
  uint zIteration = blockDim.z * blockIdx.z + threadIdx.z;

  data[((((zIteration*dim)+yIteration)*dim)+xIteration)]=DATAVAL;
}

int main(){
  int *testdata;
  int *result;
  int totalIterations = 128; // N value for single sum (i = 0; i < N)
  int testsize = totalIterations*totalIterations*totalIterations;
  dim3 threadsPerBlock(8,8,8);
  dim3 blocksPerGrid((totalIterations + threadsPerBlock.x - 1) / threadsPerBlock.x,  (totalIterations + threadsPerBlock.y - 1) / threadsPerBlock.y,  (totalIterations + threadsPerBlock.z - 1) / threadsPerBlock.z);
  cudaMalloc(&testdata, testsize*sizeof(int));
  cudaCheckErrors("cudaMalloc fail");
  cudaMemset(testdata, 0, testsize*sizeof(int));
  cudaCheckErrors("cudaMemset fail");
  result=(int *)malloc(testsize*sizeof(int));
  if (result == 0) {printf("malloc fail \n"); return 1;}
  memset(result, 0, testsize*sizeof(int));
  test<<<blocksPerGrid, threadsPerBlock>>>(testdata, totalIterations);
  cudaDeviceSynchronize();
  cudaCheckErrors("Kernel launch failure");
  cudaMemcpy(result, testdata, testsize*sizeof(int), cudaMemcpyDeviceToHost);
  cudaCheckErrors("cudaMemcpy failure");

  for (unsigned i=0; i<testsize; i++)
    if (result[i] != DATAVAL) {printf("fail! \n"); return 1;}

  printf("Success \n");
  return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多