CUDA中的2D中值滤波:如何有效地将全局内存复制到共享内存

2024-03-05

我正在尝试用一个窗口做一个中值滤波器x*y where x and y是奇数 和 程序的参数。

我的想法是首先查看一个块中可以执行多少个线程以及有多少共享内存可用,如下所示:

void cudaInit(int imgX, int imgY, int kx, int ky, int* cudaVars){
        int device;
        int deviceCount;
        cudaDeviceProp deviceProp;

            cudaGetDevice(&device);
            cudaGetDeviceProperties(&deviceProp, device);
        int kxMed = kx/2;
        int kyMed = ky/2;
        int n = deviceProp.maxThreadsPerBlock;
        while(f(n,kxMed,kyMed)>deviceProp.sharedMemPerBlock){
            n = n/2;
        }

        cudaVars[0] = n;
        cudaVars[1] = imgX/cudaVars[0];
        cudaVars[2] = imgY/cudaVars[0];
     }
    }



void mediaFilter_gpuB(uchar4* h_img,int width, int height, int kx, int ky){

    assert(h_img!=NULL && width!=0 && height!=0);
        int dev=0;
    cudaDeviceProp deviceProp;
    //DEVICE
    uchar4* d_img;
    uchar4* d_buf;

    int cudaVars[3]={0};
    cudaInit(width,height,kx,ky,cudaVars);
checkCudaErrors(cudaMalloc((void**) &(d_img), width*height*sizeof(unsigned char)*4));
    checkCudaErrors(cudaMalloc((void**) &(d_buf), width*height*sizeof(unsigned char)*4));

    cudaGetDevice(&dev);
    cudaGetDeviceProperties(&deviceProp,dev);
    checkCudaErrors(cudaMemcpy(d_img, h_img, width*height*sizeof(uchar4), cudaMemcpyHostToDevice));

    dim3 dimGrid(cudaVars[1],cudaVars[2],1);
    dim3 threads(cudaVars[0],1,1);
    mediaFilterB<<<dimGrid,threads,f(cudaVars[0],kx/2,ky/2)>>>(d_buf,d_img,width,height, kx,ky,cudaVars[0]);

    checkCudaErrors(cudaMemcpy(h_img, d_buf, width*height*sizeof(uchar4), cudaMemcpyDeviceToHost));
    checkCudaErrors(cudaFree(d_img));
    checkCudaErrors(cudaFree(d_buf));

}
__device__ void fillSmem(int* sMem, uchar4* buf, int width, int height, int kx, int ky){
    int kyMed=ky/2;
    int kxMed=kx/2;
    int sWidth = 2*kxMed+gridDim.x;
    int sHeight =2*kyMed+gridDim.x;
    int X = blockIdx.x*gridDim.x+threadIdx.x;
    int Y = blockIdx.y*gridDim.y;
    int j=0;
    while(threadIdx.x+j < sHeight){
        for(int i=0;i<sWidth;i++){
            sMem[threadIdx.x*gridDim.x+gridDim.x*j+i] = buf[X + i +  (threadIdx.x + Y)*width + j*width].x;
        }
        j++;
    }
}

目前,在函数中mediaFilterB,我只是将全局内存复制到共享内存,但是需要花费很多时间,即大约5图像中的秒数8000*8000像素。另一方面,没有 CUDA 的顺序算法需要23秒计算图像的中值滤波器。

我知道我在将全局内存复制到共享内存的过程中做错了,而且我的算法效率非常低,但我不知道如何纠正它。


我正在提供此问题的答案,以将其从未回答的列表中删除。

关于如何使用共享内存通过 CUDA 改进中值滤波的经典示例是由 Accelereyes 开发的代码,可从以下帖子下载:

中值过滤:CUDA 提示和技巧 http://blog.accelereyes.com/blog/2010/03/04/median-filtering-cuda-tips-and-tricks/

这个想法是分配一个(BLOCK_WIDTH+2)x(BLOCK_HEIGHT+2)大小的共享内存。第一步,将外部元件归零。仅当这些元素对应于真实图像元素时,它们才会被填充全局内存值,否则它们将保持为零以进行填充。

为了方便起见,我在下面提供了完整的工作代码。

#include <iostream>  
#include <fstream>   

using namespace std;

#define BLOCK_WIDTH 16 
#define BLOCK_HEIGHT 16

/*******************/
/* iDivUp FUNCTION */
/*******************/
int iDivUp(int a, int b){ return ((a % b) != 0) ? (a / b + 1) : (a / b); }

