|
| 1 | +.. _Floating_Point_Settings: |
| 2 | + |
| 3 | +Floating-point Settings |
| 4 | +======================= |
| 5 | + |
| 6 | +To propagate CPU-specific settings for floating-point computations to tasks executed by the task scheduler, you can use one of the following two methods: |
| 7 | + |
| 8 | +* When a ``task_arena`` or a task scheduler for a given application thread is initialized, they capture the current floating-point settings of the thread. |
| 9 | +* The ``task_group_context`` class has a method to capture the current floating-point settings. |
| 10 | + |
| 11 | +By default, worker threads use floating-point settings obtained during the initialization of a ``task_arena`` or the implicit arena of the application thread. The settings are applied to all computations within that ``task_arena`` or started by that application thread. |
| 12 | + |
| 13 | + |
| 14 | +For better control over floating point behavior, a thread may capture the current settings in a task group context. Do it at context creation with a special flag passed to the constructor: |
| 15 | + |
| 16 | +:: |
| 17 | + |
| 18 | + task_group_context ctx( task_group_context::isolated, |
| 19 | + task_group_context::default_traits | task_group_context::fp_settings ); |
| 20 | + |
| 21 | + |
| 22 | +Or call the ``capture_fp_settings`` method: |
| 23 | + |
| 24 | +:: |
| 25 | + |
| 26 | + task_group_context ctx; |
| 27 | + ctx.capture_fp_settings(); |
| 28 | + |
| 29 | + |
| 30 | +You can then pass the task group context to most parallel algorithms, including ``flow::graph``, to ensure that all tasks related to this algorithm use the specified floating-point settings. |
| 31 | +It is possible to execute the parallel algorithms with different floating-point settings captured to separate contexts, even at the same time. |
| 32 | + |
| 33 | +Floating-point settings captured to a task group context prevail over the settings captured during task scheduler initialization. It means, if a context is passed to a parallel algorithm, the floating-point settings captured to the context are used. |
| 34 | +Otherwise, if floating-point settings are not captured to the context, or a context is not explicitly specified, the settings captured during the task arena initialization are used. |
| 35 | + |
| 36 | +In a nested call to a parallel algorithm that does not use the context of a task group with explicitly captured floating-point settings, the outer-level settings are used. |
| 37 | +If none of the outer-level contexts capture floating-point settings, the settings captured during task arena initialization are used. |
| 38 | + |
| 39 | +It guarantees that: |
| 40 | + |
| 41 | +* Floating-point settings are applied to all tasks executed within a task arena, if they are captured: |
| 42 | + |
| 43 | + * To a task group context. |
| 44 | + * During the arena initialization. |
| 45 | + |
| 46 | +* A call to a oneTBB parallel algorithm does not change the floating-point settings of the calling thread, even if the algorithm uses different settings. |
| 47 | + |
| 48 | +.. note:: |
| 49 | + The guarantees above apply only to the following conditions: |
| 50 | + |
| 51 | + * A user code inside a task should: |
| 52 | + |
| 53 | + * Not change the floating-point settings. |
| 54 | + * Revert any modifications. |
| 55 | + * Restore previous settings before the end of the task. |
| 56 | + |
| 57 | + * oneTBB task scheduler observers are not used to set or modify floating point settings. |
| 58 | + |
| 59 | + Otherwise, the stated guarantees are not valid and the behavior related to floating-point settings is undefined. |
| 60 | + |
0 commit comments