/
t_blueprint_mpi_relay.cpp
268 lines (217 loc) · 8.17 KB
/
t_blueprint_mpi_relay.cpp
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
267
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Copyright (c) 2014, Lawrence Livermore National Security, LLC.
//
// Produced at the Lawrence Livermore National Laboratory
//
// LLNL-CODE-666778
//
// All rights reserved.
//
// This file is part of Conduit.
//
// For details, see https://lc.llnl.gov/conduit/.
//
// Please also read conduit/LICENSE
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the disclaimer below.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the disclaimer (as noted below) in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the LLNS/LLNL nor the names of its contributors may
// be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
// LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//-----------------------------------------------------------------------------
///
/// file: t_blueprint_mpi_relay.cpp
///
//-----------------------------------------------------------------------------
#include "conduit.hpp"
#include "conduit_blueprint.hpp"
#include "conduit_relay.hpp"
#include "conduit_relay_mpi.hpp"
#include "conduit_utils.hpp"
#include <mpi.h>
#include <iostream>
#include <sstream>
#include <string>
#include "gtest/gtest.h"
using namespace conduit;
using namespace conduit::relay;
using namespace conduit::relay::mpi;
using namespace conduit::utils;
using namespace std;
//-----------------------------------------------------------------------------
void mesh_blueprint_save(const Node &data,
const std::string &path,
const std::string &file_protocol)
{
Node io_protos;
relay::io::about(io_protos["io"]);
bool hdf5_enabled = io_protos["io/protocols/hdf5"].as_string() == "enabled";
// only run this test if hdf5 is enabled
if(!hdf5_enabled)
{
CONDUIT_INFO("hdf5 is disabled, skipping hdf5 dependent test");
return;
}
// For simplicity, this code assumes that all ranks have
// data and that data is NOT multi-domain.
int par_rank = mpi::rank(MPI_COMM_WORLD);
int par_size = mpi::size(MPI_COMM_WORLD);
// setup the directory
char fmt_buff[64] = {0};
int cycle = data["state/cycle"].to_int32();
snprintf(fmt_buff, sizeof(fmt_buff), "%06d",cycle);
std::string output_base_path = path;
ostringstream oss;
oss << output_base_path << ".cycle_" << fmt_buff;
string output_dir = oss.str();
bool dir_ok = false;
// let rank zero handle dir creation
if(par_rank == 0)
{
// check of the dir exists
dir_ok = is_directory(output_dir);
if(!dir_ok)
{
// if not try to let rank zero create it
dir_ok = create_directory(output_dir);
}
}
int local_domains, global_domains;
local_domains = 1;
// use an mpi sum to check if the dir exists
Node n_src, n_reduce;
if(dir_ok)
n_src = (int)1;
else
n_src = (int)0;
mpi::sum_all_reduce(n_src,
n_reduce,
MPI_COMM_WORLD);
dir_ok = (n_reduce.as_int() == 1);
// find out how many domains there are
n_src = local_domains;
mpi::sum_all_reduce(n_src,
n_reduce,
MPI_COMM_WORLD);
global_domains = n_reduce.as_int();
if(!dir_ok)
{
CONDUIT_ERROR("Error: failed to create directory " << output_dir);
}
// write out our local domain
uint64 domain = data["state/domain_id"].to_uint64();
snprintf(fmt_buff, sizeof(fmt_buff), "%06llu",domain);
oss.str("");
oss << "domain_" << fmt_buff << "." << file_protocol;
string output_file = join_file_path(output_dir,oss.str());
relay::io::save(data, output_file);
int root_file_writer = 0;
// let rank zero write out the root file
if(par_rank == root_file_writer)
{
snprintf(fmt_buff, sizeof(fmt_buff), "%06d",cycle);
oss.str("");
oss << path
<< ".cycle_"
<< fmt_buff
<< ".root";
string root_file = oss.str();
string output_dir_base, output_dir_path;
rsplit_string(output_dir,
"/",
output_dir_base,
output_dir_path);
string output_file_pattern = join_file_path(output_dir_base, "domain_%06d." + file_protocol);
Node root;
Node &bp_idx = root["blueprint_index"];
blueprint::mesh::generate_index(data,
"",
global_domains,
bp_idx["mesh"]);
// work around conduit and manually add state fields
// sometimes they were not present and bad things happened
if(data.has_path("state/cycle"))
{
bp_idx["mesh/state/cycle"] = data["state/cycle"].to_int32();
}
if(data.has_path("state/time"))
{
bp_idx["mesh/state/time"] = data["state/time"].to_double();
}
root["protocol/name"] = file_protocol;
root["protocol/version"] = "0.4.0";
root["number_of_files"] = global_domains;
// for now we will save one file per domain, so trees == files
root["number_of_trees"] = global_domains;
// TODO: make sure this is relative
root["file_pattern"] = output_file_pattern;
root["tree_pattern"] = "/";
relay::io::save(root,root_file,file_protocol);
}
}
//-----------------------------------------------------------------------------
TEST(blueprint_mpi_relay, basic_use)
{
Node io_protos;
relay::io::about(io_protos["io"]);
bool hdf5_enabled = io_protos["io/protocols/hdf5"].as_string() == "enabled";
// only run this test if hdf5 is enabled
if(!hdf5_enabled)
{
CONDUIT_INFO("hdf5 is disabled, skipping hdf5 dependent test");
return;
}
int rank = mpi::rank(MPI_COMM_WORLD);
int com_size = mpi::size(MPI_COMM_WORLD);
std::cout<<"Rank "<<rank<<" of "<<com_size<<"\n";
index_t npts_x = 10;
index_t npts_y = 10;
index_t npts_z = 10;
Node dset;
blueprint::mesh::examples::braid("uniform",
npts_x,
npts_y,
npts_z,
dset);
// the example data set has the bounds -10 to 10 in all dims
// Offset this along x to create mpi 'pencil'
dset["coordsets/coords/origin/x"] = -10.0 + 20.0 * rank;
dset["state/domain_id"] = rank;
string protocol = "hdf5";
//string protocol = "conduit_bin";
string output_path = "test_blueprint_mpi_relay";
mesh_blueprint_save(dset, output_path, protocol);
}
//-----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
int result = 0;
::testing::InitGoogleTest(&argc, argv);
MPI_Init(&argc, &argv);
result = RUN_ALL_TESTS();
MPI_Finalize();
return result;
}