/********************/
/* CUDA ERROR CHECK */
/********************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
    if (code != cudaSuccess) 
    {
        fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
        if (abort) exit(code);
    }
}

/**********************************************/
/* KERNEL WITH OPTIMIZED USE OF SHARED MEMORY */
/**********************************************/
__global__ void Optimized_Kernel_Function_shared(unsigned short *Input_Image, unsigned short *Output_Image, int Image_Width, int Image_Height)
{
    const int tx_l = threadIdx.x;                           // --- Local thread x index
    const int ty_l = threadIdx.y;                           // --- Local thread y index

    const int tx_g = blockIdx.x * blockDim.x + tx_l;        // --- Global thread x index
    const int ty_g = blockIdx.y * blockDim.y + ty_l;        // --- Global thread y index

    __shared__ unsigned short smem[BLOCK_WIDTH+2][BLOCK_HEIGHT+2];

    // --- Fill the shared memory border with zeros
    if (tx_l == 0)                      smem[tx_l]  [ty_l+1]    = 0;    // --- left border
    else if (tx_l == BLOCK_WIDTH-1)     smem[tx_l+2][ty_l+1]    = 0;    // --- right border
    if (ty_l == 0) {                    smem[tx_l+1][ty_l]      = 0;    // --- upper border
        if (tx_l == 0)                  smem[tx_l]  [ty_l]      = 0;    // --- top-left corner
        else if (tx_l == BLOCK_WIDTH-1) smem[tx_l+2][ty_l]      = 0;    // --- top-right corner
        }   else if (ty_l == BLOCK_HEIGHT-1) {smem[tx_l+1][ty_l+2]  = 0;    // --- bottom border
        if (tx_l == 0)                  smem[tx_l]  [ty_l+2]    = 0;    // --- bottom-left corder
        else if (tx_l == BLOCK_WIDTH-1) smem[tx_l+2][ty_l+2]    = 0;    // --- bottom-right corner
    }

    // --- Fill shared memory
                                                                    smem[tx_l+1][ty_l+1] =                           Input_Image[ty_g*Image_Width + tx_g];      // --- center
    if ((tx_l == 0)&&((tx_g > 0)))                                      smem[tx_l]  [ty_l+1] = Input_Image[ty_g*Image_Width + tx_g-1];      // --- left border
    else if ((tx_l == BLOCK_WIDTH-1)&&(tx_g < Image_Width - 1))         smem[tx_l+2][ty_l+1] = Input_Image[ty_g*Image_Width + tx_g+1];      // --- right border
    if ((ty_l == 0)&&(ty_g > 0)) {                                      smem[tx_l+1][ty_l]   = Input_Image[(ty_g-1)*Image_Width + tx_g];    // --- upper border
            if ((tx_l == 0)&&((tx_g > 0)))                                  smem[tx_l]  [ty_l]   = Input_Image[(ty_g-1)*Image_Width + tx_g-1];  // --- top-left corner
            else if ((tx_l == BLOCK_WIDTH-1)&&(tx_g < Image_Width - 1))     smem[tx_l+2][ty_l]   = Input_Image[(ty_g-1)*Image_Width + tx_g+1];  // --- top-right corner
         } else if ((ty_l == BLOCK_HEIGHT-1)&&(ty_g < Image_Height - 1)) {  smem[tx_l+1][ty_l+2] = Input_Image[(ty_g+1)*Image_Width + tx_g];    // --- bottom border
         if ((tx_l == 0)&&((tx_g > 0)))                                 smem[tx_l]  [ty_l+2] = Input_Image[(ty_g-1)*Image_Width + tx_g-1];  // --- bottom-left corder
        else if ((tx_l == BLOCK_WIDTH-1)&&(tx_g < Image_Width - 1))     smem[tx_l+2][ty_l+2] = Input_Image[(ty_g+1)*Image_Width + tx_g+1];  // --- bottom-right corner
    }
    __syncthreads();

    // --- Pull the 3x3 window in a local array
    unsigned short v[9] = { smem[tx_l][ty_l],   smem[tx_l+1][ty_l],     smem[tx_l+2][ty_l],
                            smem[tx_l][ty_l+1], smem[tx_l+1][ty_l+1],   smem[tx_l+2][ty_l+1],
                            smem[tx_l][ty_l+2], smem[tx_l+1][ty_l+2],   smem[tx_l+2][ty_l+2] };    

    // --- Bubble-sort
    for (int i = 0; i < 5; i++) {
        for (int j = i + 1; j < 9; j++) {
            if (v[i] > v[j]) { // swap?
                unsigned short tmp = v[i];
                v[i] = v[j];
                v[j] = tmp;
            }
         }
    }

    // --- Pick the middle one
    Output_Image[ty_g*Image_Width + tx_g] = v[4];
}

