forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
175 lines (149 loc) · 4.17 KB
/
main.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
#include <AMReX_Utility.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_MultiFab.H>
#include <AMReX_ParmParse.H>
#include <algorithm>
#include <fstream>
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace amrex;
int
main (int argc, char* argv[])
{
amrex::Initialize(argc,argv);
{
BoxArray ba;
std::string ba_file("ba.max");
int max_grid_size = 32;
int min_ba_size = 12800;
{
ParmParse pp;
pp.query("ba_file", ba_file);
pp.query("max_grid_size", max_grid_size);
pp.query("min_ba_size", min_ba_size);
}
int nAtOnce = std::min(ParallelDescriptor::NProcs(), 32);
// read boxarray
{
const int MyProc = ParallelDescriptor::MyProc();
const int NProcs = ParallelDescriptor::NProcs();
const int NSets = (NProcs + (nAtOnce - 1)) / nAtOnce;
const int MySet = MyProc/nAtOnce;
for (int iSet = 0; iSet < NSets; ++iSet)
{
if (MySet == iSet)
{
std::ifstream ifs(ba_file.c_str(), std::ios::in);
ba.readFrom(ifs);
const int iBuff = 0;
const int wakeUpPID = (MyProc + nAtOnce);
const int tag = (MyProc % nAtOnce);
if (wakeUpPID < NProcs)
ParallelDescriptor::Send(&iBuff, 1, wakeUpPID, tag);
}
if (MySet == (iSet + 1))
{
//
// Next set waits.
//
int iBuff;
int waitForPID = (MyProc - nAtOnce);
int tag = (MyProc % nAtOnce);
ParallelDescriptor::Recv(&iBuff, 1, waitForPID, tag);
}
}
}
while (ba.size() < min_ba_size) {
ba.refine(2);
ba.maxSize(max_grid_size);
};
int nlevels;
{
int bsmin = 1024000;
for (int i=0; i<ba.size(); ++i) {
for (int idim=0; idim<BL_SPACEDIM; ++idim) {
bsmin = std::min(bsmin, ba[i].length(idim));
}
}
nlevels = 1;
int L = bsmin;
for (int lev=1; lev<10; ++lev) {
int Lold = L;
L /= 2;
if (L*2 != Lold) {
break;
} else {
nlevels++;
if (L == 2) break;
}
}
if (ParallelDescriptor::IOProcessor()) {
std::cout << "min length = " << bsmin << std::endl;
std::cout << "num Pts = " << ba.numPts() << std::endl;
std::cout << "num boxes = " << ba.size() << std::endl;
std::cout << "num levels = " << nlevels << std::endl;
}
}
ParallelDescriptor::Barrier();
Vector<std::unique_ptr<MultiFab> > mfs(nlevels);
Vector<BoxArray> bas(nlevels);
bas[0] = ba;
DistributionMapping dm{ba};
mfs[0].reset(new MultiFab(ba, dm, 1, 1));
mfs[0]->setVal(1.0);
for (int lev=1; lev<nlevels; ++lev) {
bas[lev] = BoxArray(bas[lev-1]);
bas[lev].coarsen(2);
mfs[lev].reset(new MultiFab(bas[lev], dm, 1, 1));
mfs[lev]->setVal(1.0);
}
Vector<Real> points(nlevels);
for (int lev=0; lev<nlevels; ++lev) {
points[lev] = mfs[lev]->norm1();
if (ParallelDescriptor::IOProcessor()) {
std::cout << points[lev] << " points on level " << lev << std::endl;
}
}
int nrounds = 1000;
{
ParmParse pp;
pp.query("nrounds", nrounds);
}
Real err = 0.0;
ParallelDescriptor::Barrier();
Real wt0 = ParallelDescriptor::second();
for (int iround = 0; iround < nrounds; ++iround) {
for (int c=0; c<2; ++c) {
for (int lev = 0; lev < nlevels; ++lev) {
mfs[lev]->FillBoundary_nowait();
mfs[lev]->FillBoundary_finish();
}
for (int lev = nlevels-1; lev >= 0; --lev) {
mfs[lev]->FillBoundary_nowait();
mfs[lev]->FillBoundary_finish();
}
}
Real e = double(iround+ParallelDescriptor::MyProc());
ParallelDescriptor::ReduceRealMax(e);
err += e;
}
ParallelDescriptor::Barrier();
Real wt1 = ParallelDescriptor::second();
if (ParallelDescriptor::IOProcessor()) {
std::cout << "Using MPI" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Fill Boundary Time: " << wt1-wt0 << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "ignore this line " << err << std::endl;
}
//
// When MPI3 shared memory is used, the dtor of MultiFab calls MPI
// functions. Because the scope of mfs is beyond the call to
// amrex::Finalize(), which in turn calls MPI_Finalize(), we
// destroy these MultiFabs by hand now.
//
mfs.clear();
}
amrex::Finalize();
}