-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add implementation using Numpy C UFunc API #3
Open
mdboom
wants to merge
2
commits into
liberfa:master
Choose a base branch
from
mdboom:c_ufunc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
#include "Python.h" | ||
#include "numpy/arrayobject.h" | ||
#include "numpy/ufuncobject.h" | ||
|
||
#include "numpy/npy_3kcompat.h" | ||
|
||
#include "numpy/npy_math.h" | ||
|
||
#include "erfa.h" | ||
|
||
typedef npy_intp intp; | ||
|
||
static void | ||
increment_ufunc(intp ndim, char **args, intp *steps) | ||
{ | ||
intp i; | ||
for (i = 0; i < ndim; ++i) { | ||
*(args[i]) += steps[i]; | ||
} | ||
} | ||
|
||
#define BEGIN_LOOP \ | ||
intp dN = *dimensions++; \ | ||
intp N; \ | ||
for (N = 0; N < dN; N++) { \ | ||
|
||
#define END_LOOP(x) \ | ||
increment_ufunc((x), args, steps); \ | ||
} \ | ||
|
||
/* | ||
***************************************************************************** | ||
** UFUNC LOOPS ** | ||
***************************************************************************** | ||
*/ | ||
|
||
/*/////////////////////////////////////////////////////////////////////////// | ||
eraAtco13 | ||
*/ | ||
|
||
static void | ||
eraAtco13_wrapper(char **args, intp *dimensions, intp *steps, void *extra) | ||
{ | ||
int ret; | ||
|
||
BEGIN_LOOP | ||
ret = eraAtco13( | ||
*(double *)args[0], | ||
*(double *)args[1], | ||
*(double *)args[2], | ||
*(double *)args[3], | ||
*(double *)args[4], | ||
*(double *)args[5], | ||
*(double *)args[6], | ||
*(double *)args[7], | ||
*(double *)args[8], | ||
*(double *)args[9], | ||
*(double *)args[10], | ||
*(double *)args[11], | ||
*(double *)args[12], | ||
*(double *)args[13], | ||
*(double *)args[14], | ||
*(double *)args[15], | ||
*(double *)args[16], | ||
*(double *)args[17], | ||
(double *)args[18], | ||
(double *)args[19], | ||
(double *)args[20], | ||
(double *)args[21], | ||
(double *)args[22], | ||
(double *)args[23]); | ||
if (ret > 0) { | ||
PyErr_SetString(PyExc_ValueError, "dubious year"); | ||
return; | ||
} else if (ret < 0) { | ||
PyErr_SetString(PyExc_ValueError, "unacceptable date"); | ||
return; | ||
} | ||
END_LOOP(24) | ||
} | ||
|
||
static PyUFuncGenericFunction eraAtco13_functions[] = { eraAtco13_wrapper }; | ||
static void *eraAtco13_data[] = {NULL}; | ||
static char eraAtco13_types[] = { | ||
NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, | ||
NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, | ||
NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, | ||
NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE }; | ||
|
||
|
||
static void | ||
eraD2dtf_wrapper(char **args, intp *dimensions, intp *steps, void *extra) | ||
{ | ||
int ret; | ||
int ihmsf[4]; | ||
|
||
BEGIN_LOOP | ||
ret = eraD2dtf( | ||
(char *)args[0], | ||
*(int *)args[1], | ||
*(double *)args[2], | ||
*(double *)args[3], | ||
(int *)args[4], | ||
(int *)args[5], | ||
(int *)args[6], | ||
ihmsf); | ||
|
||
if (ret > 0) { | ||
PyErr_SetString(PyExc_ValueError, "dubious year"); | ||
return; | ||
} else if (ret < 0) { | ||
PyErr_SetString(PyExc_ValueError, "unacceptable date"); | ||
return; | ||
} | ||
|
||
*(int *)args[7] = ihmsf[0]; | ||
*(int *)args[8] = ihmsf[1]; | ||
*(int *)args[9] = ihmsf[2]; | ||
*(int *)args[10] = ihmsf[3]; | ||
END_LOOP(11) | ||
} | ||
|
||
static PyUFuncGenericFunction eraD2dtf_functions[] = { eraD2dtf_wrapper }; | ||
static void *eraD2dtf_data[] = {NULL}; | ||
static char eraD2dtf_types[] = { | ||
NPY_STRING, NPY_INT, NPY_DOUBLE, NPY_DOUBLE, NPY_INT, NPY_INT, NPY_INT, | ||
NPY_INT, NPY_INT, NPY_INT, NPY_INT }; | ||
|
||
|
||
/*/////////////////////////////////////////////////////////////////////////// | ||
module level | ||
*/ | ||
|
||
#if defined(NPY_PY3K) | ||
static struct PyModuleDef moduledef = { | ||
PyModuleDef_HEAD_INIT, | ||
"c_ufunc", | ||
NULL, | ||
-1, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL | ||
}; | ||
#endif | ||
|
||
#if defined(NPY_PY3K) | ||
#define RETVAL m | ||
PyObject *PyInit_c_ufunc(void) | ||
#else | ||
#define RETVAL | ||
PyMODINIT_FUNC | ||
initc_ufunc(void) | ||
#endif | ||
{ | ||
PyObject *m; | ||
PyObject *d; | ||
PyObject *f; | ||
|
||
#if defined(NPY_PY3K) | ||
m = PyModule_Create(&moduledef); | ||
#else | ||
m = Py_InitModule("c_ufunc", NULL); | ||
#endif | ||
if (m == NULL) | ||
return RETVAL; | ||
|
||
import_array(); | ||
import_ufunc(); | ||
|
||
d = PyModule_GetDict(m); | ||
|
||
f = PyUFunc_FromFuncAndData( | ||
eraAtco13_functions, eraAtco13_data, eraAtco13_types, 1, 18, 6, | ||
PyUFunc_None, "eraAtco13", "eraAtco13", 0); | ||
PyDict_SetItemString(d, "atco13", f); | ||
Py_DECREF(f); | ||
|
||
f = PyUFunc_FromFuncAndData( | ||
eraD2dtf_functions, eraD2dtf_data, eraD2dtf_types, 1, 4, 7, | ||
PyUFunc_None, "eraD2dtf", "eraD2dtf", 0); | ||
PyDict_SetItemString(d, "eraD2dtf", f); | ||
Py_DECREF(f); | ||
|
||
return RETVAL; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from distutils.core import setup | ||
from distutils.extension import Extension | ||
setup(name='c_ufunc', | ||
ext_modules=[ | ||
Extension( | ||
'c_ufunc', | ||
['c_ufunc.c'], | ||
libraries=['erfa'] | ||
)], | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: I think this is missing the numpy include path?