summaryrefslogtreecommitdiff
path: root/docs/src/userguide/fusedtypes.rst
blob: 3167c77d353fd8b65a3c6aaddbcd94a56f470c77 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
.. highlight:: cython

.. _fusedtypes:

***********************
Fused Types (Templates)
***********************

.. include::
    ../two-syntax-variants-used

Fused types allow you to have one type definition that can refer to multiple
types.  This allows you to write a single static-typed cython algorithm that can
operate on values of multiple types. Thus fused types allow `generic
programming`_ and are akin to templates in C++ or generics in languages like
Java / C#.

.. _generic programming: https://en.wikipedia.org/wiki/Generic_programming

.. Note:: Fused types are not currently supported as attributes of extension
          types.  Only variables and function/method arguments can be declared
          with fused types.


Quickstart
==========

.. tabs::

    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/userguide/fusedtypes/char_or_float.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/userguide/fusedtypes/char_or_float.pyx

This gives:

.. code-block:: pycon

    >>> show_me()
    char -128
    float 128.0

``plus_one(a)`` "specializes" the fused type ``char_or_float`` as a ``char``,
whereas ``plus_one(b)`` specializes ``char_or_float`` as a ``float``.

Declaring Fused Types
=====================

Fused types may be declared as follows:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            my_fused_type = cython.fused_type(cython.int, cython.float)

    .. group-tab:: Cython

        .. code-block:: cython

            ctypedef fused my_fused_type:
                int
                double

This declares a new type called ``my_fused_type`` which can be *either* an
``int`` *or* a ``double``.

Only names may be used for the constituent types, but they may be any
(non-fused) type, including a typedef. I.e. one may write:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            my_double = cython.typedef(cython.double)
            my_fused_type = cython.fused_type(cython.int, my_double)

    .. group-tab:: Cython

        .. code-block:: cython

            ctypedef double my_double
            ctypedef fused fused_type:
                int
                my_double

Using Fused Types
=================

Fused types can be used to declare parameters of functions or methods:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            def cfunc(arg: my_fused_type):
                return arg + 1

    .. group-tab:: Cython

        .. code-block:: cython

            cdef cfunc(my_fused_type arg):
                return arg + 1

If the same fused type appears more than once in the function arguments,
then they will all have the same specialised type:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            def cfunc(arg1: my_fused_type, arg2: my_fused_type):
                # arg1 and arg2 always have the same type here
                return arg1 + arg2

    .. group-tab:: Cython

        .. code-block:: cython

            cdef cfunc(my_fused_type arg1, my_fused_type arg2):
                # arg1 and arg2 always have the same type here
                return arg1 + arg2

Here, the type of both parameters is either an int, or a double
(according to the previous examples), because they use the same fused type
name ``my_fused_type``.  Mixing different fused types (or differently named
fused types) in the arguments will specialise them independently:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            def func(x: A, y: B):
                ...

    .. group-tab:: Cython

        .. code-block:: cython


            def func(A x, B y):
                ...

This will result in specialized code paths for all combinations of types
contained in ``A`` and ``B``, e.g.:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            my_fused_type = cython.fused_type(cython.int, cython.double)



            my_fused_type2 = cython.fused_type(cython.int, cython.double)


            @cython.cfunc
            def func(a: my_fused_type, b: my_fused_type2):
                # a and b may have the same or different types here
                print("SAME!" if my_fused_type is my_fused_type2 else "NOT SAME!")
                return a + b

    .. group-tab:: Cython

        .. code-block:: cython

            ctypedef fused my_fused_type:
                int
                double

            ctypedef fused my_fused_type2:
                int
                double

            cdef func(my_fused_type a, my_fused_type2 b):
                # a and b may have the same or different types here
                print("SAME!" if my_fused_type is my_fused_type2 else "NOT SAME!")
                return a + b




.. Note::  A simple typedef to rename the fused type does not currently work here.
    See Github issue :issue:`4302`.


Fused types and arrays
----------------------

Note that specializations of only numeric types may not be very useful, as one
can usually rely on promotion of types.  This is not true for arrays, pointers
and typed views of memory however.  Indeed, one may write:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            def myfunc(x: A[:, :]):
                ...

            # and

            @cython.cfunc
            cdef otherfunc(x: cython.pointer(A)):
                ...


    .. group-tab:: Cython

        .. code-block:: cython

            cdef myfunc(A[:, :] x):
                ...

            # and

            cdef otherfunc(A *x):
                ...

