File: _operatormodule.c

package info (click to toggle)
python-numarray 1.5.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,668 kB
  • ctags: 11,384
  • sloc: ansic: 113,864; python: 22,422; makefile: 197; sh: 11
file content (323 lines) | stat: -rw-r--r-- 8,803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
75
76
77
78
79
80
81
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
148
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "Python.h"
#include "structmember.h"
#include "libnumarray.h"

char *_operator__doc__ =
"_operator is a module which supplies the _operator baseclass of Operator.  Class\n"
"_operator is a baseclass used to accelerate selected methods of Operator by\n"
"handling simple cases directly in C.  More complex cases are delegated to\n"
"python coded methods.\n";

#define DEFERRED_ADDRESS(ADDR) 0

staticforward PyTypeObject _operator_type;

static PyGetSetDef _operator_getsets[] = {
	{0},
};

static PyObject *_operator_compute(PyObject *self, PyObject *indices, PyObject *shape);

static PyObject *
_operator_new(PyTypeObject *type, PyObject  *args, PyObject *kwds)
{
	PyOperatorObject *self = 
		(PyOperatorObject *) PyType_GenericNew(type, args, kwds);
	if (!self) return NULL;

	self->compute = _operator_compute;
	self->inputs   = Py_None; Py_INCREF(Py_None);
	self->outputs  = Py_None; Py_INCREF(Py_None);
	self->cfunc    = Py_None; Py_INCREF(Py_None);
	self->striding = 0;

	if (!self->inputs || !self->outputs || !self->cfunc)
		return NULL; 

	return (PyObject *) self;
}

static int
_operator_init(PyObject *self, PyObject  *args, PyObject *kwds)
{
	PyOperatorObject *me = (PyOperatorObject *) self;
	int striding = 0;

	Py_DECREF(me->inputs);
	Py_DECREF(me->outputs);
	Py_DECREF(me->cfunc);

	if (!PyArg_ParseTuple(args, "OOO|i", &me->cfunc, 
			      &me->inputs, &me->outputs, &striding))
		return -1;

	if (!PySequence_Check(me->inputs) || 
	    !PySequence_Check(me->outputs)) {
		PyErr_Format(PyExc_TypeError,
		    "_operator_init: inputs or outputs not a sequence.");
		return -1;
	}

	Py_INCREF(me->inputs);
	Py_INCREF(me->outputs);
	Py_INCREF(me->cfunc);
	me->striding = striding;

	return 0;
}

static void
_operator_dealloc(PyObject *self)
{
	PyOperatorObject *me = (PyOperatorObject *) self;
	Py_XDECREF(me->inputs);
	Py_XDECREF(me->outputs);
	Py_XDECREF(me->cfunc);
	self->ob_type->tp_free(self);
}

#define MAXIO (MAXDIM+10)

static PyObject *
_operator_buffer(PyOperatorObject *self, PyObject *obj, int i)
{
	PyObject *buffer;
	assert(NA_NDArrayCheck(obj));
	buffer = ((PyArrayObject *) obj)->_data;
	if (!buffer)
		PyErr_Format( PyExc_RuntimeError,
		      "_operator_compute: obj[%d] problem with _data.", i);
	return buffer;
}

static int 
_operator_buffer_offset(PyOperatorObject *self, PyObject *obj, 
			maybelong nindices, maybelong *indices, long *offset)
{
	assert(NA_NDArrayCheck(obj));
	if (((PyArrayObject *) obj)->nstrides >= 0)  /* < 0  -->  a._strides is None */
		return NA_getByteOffset((PyArrayObject *) obj, nindices, indices, offset);
	*offset = 0;
	return 0;
}

static void
_operator_buffer_strides(PyOperatorObject *self, PyObject *obj,
			 maybelong nshape, maybelong *shape, int itemsize,
			 maybelong  *nstrides, maybelong *strides)
{
	PyArrayObject *obja = (PyArrayObject *) obj;
	int i;
	assert(NA_NDArrayCheck(obj));
	if (obja->nstrides >= 0) { /* not a _UBuffer */
		*nstrides = obja->nstrides;
		for(i=0; i<obja->nstrides; i++)
			strides[i] = obja->strides[i];
	} else {
		NA_stridesFromShape( nshape, shape, itemsize, strides);
		*nstrides = nshape;
	}
}	

static int
_operator_data_offset(PyOperatorObject *self, int slen, PyObject *sequence,
		      maybelong nindices, maybelong *indices,
		      PyObject **buffer, long *offset)
{
	int i;
	for(i=0; i<slen; i++) {
		PyObject *obj = PySequence_GetItem(sequence, i);
		if  (!obj) goto _fail2;
		if (!(buffer[i] = _operator_buffer(self, obj, i)))
			goto  _fail2;
		if (_operator_buffer_offset(self, obj, nindices, indices, offset+i) < 0)
			goto _fail2;
		Py_DECREF(obj);
		continue;
	  _fail2:		       
		Py_XDECREF(obj);
		return -1;
	}
	return 0;
}

