cuda的3DArray和TextureObject

  • cudaArray_t:数据存储的对象,用于存储 2D 或 3D 数据;不涉及纹理的采样(即读取数据时的插值、过滤等操作)。

       cudaExtent ca_extent;//定义三维内存区域的结构体
       ca_extent.width = vol_in->dim[0];
       ca_extent.height = vol_in->dim[1];
       ca_extent.depth = vol_in->dim[2];
       cudaChannelFormatDesc ca_descriptor = cudaCreateChannelDesc<float>();//定义cudaArray的数据格式,shape和type
       cudaMalloc3DArray(&state->dev_vol, &ca_descriptor, ca_extent);//分配三维数据块
    

    cudaMemcpy3DParms cpy_params = { 0 };//三维内存拷贝操作参数的结构体
    cpy_params.extent = ca_extent; //定义三维内存区域的结构体
    cpy_params.kind = cudaMemcpyHostToDevice; // 拷贝类型(主机到设备、设备到设备等)
    cpy_params.dstArray = state->dev_vol;// 目标数组,和 dstPtr 互斥使用

    //http://sites.google.com/site/cudaiap2009/cookbook-1#TOC-CUDA-3D-Texture-Example-Gerald-Dall
    // The pitched pointer is really tricky to get right. We give the
    // pitch of a row, then the number of elements in a row, then the
    // height, and we omit the 3rd dimension.
    cpy_params.srcPtr = make_cudaPitchedPtr((void*)vol_in->data, ca_extent.width * sizeof(float), ca_extent.width, ca_extent.height);// 源指针,和 srcArray 互斥使用

    cudaMemcpy3D(&cpy_params);//把vol数据拷贝到显存

  • cudaTextureObject_t:纹理对象;包含数据的地址信息,还包括了如何访问这些数据的元数据(如过滤模式、边界处理、坐标转换等)。高效的图像处理支持自动插值,只读操作

    // Specify texture
    struct cudaResourceDesc resDesc;//描述资源(如数组、纹理、表面等)的结构体
    memset(&resDesc, 0, sizeof(resDesc));
    resDesc.resType = cudaResourceTypeArray;//资源是一个 3D 数组。
    resDesc.res.array.array = state->dev_vol;// 3D 数组资源

    // Specify texture object parameters
    struct cudaTextureDesc texDesc;//描述纹理(texture)对象属性的结构体
    memset(&texDesc, 0, sizeof(texDesc));
    texDesc.addressMode[0] = cudaAddressModeWrap;//当坐标超出边界时,纹理坐标会循环回到起始位置。
    texDesc.addressMode[1] = cudaAddressModeWrap;//当坐标超出边界时,纹理坐标会循环回到起始位置。
    texDesc.filterMode = cudaFilterModeLinear;//当纹理坐标落在两个像素之间时使用最近邻插值。
    texDesc.readMode = cudaReadModeElementType;//按纹理的元素类型读取
    texDesc.normalizedCoords = 0;//纹理坐标范围通常是像素索引

    state->tex_vol = 0;//只读纹理内存
    cudaCreateTextureObject(&state->tex_vol, &resDesc, &texDesc, NULL);

  • cudaSurfaceObject_t。适合需要频繁修改数据,适合需要频繁修改数据,读写操作

    // Specify texture
    struct cudaResourceDesc resDesc;//描述资源(如数组、纹理、表面等)的结构体
    memset(&resDesc, 0, sizeof(resDesc));
    resDesc.resType = cudaResourceTypeArray;//资源是一个 3D 数组。
    resDesc.res.array.array = state->dev_vol;// 3D 数组资源

    cudaCreateSurfaceObject(&surfObj, &resDesc);

      __global__ void cuda_clear_3d_array_kernel(cudaSurfaceObject_t surf, dim3 kdim)
      {
          // calculate surface coordinates
          unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
          unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
          unsigned int z = blockIdx.z * blockDim.z + threadIdx.z;
    
          // write to memeory
          if (x < kdim.x && y < kdim.y && z < kdim.z) {
              surf3Dwrite<float>(0.f, surf, x * 4, y, z, cudaBoundaryModeTrap);
          }
      }
    
相关推荐
觉醒法师1 分钟前
HarmonyOS开发 - 电商App实例二( 网络请求http)
前端·http·华为·typescript·harmonyos·ark-ts
沈剑心1 分钟前
Kotlin的协程,真能提升编程效率么?
android·前端·kotlin
堕落年代12 分钟前
Vue主流的状态保存框架对比
前端·javascript·vue.js
OpenTiny社区23 分钟前
TinyVue的DatePicker 组件支持日期面板单独使用啦!
前端·vue.js
冴羽23 分钟前
Svelte 最新中文文档教程(22)—— Svelte 5 迁移指南
前端·javascript·svelte
树上有只程序猿27 分钟前
Vue3组件通信:多个实战场景,轻松玩转复杂数据流!
前端·vue.js
剪刀石头布啊35 分钟前
css属性值计算过程
前端·css
bin915339 分钟前
DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加列宽调整功能,示例Table14基础固定表头示例
前端·javascript·vue.js·ecmascript·deepseek
小华同学ai42 分钟前
吊打中文合成!这款开源语音神器效果炸裂,逼真到离谱!
前端·后端·github
颜酱1 小时前
后台系统从零搭建(三)—— 具体页面之部门管理(抽离通用的增删改查逻辑)
前端·javascript·react.js