Learn CUDA Programming第二章 scaling image例子报错 invalid argument

本文主要是介绍Learn CUDA Programming第二章 scaling image例子报错 invalid argument,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

从Learn CUDA programming 的GitHub网站下载的例子在我的Ubuntu上运行会出现错误

Example Code如下:

#include<stdio.h>
#include"scrImagePgmPpmPackage.h"//Kernel which calculate the resized image
__global__ void createResizedImage(unsigned char *imageScaledData, int scaled_width, float scale_factor, cudaTextureObject_t texObj)
{const unsigned int tidX = blockIdx.x*blockDim.x + threadIdx.x;const unsigned int tidY = blockIdx.y*blockDim.y + threadIdx.y;const unsigned index = tidY*scaled_width+tidX;// Step 4: Read the texture memory from your texture reference in CUDA KernelimageScaledData[index] = tex2D<unsigned char>(texObj,(float)(tidX*scale_factor),(float)(tidY*scale_factor));
}int main(int argc, char*argv[])
{int height=0, width =0, scaled_height=0,scaled_width=0;//Define the scaling ratio	float scaling_ratio=0.5;unsigned char*data;unsigned char*scaled_data,*d_scaled_data;char inputStr[1024] = {"aerosmith-double.pgm"};char outputStr[1024] = {"aerosmith-double-scaled.pgm"};cudaError_t returnValue;//Create a channel Description to be used while linking to the tecturecudaArray* cu_array;cudaChannelFormatKind kind = cudaChannelFormatKindUnsigned;cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, kind);get_PgmPpmParams(inputStr, &height, &width);	//getting height and width of the current imagedata = (unsigned char*)malloc(height*width*sizeof(unsigned char));printf("\n Reading image width height and width [%d][%d]", height, width);scr_read_pgm( inputStr , data, height, width );//loading an image to "inputimage"scaled_height = (int)(height*scaling_ratio);scaled_width = (int)(width*scaling_ratio);scaled_data = (unsigned char*)malloc(scaled_height*scaled_width*sizeof(unsigned char));printf("\n scaled image width height and width [%d][%d]", scaled_height, scaled_width);//Allocate CUDA ArrayreturnValue = cudaMallocArray( &cu_array, &channelDesc, width, height);returnValue = (cudaError_t)(returnValue | cudaMemcpy( cu_array, data, height * width * sizeof(unsigned char), cudaMemcpyHostToDevice));if(returnValue != cudaSuccess)printf("\n Got error while running CUDA API Array Copy");// Step 1. Specify texturestruct cudaResourceDesc resDesc;memset(&resDesc, 0, sizeof(resDesc));resDesc.resType = cudaResourceTypeArray;resDesc.res.array.array = cu_array;// Step 2. Specify texture object parametersstruct cudaTextureDesc texDesc;memset(&texDesc, 0, sizeof(texDesc));texDesc.addressMode[0] = cudaAddressModeClamp;texDesc.addressMode[1] = cudaAddressModeClamp;texDesc.filterMode = cudaFilterModePoint;texDesc.readMode = cudaReadModeElementType;texDesc.normalizedCoords = 0;// Step 3: Create texture objectcudaTextureObject_t texObj = 0;cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL);if(returnValue != cudaSuccess) printf("\n Got error while running CUDA API Bind Texture");cudaMalloc(&d_scaled_data, scaled_height*scaled_width*sizeof(unsigned char) );dim3 dimBlock(32, 32,1);dim3 dimGrid(scaled_width/dimBlock.x,scaled_height/dimBlock.y,1);printf("\n Launching grid with blocks [%d][%d] ", dimGrid.x,dimGrid.y);createResizedImage<<<dimGrid, dimBlock>>>(d_scaled_data,scaled_width,1/scaling_ratio, texObj);returnValue = (cudaError_t)(returnValue | cudaDeviceSynchronize());returnValue = (cudaError_t)(returnValue |cudaMemcpy (scaled_data , d_scaled_data, scaled_height*scaled_width*sizeof(unsigned char), cudaMemcpyDeviceToHost ));if(returnValue != cudaSuccess) printf("\n Got error while running CUDA API kernel");// Step 5: Destroy texture objectcudaDestroyTextureObject(texObj);scr_write_pgm( outputStr, scaled_data, scaled_height, scaled_width, "####" ); //storing the image with the detectionsif(data != NULL)free(data);if(cu_array !=NULL)cudaFreeArray(cu_array);if(scaled_data != NULL)free(scaled_data);if(d_scaled_data!=NULL)cudaFree(d_scaled_data);return 0;
}

根据cudaGetErrorString(returnValue) 的输出来看,错误定位到这两句

returnValue = cudaMemcpy( cu_array, data, height * width * sizeof(unsigned char), cudaMemcpyHostToDevice)

cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL);

很明显是上一条语句的失败导致创建纹理对象失败

一开始还怀疑是不是作者读pgm的接口有问题,换成呗被图形学验证过无数次的stb_image这个库后还是报这两句错。

于是我开始怀疑CUDA API是否随着版本更新有变化,毕竟作者完成这本书是用的CUDA 10.x开发环境,而我的CUDA版本已经是12.0了。

最后发现cudaArray这个类型在新版本中从Host端复制到Device端需要使用cudaMemcpy2DToArray这个接口

重新编译

nvcc -c image_scaling.cu

nvcc -o image_scaling image_scaling.o scrImagePgmPpmPackage.o

发现已经不报错并且生成了缩放后的图片

不过我的代码是由stb_image读取的,所以在读取图片后CPU传给GPU前它的数据就是上下翻转的,因此GPU做完Kernel运算后缩放的图片转存后的图片也是上下翻转的。

这篇关于Learn CUDA Programming第二章 scaling image例子报错 invalid argument的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/824247

相关文章

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法

《Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法》本文解析Spring框架中BeanCreationExce... 目录引言一、问题描述1.1 报错示例假设我们有一个简单的Java类,代表一个用户信息的实体类:然后,

解决若依微服务框架启动报错的问题

《解决若依微服务框架启动报错的问题》Invalidboundstatement错误通常由MyBatis映射文件未正确加载或Nacos配置未读取导致,需检查XML的namespace与方法ID是否匹配,... 目录ruoyi-system模块报错报错详情nacos文件目录总结ruoyi-systnGLNYpe

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno