source: sasview/src/sas/sascalc/calculator/c_extensions/sld2i_module.c @ f54e82cf

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since f54e82cf was f54e82cf, checked in by Torin Cooper-Bennun <torin.cooper-bennun@…>, 6 years ago

Cherry-pick changes from master for tinycc compatibility (note: tinycc compilation not yet supported fully):

144e032af2 tinycc doesn't return structures, so must pass return structure as pointer
a1daf86c0d hack around broken isfinite/isnan in tinycc
7e82256ecb declare all variables at the start of the block so C89 compilers don't complain

  • Property mode set to 100755
File size: 5.2 KB
Line 
1/**
2  SLD2I module to perform point and I calculations
3 */
4#include <Python.h>
5#include <stdio.h>
6#include "sld2i.h"
7
8#if PY_MAJOR_VERSION < 3
9typedef void (*PyCapsule_Destructor)(PyObject *);
10typedef void (*PyCObject_Destructor)(void *);
11#define PyCapsule_New(pointer, name, destructor) (PyCObject_FromVoidPtr(pointer, (PyCObject_Destructor)destructor))
12#define PyCapsule_GetPointer(capsule, name) (PyCObject_AsVoidPtr(capsule))
13#endif
14
15
16// Utilities
17#define INVECTOR(obj,buf,len)                                                                           \
18    do { \
19        int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \
20        if (err < 0) return NULL; \
21        len /= sizeof(*buf); \
22    } while (0)
23
24#define OUTVECTOR(obj,buf,len) \
25    do { \
26        int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \
27        if (err < 0) return NULL; \
28        len /= sizeof(*buf); \
29    } while (0)
30
31
32/**
33 * Delete a GenI object
34 */
35void
36del_sld2i(PyObject *obj){
37        GenI* sld2i = (GenI *)(PyCapsule_GetPointer(obj, "GenI"));
38        PyMem_Free((void *)sld2i);
39}
40
41/**
42 * Create a GenI as a python object by supplying arrays
43 */
44PyObject * new_GenI(PyObject *self, PyObject *args) {
45        PyObject *x_val_obj;
46        PyObject *y_val_obj;
47        PyObject *z_val_obj;
48        PyObject *sldn_val_obj;
49        PyObject *mx_val_obj;
50        PyObject *my_val_obj;
51        PyObject *mz_val_obj;
52        PyObject *vol_pix_obj;
53        Py_ssize_t n_x;
54        //PyObject rlimit_obj;
55        //PyObject npoints_obj;
56        //PyObject nrbins_obj;
57        //PyObject nphibins_obj;
58        int n_pix;
59        double* x_val;
60        double* y_val;
61        double* z_val;
62        double* sldn_val;
63        double* mx_val;
64        double* my_val;
65        double* mz_val;
66        double* vol_pix;
67        double inspin;
68        double outspin;
69        double stheta;
70        GenI *sld2i;
71
72        if (!PyArg_ParseTuple(args, "iOOOOOOOOddd", &n_pix, &x_val_obj, &y_val_obj, &z_val_obj, &sldn_val_obj, &mx_val_obj, &my_val_obj, &mz_val_obj, &vol_pix_obj, &inspin, &outspin, &stheta)) return NULL;
73        OUTVECTOR(x_val_obj, x_val, n_x);
74        OUTVECTOR(y_val_obj, y_val, n_x);
75        OUTVECTOR(z_val_obj, z_val, n_x);
76        OUTVECTOR(sldn_val_obj, sldn_val, n_x);
77        OUTVECTOR(mx_val_obj, mx_val, n_x);
78        OUTVECTOR(my_val_obj, my_val, n_x);
79        OUTVECTOR(mz_val_obj, mz_val, n_x);
80        OUTVECTOR(vol_pix_obj, vol_pix, n_x);
81        sld2i =  PyMem_Malloc(sizeof(GenI));
82        if (sld2i != NULL) {
83                initGenI(sld2i, n_pix,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta);
84        }
85        return PyCapsule_New(sld2i, "GenI", del_sld2i);
86}
87
88/**
89 * GenI the given input (2D) according to a given object
90 */
91PyObject * genicom_inputXY(PyObject *self, PyObject *args) {
92        int npoints;
93        PyObject *qx_obj;
94        double *qx;
95        PyObject *qy_obj;
96        double *qy;
97        PyObject *I_out_obj;
98        Py_ssize_t n_out;
99        double *I_out;
100        PyObject *gen_obj;
101        GenI *sld2i;
102
103        if (!PyArg_ParseTuple(args, "OiOOO",  &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL;
104        OUTVECTOR(qx_obj, qx, n_out);
105        OUTVECTOR(qy_obj, qy, n_out);
106        OUTVECTOR(I_out_obj, I_out, n_out);
107
108        // Sanity check
109        //if(n_in!=n_out) return Py_BuildValue("i",-1);
110
111        // Set the array pointers
112        sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI");
113
114        genicomXY(sld2i, npoints, qx, qy, I_out);
115        //return PyCObject_FromVoidPtr(s, del_genicom);
116        return Py_BuildValue("i",1);
117}
118
119/**
120 * GenI the given 1D input according to a given object
121 */
122PyObject * genicom_input(PyObject *self, PyObject *args) {
123        int npoints;
124        PyObject *q_obj;
125        double *q;
126        PyObject *I_out_obj;
127        Py_ssize_t n_out;
128        double *I_out;
129        PyObject *gen_obj;
130        GenI *sld2i;
131
132        if (!PyArg_ParseTuple(args, "OiOO",  &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL;
133        OUTVECTOR(q_obj, q, n_out);
134        OUTVECTOR(I_out_obj, I_out, n_out);
135
136        // Sanity check
137        //if(n_in!=n_out) return Py_BuildValue("i",-1);
138
139        // Set the array pointers
140        sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI");
141
142        genicom(sld2i, npoints, q, I_out);
143        //return PyCObject_FromVoidPtr(s, del_genicom);
144        return Py_BuildValue("i",1);
145}
146
147/**
148 * Define module methods
149 */
150static PyMethodDef module_methods[] = {
151        {"new_GenI", (PyCFunction)new_GenI, METH_VARARGS,
152                  "Create a new GenI object"},
153        {"genicom",(PyCFunction)genicom_input, METH_VARARGS,
154                  "genicom the given 1d input arrays"},
155        {"genicomXY",(PyCFunction)genicom_inputXY, METH_VARARGS,
156                  "genicomXY the given 2d input arrays"},
157    {NULL}
158};
159
160#define MODULE_DOC "Sld2i C Library"
161#define MODULE_NAME "sld2i"
162#define MODULE_INIT2 initsld2i
163#define MODULE_INIT3 PyInit_sld2i
164#define MODULE_METHODS module_methods
165
166/* ==== boilerplate python 2/3 interface bootstrap ==== */
167
168
169#if defined(WIN32) && !defined(__MINGW32__)
170    #define DLL_EXPORT __declspec(dllexport)
171#else
172    #define DLL_EXPORT
173#endif
174
175#if PY_MAJOR_VERSION >= 3
176
177  DLL_EXPORT PyMODINIT_FUNC MODULE_INIT3(void)
178  {
179    static struct PyModuleDef moduledef = {
180      PyModuleDef_HEAD_INIT,
181      MODULE_NAME,         /* m_name */
182      MODULE_DOC,          /* m_doc */
183      -1,                  /* m_size */
184      MODULE_METHODS,      /* m_methods */
185      NULL,                /* m_reload */
186      NULL,                /* m_traverse */
187      NULL,                /* m_clear */
188      NULL,                /* m_free */
189    };
190    return PyModule_Create(&moduledef);
191  }
192
193#else /* !PY_MAJOR_VERSION >= 3 */
194
195  DLL_EXPORT PyMODINIT_FUNC MODULE_INIT2(void)
196  {
197    Py_InitModule4(MODULE_NAME,
198                 MODULE_METHODS,
199                 MODULE_DOC,
200                 0,
201                 PYTHON_API_VERSION
202                 );
203  }
204
205#endif /* !PY_MAJOR_VERSION >= 3 */
Note: See TracBrowser for help on using the repository browser.