Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
WorkGroupLoopRange.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* WorkGroupLoopRange.cc (C) 2000-2026 */
9/* */
10/* Loop for hierarchical parallelism. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/accelerator/WorkGroupLoopRange.h"
15
16#include "arccore/base/FatalErrorException.h"
17#include "arccore/base/ParallelLoopOptions.h"
18
19#include "arccore/common/accelerator/RunCommand.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane::Accelerator
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30template <bool IsCooperativeLaunch, typename IndexType_> ARCCORE_ACCELERATOR_EXPORT void
32setBlockSize(IndexType block_size)
33{
34 if ((block_size <= 0) || ((block_size % 32) != 0))
35 ARCCORE_FATAL("Invalid value '{0}' for block size: should be a multiple of 32", block_size);
36 m_block_size = block_size;
37 _setNbBlock();
38}
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43template <bool IsCooperativeLaunch, typename IndexType_> ARCCORE_ACCELERATOR_EXPORT void
46{
47 // TODO: in multi-threading, calculate based on the number of threads
48 // available and the total number of elements
49 IndexType block_size = 1024;
50 eExecutionPolicy policy = command.executionPolicy();
51 if (isAcceleratorPolicy(policy))
52 block_size = 256;
53 else if (policy == eExecutionPolicy::Thread) {
54 ParallelLoopOptions loop_options = command.parallelLoopOptions();
55 Int32 nb_thread = loop_options.maxThread();
56 if (nb_thread == 0)
57 nb_thread = 1;
58 m_nb_block = nb_thread;
59 m_block_size = (m_nb_element + (nb_thread - 1)) / nb_thread;
60 loop_options.setGrainSize(1);
61 command.setParallelLoopOptions(loop_options);
62 return;
63 }
64 else if (IsCooperativeLaunch) {
65 // TODO: handle multi-threading.
66 // In sequential mode, there is only one block whose size is the number
67 // of elements.
68 m_block_size = m_nb_element;
69 m_nb_block = 1;
70 return;
71 }
72 setBlockSize(block_size);
73}
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78template <bool IsCooperativeLaunch, typename IndexType_> void
81{
82 m_nb_block = static_cast<Int32>((m_nb_element + (m_block_size - 1)) / m_block_size);
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96} // namespace Arcane::Accelerator
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
const ParallelLoopOptions & parallelLoopOptions() const
Multi-thread loop configuration.
void setParallelLoopOptions(const ParallelLoopOptions &opt)
Sets the multi-thread loop configuration.
eExecutionPolicy executionPolicy() const
Command execution policy.
Definition RunCommand.cc:59
void setBlockSize(IndexType nb_block)
Sets the block size.
Execution options for a parallel loop in multi-threading.
Int32 maxThread() const
Maximum number of allowed threads.
void setGrainSize(Integer v)
Sets the size (approximate) of an iteration interval.
Namespace for accelerator usage.
eExecutionPolicy
Execution policy for a Runner.
@ Thread
Multi-threaded execution policy.
bool isAcceleratorPolicy(eExecutionPolicy exec_policy)
Indicates if exec_policy corresponds to an accelerator.
std::int32_t Int32
Signed integer type of 32 bits.