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

Assembly.GetTypes() problem #103

Open fbordignon opened 3 years ago

fbordignon commented 3 years ago

Hi guys, I am using managedCuda as part of a dll that is loaded as plugin for another C# software. To package the plugin, an application provided by the software vendor runs the following piece of code:

private static IEnumerable<Type> GetPluginsFromAssembly(Assembly assembly) => ((IEnumerable<Type>) 
                                    assembly.GetTypes()).Where<Type>((Func<Type, bool>)
                                       (p => typeof (Plugin).IsAssignableFrom(p) && p.IsPublic && !p.IsAbstract));

From what I gather it is listing all types inside the .dll that I've built and its dependencies to search for plugins. But when it gets to the managedCuda.dll it crashes with the following exception:

[0] {"Could not load type 'ManagedCuda.BasicTypes.CudaExternalMemoryHandleDesc' from assembly 
'ManagedCuda, Version=11.0.43.0, Culture=neutral, PublicKeyToken=242d898828717aa0'
 because it contains an object field at 
offset 4 that is incorrectly aligned or overlapped by a non-object 
 field.":"ManagedCuda.BasicTypes.CUDA_EXTERNAL_MEMORY_HANDLE_DESC"} 
System.Exception {System.TypeLoadException}

Do you happen to know why this is happennig and if I can compile managedCuda in such a way to prevent this?

I've tried to compile managedCuda in 64bit only with no success. Aligning the fields to 8 bits offsets can solve this?

kunzmi commented 3 years ago

Hi,

I was fighting a lot with ManagedCuda.BasicTypes.CudaExternalMemoryHandleDesc to get the fields aligned as in the original C struct (C# and C-unions don't go well together), and NVidia also changed it for the newer versions... I'm not totally sure if the current layout actually works for all cases, so far no one seems to have ever used it (it is only used for the new graphics API interop replacing the older OpenGL/DirectX interfaces). And your DLL-loader mechanism seems to check stronger than the actual compiler. So just delete the two struct members that are aligned to FieldOffset 4 and it should run smoothly.

Cheers Michael

fbordignon commented 3 years ago

Hey Michael, removing the two fields with offset 4 does the trick! Me and a teammate were battling this one for the good part of the last week thinking it was something with the vendor's software because we had issues before. Anyways, I own you a beer or a beverage of choice 🤣