/********/
/* MAIN */
/********/
int main()
{
    const int Image_Width = 1580;
    const int Image_Height = 1050;

    // --- Open data file
    ifstream is;         is.open("C:\\Users\\user\\Documents\\Project\\Median_Filter\\Release\\Image_To_Be_Filtered.raw", ios::binary );

    // --- Get file length
    is.seekg(0, ios::end);
    int dataLength = is.tellg();
    is.seekg(0, ios::beg);

    // --- Read data from file and close file
    unsigned short* Input_Image_Host = new unsigned short[dataLength * sizeof(char) / sizeof(unsigned short)];
    is.read((char*)Input_Image_Host,dataLength);
    is.close();

    // --- CUDA warm up
    unsigned short *forFirstCudaMalloc; gpuErrchk(cudaMalloc((void**)&forFirstCudaMalloc, dataLength * sizeof(unsigned short)));
    gpuErrchk(cudaFree(forFirstCudaMalloc));

    // --- Allocate host and device memory spaces 
    unsigned short *Output_Image_Host = (unsigned short *)malloc(dataLength);
    unsigned short *Input_Image; gpuErrchk(cudaMalloc( (void**)&Input_Image, dataLength * sizeof(unsigned short))); 
    unsigned short *Output_Image; gpuErrchk(cudaMalloc((void**)&Output_Image, dataLength * sizeof(unsigned short))); 

    // --- Copy data from host to device
    gpuErrchk(cudaMemcpy(Input_Image, Input_Image_Host, dataLength, cudaMemcpyHostToDevice));// copying Host Data To Device Memory For Filtering

    // --- Grid and block sizes
    const dim3 grid (iDivUp(Image_Width, BLOCK_WIDTH), iDivUp(Image_Height, BLOCK_HEIGHT), 1);      
    const dim3 block(BLOCK_WIDTH, BLOCK_HEIGHT, 1); 

    /**********************************************/
    /* KERNEL WITH OPTIMIZED USE OF SHARED MEMORY */
    /**********************************************/

    cudaFuncSetCacheConfig(Optimized_Kernel_Function_shared, cudaFuncCachePreferShared);
    Optimized_Kernel_Function_shared<<<grid,block>>>(Input_Image, Output_Image, Image_Width, Image_Height);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());

    // --- Copy results back to the host
    gpuErrchk(cudaMemcpy(Output_Image_Host, Output_Image, dataLength, cudaMemcpyDeviceToHost));

    // --- Open results file, write results and close the file
    ofstream of2;         of2.open("C:\\Users\\angelo\\Documents\\Project\\Median_Filter\\Release\\Filtered_Image.raw",  ios::binary);
    of2.write((char*)Output_Image_Host, dataLength);
    of2.close();

    cout << "\n Press Any Key To Exit..!!";
    gpuErrchk(cudaFree(Input_Image));

    delete Input_Image_Host;
    delete Output_Image_Host;

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CUDA中的2D中值滤波:如何有效地将全局内存复制到共享内存 的相关文章

