forked from ornladios/ADIOS2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadiosSystem.cpp
More file actions
266 lines (235 loc) · 7.05 KB
/
Copy pathadiosSystem.cpp
File metadata and controls
266 lines (235 loc) · 7.05 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
256
257
258
259
260
261
262
263
264
265
266
/*
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "adiosSystem.h"
#include <chrono> //system_clock, now
#include <ctime>
#include <stdexcept> // std::runtime_error, std::exception
#include <system_error>
#include <thread>
#ifndef _WIN32
#include <sys/resource.h> // getrlimits, setrlimits
#include <sys/time.h>
#endif
#include <adios2sys/Directory.hxx>
#include <adios2sys/SystemTools.hxx>
#include <unordered_set>
#include "adios2/common/ADIOSTypes.h"
#include "adios2/helper/adiosComm.h"
#include "adios2/helper/adiosLog.h"
#include "adios2/helper/adiosString.h"
// needed by IsHDF5File()
#include "adios2/core/IO.h"
#include "adios2/toolkit/transportman/TransportMan.h"
#include <cstring>
// remove ctime warning on Windows
#ifdef _WIN32
#pragma warning(disable : 4996) // ctime warning
#endif
namespace adios2
{
namespace helper
{
bool CreateDirectory(const std::string &fullPath) noexcept
{
return static_cast<bool>(adios2sys::SystemTools::MakeDirectory(fullPath));
}
bool IsLittleEndian() noexcept
{
uint16_t hexa = 0x1234;
return *reinterpret_cast<uint8_t *>(&hexa) != 0x12; // NOLINT
}
std::string LocalTimeDate() noexcept
{
struct tm now_tm;
char buf[30];
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
#ifdef _WIN32
localtime_s(&now_tm, &now);
#else
localtime_r(&now, &now_tm);
#endif
strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y\n", &now_tm);
return std::string(buf);
}
bool IsRowMajor(const std::string hostLanguage) noexcept
{
bool isRowMajor = true;
if (hostLanguage == "Fortran" || hostLanguage == "R" || hostLanguage == "Matlab")
{
isRowMajor = false;
}
return isRowMajor;
}
bool IsZeroIndexed(const std::string hostLanguage) noexcept
{
bool isZeroIndexed = true;
if (hostLanguage == "Fortran" || hostLanguage == "R")
{
isZeroIndexed = false;
}
return isZeroIndexed;
}
int ExceptionToError(const std::string &function)
{
try
{
throw;
}
catch (std::invalid_argument &e)
{
helper::Log("Helper", "adiosSystem", "ExceptionToError", function + ": " + e.what(),
helper::FATALERROR);
return 1;
}
catch (std::system_error &e)
{
helper::Log("Helper", "adiosSystem", "ExceptionToError", function + ": " + e.what(),
helper::FATALERROR);
return 2;
}
catch (std::runtime_error &e)
{
helper::Log("Helper", "adiosSystem", "ExceptionToError", function + ": " + e.what(),
helper::FATALERROR);
return 3;
}
catch (std::exception &e)
{
helper::Log("Helper", "adiosSystem", "ExceptionToError", function + ": " + e.what(),
helper::FATALERROR);
return 4;
}
}
bool IsHDF5FileLocal(const std::string &name, core::IO &io, helper::Comm &comm,
const std::vector<Params> &transportsParameters) noexcept
{
bool isHDF5 = false;
try
{
transportman::TransportMan tm(io, comm);
if (transportsParameters.empty())
{
std::vector<Params> defaultTransportParameters(1);
defaultTransportParameters[0]["transport"] = "File";
tm.OpenFiles({name}, adios2::Mode::Read, defaultTransportParameters, false);
}
else
{
tm.OpenFiles({name}, adios2::Mode::Read, transportsParameters, false);
}
const unsigned char HDF5Header[8] = {137, 72, 68, 70, 13, 10, 26, 10};
if (tm.GetFileSize(0) >= 8)
{
char header[8];
tm.ReadFile(header, 8, 0);
tm.CloseFiles();
isHDF5 = !std::memcmp(header, HDF5Header, 8);
}
}
catch (std::ios_base::failure &)
{
isHDF5 = false;
}
return isHDF5;
}
bool IsHDF5File(const std::string &name, core::IO &io, helper::Comm &comm,
const std::vector<Params> &transportsParameters) noexcept
{
size_t flag = 0;
if (!comm.Rank())
{
flag = IsHDF5FileLocal(name, io, comm, transportsParameters) ? 1 : 0;
}
flag = comm.BroadcastValue(flag);
return (flag == 1);
}
char BPVersionLocal(const std::string &name) noexcept
{
const std::string mmdFileName = name + PathSeparator + "mmd.0";
return adios2sys::SystemTools::PathExists(mmdFileName) ? '5' : '4';
}
unsigned int NumHardwareThreadsPerNode() { return std::thread::hardware_concurrency(); }
size_t RaiseLimitNoFile()
{
#ifdef _WIN32
return _setmaxstdio(8192);
#else
static size_t raisedLimit = 0;
static bool firstCallRaiseLimit = true;
if (firstCallRaiseLimit)
{
struct rlimit limit;
errno = 0;
int err = getrlimit(RLIMIT_NOFILE, &limit);
raisedLimit = limit.rlim_cur;
if (!err)
{
if (limit.rlim_cur < limit.rlim_max)
{
limit.rlim_cur = limit.rlim_max;
err = setrlimit(RLIMIT_NOFILE, &limit);
if (!err)
{
getrlimit(RLIMIT_NOFILE, &limit);
raisedLimit = limit.rlim_cur;
}
}
}
if (err)
{
std::cerr << "adios2::helper::RaiseLimitNoFile(soft=" << limit.rlim_cur
<< ", hard=" << limit.rlim_max << ") failed with error code " << errno << ": "
<< strerror(errno) << std::endl;
}
firstCallRaiseLimit = false;
}
return raisedLimit;
#endif
}
void CleanupBPDirectory(const std::string &directory, const std::vector<std::string> &filesToKeep,
helper::Comm &comm)
{
if (!comm.Rank())
{
// Build a set of basenames for O(1) lookup
std::unordered_set<std::string> keepSet;
for (const auto &fullPath : filesToKeep)
{
std::string basename = adios2sys::SystemTools::GetFilenameName(fullPath);
keepSet.insert(basename);
}
// Scan directory and delete files not in whitelist
adios2sys::Directory dir;
if (dir.Load(directory).IsSuccess())
{
for (unsigned long i = 0; i < dir.GetNumberOfFiles(); ++i)
{
const std::string &fileName = dir.GetFileName(i);
// Skip . and ..
if (fileName == "." || fileName == "..")
{
continue;
}
// Delete if not in whitelist
if (keepSet.find(fileName) == keepSet.end())
{
std::string filePath = dir.GetFilePath(i);
if (dir.FileIsDirectory(i))
{
adios2sys::SystemTools::RemoveADirectory(filePath);
}
else
{
adios2sys::SystemTools::RemoveFile(filePath);
}
}
}
}
}
comm.Barrier("CleanupBPDirectory");
}
} // end namespace helper
} // end namespace adios2