File: threads.rst

package info (click to toggle)
contourpy 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,688 kB
  • sloc: python: 7,998; cpp: 6,241; makefile: 13
file content (52 lines) | stat: -rw-r--r-- 1,859 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
.. _threads:

Threads
-------

The :ref:`threaded` algorithm supports the use of multiple threads.

.. name_supports::
   :filter: threads

:ref:`threaded` shares most of its code with :ref:`serial` except for the high-level processing of
chunks which it performs in parallel using a thread pool, and the creation of `NumPy`_ arrays is
limited to a single thread at a time.

.. note::

   The domain must be divided into chunks for multithreaded contouring.

Create a :class:`~.ThreadedContourGenerator` by calling
:func:`~.contour_generator` in the usual way, ensuring that the domain is chunked:

   >>> from contourpy import contour_generator
   >>> import numpy as np
   >>> z = np.ones((100, 50))  # Sample z data.
   >>> cont_gen = contour_generator(z=z, name="threaded", chunk_count=5, thread_count=4)
   >>> cont_gen.thread_count
   4
   >>> cont_gen.chunk_count
   25

Here the 25 chunks will be divided up between the 4 threads.

The ``thread_count`` argument is optional, if not specified the default is ``thread_count=0`` which
means it will use the maximum number of threads available. This number can be checked using:

   >>> import contourpy
   >>> contourpy.max_threads()

.. note::

   :func:`.max_threads()` is implemented using the C++ function
   `std::thread::hardware_concurrency
   <https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency>`_.

If you request more threads than the number of chunks, the thread count will be reduced accordingly.

.. warning::

   The order of processing chunks is not deterministic. If you use a :class:`~contourpy.LineType` or
   :class:`~contourpy.FillType` that do not arrange the results by chunk, the order of
   returned lines/polygons is also not deterministic. This includes ``LineType.Separate``,
   ``LineType.SeparateCode``, ``FillType.OuterCode`` and ``FillType.OuterOffset``.