How can I convert Npp8u * to CUdeviceptr

那年仲夏 提交于 2019-12-08 05:33:26

问题


I am new to cuda driver Api interface but I think that CUdeviceptr looks like a handle parameter.So I confused about the convertion between CUdeviceptr and npp8u *.

Npp8u * src;
......
unsigned char temp;
temp = src;
CUdeviceptr devPtr;
.......
devPtr = (CUdeviceptr)temp;

I try to write the convertion like above,is that right!


回答1:


cuDevicePtr is, in fact, a raw pointer, not a handle. You can see the original architect of the CUDA driver and driver API discuss this here (and school me in the process). So if you have an existing "typed" device pointer, it is safe to cast it to a cuDevicePtr, or vice versa, for example:

cuDevicePtr m;
cuMemAlloc(&m, size);

Npp8U* p = (Npp8U*)(m);
// Pass p to NPP library functions...

is legal and should work.




回答2:


By demoting the pointer to unsigned char before converting to CUdeviceptr, you are masking off all but the least significant 8 bits of src.

Just write:

Npp8u *src; CUdeviceptr devPtr = (CUdeviceptr) (uintptr_t) src;




回答3:


Typically you wouldn't do this explicitly, but rather cast Npp8u* to a void ** when passing to cudaMalloc:

Npp8u * src;
int length = ...
cudaMalloc( (void **)(&src), sizeof( Npp8u ) * length );


来源:https://stackoverflow.com/questions/23813275/how-can-i-convert-npp8u-to-cudeviceptr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!