|
| 1 | +Functions and Function signatures |
| 2 | +================================= |
| 3 | + |
| 4 | +Adding functions |
| 5 | +---------------- |
| 6 | + |
| 7 | +In order to add new functions to ``numexpr``, currently it is necessary to edit several files. Consider adding a function |
| 8 | +``out_type myfunc(arg_type)``. |
| 9 | + |
| 10 | +* ``numexpr/expressions.py`` |
| 11 | +Add ``'myfunc': func(numpy.myfunc, out_dtype),`` to the dict of functions, ``functions = {...``. If the return type of the function is ``bool``, add |
| 12 | +the function to the list ``if opcode in ("isnan", "isfinite"):`` in the ``__init__`` function of the ``FuncNode`` class. |
| 13 | +In the future it might be nice to refactor this function since it sets the output type based on the type of the inputs in general. |
| 14 | + |
| 15 | +* ``numexpr/necompiler.py`` |
| 16 | +Add ``"myfunc"`` to the list of functions: |
| 17 | + |
| 18 | +.. code-block:: python3 |
| 19 | +
|
| 20 | + "floor", |
| 21 | + "isnan", |
| 22 | + "isfinite", |
| 23 | + "myfunc" |
| 24 | + ] |
| 25 | +
|
| 26 | +* ``numexpr/functions.hpp`` |
| 27 | +Find the correct function signature ``FUNC_OA`` where ``O`` is the return type, and ``A`` the argument type(s). For example, if the function |
| 28 | +is ``double myfunc(double)``, one should edit within the ``FUNC_DD`` clause. If you cannot find your function signature you will have to add it, |
| 29 | +following the template of the other functions. |
| 30 | +Most likely, you will want to add support for several function signatures (e.g. double -> bool and float -> bool) and so you will have to add the |
| 31 | +function in two clauses. If your function has a float input, you will see that there are 5 arguments in the |
| 32 | +``FUNC_OA`` macro, and you will have to add ``myfunc2`` here is order to compile on MSVC machines (i.e. Windows, see following). |
| 33 | +Example: |
| 34 | + |
| 35 | +.. code-block:: cpp |
| 36 | + :emphasize-lines: 6, 20 |
| 37 | +
|
| 38 | + #ifndef FUNC_DD |
| 39 | + #define ELIDE_FUNC_DD |
| 40 | + #define FUNC_DD(...) |
| 41 | + #endif |
| 42 | + ... |
| 43 | + FUNC_DD(FUNC_MYFUNC_DD, "myfunc_dd", myfunc, vdMyfunc) |
| 44 | + FUNC_DD(FUNC_DD_LAST, NULL, NULL, NULL) |
| 45 | + #ifdef ELIDE_FUNC_DD |
| 46 | + #undef ELIDE_FUNC_DD |
| 47 | + #undef FUNC_DD |
| 48 | + #endif |
| 49 | +
|
| 50 | + ... |
| 51 | +
|
| 52 | + #ifndef FUNC_FF |
| 53 | + #define ELIDE_FUNC_FF |
| 54 | + #define FUNC_FF(...) |
| 55 | + #endif |
| 56 | + ... |
| 57 | + FUNC_FF(FUNC_MYFUNC_FF, "myfunc_ff", myfuncf, myfuncf2, vfMyfunc) |
| 58 | + FUNC_FF(FUNC_FF_LAST, NULL, NULL, NULL, NULL) |
| 59 | + #ifdef ELIDE_FUNC_FF |
| 60 | + #undef ELIDE_FUNC_FF |
| 61 | + #undef FUNC_FF |
| 62 | + #endif |
| 63 | +
|
| 64 | +* ``numexpr/msvc_function_stubs.hpp`` |
| 65 | +In order to support float arguments, due to oddities of MSVC, you have to provide explicit support for your function in this file. |
| 66 | +Add ``#define myfuncf(x) ((float)floor((double)(x)))`` (if your function is float -> float) to the ``#if`` clause at the top of the file |
| 67 | +which is for old versions of MSVC which did not have support for single precision fucntions. Then in the body, add an inline function |
| 68 | + |
| 69 | +.. code-block:: cpp |
| 70 | +
|
| 71 | + inline float myfuncf2(float x) { |
| 72 | + return myfuncf(x); |
| 73 | + } |
| 74 | +
|
| 75 | +This is the function that appears as the ``f_win32`` parameter in ``functions.hpp``. |
| 76 | + |
| 77 | +* ``numexpr/tests/test_numexpr.py`` |
| 78 | +Don't forget to add a test for your function! |
| 79 | + |
| 80 | +Adding function signatures |
| 81 | +-------------------------- |
| 82 | +It may so happen that you cannot find your desired function signature in ``functions.hpp``. This means you will have to add it yourself! |
| 83 | +This involves editing a few more files. In addition, there may be certain bespoke changes, specific to the function signature |
| 84 | +that you may have to make (see Notes, below) |
| 85 | + |
| 86 | +* ``numexpr/functions.hpp`` |
| 87 | +Firstly, add clause(s) for your function signature. For example, if the function signature is ``bool(double)`` and ``bool(float)``, add |
| 88 | +``FUNC_BD`` and ``FUNC_BF`` clauses (in the latter case you will need the macro to take 5 arguments for MSVC-compatibility.) |
| 89 | + |
| 90 | +.. code-block:: cpp |
| 91 | +
|
| 92 | + #ifndef FUNC_BD |
| 93 | + #define ELIDE_FUNC_BD |
| 94 | + #define FUNC_BD(...) |
| 95 | + #endif |
| 96 | + ... |
| 97 | + FUNC_BD(FUNC_BD_LAST, NULL, NULL, NULL) |
| 98 | + #ifdef ELIDE_FUNC_BD |
| 99 | + #undef ELIDE_FUNC_BD |
| 100 | + #undef FUNC_BD |
| 101 | + #endif |
| 102 | +
|
| 103 | + #ifndef FUNC_BF |
| 104 | + #define ELIDE_FUNC_BF |
| 105 | + #define FUNC_BF(...) |
| 106 | + #endif |
| 107 | + ... |
| 108 | + FUNC_BF(FUNC_BF_LAST, NULL, NULL, NULL, NULL) |
| 109 | + #ifdef ELIDE_FUNC_BF |
| 110 | + #undef ELIDE_FUNC_BF |
| 111 | + #undef FUNC_BF |
| 112 | + #endif |
| 113 | +
|
| 114 | +The ultimate source of the functions in the macro ``FUNC_BF(...)`` are the headers included in ``numexpr/interpreter.cpp`` (in particular |
| 115 | +``numexpr/numexpr_config.hpp``, which can be used to overwrite ``<math.h>`` functions), so the functions should be available from there. |
| 116 | + |
| 117 | +* ``numexpr/interp_body.cpp`` |
| 118 | +Add case support for OPCODES associated to your new function signatures via e.g. ``case OP_FUNC_BFN`` and ``case OP_FUNC_BDN``, following |
| 119 | +the framework suggested by the other functions: |
| 120 | + |
| 121 | +.. code-block:: cpp |
| 122 | +
|
| 123 | + case OP_FUNC_BFN: |
| 124 | + #ifdef USE_VML |
| 125 | + VEC_ARG1_VML(functions_bf_vml[arg2](BLOCK_SIZE, |
| 126 | + (float*)x1, (bool*)dest)); |
| 127 | + #else |
| 128 | + VEC_ARG1(b_dest = functions_bf[arg2](f1)); |
| 129 | + #endif |
| 130 | +
|
| 131 | +Note that it is important that the out variable matches the output type of the function (i.e. ``b_dest`` for bool, ``f_dest`` for float etc.) |
| 132 | + |
| 133 | +* ``numexpr/interpreter.hpp`` |
| 134 | +Add clauses to read the ``functions.hpp`` macros correctly |
| 135 | + |
| 136 | +.. code-block:: cpp |
| 137 | +
|
| 138 | + enum FuncBFCodes { |
| 139 | + #define FUNC_BF(fop, ...) fop, |
| 140 | + #include "functions.hpp" |
| 141 | + #undef FUNC_BF |
| 142 | + }; |
| 143 | +
|
| 144 | +* ``numexpr/interpreter.cpp`` |
| 145 | +Add clauses to generate the FUNC_CODES from the ``functions.hpp`` header, making sure to include clauses for ``_WIN32`` and |
| 146 | +``VML`` as necessary accoridng to the framework suggested by the other functions. |
| 147 | + |
| 148 | +.. code-block:: cpp |
| 149 | +
|
| 150 | + typedef bool (*FuncBFPtr)(float); |
| 151 | + #ifdef _WIN32 |
| 152 | + FuncBFPtr functions_bf[] = { |
| 153 | + #define FUNC_BF(fop, s, f, f_win32, ...) f_win32, |
| 154 | + #include "functions.hpp" |
| 155 | + #undef FUNC_BF |
| 156 | + }; |
| 157 | + #else |
| 158 | + FuncBFPtr functions_bf[] = { |
| 159 | + #define FUNC_BF(fop, s, f, ...) f, |
| 160 | + #include "functions.hpp" |
| 161 | + #undef FUNC_BF |
| 162 | + }; |
| 163 | + #endif |
| 164 | +
|
| 165 | + #ifdef USE_VML |
| 166 | + typedef void (*FuncBFPtr_vml)(MKL_INT, const float*, bool*); |
| 167 | + FuncBFPtr_vml functions_bf_vml[] = { |
| 168 | + #define FUNC_BF(fop, s, f, f_win32, f_vml) f_vml, |
| 169 | + #include "functions.hpp" |
| 170 | + #undef FUNC_BF |
| 171 | + }; |
| 172 | + #endif |
| 173 | +
|
| 174 | +Add case handling to the ``check_program`` function |
| 175 | + |
| 176 | +.. code-block:: cpp |
| 177 | +
|
| 178 | + else if (op == OP_FUNC_BDN) { |
| 179 | + if (arg < 0 || arg >= FUNC_BD_LAST) { |
| 180 | + PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc); |
| 181 | + return -1; |
| 182 | + } |
| 183 | + } |
| 184 | + else if (op == OP_FUNC_BFN) { |
| 185 | + if (arg < 0 || arg >= FUNC_BF_LAST) { |
| 186 | + PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc); |
| 187 | + return -1; |
| 188 | + } |
| 189 | + } |
| 190 | +
|
| 191 | +* ``numexpr/module.cpp`` |
| 192 | +Add code here to define the ``FUNC_OA`` macros you require |
| 193 | + |
| 194 | +.. code-block:: cpp |
| 195 | +
|
| 196 | + #define FUNC_BF(name, sname, ...) add_func(name, sname); |
| 197 | + #define FUNC_BD(name, sname, ...) add_func(name, sname); |
| 198 | + ... |
| 199 | + #include "functions.hpp" |
| 200 | + ... |
| 201 | + #undef FUNC_BD |
| 202 | + #undef FUNC_BF |
| 203 | +
|
| 204 | +* ``numexpr/opcodes.hpp`` |
| 205 | +Finally, add the ``OP_FUNC_BDN`` etc. codes here. It is necessary for the OPCODES in the file to be in (ascending order) with |
| 206 | +``NOOP`` as 0 and ``OP_LAST`` as the largest number. Secondly, all reduction OPCODES must appear last. Hence, after adding your |
| 207 | +function signatures (just before the reduction OPCODES) it is necessary to increment all succeeding OPCODES. |
| 208 | + |
| 209 | +.. code-block:: cpp |
| 210 | +
|
| 211 | + OPCODE(106, OP_FUNC_BDN, "func_bdn", Tb, Td, Tn, T0) |
| 212 | + OPCODE(107, OP_FUNC_BFN, "func_bfn", Tb, Tf, Tn, T0) |
| 213 | +
|
| 214 | +Notes |
| 215 | +----- |
| 216 | +In many cases this process will not be very smooth since one relies on the internal C/C++ standard functions (which can be fussy, to varying degrees on different platforms). Some common gotchas are then: |
| 217 | + |
| 218 | +* OPCODES are currently only supported up to 255 - if it becomes necessary to increment further, one will have to change the ``latin_1`` encoding used in ``quadrupleToString`` in ``necompiler.py``. In addition, since the OPCDE table is assumed to be of type ``unsigned char`` the ``get_return_sig`` function in ``numexpr/interpreter.cpp`` may have to be changed (possibly other changes too). |
| 219 | + |
| 220 | +* Depending on the new function signature (above all if the out type is different to the input types), one may have to edit the ``__init__`` function in the ``FuncNode`` class in ``expressions.py``. |
| 221 | + |
| 222 | +* Depending on MSVC support, namespace clashes, casting problems, it may be necessary to make various changes to ``numexpr/numexpr_config.hpp`` and ``numexpr/msvc_function_stubs.hpp``. For example, in PR #523, non-clashing wrappers were introduced for ``isnan`` and ``isfinite`` since the float versions ``isnanf, isfinitef`` were inconsistently defined (and output ints) - depending on how strict the platform interpreter is, the implicit cast from int to bool was acceptable or not for example. In addition, the base functions were in different namespaces or had different names across platforms. |
0 commit comments