Newer
Older
MpiSimulation::MpiSimulation(int& argc, char**& argv) {
int bodyBlocklengths[7] = {1, 3, 3, 3, 1, 1, 1};
MPI::Datatype bodyDatatypes[7] = {MPI::LB, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::DOUBLE, MPI::BOOL, MPI::UB};
MPI::Aint bodyDisplacements[7];
Box box[2];
int boxBlocklengths[4] = {1, 3, 3, 1};
MPI::Datatype boxDatatypes[4] = {MPI::LB, MPI::DOUBLE, MPI::DOUBLE, MPI::UB};
MPI::Aint boxDisplacements[4];
this->bodyType = MPI::DATATYPE_NULL;
this->boxType = MPI::DATATYPE_NULL;
bodyDisplacements[0] = MPI::Get_address(&(b[0])) - MPI::Get_address(&(b[0]));
bodyDisplacements[1] = MPI::Get_address(&(b[0].position[0])) - MPI::Get_address(&(b[0]));
bodyDisplacements[2] = MPI::Get_address(&(b[0].velocity[0])) - MPI::Get_address(&(b[0]));
bodyDisplacements[3] = MPI::Get_address(&(b[0].acceleration[0])) - MPI::Get_address(&(b[0]));
bodyDisplacements[4] = MPI::Get_address(&(b[0].mass)) - MPI::Get_address(&(b[0]));
bodyDisplacements[5] = MPI::Get_address(&(b[0].refinement)) - MPI::Get_address(&(b[0]));
bodyDisplacements[6] = MPI::Get_address(&(b[1])) - MPI::Get_address(&(b[0]));
this->bodyType = this->bodyType.Create_struct(7, bodyBlocklengths, bodyDisplacements, bodyDatatypes);
boxDisplacements[0] = MPI::Get_address(&(box[0])) - MPI::Get_address(&(box[0]));
boxDisplacements[1] = MPI::Get_address(&(box[0].min[0])) - MPI::Get_address(&(box[0]));
boxDisplacements[2] = MPI::Get_address(&(box[0].max[0])) - MPI::Get_address(&(box[0]));
boxDisplacements[3] = MPI::Get_address(&(box[1])) - MPI::Get_address(&(box[0]));
this->boxType = this->bodyType.Create_struct(4, boxBlocklengths, boxDisplacements, boxDatatypes);
this->boxType.Commit();
this->mpiSize = MPI::COMM_WORLD.Get_size();
this->mpiRank = MPI::COMM_WORLD.Get_rank();
this->domains = new Box[this->mpiSize];
this->correctState = true;
if (argc == 2) {
this->correctState = this->readInputData(string(argv[1]));
} else {
this->correctState = false;
}
MPI::COMM_WORLD.Bcast(&this->correctState, 1, MPI::BOOL, 0);
if (!this->correctState) {
cerr << "Error occurred: terminating ..." << endl;
this->bodyType.Free();
MPI::Finalize();
exit(-1);
}
if (this->mpiRank == 0) {
MPI::COMM_WORLD.Send(b, 2, this->bodyType, 1, 0);
} else if (this->mpiRank == 1) {
MPI::COMM_WORLD.Recv(b, 2, this->bodyType, 0, 0);
}
}
bool MpiSimulation::readInputData(string filename) {
if (this->mpiRank == 0) {
this->bodies = Tree::dubinskiParse(filename);
if (this->bodies.empty()) {
return false;
}
}
return true;
void MpiSimulation::cleanup() {
while (!this->comms.empty()) {
this->comms.back().cleanup();
this->comms.pop_back();
}
bool MpiSimulation::stateCorrect() {
return this->correctState;
int MpiSimulation::getNumberOfProcesses() {
return this->mpiSize;
}
int MpiSimulation::getProcessId() {
return this->mpiRank;
}
MPI::Datatype* MpiSimulation::getDatatype() {
return &this->bodyType;
}
void MpiSimulation::distributeBodies() {
if (this->mpiRank == 0) {
nodes.push_back(Node(NULL));
nodes.front().setBodies(this->bodies);
bb = nodes.front().getBB();
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
nodes.front().setBB(bb);
while (nodes.size() < this->mpiSize) {
int mostBodiesIndex = 0;
for (unsigned int i = 0; i < nodes.size(); i++) {
if (nodes[i].getBodies().size() > nodes[mostBodiesIndex].getBodies().size()) {
mostBodiesIndex = i;
}
}
vector<Box> subdomains = nodes[mostBodiesIndex].getBB().splitLongestSide();
vector<Body> buf = nodes[mostBodiesIndex].getBodies();
Node n(NULL);
Box bb;
n.setBodies(subdomains[0].extractBodies(buf));
bb.extendForBodies(n.getBodies());
n.setBB(bb);
nodes.insert(nodes.begin() + mostBodiesIndex, n);
n = Node(NULL);
n.setBodies(subdomains[1].extractBodies(buf));
bb.extendForBodies(n.getBodies());
n.setBB(bb);
nodes.insert(nodes.begin() + mostBodiesIndex, n);
nodes.erase(nodes.begin() + mostBodiesIndex + 2);
}
this->bodies = nodes[0].getBodies();
for (unsigned int i = 1; i < nodes.size(); i++) {
this->comms.push_back(MpiBodyComm(&this->bodyType));
this->comms.back().sendUnblocking(i, nodes[i].getBodies());
this->comms.push_back(MpiBodyComm(&this->bodyType));
this->comms[0].recvBlocking(0, this->bodies);
this->tree.build(this->bodies);
void MpiSimulation::distributeDomains() {
this->domains[this->mpiRank] = this->tree.getRootBB();
MPI::COMM_WORLD.Allgather(&this->domains[this->mpiRank], 1, this->boxType, &this->domains[0], 1, this->boxType);
}
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
void MpiSimulation::distributeLETs() {
for (int i = 0; i < this->mpiSize; i++) {
if (i != this->mpiRank) {
vector<Body> refinements = this->tree.copyRefinements(this->domains[i]);
cout << this->mpiRank << ": " << i << ": " << refinements.size() << endl;
//cout << this->mpiRank << ": " <<
//this->comms[i].sendUnblocking(i, refinements);
/*
vector<MpiBodyComm>::iterator it = this->comms.begin();
while (it != this->comms.end() && !it->sendUnblocking(i, refinements)) {
it++;
}
if (it == this->comms.end()) {
this->comms.push_back(MpiBodyComm(&this->bodyType));
this->comms.back().sendUnblocking(i, refinements);
}
*/
}
}
/*
int received = 0;
while (received < this->mpiSize - 1) {
vector<Body> refinements;
this->comms[0].recvBlocking(MPI::ANY_SOURCE, refinements);
//TODO: process refinements
received++;
}
*/
}
void MpiSimulation::runStep() {
this->distributeDomains();
this->tree.rebuild();