Half baked C API?
I recently took a closer at Numeric's and numarray's C APIs for the first time and was surprised not to find the counterparts for all the array functions that are available in the Python API. Did I overlook anything, or do I really have to re-implement things like 'sum', 'argmax', 'convolve', 'cos' in C? Ralf
On 15.03.2005, at 02:50, Ralf Juengling wrote:
I recently took a closer at Numeric's and numarray's C APIs for the first time and was surprised not to find the counterparts for all the array functions that are available in the Python API.
Did I overlook anything, or do I really have to re-implement things like 'sum', 'argmax', 'convolve', 'cos' in C?
Can you think of a real-life situation where you would want to call these from C? Usually, C modules using arrays are written to add functionality that can not be expressed efficiently in terms of existing array operations. If you want to compose things like sum and argmax, you can do that in Python. Note also that if you do need to call these routines from your C code, you can always do so via the generic Python API for calling Python functions. However, reimplementing them in C may often turn out to be simpler - doing sum() in C is really a trivial piece of work. Konrad. -- ------------------------------------------------------------------------ ------- Konrad Hinsen Laboratoire Leon Brillouin, CEA Saclay, 91191 Gif-sur-Yvette Cedex, France Tel.: +33-1 69 08 79 25 Fax: +33-1 69 08 82 61 E-Mail: khinsen@cea.fr ------------------------------------------------------------------------ -------
konrad.hinsen@laposte.net wrote:
Did I overlook anything, or do I really have to re-implement things like 'sum', 'argmax', 'convolve', 'cos' in C?
Can you think of a real-life situation where you would want to call these from C? Usually, C modules using arrays are written to add functionality that can not be expressed efficiently in terms of existing array operations. If you want to compose things like sum and argmax, you can do that in Python.
Yes. Think of dynamic programming algorithms like forward, backward, and viterbi for Hidden Markov Models. In this case you cannot avoid a loop over one axis, yet the code in the loop can be expressed in a few lines by matrix operations like 'dot', 'sum', 'outerproduct', 'argmax', elementwise multiplication, etc. As an example, the forward algorithm can be written as alpha[0] = P_YcS[y[0]]*P_S0 gamma[0] = sum(alpha[0]) alpha[0] /= gamma[0] for t in xrange(1, T): P_ScY_1_prev = dot(P_ScS, alpha[t-1]) P_SYcY_1_prev = P_YcS[y[t]]*P_ScY_1_prev gamma[t] = sum(P_SYcY_1_prev) alpha[t] = P_SYcY_1_prev/gamma[t]
Note also that if you do need to call these routines from your C code, you can always do so via the generic Python API for calling Python functions. However, reimplementing them in C may often turn out to be simpler - doing sum() in C is really a trivial piece of work.
Sure, many array functions like 'sum' are easy to implement in C, but I don't want to, if don't have to for performance reasons. In an ideal world, I'd spend most of my time prototyping the algorithms in Python, and then, if performance is a problem, translate parts to C (or hand them to weave.blitz) with only minor changes to the prototype code. And if that's still not fast enough, then I'd go and rethink the problem in C. Referring to the example above, I'd also want that an optimized BLAS implementation of 'dot' be used if available, and the scipy_core substitute version if not. So yeah, I claim that, to make weave a truly useful tool, all array functions of the Python API should also be available in the C API. Maybe having a few specialized versions, e.g., for contiguous arrays of double floats, would be a good idea, too. Ralf
Konrad. -- ------------------------------------------------------------------------ ------- Konrad Hinsen Laboratoire Leon Brillouin, CEA Saclay, 91191 Gif-sur-Yvette Cedex, France Tel.: +33-1 69 08 79 25 Fax: +33-1 69 08 82 61 E-Mail: khinsen@cea.fr ------------------------------------------------------------------------ -------
On Mar 15, 2005, at 18:04, Ralf Juengling wrote:
Yes. Think of dynamic programming algorithms like forward, backward, and viterbi for Hidden Markov Models. In this case you cannot avoid a loop over one axis, yet the code in the loop can be expressed in a few lines by matrix operations like 'dot', 'sum', 'outerproduct',
How much do you expect to gain compared to a Python loop in such a case?
I don't want to, if don't have to for performance reasons. In an ideal world, I'd spend most of my time prototyping the algorithms in Python, and then, if performance is a problem, translate parts to C (or hand them to weave.blitz) with only minor changes to the prototype code.
Did you consider Pyrex? It lets you move from pure Python to pure C with Python syntax, mixing both within a single function. Konrad. -- --------------------------------------------------------------------- Konrad Hinsen Laboratoire Léon Brillouin, CEA Saclay, 91191 Gif-sur-Yvette Cedex, France Tel.: +33-1 69 08 79 25 Fax: +33-1 69 08 82 61 E-Mail: khinsen@cea.fr ---------------------------------------------------------------------
Ralf Juengling wrote:
I recently took a closer at Numeric's and numarray's C APIs for the first time and was surprised not to find the counterparts for all the array functions that are available in the Python API.
How much to support on the C-API level is a question I am interested in right now. I have mixed feelings. On the one hand, it is much simpler on the array package developer (which from my perspective seems to be the short-end of the current stick) to have a reduced C-API and require individuals who want to access the functionality to go through the PyObject_CallMethod approach. We could perhaps provide a single function that made this a little simpler for arrays. On the other hand, a parallel API that made available everything that was present in Python might "look nicer," be a little faster, and make it easier on the extension writer. I'm interested in opinions, -Travis
participants (3)
-
konrad.hinsen@laposte.net
-
Ralf Juengling
-
Travis Oliphant