Newer
Older
#ifndef _AFJHAYYTA_PYOPENCL_HEADER_SEEN_WRAP_CL_HPP
#define _AFJHAYYTA_PYOPENCL_HEADER_SEEN_WRAP_CL_HPP
// CL 1.2 undecided:
// clSetPrintfCallback
// {{{ 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>
#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 ------------------------------------------------------------------
#define CL_TARGET_OPENCL_VERSION 220
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <CL/cl.h>
#include <CL/cl_ext.h>
#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 <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
#include <numeric>
#include "wrap_helpers.hpp"
#include "numpy_init.hpp"
#include "tools.hpp"
#ifdef PYOPENCL_PRETEND_CL_VERSION
#define PYOPENCL_CL_VERSION PYOPENCL_PRETEND_CL_VERSION
#else
#if 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
#if PY_VERSION_HEX >= 0x03000000
#define PYOPENCL_USE_NEW_BUFFER_INTERFACE
#define PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(s) std::move(s)
#else
#define PYOPENCL_STD_MOVE_IF_NEW_BUF_INTF(s) (s)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// }}}
// {{{ tools
#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; \
} \
else \
{ \
PYTHON_FOREACH(py_dev, py_devices) \
devices_vec.push_back( \
(py_dev).cast<device &>().data()); \
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
num_devices = devices_vec.size(); \
devices = devices_vec.empty( ) ? NULL : &devices_vec.front(); \
} \
#define PYOPENCL_RETRY_RETURN_IF_MEM_ERROR(OPERATION) \
try \
{ \
OPERATION \
} \
catch (pyopencl::error &e) \
{ \
if (!e.is_out_of_memory()) \
throw; \
} \
\
/* If we get here, we got an error from CL.
* We should run the Python GC to try and free up
* some memory references. */ \
run_python_gc(); \
\
/* Now retry the allocation. If it fails again,
* let it fail. */ \
{ \
OPERATION \
}
#define PYOPENCL_RETRY_IF_MEM_ERROR(OPERATION) \
{ \
bool failed_with_mem_error = false; \
try \
{ \
OPERATION \
} \
catch (pyopencl::error &e) \
{ \
failed_with_mem_error = true; \
if (!e.is_out_of_memory()) \
throw; \
} \
\
if (failed_with_mem_error) \
{ \
/* If we get here, we got an error from CL.
* We should run the Python GC to try and free up
* some memory references. */ \
run_python_gc(); \
\
/* Now retry the allocation. If it fails again,
Loading
Loading full blame...