static PyObject *_operator_compute(PyObject *me, PyObject *oindices, PyObject *oshape)
{
	PyOperatorObject *self = (PyOperatorObject*) me;
	PyObject *result = NULL;
	int ninputs = PySequence_Size(self->inputs);
	int noutputs = PySequence_Size(self->outputs);
	maybelong nshape, shape[MAXDIM];
	maybelong nindices, indices[MAXDIM];
	
	nshape = NA_maybeLongsFromIntTuple(MAXDIM, shape, oshape);
	if (nshape < 0) return NULL;

	nindices = NA_maybeLongsFromIntTuple(MAXDIM, indices, oindices);
	if (nindices < 0) return NULL;

	if (ninputs+noutputs > MAXIO)
		return PyErr_Format( PyExc_ValueError, 
		     "_operator_compute: too many inputs + outputs");

	if (self->striding) {
		PyObject *input=NULL, *output=NULL;
		PyObject *inbuffer, *outbuffer;
		long inoffset, outoffset;
		maybelong ninstrides, instrides[MAXDIM];
		maybelong noutstrides, outstrides[MAXDIM], 
			*poutstrides = outstrides;
		assert(ninputs==1 && noutputs==1);
		input = PySequence_GetItem(self->inputs, 0);
		if (!input) return NULL;
		output = PySequence_GetItem(self->outputs, 0);
		if (!output) return NULL;
		if (_operator_buffer_offset(
			    self, input, nindices, indices, &inoffset) < 0)
			goto _fail;
		if (_operator_buffer_offset(
			    self, output, nindices, indices, &outoffset) < 0)
			goto _fail;
		if (!(inbuffer = _operator_buffer(self, input, 0)))
			goto _fail;
		if (!(outbuffer = _operator_buffer(self, output, 1)))
			goto _fail;
		_operator_buffer_strides(
			self, input, nshape, shape, self->striding,
			&ninstrides, instrides);
		_operator_buffer_strides(
			self, output, nshape, shape, self->striding,
			&noutstrides, outstrides);
		if (noutstrides > ninstrides) { /* hack for reductions */
			poutstrides += noutstrides - ninstrides;
			noutstrides = ninstrides;
		}
		result = NA_callStrideConvCFuncCore( 
			self->cfunc, nshape, shape,
			inbuffer, inoffset, ninstrides, instrides,
			outbuffer, outoffset, noutstrides, poutstrides, 0);
		Py_XDECREF(input);
		Py_XDECREF(output);
		goto _done;
	  _fail:
		assert(0);
		Py_XDECREF(input);
		Py_XDECREF(output);
	} else {
		PyObject *buffers[MAXIO];
		long niter, offsets[MAXIO];
		if (NA_intTupleProduct(oshape, &niter) < 0) {
			PyErr_Format(PyExc_RuntimeError, 
				     "_operator_compute: problem with shape");
			goto _fail2;
		}
		if (_operator_data_offset(
			    self, ninputs, self->inputs, nindices, indices,
			    buffers, offsets) < 0)
			goto _fail2;
		if (_operator_data_offset(
			    self, noutputs, self->outputs, nindices, indices,
			    buffers+ninputs, offsets+ninputs) < 0)
			goto _fail2;
		result = NA_callCUFuncCore(
			self->cfunc, niter, ninputs, noutputs, buffers, offsets);
	}			
  _done:	    
  _fail2:
	return result;
}

static PyObject *
_Py_operator_compute(PyObject *self, PyObject *args)
{
	PyObject *indices, *shape;
	
	if (!PyArg_ParseTuple(args, "OO:_operator_compute", 
			      &indices, &shape))
		return NULL;
	return _operator_compute(self, indices, shape);
}

static PyMethodDef _operator_methods[] = {
	{"compute", (PyCFunction)_Py_operator_compute, METH_VARARGS,
	   "compute(self, indices, shape)  computes one block at 'indices' with 'shape'"},
	{NULL,	NULL},
};

static PyTypeObject _operator_type = {
	PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
	0,
	"numarray._operator._operator",
	sizeof(PyOperatorObject),
	0,
	_operator_dealloc,	                /* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	0,					/* tp_repr */
	0,					/* tp_as_number */
	0,                                      /* tp_as_sequence */
	0,			                /* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | 
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
	0,					/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	_operator_methods,			/* tp_methods */
	0,					/* tp_members */
	_operator_getsets,			/* tp_getset */
	0,                                      /* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	_operator_init,	                        /* tp_init */
	0, 		                        /* tp_alloc */
	_operator_new,				/* tp_new */
};

static PyMethodDef _operator_functions[] = {
/*	{"_isIntegerSequence", 
	 (PyCFunction) _operator_isIntegerSequence, 
	 METH_VARARGS,
	 "_isIntegerSequence(s) returns 1 if all elements of sequence s are integers, else 0"},
*/
	{NULL,		NULL}		/* sentinel */
};

DL_EXPORT(void)
init_operator(void)
{
	PyObject *m;

	_operator_type.tp_alloc = PyType_GenericAlloc;

	if (PyType_Ready(&_operator_type) < 0)
		return;

	m = Py_InitModule3("_operator",
			   _operator_functions,
			   _operator__doc__);
	if (m == NULL)
		return;

	Py_INCREF(&_operator_type);
	if (PyModule_AddObject(m, "_operator",
			       (PyObject *) &_operator_type) < 0)
		return;

	ADD_VERSION(m);

	import_libnumarray();
}