CUDA二维数组的内存申请和数据复制

weizhuoqun 2009-07-28 09:05:52
我有一个31*2的二维数组,下一步要把它复制到GPU上进行并行化处理计算,我应该怎么实施呢?我的程序写完了,编译成功也可以运行,但是这个数组似乎没有传到核函数当中去,因为这个数组本身就没有变化,请指点,非常感谢

float coordinate[31][2]={{1304.0,2312.0},{3639.0,1315.0},{4177.0,2244.0},{3712.0,1399.0},{3488.0,1535.0},{3326.0,1556.0},{3238.0,1229.0},{4196.0,1004.0},{4312.0,790.0},{4386.0,570.0},{3007.0,1970.0},{2562.0,1756.0},{2788.0,1491.0},{2381.0,1676.0},{1332.0,695.0},{3715.0,1678.0},{3918.0,2179.0},{4061.0,2370.0},{3780.0,2212.0},{3676.0,2578.0},{4029.0,2838.0},{4263.0,2931.0},{3429.0,1908.0},{3507.0,2367.0},{3394.0,2643.0},{3439.0,3201.0},{2935.0,3240.0},{3140.0,3550.0},{2545.0,2357.0},{2778.0,2826.0},{2370.0,2975.0}};

float (*GPU)[2];
size_t GPUPitch;
cudaMallocPitch((void**)&GPU,&GPUPitch,sizeof(float)*n,m);
cudaMemcpy2D(GPU,GPUPitch,coordinate,n,sizeof(float)*n,m,cudaMemcpyHostToDevice);
caculate<<<1,1,0>>>(GPU);
float (*asdcpu)[2];
cudaMemcpy2D(asdcpu,GPUPitch,GPU,n,sizeof(float)*n,m,cudaMemcpyDeviceToHost);

//核函数
__global__ void caculate(float (*asd)[2])
{
for(int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
asd[i][j]=asd[i][j]+10000.0;
}
}
}
...全文
1960 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
aban-mtd 2012-06-16
  • 打赏
  • 举报
回复
在CUDA如何使用二位数组(**[M][N]): http://blog.csdn.net/bendanban/article/details/7669624
gssar 2012-04-02
  • 打赏
  • 举报
回复
你好!请问哪个文档里有对cuda函数参数含义和实用方法的详细说明?谢谢!

[Quote=引用 2 楼 的回复:]
LZ仔细看下参数说明吧:

cudaError_t cudaMemcpy2D ( void * dst,
size_t dpitch,
const void * src,
size_t spitch,
size_t width,
size_t height,
enum cudaMemcpyKind kind
)

Co……
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhumilan 的回复:]
我想请问下 CUDA中 如何一次申请一个数组的二位纹理的标示
类似于opengl里面的 glGenTextures(TexNum, &texture[0]);
这样的语句
[/Quote]

申请2D数组,然后绑定.具体的API请查手册.
zhumilan 2009-08-02
  • 打赏
  • 举报
回复
我想请问下 CUDA中 如何一次申请一个数组的二位纹理的标示
类似于opengl里面的 glGenTextures(TexNum, &texture[0]);
这样的语句
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 weizhuoqun 的回复:]
您好,谢谢您的建议。还有一个问题就是,pitch的值怎么获得。因为我的第二个cudaMemcpy2D里的那个pitch我不知道怎么处理,怎么得到值
[/Quote]

主机上内存的pitch是width*sizeof(type)吧?gpu上内存的pitch,cudaMallocPitch不是已经返回了吗?
要注意的的是,用的是那里的指针,就要跟着用那里的pitch!
weizhuoqun 2009-07-31
  • 打赏
  • 举报
回复
您好,谢谢您的建议。还有一个问题就是,pitch的值怎么获得。因为我的第二个cudaMemcpy2D里的那个pitch我不知道怎么处理,怎么得到值
thinkfan 2009-07-31
  • 打赏
  • 举报
回复
可能有如下两个问题:

第一个memcpy
cudaMemcpy2D(GPU,GPUPitch,coordinate,n,sizeof(float)*n,m,cudaMemcpyHostToDevice);
spitch的单位是bytes,所以第四个参数n应该是sizeof(float)*n
如果你只取图片的一部分,第四个参数就大于第五个参数

第二个memcpy
cudaMemcpy2D(asdcpu,GPUPitch,GPU,n,sizeof(float)*n,m,cudaMemcpyDeviceToHost);
dpitch和spitch弄反了,spitch也不对,原因同上
Thomasyang5 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 weizhuoqun 的帖子:]
cudaMemcpy2D(GPU,GPUPitch,coordinate,n,sizeof(float)*n,m,cudaMemcpyHostToDevice);
cudaMemcpy2D(asdcpu,GPUPitch,GPU,n,sizeof(float)*n,m,cudaMemcpyDeviceToHost);
[/Quote]

改为
cudaMemcpy2D(GPU,GPUPitch,coordinate,sizeof(float)*m,sizeof(float)*m,m,cudaMemcpyHostToDevice);
cudaMemcpy2D(asdcpu,GPUPitch,GPU,sizeof(float)*m,sizeof(float)*m,m,cudaMemcpyDeviceToHost);
试试
  • 打赏
  • 举报
回复
还有第二个cudaMemcpy2D的pitch和内存指针是否配套?
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 l7331014 的回复:]
LZ仔细看下参数说明吧:

cudaError_t cudaMemcpy2D ( void * dst,
size_t dpitch,
const void * src,
size_t spitch,
size_t width,
size_t height,
enum cudaMemcpyKind kind
)

Copies a matrix (height rows of width bytes each) from the memory area pointed to by src to the memory area pointed to by dst, where kind is one of cudaMemcpyHostToHost, cudaMemcpyH…
[/Quote]

考虑一下,spitch是什么单位.
  • 打赏
  • 举报
回复
LZ仔细看下参数说明吧:

cudaError_t cudaMemcpy2D ( void * dst,
size_t dpitch,
const void * src,
size_t spitch,
size_t width,
size_t height,
enum cudaMemcpyKind kind
)

Copies a matrix (height rows of width bytes each) from the memory area pointed to by src to the memory area pointed to by dst, where kind is one of cudaMemcpyHostToHost, cudaMemcpyHostToDevice, cudaMemcpyDeviceToHost, or cudaMemcpyDeviceToDevice, and specifies the direction of the copy. dpitch and spitch are the widths in memory in bytes of the 2D arrays pointed to by dst and src, including any padding added to the end of each row. The memory areas may not overlap. Calling cudaMemcpy2D() with dst and src pointers that do not match the direction of the copy results in an undefined behavior. cudaMemcpy2D() returns an error if dpitch or spitch is greater than the maximum allowed.


Parameters:
dst - Destination memory address
dpitch - Pitch of destination memory
src - Source memory address
spitch - Pitch of source memory
width - Width of matrix transfer (columns in bytes)
height - Height of matrix transfer (rows)
kind - Type of transfer

353

社区成员

发帖
与我相关
我的任务
社区描述
CUDA高性能计算讨论
社区管理员
  • CUDA高性能计算讨论社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