-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathMemoryInfos.cpp
More file actions
255 lines (227 loc) · 9.57 KB
/
MemoryInfos.cpp
File metadata and controls
255 lines (227 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
#include "MemoryInfos.hpp"
#include "MpiWrapper.hpp"
#if defined( GEOS_USE_CALIPER )and defined( GEOS_USE_ADIAK )
#include <adiak.hpp>
#endif
namespace geos
{
MemoryInfos::MemoryInfos( umpire::MemoryResourceTraits::resource_type resourceType ):
m_totalMemory( 0 ),
m_availableMemory( 0 ),
m_physicalMemoryHandled( 1 )
{
switch( resourceType )
{
case umpire::MemoryResourceTraits::resource_type::host:
case umpire::MemoryResourceTraits::resource_type::pinned:
#if defined( _SC_PHYS_PAGES ) && defined( _SC_PAGESIZE )
m_totalMemory = sysconf( _SC_PHYS_PAGES ) * sysconf( _SC_PAGESIZE );
#if defined(_SC_AVPHYS_PAGES)
m_availableMemory = sysconf( _SC_AVPHYS_PAGES ) * sysconf( _SC_PAGESIZE );
#else
GEOS_WARNING( "Unknown device avaialable memory size getter for this system." );
m_availableMemory = 0;
#endif
#else
GEOS_WARNING( "Unknown device physical memory size getter for this compiler." );
m_physicalMemoryHandled = 0;
#endif
break;
case umpire::MemoryResourceTraits::resource_type::device:
case umpire::MemoryResourceTraits::resource_type::device_const:
case umpire::MemoryResourceTraits::resource_type::um:
#if defined( GEOS_USE_CUDA )
cudaMemGetInfo( &m_availableMemory, &m_totalMemory );
#else
GEOS_WARNING( "Unknown device physical memory size getter for this compiler." );
m_physicalMemoryHandled = 0;
#endif
break;
default:
GEOS_WARNING( "Physical memory lookup not implemented" );
m_physicalMemoryHandled = 0;
break;
}
}
size_t MemoryInfos::getTotalMemory() const
{
return m_totalMemory;
}
size_t MemoryInfos::getAvailableMemory() const
{
return m_availableMemory;
}
bool MemoryInfos::isPhysicalMemoryHandled() const
{
return m_physicalMemoryHandled;
}
MemoryLogging::MemoryLogging():
m_umpireStatsLogReport( true ),
m_umpireStatsCsvReport( false ),
m_umpireStatsCsvReportFilename( "./umpireStats.csv" ),
m_currentCycle( 0 ),
m_currentTime( 0.0 )
{
TableLayout memoryStatLogLayout = { "Umpire Memory Pool",
TableLayout::Column( "Min reserved rank memory" )
.addSubColumns( { "bytes", "over total" } ),
TableLayout::Column( "Max reserved rank memory" )
.addSubColumns( { "bytes", "over total" } ),
TableLayout::Column( "Avg reserved rank memory" )
.addSubColumns( { "bytes", "over total" } ),
TableLayout::Column( "Sum reserved rank memory" )
.addSubColumns( { "bytes", "over total" } ),
};
memoryStatLogLayout.setMargin( TableLayout::MarginValue::small );
m_memoryStatLogFormatter = std::make_unique< TableTextFormatter >( memoryStatLogLayout );
TableLayout const memoryStatCsvLayout = { "Cycle",
"Time",
"Umpire Memory Pool",
"Min rank mem bytes",
"Min rank mem %",
"Max rank mem bytes",
"Max rank mem %",
"Avg rank mem bytes",
"Avg rank mem %",
"Sum rank mem bytes",
"Sum rank mem %" };
m_memoryStatCsvFormatter = std::make_unique< TableCSVFormatter >( memoryStatCsvLayout );
}
MemoryLogging & MemoryLogging::getInstance()
{
static MemoryLogging instance;
return instance;
}
void MemoryLogging::enableUmpireStatsCsvReport( bool enable, string_view filename )
{
m_umpireStatsCsvReport = enable;
m_umpireStatsCsvReportFilename = filename;
if( enable )
{
// start a new file
std::ofstream csvFile{ m_umpireStatsCsvReportFilename, std::ios_base::out };
m_memoryStatCsvFormatter->headerToStream( csvFile );
}
}
void MemoryLogging::memoryStatsReport() const
{
if( !m_umpireStatsLogReport && !m_umpireStatsCsvReport )
return;
umpire::ResourceManager & rm = umpire::ResourceManager::getInstance();
integer size;
MPI_Comm_size( MPI_COMM_WORLD, &size );
size_t nbRank = (std::size_t)size;
// Get a list of all the allocators and sort it so that it's in the same order on each rank.
stdVector< string > allocatorNames = rm.getAllocatorNames();
std::sort( allocatorNames.begin(), allocatorNames.end() );
// If each rank doesn't have the same number of allocators you can't aggregate them.
std::size_t const numAllocators = allocatorNames.size();
std::size_t const minNumAllocators = MpiWrapper::min( numAllocators );
if( numAllocators != minNumAllocators )
{
GEOS_WARNING( "Not all ranks have created the same number of umpire allocators, cannot compute high water marks." );
return;
}
// Loop over the allocators to collect stats.
TableData tableDataLog;
TableData tableDataCsv;
for( string const & allocatorName : allocatorNames )
{
// Skip umpire internal allocators.
if( allocatorName.rfind( "__umpire_internal", 0 ) == 0 )
continue;
static constexpr int MAX_NAME_LENGTH = 100;
GEOS_ERROR_IF_GT( allocatorName.size(), MAX_NAME_LENGTH );
string allocatorNameFixedSize = allocatorName;
allocatorNameFixedSize.resize( MAX_NAME_LENGTH, '\0' );
string allocatorNameMinChars = string( MAX_NAME_LENGTH, '\0' );
// Make sure that each rank is looking at the same allocator.
MpiWrapper::allReduce( allocatorNameFixedSize, allocatorNameMinChars, MpiWrapper::Reduction::Min, MPI_COMM_GEOS );
if( allocatorNameFixedSize != allocatorNameMinChars )
{
GEOS_WARNING( "Not all ranks have an allocator named " << allocatorNameFixedSize << ", cannot compute high water mark." );
continue;
}
umpire::Allocator allocator = rm.getAllocator( allocatorName );
umpire::strategy::AllocationStrategy const * allocationStrategy = allocator.getAllocationStrategy();
umpire::MemoryResourceTraits const traits = allocationStrategy->getTraits();
umpire::MemoryResourceTraits::resource_type resourceType = traits.resource;
MemoryInfos const memInfos( resourceType );
if( !memInfos.isPhysicalMemoryHandled() )
{
continue;
}
// Get the total number of bytes allocated with this allocator across ranks.
// This is a little redundant since
std::size_t const mark = allocator.getHighWatermark();
std::size_t const minMark = MpiWrapper::min( mark );
std::size_t const maxMark = MpiWrapper::max( mark );
std::size_t const sumMark = MpiWrapper::sum( mark );
std::size_t const avgMark = sumMark / nbRank;
real32 minPercentage;
real32 maxPercentage;
real32 avgPercentage;
real32 sumPercentage;
if( memInfos.getTotalMemory() == 0 )
{
// TODO: after PR 3614, this warning should rather be in table error list.
GEOS_WARNING( "umpire memory percentage could not be resolved" );
}
else
{
real32 const memDivider = 1.0 / real32( memInfos.getTotalMemory() );
minPercentage = 100.0 * ( real32( minMark ) * memDivider );
maxPercentage = 100.0 * ( real32( maxMark ) * memDivider );
avgPercentage = 100.0 * ( real32( avgMark ) * memDivider );
sumPercentage = 100.0 * ( real32( sumMark ) * memDivider );
}
if( m_umpireStatsLogReport )
{
tableDataLog.addRow( allocatorName,
LvArray::system::calculateSize( minMark ), GEOS_FMT( "{:.1f} %", minPercentage ),
LvArray::system::calculateSize( maxMark ), GEOS_FMT( "{:.1f} %", maxPercentage ),
LvArray::system::calculateSize( avgMark ), GEOS_FMT( "{:.1f} %", avgPercentage ),
LvArray::system::calculateSize( sumMark ), GEOS_FMT( "{:.1f} %", sumPercentage ) );
}
if( m_umpireStatsCsvReport )
{
tableDataCsv.addRow( m_currentCycle,
m_currentTime,
allocatorName,
minMark, minPercentage,
maxMark, maxPercentage,
avgMark, avgPercentage,
sumMark, sumPercentage );
}
#if defined( GEOS_USE_CALIPER )and defined( GEOS_USE_ADIAK )
pushStatsIntoAdiak( allocatorName + " sum across ranks", mark );
pushStatsIntoAdiak( allocatorName + " rank max", mark );
#endif
}
{ // output statistics
if( m_umpireStatsLogReport )
{
GEOS_LOG_RANK_0( m_memoryStatLogFormatter->toString( tableDataLog ) );
}
if( m_umpireStatsCsvReport )
{
std::ofstream csvFile{ m_umpireStatsCsvReportFilename, std::ios_base::app };
m_memoryStatCsvFormatter->dataToStream( csvFile, tableDataCsv );
}
}
}
} /* namespace geos */