~ubuntu-branches/debian/squeeze/pyopencl/squeeze

« back to all changes in this revision

Viewing changes to src/wrapper/wrap_helpers.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Rybak
  • Date: 2010-05-31 19:29:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100531192900-ll7guuro37nntr4y
Tags: upstream-0.92~beta+git20100709
ImportĀ upstreamĀ versionĀ 0.92~beta+git20100709

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef PYCUDA_WRAP_HELPERS_HEADER_SEEN
 
2
#define PYCUDA_WRAP_HELPERS_HEADER_SEEN
 
3
 
 
4
 
 
5
 
 
6
 
 
7
#include <boost/version.hpp>
 
8
#include <boost/python.hpp>
 
9
#include <boost/python/stl_iterator.hpp>
 
10
 
 
11
 
 
12
 
 
13
 
 
14
namespace py = boost::python;
 
15
 
 
16
 
 
17
 
 
18
 
 
19
#if (BOOST_VERSION/100) < 1035
 
20
#warning *******************************************************************
 
21
#warning **** Your version of Boost C++ is likely too old for PyOpenCL. ****
 
22
#warning *******************************************************************
 
23
#endif
 
24
 
 
25
 
 
26
 
 
27
 
 
28
#define PYTHON_ERROR(TYPE, REASON) \
 
29
{ \
 
30
  PyErr_SetString(PyExc_##TYPE, REASON); \
 
31
  throw boost::python::error_already_set(); \
 
32
}
 
33
 
 
34
#define ENUM_VALUE(NAME) \
 
35
  value(#NAME, NAME)
 
36
 
 
37
#define DEF_SIMPLE_METHOD(NAME) \
 
38
  def(#NAME, &cls::NAME)
 
39
 
 
40
#define DEF_SIMPLE_METHOD_WITH_ARGS(NAME, ARGS) \
 
41
  def(#NAME, &cls::NAME, boost::python::args ARGS)
 
42
 
 
43
#define DEF_SIMPLE_FUNCTION(NAME) \
 
44
  boost::python::def(#NAME, &NAME)
 
45
 
 
46
#define DEF_SIMPLE_FUNCTION_WITH_ARGS(NAME, ARGS) \
 
47
  boost::python::def(#NAME, &NAME, boost::python::args ARGS)
 
48
 
 
49
#define DEF_SIMPLE_RO_MEMBER(NAME) \
 
50
  def_readonly(#NAME, &cls::m_##NAME)
 
51
 
 
52
#define DEF_SIMPLE_RW_MEMBER(NAME) \
 
53
  def_readwrite(#NAME, &cls::m_##NAME)
 
54
 
 
55
#define PYTHON_FOREACH(NAME, ITERABLE) \
 
56
  BOOST_FOREACH(boost::python::object NAME, \
 
57
      std::make_pair( \
 
58
        boost::python::stl_input_iterator<boost::python::object>(ITERABLE), \
 
59
        boost::python::stl_input_iterator<boost::python::object>()))
 
60
 
 
61
#define COPY_PY_LIST(TYPE, NAME) \
 
62
  std::copy( \
 
63
      boost::python::stl_input_iterator<TYPE>(py_##NAME), \
 
64
      boost::python::stl_input_iterator<TYPE>(), \
 
65
      std::back_inserter(NAME));
 
66
 
 
67
#define COPY_PY_COORD_TRIPLE(NAME) \
 
68
  size_t NAME[3] = {0, 0, 0}; \
 
69
  { \
 
70
    size_t my_len = len(py_##NAME); \
 
71
    if (my_len > 3) \
 
72
      throw error("transfer", CL_INVALID_VALUE, #NAME "has too many components"); \
 
73
    for (size_t i = 0; i < my_len; ++i) \
 
74
      NAME[i] = py::extract<size_t>(py_##NAME[i])(); \
 
75
  }
 
76
 
 
77
#define COPY_PY_PITCH_TUPLE(NAME) \
 
78
  size_t NAME[2] = {0, 0}; \
 
79
  if (py_##NAME.ptr() != Py_None) \
 
80
  { \
 
81
    size_t my_len = len(py_##NAME); \
 
82
    if (my_len > 2) \
 
83
      throw error("transfer", CL_INVALID_VALUE, #NAME "has too many components"); \
 
84
    for (size_t i = 0; i < my_len; ++i) \
 
85
      NAME[i] = py::extract<size_t>(py_##NAME[i])(); \
 
86
  }
 
87
 
 
88
#define COPY_PY_REGION_TRIPLE(NAME) \
 
89
  size_t NAME[3] = {1, 1, 1}; \
 
90
  { \
 
91
    size_t my_len = len(py_##NAME); \
 
92
    if (my_len > 3) \
 
93
      throw error("transfer", CL_INVALID_VALUE, #NAME "has too many components"); \
 
94
    for (size_t i = 0; i < my_len; ++i) \
 
95
      NAME[i] = py::extract<size_t>(py_##NAME[i])(); \
 
96
  }
 
97
 
 
98
#define PYOPENCL_PARSE_NUMPY_ARRAY_SPEC \
 
99
    PyArray_Descr *tp_descr; \
 
100
    if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED) \
 
101
      throw py::error_already_set(); \
 
102
    \
 
103
    py::extract<npy_intp> shape_as_int(py_shape); \
 
104
    std::vector<npy_intp> shape; \
 
105
    \
 
106
    if (shape_as_int.check()) \
 
107
      shape.push_back(shape_as_int()); \
 
108
    else \
 
109
      COPY_PY_LIST(npy_intp, shape); \
 
110
    \
 
111
    NPY_ORDER order = PyArray_CORDER; \
 
112
    PyArray_OrderConverter(order_py.ptr(), &order); \
 
113
    \
 
114
    int ary_flags = 0; \
 
115
    if (order == PyArray_FORTRANORDER) \
 
116
      ary_flags |= NPY_FARRAY; \
 
117
    else if (order == PyArray_CORDER) \
 
118
      ary_flags |= NPY_CARRAY; \
 
119
    else \
 
120
      throw std::runtime_error("unrecognized order specifier"); \
 
121
 
 
122
#define PYOPENCL_RETURN_VECTOR(ITEMTYPE, NAME) \
 
123
  { \
 
124
    py::list pyopencl_result; \
 
125
    BOOST_FOREACH(ITEMTYPE item, NAME) \
 
126
      pyopencl_result.append(item); \
 
127
    return pyopencl_result; \
 
128
  }
 
129
 
 
130
namespace
 
131
{
 
132
  template <typename T>
 
133
  inline boost::python::handle<> handle_from_new_ptr(T *ptr)
 
134
  {
 
135
    return boost::python::handle<>(
 
136
        typename boost::python::manage_new_object::apply<T *>::type()(ptr));
 
137
  }
 
138
}
 
139
 
 
140
 
 
141
 
 
142
 
 
143
#endif