C# 我有一个错误;Dll加载错误“;在C中使用杂交试剂时#
我第一次使用杂交剂。我使用的是“2019年9月5日发布”的Windows 10、Visual Studio 2019、CUDA 10.1和Hybridizer 1.3.0。尽管我遵循了他们的步骤,但我还是不断遇到同样的错误: 加载Hello\u World\u CUDA.Dll时发生Dll加载错误:126 每当我尝试编写任何简单的代码来进行如下测试时:C# 我有一个错误;Dll加载错误“;在C中使用杂交试剂时#,c#,visual-studio,hybridizer,C#,Visual Studio,Hybridizer,我第一次使用杂交剂。我使用的是“2019年9月5日发布”的Windows 10、Visual Studio 2019、CUDA 10.1和Hybridizer 1.3.0。尽管我遵循了他们的步骤,但我还是不断遇到同样的错误: 加载Hello\u World\u CUDA.Dll时发生Dll加载错误:126 每当我尝试编写任何简单的代码来进行如下测试时: using System; using Hybridizer.Runtime.CUDAImports; public class Hello_W
using System;
using Hybridizer.Runtime.CUDAImports;
public class Hello_World
{
[EntryPoint]
public static void Hello()
{
Console.Out.Write("Hello from GPU");
}
static void Main()
{
cuda.DeviceSynchronize();
HybRunner runner = HybRunner.Cuda().SetDistrib(1, 2);
runner.Wrap(new Hello_World()).Hello();
}
}
即使使用他们的示例时没有任何更改
如何解决此问题?要进行故障排除,请执行以下三个步骤:
using System;
using System.IO;
using System.Runtime.InteropServices;
using Hybridizer.Runtime.CUDAImports;
public class Hello_World
{
[EntryPoint]
public static void Hello()
{
Console.Out.Write("Hello from GPU");
}
[DllImport("kernel32.dll", EntryPoint = "LoadLibraryA", SetLastError = true)]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport("kernel32.dll", EntryPoint = "FormatMessage", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, [MarshalAs(UnmanagedType.LPArray)] char[] data, uint dwSize, IntPtr args);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string symbol);
static unsafe string ErrorToString(int er)
{
char[] buffer = new char[2048];
int res = FormatMessage(0x00001000, // FORMAT_MESSAGE_FROM_SYSTEM
IntPtr.Zero, er,
0x0409, // US language -- in case of issue, replace with 0
buffer, 2048, IntPtr.Zero);
if (res == 0)
throw new ApplicationException(string.Format("Cannot format message - Error : {0}", res));
string resstring;
fixed (char* ptr = &buffer[0])
{
resstring = new string(ptr);
}
return resstring;
}
static void Main()
{
// Trouble-shooting
// print execution directory
Console.Out.WriteLine("Current directory : {0}", Environment.CurrentDirectory);
Console.Out.WriteLine("Size of IntPtr = {0}", Marshal.SizeOf(IntPtr.Zero));
// first, make sure file exists
string path = @"Troubleshooting_CUDA.dll"; // replace with actual dll name - you can read that on the output of the build
if (!File.Exists(path))
{
Console.Out.WriteLine("Dll could not be found in path, please verify dll is located in the appropriate directory that LoadLibrary may find it");
Environment.Exit(1);
}
// make sure it can be loaded -- open DLL in depends to missing troubleshoot dependencies (may be long to load)
IntPtr lib = LoadLibrary(path);
if (lib == IntPtr.Zero)
{
int code = Marshal.GetLastWin32Error();
string er = ErrorToString(code);
Console.Out.WriteLine("Dll could not be loaded : {0}", er);
Environment.Exit(2);
}
// finally try to get the proc address -- open DLL in depends to see list of symbols (may be long to load)
IntPtr procAddress = GetProcAddress(lib, "Hello_Worldx46Hello_ExternCWrapper_CUDA");
if (procAddress == IntPtr.Zero)
{
int code = Marshal.GetLastWin32Error();
string er = ErrorToString(code);
Console.Out.WriteLine("Could not find symbol in dll : {0}", er);
Environment.Exit(3);
}
cuda.DeviceSynchronize();
HybRunner runner = HybRunner.Cuda().SetDistrib(1, 2);
runner.Wrap(new Hello_World()).Hello();
}
}
若第一步失败,您可能需要更改CUDA卫星项目的输出目录和/或应用程序的执行目录
如果在第二步失败,您需要验证是否在x64中执行(IntPtr的大小应为8),以及是否加载了dll。您可能会发现,Dependency walker是用于此目的的一个很好的工具,您需要x64版本。注意:装载时间可能很长
如果在第三步失败,请使用depends查找符号名称,可能您的CUDA卫星项目不是最新的。要进行故障排除,请按照以下三个步骤进行:
using System;
using System.IO;
using System.Runtime.InteropServices;
using Hybridizer.Runtime.CUDAImports;
public class Hello_World
{
[EntryPoint]
public static void Hello()
{
Console.Out.Write("Hello from GPU");
}
[DllImport("kernel32.dll", EntryPoint = "LoadLibraryA", SetLastError = true)]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport("kernel32.dll", EntryPoint = "FormatMessage", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, [MarshalAs(UnmanagedType.LPArray)] char[] data, uint dwSize, IntPtr args);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string symbol);
static unsafe string ErrorToString(int er)
{
char[] buffer = new char[2048];
int res = FormatMessage(0x00001000, // FORMAT_MESSAGE_FROM_SYSTEM
IntPtr.Zero, er,
0x0409, // US language -- in case of issue, replace with 0
buffer, 2048, IntPtr.Zero);
if (res == 0)
throw new ApplicationException(string.Format("Cannot format message - Error : {0}", res));
string resstring;
fixed (char* ptr = &buffer[0])
{
resstring = new string(ptr);
}
return resstring;
}
static void Main()
{
// Trouble-shooting
// print execution directory
Console.Out.WriteLine("Current directory : {0}", Environment.CurrentDirectory);
Console.Out.WriteLine("Size of IntPtr = {0}", Marshal.SizeOf(IntPtr.Zero));
// first, make sure file exists
string path = @"Troubleshooting_CUDA.dll"; // replace with actual dll name - you can read that on the output of the build
if (!File.Exists(path))
{
Console.Out.WriteLine("Dll could not be found in path, please verify dll is located in the appropriate directory that LoadLibrary may find it");
Environment.Exit(1);
}
// make sure it can be loaded -- open DLL in depends to missing troubleshoot dependencies (may be long to load)
IntPtr lib = LoadLibrary(path);
if (lib == IntPtr.Zero)
{
int code = Marshal.GetLastWin32Error();
string er = ErrorToString(code);
Console.Out.WriteLine("Dll could not be loaded : {0}", er);
Environment.Exit(2);
}
// finally try to get the proc address -- open DLL in depends to see list of symbols (may be long to load)
IntPtr procAddress = GetProcAddress(lib, "Hello_Worldx46Hello_ExternCWrapper_CUDA");
if (procAddress == IntPtr.Zero)
{
int code = Marshal.GetLastWin32Error();
string er = ErrorToString(code);
Console.Out.WriteLine("Could not find symbol in dll : {0}", er);
Environment.Exit(3);
}
cuda.DeviceSynchronize();
HybRunner runner = HybRunner.Cuda().SetDistrib(1, 2);
runner.Wrap(new Hello_World()).Hello();
}
}
若第一步失败,您可能需要更改CUDA卫星项目的输出目录和/或应用程序的执行目录
如果在第二步失败,您需要验证是否在x64中执行(IntPtr的大小应为8),以及是否加载了dll。您可能会发现,Dependency walker是用于此目的的一个很好的工具,您需要x64版本。注意:装载时间可能很长
如果在第三步失败,请使用depends查找符号名称,可能您的CUDA卫星项目不是最新的。您是否验证了CUDA项目也已构建,并且解决方案平台是x64?两者都已完成。同样的问题。你应该在github上提交一个问题,和/或向支持电子邮件报告。看起来他们已经有几个月的问题了。这将有助于获得完整的异常堆栈和完整的输出日志。你能提供这个吗?你是否验证了CUDA项目也已经建立,并且解决方案平台是x64?你已经完成了这两项工作。同样的问题。你应该在github上提交一个问题,和/或向支持电子邮件报告。看起来他们已经有几个月的问题了。这将有助于获得完整的异常堆栈和完整的输出日志。你能提供这个吗?正如我所料,这是第一步。我找不到这个“Hello\u World\u CUDA.dll”文件。应该如何生成?通过构建卫星项目(CUDA),我如何在C#中实现这一点?我真的不明白。卫星项目是VCXPROJ,因此是一个依赖CUDA的C++项目。首先,这里是一个不错的入口点,我尝试过这样做,但它一直给我不同的错误,如“内部错误”“为源文件指定的路径为'D:\hybridger\Hello\u World\u CUDA\hybridger generated sources\hybridger.wrappers.cu'不存在”。我想我会放弃杂交。正如我所预料的,这是第一步。我找不到这个“Hello\u World\u CUDA.dll”文件。应该如何生成?通过构建卫星项目(CUDA),我如何在C#中实现这一点?我真的不明白。卫星项目是VCXPROJ,因此是一个依赖CUDA的C++项目。首先,这里是一个不错的入口点,我尝试过这样做,但它一直给我不同的错误,如“内部错误”“为源文件指定的路径为'D:\hybridger\Hello\u World\u CUDA\hybridger generated sources\hybridger.wrappers.cu'不存在”。我想我会放弃杂交。