开普勒

时间:2017-02-20 22:25:35

标签: cuda reduction kepler

我只是一个CUDA初学者,并尝试在我的程序中使用Faster Parallel Reductions on Kepler,但我没有得到结果,下面是我正在做的事情的函数,输出为0,我将不胜感激,知道我的错误是什么?

#ifndef __CUDACC__  
#define __CUDACC__
#endif

#include <cuda.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <iostream>
#include <cuda_runtime_api.h>
#include <device_functions.h>
#include <stdio.h>
#include <math.h>

__inline__ __device__
float warpReduceSum(float val) {
  for (int offset = warpSize/2; offset > 0; offset /= 2) 
    val += __shfl_down(val, offset);
  return val;
}

__inline__ __device__
float blockReduceSum(float val) {

  static __shared__ int shared[32]; // Shared mem for 32 partial sums
  int lane = threadIdx.x % warpSize;
  int wid = threadIdx.x / warpSize;

  val = warpReduceSum(val);     // Each warp performs partial reduction

  if (lane==0) shared[wid]=val; // Write reduced value to shared memory

  __syncthreads();              // Wait for all partial reductions

  //read from shared memory only if that warp existed
  val = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;

  if (wid==0) val = warpReduceSum(val); //Final reduce within first warp

  return val;
}

__global__ void deviceReduceKernel(float *in, float* out, size_t N)
{
  float sum = 0;
  //reduce multiple elements per thread
  for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N; i += blockDim.x * gridDim.x) 
  {
    sum += in[i];
  }
  sum = blockReduceSum(sum);
  if (threadIdx.x==0)
    out[blockIdx.x]=sum;
}

int main()
{
    int n = 1000000;
    float *b = new float[1]();
    float *d = new float[1]();
    float *a ;


    int blocks = (n/512)+1;
    float *d_intermediate;

    cudaMalloc((void**)&d_intermediate, n*sizeof(float));
    cudaMalloc((void**)&a, n*sizeof(float));

    cudaMemset(a, 1, n*sizeof(float));

    deviceReduceKernel<<<blocks, 512>>>(a, d_intermediate, n);
    deviceReduceKernel<<<1, 1024>>>(d_intermediate, &b[0], blocks);
    cudaMemcpy(d, b, sizeof(float), cudaMemcpyDeviceToHost);
    cudaFree(d_intermediate);
    std::cout << d[0];
    return 0;

}

1 个答案:

答案 0 :(得分:4)

您的代码存在各种问题:

  1. 如果您在使用CUDA代码时出现问题,则应使用proper cuda error checking并使用cuda-memcheck运行代码,然后再向其他人寻求帮助。即使您不理解错误输出,对于试图帮助您的其他人也会有所帮助。如果您使用此代码完成了此操作,则会告知您各种错误/问题

  2. 传递给CUDA内核的任何指针都应该是有效的CUDA设备指针。您的b指针是主机指针:

    float *b = new float[1]();
    

    所以你不能在这里使用它:

    deviceReduceKernel<<<1, 1024>>>(d_intermediate, &b[0], blocks);
                                                     ^
    

    由于您显然希望将其用于存储设备上的单个float数量,因此我们可以轻松地重复使用a指针。

  3. 出于类似的原因,这是不明智的:

    cudaMemcpy(d, b, sizeof(float), cudaMemcpyDeviceToHost);
    

    在这种情况下,bd都是主机指针。这不会将数据从设备复制到主机。

  4. 这可能不符合您的想法:

    cudaMemset(a, 1, n*sizeof(float));
    

    我想你认为这会填充数量为1的float数组,但事实并非如此。 cudaMemsetmemset一样,填充字节并获取字节数量。如果您使用它来填充float数组,则实际上是在创建一个填充了0x01010101的数组。我不知道将位模式转换为float数量时转化为什么值,但它不会给你float值1.我们将通过填充普通值来解决这个问题。带有循环的主机数组,然后将该数据传输到要还原的设备。

  5. 这是一个经过修改的代码,解决了上述问题,并为我正确运行:

    $ cat t1290.cu
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    
    __inline__ __device__
    float warpReduceSum(float val) {
      for (int offset = warpSize/2; offset > 0; offset /= 2)
        val += __shfl_down(val, offset);
      return val;
    }
    
    __inline__ __device__
    float blockReduceSum(float val) {
    
      static __shared__ int shared[32]; // Shared mem for 32 partial sums
      int lane = threadIdx.x % warpSize;
      int wid = threadIdx.x / warpSize;
    
      val = warpReduceSum(val);     // Each warp performs partial reduction
    
      if (lane==0) shared[wid]=val; // Write reduced value to shared memory
    
      __syncthreads();              // Wait for all partial reductions
    
      //read from shared memory only if that warp existed
      val = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
    
      if (wid==0) val = warpReduceSum(val); //Final reduce within first warp
    
      return val;
    }
    
    __global__ void deviceReduceKernel(float *in, float* out, size_t N)
    {
      float sum = 0;
      //reduce multiple elements per thread
      for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N; i += blockDim.x * gridDim.x)
      {
        sum += in[i];
      }
      sum = blockReduceSum(sum);
      if (threadIdx.x==0)
        out[blockIdx.x]=sum;
    }
    
    int main()
    {
            int n = 1000000;
            float b;
            float *a, *a_host;
            a_host = new float[n];
    
            int blocks = (n/512)+1;
            float *d_intermediate;
    
            cudaMalloc((void**)&d_intermediate, blocks*sizeof(float));
            cudaMalloc((void**)&a, n*sizeof(float));
            for (int i = 0; i < n; i++) a_host[i] = 1;
            cudaMemcpy(a, a_host, n*sizeof(float), cudaMemcpyHostToDevice);
    
            deviceReduceKernel<<<blocks, 512>>>(a, d_intermediate, n);
            deviceReduceKernel<<<1, 1024>>>(d_intermediate, a, blocks);
            cudaMemcpy(&b, a, sizeof(float), cudaMemcpyDeviceToHost);
            cudaFree(d_intermediate);
            std::cout << b << std::endl;
            return 0;
    }
    $ nvcc -arch=sm_35 -o t1290 t1290.cu
    $ cuda-memcheck ./t1290
    ========= CUDA-MEMCHECK
    1e+06
    ========= ERROR SUMMARY: 0 errors
    $