Skip to content

Commit 60c401f

Browse files
Merge pull request #1803 from arcaneframework/dev/gg-add-memory-location-info-in-array
Add memory location information in AbstractArray
2 parents e4783dd + 960ee6e commit 60c401f

10 files changed

+226
-4
lines changed

arccore/src/collections/arccore/collections/Array.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ _setMemoryLocationHint(eMemoryLocationHint new_hint,void* ptr,Int64 sizeof_true_
107107
/*---------------------------------------------------------------------------*/
108108
/*---------------------------------------------------------------------------*/
109109

110+
void ArrayMetaData::
111+
_setHostDeviceMemoryLocation(eHostDeviceMemoryLocation location)
112+
{
113+
allocation_options.setHostDeviceMemoryLocation(location);
114+
}
115+
116+
/*---------------------------------------------------------------------------*/
117+
/*---------------------------------------------------------------------------*/
118+
110119
void ArrayMetaData::
111120
_copyFromMemory(MemoryPointer destination, ConstMemoryPointer source, Int64 sizeof_true_type, RunQueue* queue)
112121
{

arccore/src/collections/arccore/collections/Array.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class ARCCORE_COLLECTIONS_EXPORT ArrayMetaData
7777
Int32 nb_ref = 0;
7878
//! Indique is cette instance a été allouée par l'opérateur new.
7979
bool is_allocated_by_new = false;
80+
//! Indique si cette instance n'est pas l'instance nulle (partagée par tous les SharedArray)
8081
bool is_not_null = false;
8182

8283
protected:
@@ -102,6 +103,7 @@ class ARCCORE_COLLECTIONS_EXPORT ArrayMetaData
102103
MemoryPointer _reallocate(const AllocatedMemoryInfo& mem_info, Int64 new_capacity, Int64 sizeof_true_type, RunQueue* queue);
103104
void _deallocate(const AllocatedMemoryInfo& mem_info, RunQueue* queue) ARCCORE_NOEXCEPT;
104105
void _setMemoryLocationHint(eMemoryLocationHint new_hint, void* ptr, Int64 sizeof_true_type);
106+
void _setHostDeviceMemoryLocation(eHostDeviceMemoryLocation location);
105107
void _copyFromMemory(MemoryPointer destination, ConstMemoryPointer source, Int64 sizeof_true_type, RunQueue* queue);
106108

107109
private:
@@ -465,6 +467,23 @@ class AbstractArray
465467
m_md->_setMemoryLocationHint(new_hint,m_ptr,sizeof(T));
466468
}
467469

470+
/*!
471+
* \brief Positionne l'emplacement physique de la zone mémoire.
472+
*
473+
* \warning L'appelant doit garantir la cohérence entre l'allocateur
474+
* et la zone mémoire spécifiée.
475+
*/
476+
void _internalSetHostDeviceMemoryLocation(eHostDeviceMemoryLocation location)
477+
{
478+
m_md->_setHostDeviceMemoryLocation(location);
479+
}
480+
481+
//! Positionne l'emplacement physique de la zone mémoire.
482+
eHostDeviceMemoryLocation hostDeviceMemoryLocation() const
483+
{
484+
return m_md->allocation_options.hostDeviceMemoryLocation();
485+
}
486+
468487
public:
469488

470489
friend bool operator==(const AbstractArray<T>& rhs, const AbstractArray<T>& lhs)

arccore/src/collections/arccore/collections/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(SOURCES
66
ArrayTraits.h
77
ArrayDebugInfo.h
88
CollectionsGlobal.h
9+
CollectionsGlobal.cc
910
IMemoryAllocator.h
1011
MemoryAllocationArgs.h
1112
MemoryAllocationOptions.h
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
/* CollectionsGlobal.cc (C) 2000-2024 */
9+
/* */
10+
/* Définitions globales de la composante 'Collections' de 'Arccore'. */
11+
/*---------------------------------------------------------------------------*/
12+
/*---------------------------------------------------------------------------*/
13+
14+
#include "arccore/collections/CollectionsGlobal.h"
15+
16+
#include <iostream>
17+
18+
/*---------------------------------------------------------------------------*/
19+
/*---------------------------------------------------------------------------*/
20+
21+
namespace Arccore
22+
{
23+
24+
/*---------------------------------------------------------------------------*/
25+
/*---------------------------------------------------------------------------*/
26+
27+
namespace
28+
{
29+
const char* _toName(eHostDeviceMemoryLocation v)
30+
{
31+
switch (v) {
32+
case eHostDeviceMemoryLocation::Unknown:
33+
return "Unknown";
34+
case eHostDeviceMemoryLocation::Device:
35+
return "Device";
36+
case eHostDeviceMemoryLocation::Host:
37+
return "Host";
38+
case eHostDeviceMemoryLocation::ManagedMemoryDevice:
39+
return "ManagedMemoryDevice";
40+
case eHostDeviceMemoryLocation::ManagedMemoryHost:
41+
return "ManagedMemoryHost";
42+
}
43+
return "Invalid";
44+
}
45+
46+
const char* _toName(eMemoryResource r)
47+
{
48+
switch (r) {
49+
case eMemoryResource::Unknown:
50+
return "Unknown";
51+
case eMemoryResource::Host:
52+
return "Host";
53+
case eMemoryResource::HostPinned:
54+
return "HostPinned";
55+
case eMemoryResource::Device:
56+
return "Device";
57+
case eMemoryResource::UnifiedMemory:
58+
return "UnifiedMemory";
59+
}
60+
return "Invalid";
61+
}
62+
63+
} // namespace
64+
65+
extern "C++" ARCCORE_COLLECTIONS_EXPORT std::ostream&
66+
operator<<(std::ostream& o, eHostDeviceMemoryLocation v)
67+
{
68+
o << _toName(v);
69+
return o;
70+
}
71+
72+
extern "C++" ARCCORE_COLLECTIONS_EXPORT std::ostream&
73+
operator<<(std::ostream& o, eMemoryResource v)
74+
{
75+
o << _toName(v);
76+
return o;
77+
}
78+
79+
/*---------------------------------------------------------------------------*/
80+
/*---------------------------------------------------------------------------*/
81+
82+
} // End namespace Arccore
83+
84+
/*---------------------------------------------------------------------------*/
85+
/*---------------------------------------------------------------------------*/

arccore/src/collections/arccore/collections/CollectionsGlobal.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "arccore/base/ArccoreGlobal.h"
1818

19+
#include <iosfwd>
20+
1921
/*---------------------------------------------------------------------------*/
2022
/*---------------------------------------------------------------------------*/
2123

@@ -85,6 +87,60 @@ enum class eMemoryLocationHint : int8_t
8587
HostAndDeviceMostlyRead = 3
8688
};
8789

90+
/*---------------------------------------------------------------------------*/
91+
/*---------------------------------------------------------------------------*/
92+
/*!
93+
* \brief Localisation physique d'une adresse mémoire.
94+
*
95+
* Pour les valeurs ManagedMemoryDevice et ManagedMemoryHost il s'agit d'une
96+
* indication car il n'y a pas de moyen simple de savoir où se trouve
97+
* réellement la mémoire.
98+
*/
99+
enum class eHostDeviceMemoryLocation : int8_t
100+
{
101+
//! Localisation inconnue
102+
Unknown = 0,
103+
//! La mémoire est sur accélérateur
104+
Device = 1,
105+
//! La mémoire est sur l'hôte.
106+
Host = 2,
107+
//! La mémoire est de la mémoire managée sur accélérateur
108+
ManagedMemoryDevice = 3,
109+
//! La mémoire est de la mémoire managée sur l'hôte.
110+
ManagedMemoryHost = 4,
111+
};
112+
extern "C++" ARCCORE_COLLECTIONS_EXPORT std::ostream&
113+
operator<<(std::ostream& o, eHostDeviceMemoryLocation r);
114+
115+
/*---------------------------------------------------------------------------*/
116+
/*---------------------------------------------------------------------------*/
117+
118+
/*!
119+
* \brief Liste des ressources mémoire disponibles.
120+
*/
121+
enum class eMemoryResource
122+
{
123+
//! Valeur inconnue ou non initialisée
124+
Unknown = 0,
125+
//! Alloue sur l'hôte.
126+
Host,
127+
//! Alloue sur l'hôte.
128+
HostPinned,
129+
//! Alloue sur le device
130+
Device,
131+
//! Alloue en utilisant la mémoire unifiée.
132+
UnifiedMemory
133+
};
134+
135+
//! Nombre de valeurs valides pour eMemoryRessource
136+
static constexpr int ARCCORE_NB_MEMORY_RESOURCE = 5;
137+
138+
/*---------------------------------------------------------------------------*/
139+
/*---------------------------------------------------------------------------*/
140+
141+
extern "C++" ARCCORE_COLLECTIONS_EXPORT std::ostream&
142+
operator<<(std::ostream& o, eMemoryResource r);
143+
88144
/*---------------------------------------------------------------------------*/
89145
/*---------------------------------------------------------------------------*/
90146
/*!

arccore/src/collections/arccore/collections/IMemoryAllocator.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
22
//-----------------------------------------------------------------------------
3-
// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
3+
// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
44
// See the top-level COPYRIGHT file for details.
55
// SPDX-License-Identifier: Apache-2.0
66
//-----------------------------------------------------------------------------
77
/*---------------------------------------------------------------------------*/
8-
/* IMemoryAllocator.h (C) 2000-2023 */
8+
/* IMemoryAllocator.h (C) 2000-2024 */
99
/* */
1010
/* Interface d'un allocateur mémoire. */
1111
/*---------------------------------------------------------------------------*/
@@ -139,6 +139,9 @@ class ARCCORE_COLLECTIONS_EXPORT IMemoryAllocator
139139
*/
140140
virtual void copyMemory(MemoryAllocationArgs args, AllocatedMemoryInfo destination, AllocatedMemoryInfo source);
141141

142+
//! Ressource mémoire fournie par l'allocateur
143+
virtual eMemoryResource memoryRessource() const { return eMemoryResource::Unknown; }
144+
142145
public:
143146

144147
// Méthodes historiques (avant 2023) sans arguments supplémentaires.

arccore/src/collections/arccore/collections/MemoryAllocationArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationArgs
3434
void setMemoryLocationHint(eMemoryLocationHint mem_advice) { m_memory_location_hint = mem_advice; }
3535
eMemoryLocationHint memoryLocationHint() const { return m_memory_location_hint; }
3636

37+
void setHostDeviceMemoryLocation(eHostDeviceMemoryLocation v) { m_host_device_memory_location = v; }
38+
eHostDeviceMemoryLocation hostDeviceMemoryLocation() const { return m_host_device_memory_location; }
39+
3740
Int16 device() const { return m_device; }
3841
void setDevice(Int16 device) { m_device = device; }
3942

@@ -49,6 +52,7 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationArgs
4952
private:
5053

5154
eMemoryLocationHint m_memory_location_hint = eMemoryLocationHint::None;
55+
eHostDeviceMemoryLocation m_host_device_memory_location = eHostDeviceMemoryLocation::Unknown;
5256
Int16 m_device = (-1);
5357
RunQueue* m_run_queue = nullptr;
5458
ArrayDebugInfo* m_debug_info = nullptr;

arccore/src/collections/arccore/collections/MemoryAllocationOptions.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ allocationArgs(RunQueue* queue) const
3838
{
3939
MemoryAllocationArgs x;
4040
x.setMemoryLocationHint(m_memory_location_hint);
41+
x.setHostDeviceMemoryLocation(m_host_device_memory_location);
4142
x.setDevice(m_device);
4243
x.setDebugInfo(m_debug_info);
4344
x.setRunQueue(queue);

arccore/src/collections/arccore/collections/MemoryAllocationOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationOptions
6060
, m_debug_info(rhs.m_debug_info)
6161
, m_device(rhs.m_device)
6262
, m_memory_location_hint(rhs.m_memory_location_hint)
63+
, m_host_device_memory_location(rhs.m_host_device_memory_location)
6364
{
6465
if (m_debug_info)
6566
_addDebugReference();
@@ -78,6 +79,7 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationOptions
7879
_removeDebugReference();
7980
m_allocator = rhs.m_allocator;
8081
m_memory_location_hint = rhs.m_memory_location_hint;
82+
m_host_device_memory_location = rhs.m_host_device_memory_location;
8183
m_device = rhs.m_device;
8284
m_debug_info = rhs.m_debug_info;
8385
if (m_debug_info)
@@ -93,6 +95,9 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationOptions
9395
eMemoryLocationHint memoryLocationHint() const { return m_memory_location_hint; }
9496
void setMemoryLocationHint(eMemoryLocationHint mem_advice) { m_memory_location_hint = mem_advice; }
9597

98+
void setHostDeviceMemoryLocation(eHostDeviceMemoryLocation v) { m_host_device_memory_location = v; }
99+
eHostDeviceMemoryLocation hostDeviceMemoryLocation() const { return m_host_device_memory_location; }
100+
96101
Int16 device() const { return m_device; }
97102
void setDevice(Int16 device) { m_device = device; }
98103

@@ -106,12 +111,15 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationOptions
106111

107112
public:
108113

114+
// TODO: A supprimer car ne sert que pour les tests
109115
friend bool operator==(const MemoryAllocationOptions& a, const MemoryAllocationOptions& b)
110116
{
111117
if (a.m_allocator != b.m_allocator)
112118
return false;
113119
if (a.m_memory_location_hint != b.m_memory_location_hint)
114120
return false;
121+
if (a.m_host_device_memory_location != b.m_host_device_memory_location)
122+
return false;
115123
if (a.m_device != b.m_device)
116124
return false;
117125
if (a.m_queue != b.m_queue)
@@ -125,6 +133,7 @@ class ARCCORE_COLLECTIONS_EXPORT MemoryAllocationOptions
125133
ArrayDebugInfo* m_debug_info = nullptr;
126134
Int16 m_device = -1;
127135
eMemoryLocationHint m_memory_location_hint = eMemoryLocationHint::None;
136+
eHostDeviceMemoryLocation m_host_device_memory_location = eHostDeviceMemoryLocation::Unknown;
128137
RunQueue* m_queue = nullptr;
129138

130139
private:

arccore/src/collections/tests/TestArray.cc

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@ void _testArraySwap(bool use_own_swap)
2323
{
2424
std::cout << "** TestArraySwap is_own=" << use_own_swap << "\n";
2525

26+
String c1_name = "TestC1";
2627
UniqueArray<IntSubClass> c1(7);
27-
IntSubClass* x1 = c1.unguardedBasePointer();
28+
c1.setDebugName(c1_name);
29+
IntSubClass* x1 = c1.data();
2830
std::cout << "** C1_this = " << &c1 << "\n";
2931
std::cout << "** C1_BASE = " << x1 << "\n";
3032
UniqueArray<IntSubClass> c2(3);
31-
IntSubClass* x2 = c2.unguardedBasePointer();
33+
IntSubClass* x2 = c2.data();
3234
std::cout << "** C2_this = " << &c2 << "\n";
3335
std::cout << "** C2_BASE = " << x2 << "\n";
3436

37+
ASSERT_EQ(c1.debugName(), c1_name);
38+
ASSERT_EQ(c2.debugName(), String{});
39+
3540
if (use_own_swap) {
3641
swap(c1, c2);
3742
}
3843
else
3944
std::swap(c1, c2);
4045

46+
ASSERT_EQ(c2.debugName(), c1_name);
47+
ASSERT_EQ(c1.debugName(), String{});
48+
4149
IntSubClass* after_x1 = c1.data();
4250
IntSubClass* after_x2 = c2.data();
4351
std::cout << "** C1_BASE_AFTER = " << after_x1 << " size=" << c1.size() << "\n";
@@ -990,6 +998,9 @@ TEST(Array, AllocatorV2)
990998
}
991999
}
9921000

1001+
/*---------------------------------------------------------------------------*/
1002+
/*---------------------------------------------------------------------------*/
1003+
9931004
TEST(Array, DebugInfo)
9941005
{
9951006
using namespace Arccore;
@@ -1034,6 +1045,30 @@ TEST(Array, DebugInfo)
10341045
/*---------------------------------------------------------------------------*/
10351046
/*---------------------------------------------------------------------------*/
10361047

1048+
TEST(Collections, Memory)
1049+
{
1050+
using namespace Arccore;
1051+
std::cout << eHostDeviceMemoryLocation::Unknown << " "
1052+
<< eHostDeviceMemoryLocation::Device << " "
1053+
<< eHostDeviceMemoryLocation::Host << " "
1054+
<< eHostDeviceMemoryLocation::ManagedMemoryDevice << " "
1055+
<< eHostDeviceMemoryLocation::ManagedMemoryHost << "\n";
1056+
1057+
std::cout << eMemoryResource::Unknown << " "
1058+
<< eMemoryResource::Host << " "
1059+
<< eMemoryResource::HostPinned << " "
1060+
<< eMemoryResource::Device << " "
1061+
<< eMemoryResource::UnifiedMemory << "\n";
1062+
1063+
UniqueArray<Int32> a1;
1064+
ASSERT_EQ(a1.hostDeviceMemoryLocation(), eHostDeviceMemoryLocation::Unknown);
1065+
a1._internalSetHostDeviceMemoryLocation(eHostDeviceMemoryLocation::Host);
1066+
ASSERT_EQ(a1.hostDeviceMemoryLocation(), eHostDeviceMemoryLocation::Host);
1067+
}
1068+
1069+
/*---------------------------------------------------------------------------*/
1070+
/*---------------------------------------------------------------------------*/
1071+
10371072
namespace Arccore
10381073
{
10391074
// Instancie explicitement les classes tableaux pour garantir

0 commit comments

Comments
 (0)