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

Why `cuD3D11GetDevice` success, but still got a `0` device? #124

Open GF-Huang opened 6 months ago

GF-Huang commented 6 months ago

image

kunzmi commented 6 months ago

0 is a perfectly valid device value, they are more or less equal to the device ID. (In very old Cuda versions, the value used to be a 32-bit pointer, hence the name in the structure, but in current versions, the value mostly corresponds to the device ID)

Michael

GF-Huang commented 6 months ago

But the Pointer is zero as an IntPtr which used for other 3rd library, is it really OK?

For example MarshalInterface<IDirect3DDevice>.FromAbi(device.Pointer) will return null.

kunzmi commented 6 months ago

CUdevice.Pointer is just an int, not an IntPtr. CUdevice is also a Cuda specific type and is not supposed to be used outside of the Cuda API. Your example with MarshalInterface<IDirect3DDevice>.FromAbi(device.Pointer) can't work with a Cuda-API provided value, it will only work with adapter.NativePointer.

In the orginal C++ header CUdevice is defined as following:

typedef int CUdevice_v1;                                     /**< CUDA device */
typedef CUdevice_v1 CUdevice;                                /**< CUDA device */

As C# does not have something like a typedef in C++, I use structs to define the custom type. At the end it is just an int. Hope that clarifies things.