Following code snippet shows an example with pointer to the fused type:

.. tabs::

    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/userguide/fusedtypes/pointer.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/userguide/fusedtypes/pointer.pyx

.. Note::

    In Cython 0.20.x and earlier, the compiler generated the full cross
    product of all type combinations when a fused type was used by more than one
    memory view in a type signature, e.g.

    ::

        def myfunc(A[:] a, A[:] b):
            # a and b had independent item types in Cython 0.20.x and earlier.
            ...

    This was unexpected for most users, unlikely to be desired, and also inconsistent
    with other structured type declarations like C arrays of fused types, which were
    considered the same type.  It was thus changed in Cython 0.21 to use the same
    type for all memory views of a fused type.  In order to get the original
    behaviour, it suffices to declare the same fused type under different names, and
    then use these in the declarations::

        ctypedef fused A:
            int
            long

        ctypedef fused B:
            int
            long

        def myfunc(A[:] a, B[:] b):
            # a and b are independent types here and may have different item types
            ...

    To get only identical types also in older Cython versions (pre-0.21), a ``ctypedef``
    can be used::

        ctypedef A[:] A_1d

        def myfunc(A_1d a, A_1d b):
            # a and b have identical item types here, also in older Cython versions
            ...


Selecting Specializations
=========================

You can select a specialization (an instance of the function with specific or
specialized (i.e., non-fused) argument types) in two ways: either by indexing or
by calling.


.. _fusedtypes_indexing:

Indexing
--------

You can index functions with types to get certain specializations, i.e.:

.. tabs::

    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/userguide/fusedtypes/indexing.py
            :caption: indexing.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/userguide/fusedtypes/indexing.pyx
            :caption: indexing.pyx

Indexed functions can be called directly from Python:

.. code-block:: pycon

    >>> import cython
    >>> import indexing
    cfunc called: double 5.0 double 1.0
    cpfunc called: float 1.0 double 2.0
    func called: float 1.0 double 2.0
    >>> indexing.cpfunc[cython.float, cython.float](1, 2)
    cpfunc called: float 1.0 float 2.0
    >>> indexing.func[cython.float, cython.float](1, 2)
    func called: float 1.0 float 2.0

If a fused type is used as a component of a more complex type
(for example a pointer to a fused type, or a memoryview of a fused type),
then you should index the function with the individual component and
not the full argument type:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            @cython.cfunc
            def myfunc(x: cython.pointer(A)):
                ...

            # Specialize using int, not int *
            myfunc[cython.int](myint)

    .. group-tab:: Cython

        .. code-block:: cython

            cdef myfunc(A *x):
                ...

            # Specialize using int, not int *
            myfunc[int](myint)

For memoryview indexing from python space we can do the following:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            my_fused_type = cython.fused_type(cython.int[:, ::1], cython.float[:, ::1])

            def func(array: my_fused_type):
                print("func called:", cython.typeof(array))

            my_fused_type[cython.int[:, ::1]](myarray)

    .. group-tab:: Cython

        .. code-block:: cython

            ctypedef fused my_fused_type:
                int[:, ::1]
                float[:, ::1]

            def func(my_fused_type array):
                print("func called:", cython.typeof(array))

            my_fused_type[cython.int[:, ::1]](myarray)

The same goes for when using e.g. ``cython.numeric[:, :]``.

Calling
-------

A fused function can also be called with arguments, where the dispatch is
figured out automatically:

.. tabs::

    .. group-tab:: Pure Python

        .. code-block:: python

            def main():
                p1: cython.double = 1.0
                p2: cython.float = 2.0
                cfunc(p1, p1)          # prints "cfunc called: double 1.0 double 1.0"
                cpfunc(p1, p2)         # prints "cpfunc called: double 1.0 float 2.0"

    .. group-tab:: Cython

        .. code-block:: cython

            def main():
                cdef double p1 = 1.0
                cdef float p2 = 2.0
                cfunc(p1, p1)          # prints "cfunc called: double 1.0 double 1.0"
                cpfunc(p1, p2)         # prints "cpfunc called: double 1.0 float 2.0"

