1 |
i#include <cstdlib> |
2 |
#include <cstring> |
3 |
#include <mpi.h> |
4 |
|
5 |
#include "mpiSimulation.hpp" |
6 |
#include "simError.h" |
7 |
|
8 |
|
9 |
|
10 |
mpiSimulation::mpiSimulation(SimInfo* the_entryPlug) |
11 |
{ |
12 |
entryPlug = the_entryPlug; |
13 |
|
14 |
numberProcessors = MPI::COMM_WORLD.Get_size(); |
15 |
myNode = worldRank; |
16 |
|
17 |
// let the simulation know were there. |
18 |
entryPlug->mpiSim = this; |
19 |
} |
20 |
|
21 |
|
22 |
mpiSimulation::~mpiSimulation(){ |
23 |
|
24 |
// empty for now |
25 |
|
26 |
} |
27 |
|
28 |
|
29 |
void mpiSimulation::divideLabor( void ){ |
30 |
|
31 |
int nComponents; |
32 |
MoleculeStamp** compStamps; |
33 |
int* componentsNmol; |
34 |
|
35 |
double numerator; |
36 |
double denominator; |
37 |
double precast; |
38 |
|
39 |
int nTarget; |
40 |
int molIndex, atomIndex, compIndex, compStart; |
41 |
int done; |
42 |
int nLocal, molLocal; |
43 |
int i; |
44 |
int smallDiff, bigDiff; |
45 |
|
46 |
int testSum; |
47 |
|
48 |
nComponents = entryPlug->nComponents; |
49 |
compStamps = entryPlug->compStamps; |
50 |
componentsNmol = entryPlug->componentsNmol; |
51 |
|
52 |
numerator = (double) entryPlug->n_atoms; |
53 |
denominator = (double) numberProcessors; |
54 |
precast = numerator / denominator; |
55 |
nTarget = (int)( precast + 0.5 ); |
56 |
|
57 |
molIndex = 0; |
58 |
atomIndex = 0; |
59 |
compIndex = 0; |
60 |
compStart = 0; |
61 |
for( i=0; i<(numberProcessors-1); i++){ |
62 |
|
63 |
done = 0; |
64 |
nLocal = 0; |
65 |
molLocal = 0; |
66 |
|
67 |
if( i == myNode ){ |
68 |
myMolStart = molIndex; |
69 |
myAtomStart = atomIndex; |
70 |
} |
71 |
|
72 |
while( !done ){ |
73 |
|
74 |
if( (molIndex-compStart) >= componentsNmol[compIndex] ){ |
75 |
compStart = molIndex; |
76 |
compIndex++; |
77 |
continue; |
78 |
} |
79 |
|
80 |
nLocal += compStamps[compIndex]->getNAtoms(); |
81 |
atomIndex += compStamps[compIndex]->getNAtoms(); |
82 |
molIndex++; |
83 |
molLocal++; |
84 |
|
85 |
if ( nLocal == nTarget ) done = 1; |
86 |
|
87 |
else if( nLocal < nTarget ){ |
88 |
smallDiff = nTarget - nLocal; |
89 |
} |
90 |
else if( nLocal > nTarget ){ |
91 |
bigDiff = nLocal - nTarget; |
92 |
|
93 |
if( bigDiff < smallDiff ) done = 1; |
94 |
else{ |
95 |
molIndex--; |
96 |
molLocal--; |
97 |
atomIndex -= compStamps[compIndex]->getNAtoms(); |
98 |
nLocal -= compStamps[compIndex]->getNAtoms(); |
99 |
done = 1; |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
if( i == myNode ){ |
105 |
myMolEnd = (molIndex - 1); |
106 |
myAtomEnd = (atomIndex - 1); |
107 |
myNlocal = nLocal; |
108 |
myMol = molLocal; |
109 |
} |
110 |
|
111 |
numerator = (double)( entryPlug->n_atoms - atomIndex ); |
112 |
denominator = (double)( numberProcessors - (i+1) ); |
113 |
precast = numerator / denominator; |
114 |
nTarget = (int)( precast + 0.5 ); |
115 |
} |
116 |
|
117 |
if( myNode == numberProcessors-1 ){ |
118 |
myMolStart = molIndex; |
119 |
myAtomStart = atomIndex; |
120 |
|
121 |
nLocal = 0; |
122 |
molLocal = 0; |
123 |
while( compIndex < nComponents ){ |
124 |
|
125 |
if( (molIndex-compStart) >= componentsNmol[compIndex] ){ |
126 |
compStart = molIndex; |
127 |
compIndex++; |
128 |
continue; |
129 |
} |
130 |
|
131 |
nLocal += compStamps[compIndex]->getNAtoms(); |
132 |
atomIndex += compStamps[compIndex]->getNAtoms(); |
133 |
molIndex++; |
134 |
molLocal++; |
135 |
} |
136 |
|
137 |
myMolEnd = (molIndex - 1); |
138 |
myAtomEnd = (atomIndex - 1); |
139 |
myNlocal = nLocal; |
140 |
myMol = molLocal; |
141 |
} |
142 |
|
143 |
|
144 |
MPI_Allreduce( &Nlocal, &testSum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD ); |
145 |
|
146 |
if( myNode == 0 ){ |
147 |
if( testSum != entryPlug->n_atoms ){ |
148 |
sprintf( painCave.errMsg, |
149 |
"The summ of all nLocals, %d, did not equal the total number of atoms, %d.\n", |
150 |
testSum, entryPlug->n_atoms ); |
151 |
painCave.isFatal = 1; |
152 |
simError(); |
153 |
} |
154 |
} |
155 |
|
156 |
sprintf( checkPointMsg, |
157 |
"Successfully divided the molecules among the processors.\n" ); |
158 |
MPIcheckPoint(); |
159 |
|
160 |
// lets create the identity array |
161 |
} |