Я просто начинающий CUDA и пытаюсь использовать Faster Parallel Reductions on Kepler в своей программе, но я не получил результат, ниже функция того, что я делаю, вывод 0, I было бы полезно узнать, в чем моя ошибка?Быстрые параллельные сокращения на Kepler
#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;
}