Newer
Older
// PyOpenCL-flavored C++ wrapper of the CL API
//
// Copyright (C) 2009 Andreas Kloeckner
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef _AFJHAYYTA_PYOPENCL_HEADER_SEEN_WRAP_CL_HPP
#define _AFJHAYYTA_PYOPENCL_HEADER_SEEN_WRAP_CL_HPP
// CL 1.2 undecided:
// clSetPrintfCallback
// CL 2.2 complete
// CL 3.0 missing:
// clCreateBufferWithProperties
// clCreateImageWithProperties
Andreas Klöckner
committed
// (no wrappers for now: OpenCL 3.0 does not define any optional properties for
// buffers or images, no implementations to test with.)
// {{{ includes
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
// #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#ifdef __APPLE__
// Mac ------------------------------------------------------------------------
#include <OpenCL/opencl.h>
#include "pyopencl_ext.h"
#ifdef HAVE_GL
#define PYOPENCL_GL_SHARING_VERSION 1
#include <OpenGL/OpenGL.h>
#include <OpenCL/cl_gl.h>
#include <OpenCL/cl_gl_ext.h>
#endif
#else
// elsewhere ------------------------------------------------------------------
#if defined(_WIN32)
#define NOMINMAX
#include <windows.h>
#endif
#ifdef HAVE_GL
#include <GL/gl.h>
#include <CL/cl_gl.h>
#endif
#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1)
#define PYOPENCL_GL_SHARING_VERSION cl_khr_gl_sharing
#endif
#endif
#include <functional>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <cstdio>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
#include <numeric>
#include "wrap_helpers.hpp"
Andreas Klöckner
committed
#include <numpy/arrayobject.h>
#include "tools.hpp"
#ifdef PYOPENCL_PRETEND_CL_VERSION
#define PYOPENCL_CL_VERSION PYOPENCL_PRETEND_CL_VERSION
#else
#if defined(CL_VERSION_3_0)
#define PYOPENCL_CL_VERSION 0x3000
#elif defined(CL_VERSION_2_2)
#define PYOPENCL_CL_VERSION 0x2020
#elif defined(CL_VERSION_2_1)
#define PYOPENCL_CL_VERSION 0x2010
#elif defined(CL_VERSION_2_0)
#define PYOPENCL_CL_VERSION 0x2000
#elif defined(CL_VERSION_1_2)
#define PYOPENCL_CL_VERSION 0x1020
#elif defined(CL_VERSION_1_1)
#define PYOPENCL_CL_VERSION 0x1010
#else
#define PYOPENCL_CL_VERSION 0x1000
#endif
#endif
Andreas Klöckner
committed
#if defined(_WIN32)
// MSVC does not understand variable-length arrays
#define PYOPENCL_STACK_CONTAINER(TYPE, NAME, COUNT) std::vector<TYPE> NAME(COUNT)
#define PYOPENCL_STACK_CONTAINER_GET_PTR(NAME) (NAME.size() ? NAME.data() : nullptr)
Andreas Klöckner
committed
#else
// gcc et al complain about stripping attributes in template arguments
#define PYOPENCL_STACK_CONTAINER(TYPE, NAME, COUNT) TYPE NAME[COUNT]
#define PYOPENCL_STACK_CONTAINER_GET_PTR(NAME) NAME
#endif
// {{{ macros and typedefs for wrappers
#if NPY_ABI_VERSION < 0x02000000
#define PyDataType_ELSIZE(descr) ((descr)->elsize)
#endif
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#if PY_VERSION_HEX >= 0x02050000
typedef Py_ssize_t PYOPENCL_BUFFER_SIZE_T;
#else
typedef int PYOPENCL_BUFFER_SIZE_T;
#endif
#define PYOPENCL_CAST_BOOL(B) ((B) ? CL_TRUE : CL_FALSE)
#define PYOPENCL_DEPRECATED(WHAT, KILL_VERSION, EXTRA_MSG) \
{ \
PyErr_Warn( \
PyExc_DeprecationWarning, \
WHAT " is deprecated and will stop working in PyOpenCL " KILL_VERSION". " \
EXTRA_MSG); \
}
#if PYOPENCL_CL_VERSION >= 0x1020
#define PYOPENCL_GET_EXT_FUN(PLATFORM, NAME, VAR) \
NAME##_fn VAR \
= (NAME##_fn) \
clGetExtensionFunctionAddressForPlatform(PLATFORM, #NAME); \
\
if (!VAR) \
throw error(#NAME, CL_INVALID_VALUE, #NAME \
"not available");
#else
#define PYOPENCL_GET_EXT_FUN(PLATFORM, NAME, VAR) \
NAME##_fn VAR \
= (NAME##_fn) \
clGetExtensionFunctionAddress(#NAME); \
\
if (!VAR) \
throw error(#NAME, CL_INVALID_VALUE, #NAME \
"not available");
#endif
#define PYOPENCL_PARSE_PY_DEVICES \
std::vector<cl_device_id> devices_vec; \
cl_uint num_devices; \
cl_device_id *devices; \
\
if (py_devices.ptr() == Py_None) \
{ \
num_devices = 0; \
devices = 0; \
Loading
Loading full blame...