user2968731
user2968731

Reputation: 117

How can I convert Npp8u * to CUdeviceptr

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!

Upvotes: 2

Views: 1192

Answers (3)

ArchaeaSoftware
ArchaeaSoftware

Reputation: 4422

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;

Upvotes: 2

talonmies
talonmies

Reputation: 72348

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.

Upvotes: 4

Andy
Andy

Reputation: 1715

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 );

Upvotes: -1

Related Questions