在 64 位窗口中为 AMD 系统实现 OpenCL 1.2 平台的数量

OpenCL 是低级 API,因此必须首先在“C 空间”中实现。为此,需要从 Khronos 的网站下载头文件。我的硬件是 AMD,能够支持 1.2 版,下载

opencl.h 
cl_platform.h 
cl.h 
cl_ext.h 
cl_egl.h 
cl_dx9_media_sharing.h 
cl_d3d10.h 
cl_d3d11.h 
cl_gl.h 
cl_gl_ext.h 
cl.hpp

这个页面

应该足够 C++绑定所以在将这些文件添加到项目并设置适当的二进制(和库)文件位置后(

用于 64 位 amd 库的$(AMDAPPSDKROOT)\ lib \ x86_64(首选 amd app sdk 的库)

用于 64 位 opencl.dll 的 C:\ Windows \ SysWOW64(如果 ICD 是 Linux 系统,则为 .so 文件)

例如,但对于 Intel-Nvidia 不同,你可以在安装适当的驱动程序(例如 amd 的 crimson)后开始查询平台列表(amd, intel, xilinx, nvidia)。驱动程序用于运行 opencl 应用程序(使用 ICD),库和头文件用于简化开发。

要查询平台:

#define __CL_ENABLE_EXCEPTIONS
#include "stdafx.h"
#include <vector>
#include <CL/cl.hpp>

extern "C"
    {
       // when this class is created, it contains a list of platforms in "platforms" field.
       class OpenClPlatformList
       {
           public:
               std::vector<cl::Platform> platforms;
               int platformNum;
               OpenClPlatformList()
               {
                   platforms= std::vector< cl::Platform>();
                   cl::Platform::get(&platforms);
                   platformNum= platforms.size();
               }
        };

        // this is seen from C# when imported. Creates an object in memory.
        __declspec(dllexport)
            OpenClPlatformList * createPlatformList()
        {
            return new OpenClPlatformList();
        }

        __declspec(dllexport)
            int platformNumber(OpenClPlatformList * hList)
        {
            return hList->platformNum;
        }

        __declspec(dllexport)
            void deletePlatformList(OpenClPlatformList * p)
        {
            if (p != NULL)
                delete p;
            p = NULL;
        }

    }

可以内置到一个 DLL(如 OCLImplementation.dll)

并从 C#侧使用它,

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace WrapperCSharp
{
    public class WrapperCSharp
    {
        [DllImport("OCLImplementation", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr createPlatformList();

        [DllImport("OCLImplementation", CallingConvention = CallingConvention.Cdecl)]
        private static extern int platformNumber(IntPtr hList);

        [DllImport("OCLImplementation", CallingConvention = CallingConvention.Cdecl)]
        private static extern void deletePlatformList(IntPtr hList);
    }
}

当然,必须通过 C#项目看到 dll,只需将它放在项目的可执行文件附近即可。

现在,如果示例计算机至少有一个支持 opencl 的平台,

IntPtr platformList = createPlatformList(); // just an address in C-space
int totalPlatforms = platformNumber(platformList); // AMD+NVIDIA systems should have "2"
deletePlatformList(platformList); //

totalPlatforms 变量必须至少具有 1 值。然后,你可以使用 C 空间中的平台变量,使用其他函数迭代所有平台,以查询所有设备,如 CPU,GPU 和特殊用途加速器,如 phi 或某些 fpga。

人们不会简单地将所有这些 C++写入 C#包装器以用于时间关键型项目。有许多为 C#,Java 和其他语言编写的包装器。对于 java,有一个 Aparapi 是“java 字节码到 opencl-c”转换器 api,它将你在 java 中纯粹编写的内容转换为动态的 gpu-parallel 版本,因此它具有一定的可移植性。