【发布时间】:2016-04-29 06:17:32
【问题描述】:
编辑:好的,所有的编辑都让问题的布局有点混乱,所以我会尝试重写问题(不改变内容,但改进其结构)。
简而言之问题
如果我将它编译为可执行文件,我有一个运行良好的 openCL 程序。现在我尝试使用boost.python 使其可从Python 调用。但是,一旦我退出 Python(在导入我的模块之后),python 就会崩溃。
原因似乎与此有关
在程序终止时仅静态存储 GPU CommandQueues 及其释放机制
MWE 和设置
设置
使用的 IDE:Visual Studio 2015
使用的操作系统:Windows 7 64bit
Python 版本:3.5
AMD OpenCL APP 3.0 标头
cl2.hpp直接来自 Khronos,如下所示:empty openCL program throws deprecation warning我还有一个带有集成显卡硬件的 Intel CPU,没有其他专用显卡
我使用 1.60 版本的 boost 库编译为 64 位版本
我使用的boost dll叫做:
boost_python-vc140-mt-1_60.dll不用python的openCL程序也能正常运行
没有openCL的python模块可以正常工作
MWE
#include <vector>
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#define CL_HPP_MINIMUM_OPENCL_VERSION 200 // I have the same issue for 100 and 110
#include "cl2.hpp"
#include <boost/python.hpp>
using namespace std;
class TestClass
{
private:
std::vector<cl::CommandQueue> queues;
TestClass();
public:
static const TestClass& getInstance()
{
static TestClass instance;
return instance;
}
};
TestClass::TestClass()
{
std::vector<cl::Device> devices;
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
//remove non 2.0 platforms (as suggested by doqtor)
platforms.erase(
std::remove_if(platforms.begin(), platforms.end(),
[](const cl::Platform& platform)
{
int v = cl::detail::getPlatformVersion(platform());
short version_major = v >> 16;
return !(version_major >= 2);
}),
platforms.end());
//Get all available GPUs
for (const cl::Platform& pl : platforms)
{
vector<cl::Device> plDevices;
try {
pl.getDevices(CL_DEVICE_TYPE_GPU, &plDevices);
}
catch (cl::Error&)
{
// Doesn't matter. No GPU is available on the current machine for
// this platform. Just check afterwards, that you have at least one
// device
continue;
}
devices.insert(end(devices), begin(plDevices), end(plDevices));
}
cl::Context context(devices[0]);
cl::CommandQueue queue(context, devices[0]);
queues.push_back(queue);
}
int main()
{
TestClass::getInstance();
return 0;
}
BOOST_PYTHON_MODULE(FrameWork)
{
TestClass::getInstance();
}
调用程序
所以在将程序编译为dll 之后,我启动 python 并运行以下程序
import FrameWork
exit()
虽然导入没有问题,但 python 在 exit() 上崩溃。所以我点击调试,Visual Studio 告诉我以下代码部分(在cl2.hpp)中有一个异常:
template <>
struct ReferenceHandler<cl_command_queue>
{
static cl_int retain(cl_command_queue queue)
{ return ::clRetainCommandQueue(queue); }
static cl_int release(cl_command_queue queue) // -- HERE --
{ return ::clReleaseCommandQueue(queue); }
};
如果您将上述代码编译为简单的可执行文件,则它可以正常工作。如果满足以下条件之一,该代码也可以工作:
CL_DEVICE_TYPE_GPU替换为CL_DEVICE_TYPE_ALLqueues.push_back(queue)行被删除
问题
那么这可能是什么原因以及可能的解决方案是什么?我怀疑这与我的测试类是静态的这一事实有关,但由于它与可执行文件一起使用,我不知道是什么原因造成的。
【问题讨论】:
-
请仔细阅读发布指南,有一堆信息丢失。
-
@UlrichEckhardt 添加了 MWE。这对您有帮助吗?
-
我不需要帮助,你需要。您的示例可能不是最小的,三个文件是一堆。缺少其他信息。
标签: python c++ boost opencl boost-python