Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
ReduceMemoryImpl.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* ReduceMemoryImpl.cc (C) 2000-2024 */
9/* */
10/* Gestion de la mémoire pour les réductions. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/accelerator/core/internal/ReduceMemoryImpl.h"
15
16#include "arcane/utils/CheckedConvert.h"
17#include "arcane/utils/PlatformUtils.h"
18#include "arcane/utils/IMemoryRessourceMng.h"
19
20#include "arcane/accelerator/core/Runner.h"
21#include "arcane/accelerator/core/Memory.h"
22#include "arcane/accelerator/core/internal/IRunQueueStream.h"
23#include "arcane/accelerator/core/internal/RunCommandImpl.h"
24#include "arcane/accelerator/core/internal/RunnerImpl.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane::Accelerator::impl
30{
31namespace
32{
33 IMemoryAllocator* _getAllocator(eMemoryRessource r)
34 {
36 }
37} // namespace
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42ReduceMemoryImpl::
43ReduceMemoryImpl(RunCommandImpl* p)
44: m_command(p)
45, m_device_memory_bytes(_getAllocator(eMemoryRessource::Device))
46, m_host_memory_bytes(_getAllocator(eMemoryRessource::HostPinned))
47, m_grid_buffer(_getAllocator(eMemoryRessource::Device))
48, m_grid_device_count(_getAllocator(eMemoryRessource::Device))
49{
50 _allocateMemoryForReduceData(128);
51 _allocateMemoryForGridDeviceCount();
52}
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57void ReduceMemoryImpl::
58release()
59{
60 m_command->releaseReduceMemoryImpl(this);
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66void ReduceMemoryImpl::
67_setReducePolicy()
68{
69 m_grid_memory_info.m_reduce_policy = m_command->runner()->reducePolicy();
70}
71
72/*---------------------------------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74
75void* ReduceMemoryImpl::
76allocateReduceDataMemory(ConstMemoryView identity_view)
77{
78 auto identity_span = identity_view.bytes();
79 Int32 data_type_size = static_cast<Int32>(identity_span.size());
80 m_data_type_size = data_type_size;
81 if (data_type_size > m_size)
82 _allocateMemoryForReduceData(data_type_size);
83
84 // Recopie \a identity_view dans un buffer car on utilise l'asynchronisme
85 // et la zone pointée par \a identity_view n'est pas forcément conservée
86 m_identity_buffer.copy(identity_view.bytes());
87 MemoryCopyArgs copy_args(m_device_memory, m_identity_buffer.span().data(), data_type_size);
88 m_command->internalStream()->copyMemory(copy_args.addAsync());
89
90 return m_device_memory;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96void ReduceMemoryImpl::
97_allocateGridDataMemory()
98{
99 // TODO: pouvoir utiliser un padding pour éviter que les lignes de cache
100 // entre les blocs se chevauchent
101 Int32 total_size = CheckedConvert::toInt32(m_data_type_size * m_grid_size);
102 if (total_size <= m_grid_memory_info.m_grid_memory_values.bytes().size())
103 return;
104
105 m_grid_buffer.resize(total_size);
106
107 auto mem_view = makeMutableMemoryView(m_grid_buffer.span());
108 m_grid_memory_info.m_grid_memory_values = mem_view;
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114void ReduceMemoryImpl::
115_allocateMemoryForGridDeviceCount()
116{
117 // Alloue sur le device la mémoire contenant le nombre de blocs restant à traiter
118 // Il s'agit d'un seul entier non signé.
119 Int64 size = sizeof(unsigned int);
120 const unsigned int zero = 0;
121 m_grid_device_count.resize(1);
122 auto* ptr = m_grid_device_count.data();
123
124 m_grid_memory_info.m_grid_device_count = ptr;
125
126 // Initialise cette zone mémoire avec 0.
127 MemoryCopyArgs copy_args(ptr, &zero, size);
128 m_command->internalStream()->copyMemory(copy_args);
129}
130
131/*---------------------------------------------------------------------------*/
132/*---------------------------------------------------------------------------*/
133
134void ReduceMemoryImpl::
135copyReduceValueFromDevice()
136{
137 void* destination = m_grid_memory_info.m_host_memory_for_reduced_value;
138 void* source = m_device_memory;
139 MemoryCopyArgs copy_args(destination, source, m_data_type_size);
140 m_command->internalStream()->copyMemory(copy_args);
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
146extern "C++" IReduceMemoryImpl*
147internalGetOrCreateReduceMemoryImpl(RunCommand* command)
148{
149 return command->m_p->getOrCreateReduceMemoryImpl();
150}
151
152/*---------------------------------------------------------------------------*/
153/*---------------------------------------------------------------------------*/
154
155} // namespace Arcane::Accelerator::impl
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
virtual IMemoryAllocator * getAllocator(eMemoryRessource r)=0
Allocateur mémoire pour la ressource r.
IMemoryRessourceMng * getDataMemoryRessourceMng()
Gestionnaire de ressource mémoire pour les données.
eMemoryRessource
Liste des ressources mémoire disponibles.
@ HostPinned
Alloue sur l'hôte.
@ Device
Alloue sur le device.