我在Ubuntu linux下从mono调用opencl库时遇到“对象引用未设置为对象实例”的错误。
环境:
Ubuntu Linux 16.04
GeForce GTX 960
包裹:
ocl-icd-opencl-dev
nvidia-opencl-icd-384
opencl-headers
clinfo
单完成
在/ usr / lib / x86_64-linux-gnu中的/ usr / lib中为libOpenCL.so创建了符号链接,因为它似乎不在mono的路径中。
clinfo看到平台,并显示出正确的卡数据。
代码如下:
using cl_int = Int32;
using cl_uint = UInt32;
using cl_platform_id = IntPtr;
class Program
{
[DllImport("OpenCL.dll")]
public extern static cl_int clGetPlatformIDs(cl_uint num_entries, [Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] cl_platform_id[] platforms, out cl_uint num_platforms);
static void Main(string[] args)
{
System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
try
{
IntPtr[] platformIDs=null;
cl_int result=0;
cl_uint returnedPlatforms=0;
Console.WriteLine("call");
// determine how many platforms are available - call based in chronos docs.
// running with MONO_LOG_LEVEL=debug and MONO_LOG_MASK=dll shows the dll is found
// abends with 'object reference not set to an instance of an object'
result = (cl_int)clGetPlatformIDs(0, null, out returnedPlatforms);
Console.WriteLine(string.Format("ERR: {0} Platforms {1}", returnedPlatforms, result));
}
catch (Exception ex)
{
Console.WriteLine("Oops");
string stack = null;
StringBuilder sb = new StringBuilder();
while (ex != null)
{
sb.AppendLine(ex.Message);
if (ex.StackTrace != null && stack == null)
{
stack = ex.StackTrace;
}
ex = ex.InnerException;
}
if (stack != null)
sb.AppendLine(stack);
Console.WriteLine(sb.ToString());
}
Console.WriteLine("End");
}
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Oops deep");
Console.WriteLine(e.ExceptionObject.ToString());
}
}
使用代码中所述的单声道调试集执行将导致:
call
Oops
Object reference not set to an instance of an object
at (wrapper managed-to-native) TestOCL.Program:clGetPlatformIDs (uint,intptr[],uint&)
at TestOCL.Program.Main (System.String[] args) <0x40740d60 + 0x000c7> in <filename unknown>:0
End
Mono: DllImport attempting to load: 'OpenCL.dll'.
Mono: DllImport error loading library
'/home/oxygen/configuration/libOpenCL.dll':
'/home/oxygen/configuration/libOpenCL.dll: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/home/oxygen/configuration/libOpenCL.dll.so': '/home/oxygen/configuration/libOpenCL.dll.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/usr/lib/libOpenCL.dll': '/usr/lib/libOpenCL.dll: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/usr/lib/libOpenCL.dll.so': '/usr/lib/libOpenCL.dll.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'libOpenCL.dll': 'libOpenCL.dll: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'libOpenCL.dll.so': 'libOpenCL.dll.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'OpenCL.dll': 'OpenCL.dll: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/home/oxygen/configuration/libOpenCL': '/home/oxygen/configuration/libOpenCL: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/home/oxygen/configuration/libOpenCL.so': '/home/oxygen/configuration/libOpenCL.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/usr/lib/libOpenCL': '/usr/lib/libOpenCL: cannot open shared object file: No such file or directory'.
Mono: DllImport loaded library '/usr/lib/libOpenCL.so'.
Mono: DllImport searching in: 'OpenCL.dll' ('/usr/lib/libOpenCL.so').
Mono: Searching for 'clGetPlatformIDs'.
Mono: Probing 'clGetPlatformIDs'.
Mono: Found as 'clGetPlatformIDs'.
“对象引用未设置为对象的实例”在常规异常处理中被捕获。
该呼叫尝试获取可用平台的数量,并遵循Khronos文档。
我找不到导致它不起作用的任何原因,也找不到对类似问题的引用。有人可以提供指示/解决方案吗?