/*NUMPY_API * Convert an object to FORTRAN / C / ANY / KEEP */ NPY_NO_EXPORT int PyArray_OrderConverter(PyObject *object, NPY_ORDER *val) { char *str; /* Leave the desired default from the caller for NULL/Py_None */ if (object == NULL || object == Py_None) { return NPY_SUCCEED; } else if (PyUnicode_Check(object)) { PyObject *tmp; int ret; tmp = PyUnicode_AsASCIIString(object); ret = PyArray_OrderConverter(tmp, val); Py_DECREF(tmp); return ret; } else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) { if (PyObject_IsTrue(object)) { *val = NPY_FORTRANORDER; } else { *val = NPY_CORDER; } if (PyErr_Occurred()) { return NPY_FAIL; } return NPY_SUCCEED; } else { str = PyBytes_AS_STRING(object); if (str[0] == 'C' || str[0] == 'c') { *val = NPY_CORDER; } else if (str[0] == 'F' || str[0] == 'f') { *val = NPY_FORTRANORDER; } else if (str[0] == 'A' || str[0] == 'a') { *val = NPY_ANYORDER; } else if (str[0] == 'K' || str[0] == 'k') { *val = NPY_KEEPORDER; } else { PyErr_SetString(PyExc_TypeError, "order not understood"); return NPY_FAIL; } } return NPY_SUCCEED; }
py::handle<> host_pool_allocate( boost::shared_ptr<pycuda::memory_pool<host_allocator> > pool, py::object shape, py::object dtype, py::object order_py) { PyArray_Descr *tp_descr; if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED) throw py::error_already_set(); std::vector<npy_intp> dims; std::copy( py::stl_input_iterator<npy_intp>(shape), py::stl_input_iterator<npy_intp>(), back_inserter(dims)); std::auto_ptr<pooled_host_allocation> alloc( new pooled_host_allocation( pool, tp_descr->elsize*pycuda::size_from_dims(dims.size(), &dims.front()))); NPY_ORDER order = PyArray_CORDER; PyArray_OrderConverter(order_py.ptr(), &order); int flags = 0; if (order == PyArray_FORTRANORDER) flags |= NPY_FARRAY; else if (order == PyArray_CORDER) flags |= NPY_CARRAY; else throw std::runtime_error("unrecognized order specifier"); py::handle<> result = py::handle<>(PyArray_NewFromDescr( &PyArray_Type, tp_descr, int(dims.size()), &dims.front(), /*strides*/ NULL, alloc->ptr(), flags, /*obj*/NULL)); py::handle<> alloc_py(handle_from_new_ptr(alloc.release())); PyArray_BASE(result.get()) = alloc_py.get(); Py_INCREF(alloc_py.get()); return result; }
/*NUMPY_API * Convert an object to FORTRAN / C / ANY / KEEP */ NPY_NO_EXPORT int PyArray_OrderConverter(PyObject *object, NPY_ORDER *val) { char *str; /* Leave the desired default from the caller for NULL/Py_None */ if (object == NULL || object == Py_None) { return NPY_SUCCEED; } else if (PyUnicode_Check(object)) { PyObject *tmp; int ret; tmp = PyUnicode_AsASCIIString(object); if (tmp == NULL) { PyErr_SetString(PyExc_ValueError, "Invalid unicode string passed in " "for the array ordering. " "Please pass in 'C', 'F', 'A' " "or 'K' instead"); return NPY_FAIL; } ret = PyArray_OrderConverter(tmp, val); Py_DECREF(tmp); return ret; } else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) { /* 2015-12-14, 1.11 */ int ret = DEPRECATE("Non-string object detected for " "the array ordering. Please pass " "in 'C', 'F', 'A', or 'K' instead"); if (ret < 0) { return -1; } if (PyObject_IsTrue(object)) { *val = NPY_FORTRANORDER; } else { *val = NPY_CORDER; } if (PyErr_Occurred()) { return NPY_FAIL; } return NPY_SUCCEED; } else { str = PyBytes_AS_STRING(object); if (strlen(str) != 1) { /* 2015-12-14, 1.11 */ int ret = DEPRECATE("Non length-one string passed " "in for the array ordering. " "Please pass in 'C', 'F', 'A', " "or 'K' instead"); if (ret < 0) { return -1; } } if (str[0] == 'C' || str[0] == 'c') { *val = NPY_CORDER; } else if (str[0] == 'F' || str[0] == 'f') { *val = NPY_FORTRANORDER; } else if (str[0] == 'A' || str[0] == 'a') { *val = NPY_ANYORDER; } else if (str[0] == 'K' || str[0] == 'k') { *val = NPY_KEEPORDER; } else { PyErr_SetString(PyExc_TypeError, "order not understood"); return NPY_FAIL; } } return NPY_SUCCEED; }