kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

CudaDataTypeTranslator #91

Closed chenw11 closed 3 years ago

chenw11 commented 3 years ago

There is a static class in ManagedCuda.BasicType called CudaDataTypeTranslator. I'm trying to use it to convert a device buffer from ushort to cuFloatComplex. Are there any example you can provide please? The following code represent what I mean:

The Cudavariable is initialize as: CudaDeviceVariable(ushort) d_ushortBuffer;

Then a ushort buffer is copied into this device buffer; d_ushortBuffer.CopyToDevice(h_bufferPtr);

Now how do I convert this cuda variable into cuFloatComplex data type using the CudaDataTypeTranslator static class?

kunzmi commented 3 years ago

CudaDataTypeTranslator is only meant to give a C# datatype name for CudaDatatype enum used in some Cuda libraries, e.g. Cusparse. It doesen't do any datatype conversion. If you want to change the datatype of a CudaDeviceVariable, like a reinterprete_cast in C++, you can simply create a new CudaDeviceVariable using the inner DevicePointer. If you want to really change the datatype, i.e. the binary representation; you have to either write your own kernel for that, or have a look at the NPPs extension methods, there are some convert-function, but not sure for ushort->cuFloatComplex.

chenw11 commented 3 years ago

Thanks for the detail info. I tried to write my own kernel using the __uint2float_rd() Type Casting Intrinsics from cuda. It looks like it is working now. My kernel looks like this: extern "C" { global void inputCastKernel(cufftComplex output, const void input, const int inputBitDepth) { int index = threadIdx.x + blockIdx.xblockDim.x; unsigned char in = (unsigned char*)input; output[index].x = __uint2float_rd(in[index]); output[index].y = 0; } }