Arcane  v4.1.2.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ReduceMemoryImpl.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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-2025 */
9/* */
10/* Gestion de la mémoire pour les réductions. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/accelerator/internal/ReduceMemoryImpl.h"
15
16#include "arccore/base/CheckedConvert.h"
17#include "arccore/base/PlatformUtils.h"
18
20#include "arccore/common/accelerator/Runner.h"
21#include "arccore/common/accelerator/Memory.h"
22#include "arccore/common/accelerator/internal/IRunQueueStream.h"
23#include "arccore/common/accelerator/internal/RunCommandImpl.h"
24#include "arccore/common/accelerator/internal/RunnerImpl.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane::Accelerator::Impl
30{
31namespace
32{
33 IMemoryAllocator* _getAllocator(eMemoryResource r)
34 {
36 }
37} // namespace
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42ReduceMemoryImpl::
43ReduceMemoryImpl(RunCommandImpl* p)
44: m_command(p)
45, m_host_memory_bytes(_getAllocator(eMemoryResource::HostPinned))
46, m_grid_buffer(_getAllocator(eMemoryResource::Device))
47, m_grid_device_count(_getAllocator(eMemoryResource::Device))
48{
49 _allocateMemoryForReduceData(128);
50 _allocateMemoryForGridDeviceCount();
51 m_grid_memory_info.m_warp_size = p->runner()->deviceInfo().warpSize();
52}
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57void ReduceMemoryImpl::
58release()
59{
60 m_command->releaseReduceMemoryImpl(this);
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66void ReduceMemoryImpl::
67allocateReduceDataMemory(Int32 data_type_size)
68{
69 m_data_type_size = data_type_size;
70 if (data_type_size > m_size)
71 _allocateMemoryForReduceData(data_type_size);
72}
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77void ReduceMemoryImpl::
78_allocateMemoryForReduceData(Int32 new_size)
79{
80 m_host_memory_bytes.resize(new_size);
81 m_grid_memory_info.m_host_memory_for_reduced_value = m_host_memory_bytes.data();
82
83 m_size = new_size;
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
89void ReduceMemoryImpl::
90_allocateGridDataMemory()
91{
92 // TODO: pouvoir utiliser un padding pour éviter que les lignes de cache
93 // entre les blocs se chevauchent
94 Int32 total_size = CheckedConvert::toInt32(m_data_type_size * m_grid_size);
95 if (total_size <= m_grid_memory_info.m_grid_memory_values.bytes().size())
96 return;
97
98 m_grid_buffer.resize(total_size);
99
100 auto mem_view = makeMutableMemoryView(m_grid_buffer.span());
101 m_grid_memory_info.m_grid_memory_values = mem_view;
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107void ReduceMemoryImpl::
108_allocateMemoryForGridDeviceCount()
109{
110 // Alloue sur le device la mémoire contenant le nombre de blocs restant à traiter
111 // Il s'agit d'un seul entier non signé.
112 Int64 size = sizeof(unsigned int);
113 const unsigned int zero = 0;
114 m_grid_device_count.resize(1);
115 auto* ptr = m_grid_device_count.data();
116
117 m_grid_memory_info.m_grid_device_count = ptr;
118
119 // Initialise cette zone mémoire avec 0.
120 MemoryCopyArgs copy_args(ptr, &zero, size);
121 m_command->internalStream()->copyMemory(copy_args);
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
126
127extern "C++" IReduceMemoryImpl*
128internalGetOrCreateReduceMemoryImpl(RunCommand* command)
129{
130 return command->m_p->getOrCreateReduceMemoryImpl();
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
135
136} // namespace Arcane::Accelerator::Impl
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
Fonctions utilitaires de gestion mémoire.
Int64 m_size
Taille allouée pour m_device_memory.
Int64 m_data_type_size
Taille de la donnée actuelle.
ARCCORE_COMMON_EXPORT IMemoryAllocator * getAllocator(eMemoryResource mem_resource)
Allocateur par défaut pour la ressource mem_resource.
std::int64_t Int64
Type entier signé sur 64 bits.
MutableMemoryView makeMutableMemoryView(void *ptr, Int32 datatype_size, Int64 nb_element)
Créé une vue mémoire modifiable.
Definition MemoryView.cc:26
eMemoryResource
Liste des ressources mémoire disponibles.
@ HostPinned
Alloue sur l'hôte.
std::int32_t Int32
Type entier signé sur 32 bits.