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