Created
September 8, 2017 11:10
-
-
Save rhardih/8cef29ccefe7af9166a2aa03799ca3ec to your computer and use it in GitHub Desktop.
OpenCV cv::ocl::attachContext problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INC=-I/Users/rene/Code/OpenCL-Headers/opencl12 | |
PKG=`pkg-config --cflags --libs opencv` | |
LD=`pkg-config --libs-only-L opencv | cut -c 3-` | |
.PHONY: default a.out.lldb | |
default: build | |
LD_LIBRARY_PATH=$(LD) ./a.out | |
lldb: build a.out.lldb | |
LD_LIBRARY_PATH=$(LD) lldb --source a.out.lldb | |
build: | |
g++ $(PKG) $(INC) -glldb -g -framework OpenGL -framework OpenCL ocl-err.cpp | |
a.out.lldb: | |
echo "env LD_LIBRARY_PATH=$(LD)\nfile a.out\nb UMatData::UMatData\nr\n" > a.out.lldb | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <OpenGL/OpenGL.h> | |
#include <CL/cl.hpp> | |
#include <opencv2/core.hpp> | |
#include <opencv2/core/ocl.hpp> | |
#define CHECK(status, msg) if ((status) != CL_SUCCESS) \ | |
{ \ | |
cout << (msg) << endl; \ | |
return -1; \ | |
} | |
using namespace std; | |
class Foo | |
{ | |
public: | |
Foo(const char* name) { | |
cout << "new Foo(" << name << ") created!" << endl; | |
}; | |
//~Foo() { | |
// cv::ocl::finish(); | |
//}; | |
int init(); | |
void process(); | |
static void clCallback(const char *errinfo, const void *private_info, size_t cb, void *user_data); | |
private: | |
std::vector<cl_platform_id> m_platform_ids; | |
cl::Context m_clContext; | |
}; | |
int Foo::init() | |
{ | |
cl_int res = CL_SUCCESS; | |
std::vector<cl::Platform> platforms; | |
cl::Platform::get(&platforms); | |
cl::Platform defaultPlatform = cl::Platform::getDefault(&res); | |
CHECK(res, "Failed to get default platform"); | |
cl_context_properties contextProps[] = | |
{ | |
CL_CONTEXT_PLATFORM, | |
(cl_context_properties)(defaultPlatform()), | |
0 | |
}; | |
cout << "Creating a custom OpenCL context" << endl; | |
m_clContext = cl::Context(CL_DEVICE_TYPE_GPU, contextProps, &Foo::clCallback, 0, &res); | |
CHECK(res, "Failed to create OpenCL context"); | |
cout << "Created context (cl_context): " << m_clContext() << endl; | |
cout << "Default OpenCV<>OpenCL Queue (cl_command_queue): " << cv::ocl::Queue::getDefault().ptr() << endl; | |
std::vector<cl::Device> devs = m_clContext.getInfo<CL_CONTEXT_DEVICES>(&res); | |
CHECK(res, "Failed to get devices from context"); | |
cout << "Attaching new context to OpenCV using first device: " << devs[0].getInfo<CL_DEVICE_NAME>() << endl; | |
cv::ocl::attachContext(defaultPlatform.getInfo<CL_PLATFORM_NAME>(), defaultPlatform(), m_clContext(), devs[0]()); | |
cout << "Default OpenCV<>OpenCL Queue (cl_command_queue): " << cv::ocl::Queue::getDefault().ptr() << endl; | |
return 0; | |
} | |
void Foo::process() | |
{ | |
cout << "Foo start processing" << endl; | |
cout << "Create Mat with random data" << endl; | |
cv::Mat data(100, 100, CV_8U); | |
cv::randu(data, cv::Scalar(0), cv::Scalar(255)); | |
cout << "Uploading data to UMat" << endl; | |
cv::UMat src; | |
data.copyTo(src); | |
cout << "Transposing UMat (t-api)" << endl; | |
cv::transpose(src, src); | |
cv::ocl::finish(); | |
cout << "Foo done processing" << endl; | |
} | |
void Foo::clCallback(const char *errinfo, const void *private_info, size_t cb, void *user_data) | |
{ | |
cout << endl << "OPENCL #" << endl << errinfo; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
Foo *f0 = new Foo("f0"); | |
f0->init(); | |
f0->process(); | |
delete f0; | |
Foo *f1 = new Foo("f1"); | |
f1->init(); | |
f1->process(); | |
delete f1; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp | |
index f25b9c0d7..9ea23b313 100644 | |
--- a/modules/core/src/ocl.cpp | |
+++ b/modules/core/src/ocl.cpp | |
@@ -59,7 +59,7 @@ | |
#include "opencv2/core/bufferpool.hpp" | |
#ifndef LOG_BUFFER_POOL | |
-# if 0 | |
+# if 1 | |
# define LOG_BUFFER_POOL printf | |
# else | |
# define LOG_BUFFER_POOL(...) | |
@@ -3206,6 +3206,7 @@ public: | |
#endif | |
if (createFlags == 0) | |
{ | |
+ std::cout << "OpenCLAllocator: " << this << std::endl; | |
allocatorFlags = ALLOCATOR_FLAGS_BUFFER_POOL_USED; | |
handle = bufferPool.allocate(total); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ `pkg-config --cflags --libs opencv` -I/Users/rene/Code/OpenCL-Headers/opencl12 -glldb -g -framework OpenGL -framework OpenCL ocl-err.cpp | |
LD_LIBRARY_PATH=`pkg-config --libs-only-L opencv | cut -c 3-` ./a.out | |
new Foo(f0) created! | |
Creating a custom OpenCL context | |
Created context (cl_context): 0x7fd92af0cc80 | |
Default OpenCV<>OpenCL Queue (cl_command_queue): 0x7fd92af0d810 | |
Attaching new context to OpenCV using first device: Iris Pro | |
Default OpenCV<>OpenCL Queue (cl_command_queue): 0x7fd92af2b850 | |
Foo start processing | |
Create Mat with random data | |
Uploading data to UMat | |
OpenCLAllocator: 0x7fd92af2b350 | |
OpenCL allocate 12288 (0x3000) bytes: 0x7fd92af2b550 | |
Transposing UMat (t-api) | |
Foo done processing | |
new Foo(f1) created! | |
Creating a custom OpenCL context | |
Created context (cl_context): 0x7fd92af2cc10 | |
Default OpenCV<>OpenCL Queue (cl_command_queue): 0x7fd92af2b850 | |
Attaching new context to OpenCV using first device: Iris Pro | |
Default OpenCV<>OpenCL Queue (cl_command_queue): 0x7fd92af2b120 | |
Foo start processing | |
Create Mat with random data | |
Uploading data to UMat | |
OpenCLAllocator: 0x7fd92af2b350 | |
Reuse reserved buffer: 0x7fd92af2b550 | |
OPENCL # | |
OpenCV Error: Assertion failed (clEnqueueWriteBuffer(q, (cl_mem)u->handle, CL_TRUE, dstrawofs, total, alignedPtr.getAlignedPtr(), 0, 0, 0) >= 0) in upload, file /Users/rene/Code/opencv/modules/core/src/ocl.cpp, line 3984 | |
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/rene/Code/opencv/modules/core/src/ocl.cpp:3984: error: (-215) clEnqueueWriteBuffer(q, (cl_mem)u->handle, CL_TRUE, dstrawofs, total, alignedPtr.getAlignedPtr(), 0, 0, 0) >= 0 in function upload | |
[CL_INVALID_CONTEXT] : OpenCL Error : clEnqueueWriteBuffer failed: cl_mem object belongs to a different context 0x7fd92af0cc80 than the queue's context 0x7fd92af2cc10.make: *** [default] Abort trap: 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PKG_CONFIG_PATH must be set accordingly before the Makefile can be used.