Reply to comment

я открыл код reduction из toolkit, однако такой код мне не очень понятен, я написал свой, видимо это тоже каскадное суммирование, он работает, однако мне кажется, что выдает неверные значения, так как есть реализация обычная для тех же данных (того же изображения), и эти ср. арифметические отличаются от тех. Я разбил задачу на две стадии, сделав предварительный массив сумм.

  1. __global__ void __meanStage1(float *A, float *Sum, long N, long M)
  2. {
  3.     long iS = blockIdx.y * gridDim.x + blockIdx.x;
  4.     for (long j= blockIdx.y * BLOCK_SIZE; j< (blockIdx.y+1) * BLOCK_SIZE ; j++)
  5.                 for (long i= blockIdx.x * BLOCK_SIZE; i< (blockIdx.x+1) * BLOCK_SIZE ; i++)
  6.             if( i < N && j < M)
  7.                 Sum[iS] = Sum[iS] + A[j * M + i];
  8.     __syncthreads();
  9. }
  10. __global__ void __meanStage2(float *a,float *mean, float *sum, long dX, long dY, long N, long M)
  11. {
  12.         *mean = 0;
  13.     for (long i=0;i<dX * dY;i++)
  14.         *mean = *mean + sum[i];
  15.     if (M*N!=0)
  16.     *mean = (*mean)/(N*M);
  17.     else
  18.     *mean = 0;
  19.     __syncthreads();
  20. }
  21. void mean(float *a,float &Means, long N, long M)
  22. {
  23.         float *sum,*mean;
  24.         long iS,jS;
  25.         iS = N / BLOCK_SIZE;
  26.         jS = M / BLOCK_SIZE;
  27.         cudaMalloc((void**) &mean , sizeof(float));
  28.         cudaMalloc((void**) &sum , iS * jS * sizeof(float));
  29.         cudaMemset(sum,0, iS * jS * sizeof(float));
  30.     dim3 dimBlock1(1, 1);
  31.     dim3 dimGrid1(N/BLOCK_SIZE, M/BLOCK_SIZE);
  32.     __meanStage1<<<dimGrid1, dimBlock1>>>(a, sum, N, M);
  33.     dim3 dimBlock2(1, 1);
  34.     dim3 dimGrid2(1, 1);
  35.     __meanStage2<<<dimGrid2, dimBlock2>>>(a, mean, sum, iS, jS, N, M);
  36.     cudaFree(sum);
  37.     cudaMemcpy(&Means, mean, sizeof(float),cudaMemcpyDeviceToHost);
  38.     cudaFree(mean);
  39. }

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <i> <table> <td> <tr> <th>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Images can be added to this post.

More information about formatting options

Copyright © 2008-2011 Alex Tutubalin