随机推荐

  • 默默忽略remove()

    实体 A 引用 多对一 实体 B 具有从 B 到 A 的反向 映射 引用 此外 还存在 A 到 C 的引用以及 C 到 A 的反向引用 当我发出entityManager remove A 然后flush 时 不会生成 删除 但也没有例外
  • ld:找不到 AudioUnit 框架

    我正在添加另一个项目 即使我添加了所需的所有库 我也会收到此错误 这是错误详细信息 Ld Users alialzahrani Library Developer Xcode DerivedData IMS3 ezltqoccjhjpvua
  • 如何在 React.js 中预加载图像?

    如何在 React js 中预加载图像 我有下拉选择组件 其工作方式类似于菜单 但我必须预加载项目的图像图标 因为有时它们在第一次打开时不可见 我努力了 https github com sambernard react preload h
  • 使用 xslt 从 xml 转换而来的平面文件中的行数

    下面是我用来将 xml 转换为平面文件的 xsl 它还满足各种其他所需条件
  • iOS7 深色键盘和浅色键盘之间的切换

    In iOS7 we have both a dark and a light keyboard Is it possible for me to change between these in my app by code textfie
  • 当任何包含公式的单元格发生更改时触发宏

    我有一个包含大约 50 个单元格 包含公式 的工作表 这些单元格根据外部工作簿中的单元格而变化 当这些单元格中的任何一个更改其值时 我想触发某个宏 Worksheet change 事件不起作用 并且 Worksheet Calculate
  • C# RichEditBox 性能极慢(加载 4 分钟)

    The RichEditBoxC 中的控件 我使用 VS 2005 性能较差 我将包含 45 000 行彩色文本的 2 5 MB RTF 文件加载到控件中 需要 4 分钟 我将相同的 RTF 加载到 Windows XP 写字板的 RTF
  • Python Flask 从像 render_template 这样的变量渲染文本

    我知道烧瓶功能render template 我必须给出模板的文件名 但现在我想渲染模板的字符串 即模板的内容 这就说得通了 但我现在不想解释原因 如何简单地渲染模板的文本 您可以使用render template string http
  • 什么是滑动窗口算法?例子?

    在解决几何问题时 我遇到了一种称为滑动窗口算法的方法 确实找不到任何学习材料 详细信息 该算法是关于什么的 我认为它更多的是一种技术而不是算法 这是一种可用于各种算法的技术 我认为通过以下示例可以最好地理解该技术 想象一下我们有这个数组 5
  • 更改 div 与背景图像之间的 Flex 间距

    hi i am remaking the google chrome home page but i cant seem to do the part at the bottom of the page were the most used
  • typeof 在 IE 中返回“未知”

    我有一个窗口 在关闭之前我会刷新底层页面 if opener typeof opener Refresh undefined opener Refresh 如果我离开原来的打开页面 这段代码会抛出一个 没有权限 error 调试代码发现ty
  • 如何在禁用状态下自定义 mat-form-field

    我正在尝试自定义角度材料 mat form field 我可以使用以下命令自定义下划线边框 ng deep mat form field ripple background color yellow 现在我正在尝试将禁用状态下的下划线边框自
  • 从其他线程访问 VT 数据是否安全?

    从辅助线程更改 Virtual TreeView 数据是否安全 如果是 我应该使用关键部分 甚至是同步方法 吗 我担心当我从另一个线程写入 VT 的数据记录时 主线程同时调用其重绘 并且此刷新将导致同时读取同一记录 我想补充一下 我在应用程
  • 为评估为 true 的 IN 条件元素设置限制

    table t Id price Date 1 30 2021 05 09 1 24 2021 04 26 1 33 2021 04 13 2 36 2021 04 18 3 15 2021 04 04 3 33
  • 生成具有总和约束的排列

    I have n可变长度的集合 并希望从每个集合中获取总和在一定范围内的所有项目排列 例如在R我们可以做的 set1 lt c 10 15 20 set2 lt c 8 9 set3 lt c 1 2 3 4 permutations lt
  • 在 Jupyter 中可视化 TensorFlow 图的简单方法?

    可视化 TensorFlow 图的官方方法是使用 TensorBoard 但有时我只是想在使用 Jupyter 时快速浏览一下图 是否有一个快速的解决方案 最好基于 TensorFlow 工具或标准 SciPy 包 如 matplotlib
  • 在 Meteor 中使用 jQuery 插件

    我一直在尝试向 Meteor 添加 jQuery 插件 但 Meteor 拒绝让该插件在客户端工作 例子是我有这个插件 它允许我随机播放一堆名为jQuery 随机播放 https vestride github io Shuffle 但是当
  • ant 无法导入 R.java

    我正在开发一个 ant 构建文件 独立于 Eclipse 来构建我的 Android 应用程序 我需要生成 R java 文件 然而 当它尝试从以下位置构建我的项目时 我已经成功地完成了src它抱怨找不到导入的 R java 文件 我看到它
  • JAX-WS Web 服务和 @rolesAllowed

    是否可以使用 RolesAllowedJAX WS Web 服务上的注释 如果是的话如何 我在 glassfish 3 1 1 上有一个使用基本身份验证的网络服务 但使用表达的限制 RolesAllowed被忽略 角色信息应该可用 因为我可
  • CUDA中的2D中值滤波:如何有效地将全局内存复制到共享内存

    我正在尝试用一个窗口做一个中值滤波器x y where x and y是奇数 和 程序的参数 我的想法是首先查看一个块中可以执行多少个线程以及有多少共享内存可用 如下所示 void cudaInit int imgX int imgY in