首页 > 解决方案 > 为什么我在 Linux 上从 mono 调用 opencl 库时出错?

问题描述

我在 ubuntu linux 下从 mono 调用 opencl 库时收到“对象引用未设置为对象实例”错误。

环境:
Ubuntu Linux 16.04
GeForce GTX 960
软件包:
ocl-icd-opencl-dev
nvidia-opencl-icd-384
opencl-headers
clinfo
mono-complete

从 /usr/lib/x86_64-linux-gnu 在 /usr/lib 中为 libOpenCL.so 创建了一个符号链接,因为它似乎不在单声道的路径中。

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 文档。

我找不到它不起作用的任何原因,也找不到对类似问题的引用。有人可以提供方向/解决方案吗?

标签: linuxubuntumonoopencl

解决方案


推荐阅读