-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcopier.cpp
185 lines (164 loc) · 6.52 KB
/
copier.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
#include <adios2.h>
#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <map>
#include <math.h>
#include <mpi.h>
#include <numeric>
#include <stdexcept> //std::invalid_argument std::exception
#include <stdio.h>
#include <vector>
int main(int argc, char **argv)
{
int rank = 0, numProcs = 1;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
if (argc != 3)
{
std::cerr << "****************************************************" << std::endl;
std::cerr << "Error: Usage: " << argv[0] << " input output" << std::endl;
std::cerr << " Please rerun with the proper arguments." << std::endl;
std::cerr << " Thanks for stopping by!" << std::endl
<< std::endl;
MPI_Finalize();
return 0;
}
if (numProcs != 1)
{
std::cerr << "****************************************************" << std::endl;
std::cerr << " Error. This probably only works with 1 rank..." << std::endl
<< std::endl;
MPI_Finalize();
return 0;
}
adios2::ADIOS adios("adios2.xml", MPI_COMM_WORLD);
std::string inputFname(argv[1]), outputFname(argv[2]);
if (rank == 0)
std::cerr << "Copier: Reading from: " << inputFname << std::endl;
auto inIO = adios.DeclareIO("WriteIO");
auto reader = inIO.Open(inputFname, adios2::Mode::Read);
auto outIO = adios.DeclareIO("CopierOutput");
auto writer = outIO.Open(outputFname, adios2::Mode::Write);
int step = 0;
while (true)
{
std::vector<
std::pair<std::string, std::pair<std::vector<double>, std::vector<std::size_t>>>>
data;
std::vector<std::pair<std::string, std::pair<std::vector<int>, std::vector<std::size_t>>>>
datai;
std::map<std::string, std::string> attrs;
auto status = reader.BeginStep();
if (status != adios2::StepStatus::OK)
break;
auto variables = inIO.AvailableVariables();
std::cout << "Copier: Reading Step= " << step << std::endl;
for (const auto &vi : variables)
{
if (vi.first == "step" || vi.first == "MaxStep")
{
auto var = inIO.InquireVariable<int>(vi.first);
if (!var)
continue;
auto shape = var.Shape();
std::size_t dataSz =
std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<std::size_t>());
std::vector<int> arr(dataSz, 0.0);
reader.Get<int>(var, arr.data(), adios2::Mode::Sync);
std::vector<std::size_t> varS = shape;
datai.push_back(std::move(std::make_pair(var.Name(), std::make_pair(arr, varS))));
}
else
{
auto var = inIO.InquireVariable<double>(vi.first);
if (!var)
continue;
auto shape = var.Shape();
std::size_t dataSz =
std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<std::size_t>());
// std::cout<<" Read "<<vi.first<<" : sz= "<<dataSz<<std::endl;
std::vector<double> arr(dataSz, 0.0);
reader.Get<double>(var, arr.data(), adios2::Mode::Sync);
std::vector<std::size_t> varS = shape;
data.push_back(std::move(std::make_pair(var.Name(), std::make_pair(arr, varS))));
}
}
if (step == 0)
{
auto allAttrs = inIO.AvailableAttributes();
for (const auto &ai : allAttrs)
{
const auto a = inIO.InquireAttribute<std::string>(ai.first);
attrs[a.Name()] = a.Data()[0];
// std::cout<<" attr: "<<a.Name()<<" "<<a.Data()[0]<<std::endl;
}
}
reader.EndStep();
// Write data.
std::cout << "Copier: Writing Step= " << step << std::endl;
status = writer.BeginStep();
if (status != adios2::StepStatus::OK)
{
std::cerr << "Copier: Failure on writer.BeginStep()" << std::endl;
break;
}
if (step == 0 && !attrs.empty())
{
for (const auto &a : attrs)
{
// std::cout<<" attr= "<<a.first<<std::endl;
outIO.DefineAttribute<std::string>(a.first, a.second);
}
}
for (const auto &d : data)
{
auto varNm = d.first;
auto varSz = d.second.second;
std::size_t sz = d.second.first.size();
// const adios2::Dims shape, start, count; //{sz}, start{0}, count{sz};
std::vector<std::size_t> shape, start, count;
for (std::size_t i = 0; i < varSz.size(); i++)
{
shape.push_back(varSz[i]);
count.push_back(varSz[i]);
start.push_back(0);
}
// const adios2::Box<adios2::Dims> sel(0,sz); //(start, count);
// const adios2::Box<adios2::Dims> sel(start, count); //(start, count);
adios2::Variable<double> vOut = outIO.InquireVariable<double>(varNm);
if (!vOut)
vOut = outIO.DefineVariable<double>(varNm, shape, start, count);
// vOut.SetSelection(sel);
// std::cout<<" Write "<<varNm<<" : sz= "<<sz<<std::endl;
writer.Put(vOut, d.second.first.data());
}
for (const auto &d : datai)
{
auto varNm = d.first;
auto varSz = d.second.second;
std::size_t sz = d.second.first.size();
// const adios2::Dims shape, start, count; //{sz}, start{0}, count{sz};
std::vector<std::size_t> shape, start, count;
for (std::size_t i = 0; i < varSz.size(); i++)
{
shape.push_back(varSz[i]);
count.push_back(varSz[i]);
start.push_back(0);
}
// const adios2::Box<adios2::Dims> sel(start, count); //(start, count);
adios2::Variable<int> vOut = outIO.InquireVariable<int>(varNm);
if (!vOut)
vOut = outIO.DefineVariable<int>(varNm, shape, start, count);
// vOut.SetSelection(sel);
// std::cout<<" Write "<<varNm<<" : sz= "<<sz<<std::endl;
writer.Put(vOut, d.second.first.data());
}
writer.EndStep();
step++;
}
reader.Close();
writer.Close();
MPI_Finalize();
return 0;
}