Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
IndexSelecter.h
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/* IndexSelecter.h (C) 2000-2024 */
9/* */
10/* Index selection with accelerator API */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/accelerator/core/Memory.h"
17#include "arcane/accelerator/GenericFilterer.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane::Accelerator
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
31class IndexSelecter
32{
33 public:
34
35 IndexSelecter() {}
36 IndexSelecter(const RunQueue& runqueue)
37 // -------------------------------------------------------
38 {
39 m_is_accelerator_policy = isAcceleratorPolicy(runqueue.executionPolicy());
40 m_memory_host = eMemoryRessource(m_is_accelerator_policy ? eMemoryRessource::HostPinned : eMemoryRessource::Host);
41 m_memory_device = eMemoryRessource(m_is_accelerator_policy ? eMemoryRessource::Device : eMemoryRessource::Host);
42 m_localid_select_device = UniqueArray<Int32>(MemoryUtils::getAllocator(m_memory_device));
43 m_localid_select_host = UniqueArray<Int32>(MemoryUtils::getAllocator(m_memory_host));
44 }
45
46 ~IndexSelecter()
47 {
49 }
50
54 void resize(Int32 nb_idx)
55 {
56 m_index_number = nb_idx;
57 m_localid_select_device.resize(m_index_number);
58 m_localid_select_host.resize(m_index_number);
59 }
60
65 template <typename PredicateType>
66 ConstArrayView<Int32> syncSelectIf(const RunQueue& rqueue_async, PredicateType pred, bool host_view = false)
67 {
68 // We try to reuse the same GenericFilterer instance as much as possible
69 // in order to minimize dynamic allocations in this class.
70 // The GenericFilterer instance depends on the RunQueue pointer, so
71 // if this pointer changes, a new instance must be destroyed and reallocated.
72 bool to_instantiate = (m_generic_filterer_instance == nullptr);
73 if (m_asynchronous_queue_pointer != rqueue_async) {
74 m_asynchronous_queue_pointer = rqueue_async;
76 to_instantiate = true;
77 }
78 if (to_instantiate) {
80 }
81
82 // We select the indices i in [0,m_index_number[ for which pred(i) is true
83 // and copy them into out_lid_select.
84 // The number of selected indices is given by nbOutputElement()
85 SmallSpan<Int32> out_lid_select(m_localid_select_device.data(), m_index_number);
86
87 m_generic_filterer_instance->applyWithIndex(m_index_number, pred,
88 [=] ARCCORE_HOST_DEVICE(Int32 input_index, Int32 output_index) -> void {
89 out_lid_select[output_index] = input_index;
90 });
91 Int32 nb_idx_selected = m_generic_filterer_instance->nbOutputElement();
92
93 if (nb_idx_selected && host_view) {
94 // Asynchronous copy Device to Host (m_localid_select_device ==> m_localid_select_host)
95 rqueue_async.copyMemory(MemoryCopyArgs(m_localid_select_host.subView(0, nb_idx_selected).data(),
96 m_localid_select_device.subView(0, nb_idx_selected).data(),
97 nb_idx_selected * sizeof(Int32))
98 .addAsync());
99 }
100 rqueue_async.barrier();
101
102 ConstArrayView<Int32> lid_select_view = (host_view ? m_localid_select_host.subConstView(0, nb_idx_selected) : m_localid_select_device.subConstView(0, nb_idx_selected));
103
104 return lid_select_view;
105 }
106
107 private:
108
109 bool m_is_accelerator_policy = false; // indicates whether the accelerator is available or not
110 eMemoryRessource m_memory_host; // identification of the HOST allocator
111 eMemoryRessource m_memory_device; // identification of the DEVICE allocator
112 UniqueArray<Int32> m_localid_select_device; // list of selected IDs using a Filterer (allocated on DEVICE)
113 UniqueArray<Int32> m_localid_select_host; // list of selected IDs using a Filterer (allocated on HOST)
114
116
119};
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124} // namespace Arcane::Accelerator
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
Memory and allocator management functions.
void resize(Int32 nb_idx)
Defines the interval [0,nb_idx[ on which the selection will be performed.
GenericFilterer * m_generic_filterer_instance
GenericFilterer instance.
Int32 m_index_number
Interval [0, m_index_number[ on which the selection will be performed.
RunQueue m_asynchronous_queue_pointer
Pointer to the GenericFilterer queue.
ConstArrayView< Int32 > syncSelectIf(const RunQueue &rqueue_async, PredicateType pred, bool host_view=false)
Selects the indices according to the predicate pred and synchronizes rqueue_async.
void copyMemory(const MemoryCopyArgs &args) const
Copies information between two memory regions.
Definition RunQueue.cc:237
void barrier() const
Blocks until all commands associated with the queue are finished.
Definition RunQueue.cc:159
eExecutionPolicy executionPolicy() const
Execution policy of the queue.
Definition RunQueue.cc:169
Constant view of an array of type T.
View of an array of elements of type T.
Definition Span.h:805
1D data vector with value semantics (STL style).
bool isAcceleratorPolicy(eExecutionPolicy exec_policy)
Indicates if exec_policy corresponds to an accelerator.
IMemoryAllocator * getAllocator(eMemoryResource mem_resource)
Default allocator for the resource mem_resource.
Arcane::eMemoryResource eMemoryRessource
Typedef for the historical Arcane version (with 2's').
std::int32_t Int32
Signed integer type of 32 bits.