2016-07-20 89 views
1

我刚刚开始使用CUDA。现在我有一个问题。 我有N * N矩阵,窗口尺度是8x8。我想把这个矩阵细分成多个子矩阵并且找到它的最大值。 例如,如果我有64 * 64的矩阵,所以我将有8 * 8的小矩阵,并找出8个最大值。最后,我将所有最大值保存到新数组中,但其顺序始终更改。我想找到解决方案,让他们在正确的顺序在CUDA中查找矩阵的最大值

__global__ void calculate_emax_kernel(float emap[],float emax[], int img_height, int img_width,int windows_size) 
{ 
    int x_index = blockIdx.x*blockDim.x+threadIdx.x; 
    int y_index = blockIdx.y*blockDim.y+threadIdx.y; 

    int num_row_block = img_height/windows_size; 
    int num_col_block = img_width/windows_size; 
    __shared__ float window_elements[256]; 
    __shared__ int counter; 
    __shared__ int emax_count; 

    if (threadIdx.x == 0) emax_count = 0; 
    __syncthreads(); 
    int index; 
    int emax_idx = 0; 


    if(y_index >= img_height|| x_index >= img_width) return; 
    for(int i = 0; i < num_row_block; i++) 
    { 
     for(int j = 0; j < num_col_block; j++) 
     { 
      counter = 0; 
      if(y_index >= i*windows_size && y_index < (i+1)*windows_size 
        && x_index >= j*windows_size && x_index < (j+1)*windows_size) 
      { 
       int idx = y_index*img_height + x_index; 
       index = atomicAdd(&counter, 1); 

       window_elements[index] = emap[idx]; 
       __syncthreads(); 


       // reduction 
       unsigned int k = (windows_size*windows_size)/2; 
       while(k != 0) 
       { 
        if(index < k) 
        { 
         window_elements[index] = fmaxf(window_elements[index], window_elements[index+k]); 

        } 
        k /= 2; 
       } 
       if(index == 0) 
       { 
        emax[i*num_row_block+j] = window_elements[index]; 
       } 
      } 
      __syncthreads(); 
     } 
     __syncthreads(); 
    } 
    __syncthreads(); 
} 

这是我的配置

void construct_emax(float *input,float *output, int img_height, int img_width) 
{ 
    int windows_size = 4; 
    float * d_input, * d_output; 
    cudaMalloc(&d_input, img_width*img_height*sizeof(float)); 
    cudaMalloc(&d_output, img_width*img_height*sizeof(float)); 

    cudaMemcpy(d_input, input, img_width*img_height*sizeof(float), cudaMemcpyHostToDevice); 
    dim3 blocksize(16,16); 
    dim3 gridsize; 

    gridsize.x=(img_width+blocksize.x-1)/blocksize.x; 
    gridsize.y=(img_height+blocksize.y-1)/blocksize.y; 

    calculate_emax_kernel<<<gridsize,blocksize>>>(d_input,d_output,img_height,img_width,windows_size); 

} 
+0

你的意思是“我将有一个8×8的8×8小矩阵,找出8×8的最大值”? – kangshiyin

+0

@ kangshiyin对不起,这很难解释,它意味着我将输入矩阵分成一些小矩阵,它取决于窗口的大小。 例如,如果我有16 * 16矩阵和8 * 8窗口大小,所以我将有4个小矩阵。并找出每个小矩阵的最大值。 –

+0

什么是网格/块配置? – kangshiyin

回答

2

有了CUDA,parallel reduction是棘手的; segmented parallel reduction更棘手。现在你正在以二维方式进行,而你的片段/窗口比线程块小。

对于大窗口大小,我不认为这是一个问题。您可以使用一个线程块来减少一个窗口。例如,如果你有一个16x16的窗口,你可以简单地使用16x16的线程块。如果窗口尺寸更大,例如64x64,则仍然可以使用16x16线程块。首先在数据加载期间将64x64窗口缩小为16x16元素,然后在线程块内将其缩小为1标量。

对于小于块大小的窗口大小,您必须减少每个线程块的多个窗口以获得更高的性能。您可以使用当前的块/网格配置,其中每个256线程块(16x16)负责16个4x4窗口。但是这不会是最佳的,因为每个32线程的包装分为两部分(2x16)。这对于coalesced global memory access并不合适,并且很难将2x16 warp映射到一个或多个4x4窗口以进行有效的并行缩减。

或者我建议你使用256线程的一维线程块。每个线程减少一个m窗口。然后,您可以使用二维网格来覆盖整个图像。

const int m = window_size; 
dim3 blocksize(256); 
dim3 gridsize((img_width+255)/256, (img_height+m-1)/m); 

在内核的功能,你可以

  1. 减少每个m X m窗口全球数据加载时的1倍m向量;
  2. 使用树简化方法将1x m向量减少为标量。

下面的代码是一个概念性的演示,当m是2的幂和m <= 32的功能。你可以进一步修改它以获得更好的边界检查m

#include <assert.h> 
#include <cuda.h> 
#include <thrust/device_vector.h> 

__global__ void calculate_emax_kernel(const float* input, float* output, 
             int height, int width, int win_size, 
             int out_width) { 
    const int tid = threadIdx.x; 
    const int i = blockIdx.y * win_size; 
    const int j = blockIdx.x * 256 + tid; 
    const int win_id = j % win_size; 

    __shared__ float smax[256]; 

    float tmax = -1e20; 
    if (j < width) { 
    for (int tile = 0; tile < win_size; tile++) { 
     if (i + tile < height) { 
     tmax = max(tmax, input[(i + tile) * width + j]); 
     } 
    } 
    } 
    smax[tid] = tmax; 
    for (int shift = win_size/2; shift > 0; shift /= 2) { 
    if (win_id < shift) { 
     smax[tid] = max(smax[tid], smax[tid + shift]); 
    } 
    } 
    if (win_id == 0 && j < width) { 
    output[blockIdx.y * out_width + (j/win_size)] = smax[tid]; 
    } 
} 

int main() { 
    const int height = 1024; 
    const int width = 1024; 
    const int m = 4; 
    thrust::device_vector<float> in(height * width); 
    thrust::device_vector<float> out(
     ((height + m - 1)/m) * ((width + m - 1)/m)); 

    dim3 blocksize(256); 
    dim3 gridsize((width + 255)/256, (height + m - 1)/m); 

    assert(m == 2 || m == 4 || m == 8 || m == 16 || m == 32); 
    calculate_emax_kernel<<<gridsize, blocksize>>>(
     thrust::raw_pointer_cast(in.data()), 
     thrust::raw_pointer_cast(out.data()), 
     height, width, m, (width + m - 1)/m); 

    return 0; 
}