For a ``cdef`` or ``cpdef`` function called from Cython this means that the
specialization is figured out at compile time. For ``def`` functions the
arguments are typechecked at runtime, and a best-effort approach is performed to
figure out which specialization is needed. This means that this may result in a
runtime ``TypeError`` if no specialization was found. A ``cpdef`` function is
treated the same way as a ``def`` function if the type of the function is
unknown (e.g. if it is external and there is no cimport for it).

The automatic dispatching rules are typically as follows, in order of
preference:

* try to find an exact match
* choose the biggest corresponding numerical type (biggest float, biggest
  complex, biggest int)

Built-in Fused Types
====================

There are some built-in fused types available for convenience, these are::

    cython.integral # short, int, long
    cython.floating # float, double
    cython.numeric  # short, int, long, float, double, float complex, double complex

Casting Fused Functions
=======================

.. note:: Pointers to functions are currently not supported by pure Python mode. (GitHub issue :issue:`4279`)

Fused ``cdef`` and ``cpdef`` functions may be cast or assigned to C function pointers as follows::

    cdef myfunc(cython.floating, cython.integral):
        ...

    # assign directly
    cdef object (*funcp)(float, int)
    funcp = myfunc
    funcp(f, i)

    # alternatively, cast it
    (<object (*)(float, int)> myfunc)(f, i)

    # This is also valid
    funcp = myfunc[float, int]
    funcp(f, i)

Type Checking Specializations
=============================

Decisions can be made based on the specializations of the fused parameters.
False conditions are pruned to avoid invalid code. One may check with ``is``,
``is not`` and ``==`` and ``!=`` to see if a fused type is equal to a certain
other non-fused type (to check the specialization), or use ``in`` and ``not in``
to figure out whether a specialization is part of another set of types
(specified as a fused type). In example:

.. tabs::

    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/userguide/fusedtypes/type_checking.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/userguide/fusedtypes/type_checking.pyx

.. _fused_gil_conditional:

Conditional GIL Acquiring / Releasing
=====================================

Acquiring and releasing the GIL can be controlled by a condition
which is known at compile time (see :ref:`gil_conditional`).

.. Note:: Pure python mode currently does not support Conditional GIL Acquiring / Releasing. See Github issue :issue:`5113`.

This is most useful when combined with fused types.
A fused type function may have to handle both cython native types
(e.g. cython.int or cython.double) and python types (e.g. object or bytes).
Conditional Acquiring / Releasing the GIL provides a method for running
the same piece of code either with the GIL released (for cython native types)
and with the GIL held (for python types):

.. literalinclude:: ../../examples/userguide/fusedtypes/conditional_gil.pyx

__signatures__
==============

Finally, function objects from ``def`` or ``cpdef`` functions have an attribute
``__signatures__``, which maps the signature strings to the actual specialized
functions. This may be useful for inspection:

.. tabs::

    .. group-tab:: Pure Python

        .. literalinclude:: ../../examples/userguide/fusedtypes/indexing.py
            :lines: 1-9,14-16
            :caption: indexing.py

    .. group-tab:: Cython

        .. literalinclude:: ../../examples/userguide/fusedtypes/indexing.pyx
            :lines: 1-9,14-16
            :caption: indexing.pyx

.. code-block:: pycon

   >>> from indexing import cpfunc
   >>> cpfunc.__signatures__,
   ({'double|double': <cyfunction __pyx_fuse_0_0cpfunc at 0x107292f20>, 'double|float': <cyfunction __pyx_fuse_0_1cpfunc at 0x1072a6040>, 'float|double': <cyfunction __pyx_fuse_1_0cpfunc at 0x1072a6120>, 'float|float': <cyfunction __pyx_fuse_1_1cpfunc at 0x1072a6200>},)

Listed signature strings may also
be used as indices to the fused function, but the index format may change between
Cython versions

.. code-block:: pycon

    >>> specialized_function = cpfunc["double|float"]
    >>> specialized_function(5.0, 1.0)
    cpfunc called: double 5.0 float 1.0

However, the better way how to index is by providing list of types as mentioned in :ref:`fusedtypes_indexing` section.