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