ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/mpi_implementation/mpiSimulation.cpp
Revision: 200
Committed: Mon Dec 9 20:54:42 2002 UTC (22 years, 4 months ago) by mmeineke
File size: 3390 byte(s)
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 chuckv 195 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 chuckv 196 molLocal = 0;
66 chuckv 195
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 chuckv 196 molLocal++;
84 chuckv 195
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 chuckv 196 molLocal--;
97 chuckv 195 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 chuckv 196 myMol = molLocal;
109 chuckv 195 }
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 chuckv 196 molLocal = 0;
123 chuckv 195 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 chuckv 196 molLocal++;
135 chuckv 195 }
136    
137     myMolEnd = (molIndex - 1);
138     myAtomEnd = (atomIndex - 1);
139 chuckv 196 myNlocal = nLocal;
140     myMol = molLocal;
141 chuckv 195 }
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 chuckv 196
160     // lets create the identity array
161 chuckv 195 }