ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/mpiSimulation.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/mpiSimulation.cpp (file contents):
Revision 406 by chuckv, Wed Mar 26 19:34:49 2003 UTC vs.
Revision 829 by gezelter, Tue Oct 28 16:03:37 2003 UTC

# Line 1 | Line 1
1   #ifdef IS_MPI
2 <
3 < #include <cstdlib>
4 < #include <cstring>
2 > #include <iostream>
3 > #include <stdlib.h>
4 > #include <string.h>
5 > #include <math.h>
6   #include <mpi.h>
6 #include <mpi++.h>
7  
8   #include "mpiSimulation.hpp"
9   #include "simError.h"
10   #include "fortranWrappers.hpp"
11   #include "randomSPRNG.hpp"
12  
13
13   mpiSimulation* mpiSim;
14  
15   mpiSimulation::mpiSimulation(SimInfo* the_entryPlug)
# Line 18 | Line 17 | mpiSimulation::mpiSimulation(SimInfo* the_entryPlug)
17    entryPlug = the_entryPlug;
18    mpiPlug = new mpiSimData;
19    
20 <  mpiPlug->numberProcessors = MPI::COMM_WORLD.Get_size();
20 >  MPI_Comm_size(MPI_COMM_WORLD, &(mpiPlug->numberProcessors) );
21    mpiPlug->myNode = worldRank;
22  
23    MolToProcMap = new int[entryPlug->n_mol];
24    MolComponentType = new int[entryPlug->n_mol];
26
25    AtomToProcMap = new int[entryPlug->n_atoms];
26  
27    mpiSim = this;
# Line 33 | Line 31 | mpiSimulation::~mpiSimulation(){
31  
32   mpiSimulation::~mpiSimulation(){
33    
34 +  delete[] MolToProcMap;
35 +  delete[] MolComponentType;
36 +  delete[] AtomToProcMap;
37 +
38    delete mpiPlug;
39    // perhaps we should let fortran know the party is over.
40    
# Line 44 | Line 46 | int* mpiSimulation::divideLabor( void ){
46  
47    int nComponents;
48    MoleculeStamp** compStamps;
49 <  randomSPRNG myRandom;
49 >  randomSPRNG *myRandom;
50    int* componentsNmol;
51    int* AtomsPerProc;
52  
# Line 55 | Line 57 | int* mpiSimulation::divideLabor( void ){
57    int old_atoms, add_atoms, new_atoms;
58  
59    int nTarget;
60 <  int molIndex, atomIndex, compIndex, compStart;
60 >  int molIndex, atomIndex;
61    int done;
62 <  int nLocal, molLocal;
63 <  int i, index;
64 <  int smallDiff, bigDiff;
62 >  int i, j, loops, which_proc, nmol_local, natoms_local;
63 >  int nmol_global, natoms_global;
64 >  int local_index;
65 >  int baseSeed = entryPlug->getSeed();
66  
64  int testSum;
65
67    nComponents = entryPlug->nComponents;
68    compStamps = entryPlug->compStamps;
69    componentsNmol = entryPlug->componentsNmol;
# Line 75 | Line 76 | int* mpiSimulation::divideLabor( void ){
76    mpiPlug->nSRIGlobal = entryPlug->n_SRI;
77    mpiPlug->nMolGlobal = entryPlug->n_mol;
78  
79 <  myRandom = new randomSPRNG();
79 >  
80 >  myRandom = new randomSPRNG( baseSeed );
81  
82 <  a = (double)mpiPlug->nMolGlobal / (double)mpiPlug->nAtomsGlobal;
82 >  a = 3.0 * (double)mpiPlug->nMolGlobal / (double)mpiPlug->nAtomsGlobal;
83  
84    // Initialize things that we'll send out later:
85    for (i = 0; i < mpiPlug->numberProcessors; i++ ) {
# Line 120 | Line 122 | int* mpiSimulation::divideLabor( void ){
122          
123          // Pick a processor at random
124  
125 <        which_proc = (int) (myRandom.getRandom() * mpiPlug->numberProcessors);
125 >        which_proc = (int) (myRandom->getRandom() * mpiPlug->numberProcessors);
126  
127          // How many atoms does this processor have?
128          
129          old_atoms = AtomsPerProc[which_proc];
130 <
129 <        // If the processor already had too many atoms, just skip this
130 <        // processor and try again.
131 <
132 <        if (old_atoms >= nTarget) continue;
133 <
134 <        add_atoms = compStamps[MolComponentType[i]]->getNatoms();
130 >        add_atoms = compStamps[MolComponentType[i]]->getNAtoms();
131          new_atoms = old_atoms + add_atoms;
136    
137        // If we can add this molecule to this processor without sending
138        // it above nTarget, then go ahead and do it:
139    
140        if (new_atoms <= nTarget) {
141          MolToProcMap[i] = which_proc;
142          AtomsPerProc[which_proc] += add_atoms;
143          for (j = 0 ; j < add_atoms; j++ ) {
144            atomIndex++;
145            AtomToProcMap[atomIndex] = which_proc;
146          }
147          done = 1;
148          continue;
149        }
132  
133          // If we've been through this loop too many times, we need
134          // to just give up and assign the molecule to this processor
# Line 164 | Line 146 | int* mpiSimulation::divideLabor( void ){
146            MolToProcMap[i] = which_proc;
147            AtomsPerProc[which_proc] += add_atoms;
148            for (j = 0 ; j < add_atoms; j++ ) {
149 <            atomIndex++;
150 <            AtomToProcMap[atomIndex] = which_proc;
149 >            AtomToProcMap[atomIndex] = which_proc;
150 >            atomIndex++;
151            }
152            done = 1;
153            continue;
154          }
155 +    
156 +        // If we can add this molecule to this processor without sending
157 +        // it above nTarget, then go ahead and do it:
158 +    
159 +        if (new_atoms <= nTarget) {
160 +          MolToProcMap[i] = which_proc;
161 +          AtomsPerProc[which_proc] += add_atoms;
162 +          for (j = 0 ; j < add_atoms; j++ ) {
163 +            AtomToProcMap[atomIndex] = which_proc;
164 +            atomIndex++;
165 +          }
166 +          done = 1;
167 +          continue;
168 +        }
169  
174        // The only situation left is where old_atoms < nTarget, but
175        // new_atoms > nTarget.   We want to accept this with some
176        // probability that dies off the farther we are from nTarget
170  
171 +        // The only situation left is when new_atoms > nTarget.  We
172 +        // want to accept this with some probability that dies off the
173 +        // farther we are from nTarget
174 +
175          // roughly:  x = new_atoms - nTarget
176          //           Pacc(x) = exp(- a * x)
177 <        // where a = 1 / (average atoms per molecule)
177 >        // where a = penalty / (average atoms per molecule)
178  
179          x = (double) (new_atoms - nTarget);
180 <        y = myRandom.getRandom();
181 <        
182 <        if (exp(- a * x) > y) {
180 >        y = myRandom->getRandom();
181 >      
182 >        if (y < exp(- a * x)) {
183            MolToProcMap[i] = which_proc;
184            AtomsPerProc[which_proc] += add_atoms;
185            for (j = 0 ; j < add_atoms; j++ ) {
186 <            atomIndex++;
187 <            AtomToProcMap[atomIndex] = which_proc;
188 <          }
186 >            AtomToProcMap[atomIndex] = which_proc;
187 >            atomIndex++;
188 >           }
189            done = 1;
190            continue;
191          } else {
# Line 200 | Line 197 | int* mpiSimulation::divideLabor( void ){
197  
198      // Spray out this nonsense to all other processors:
199  
200 <    MPI::COMM_WORLD.Bcast(&MolToProcMap, mpiPlug->nMolGlobal,
201 <                          MPI_INT, 0);
200 >    MPI_Bcast(MolToProcMap, mpiPlug->nMolGlobal,
201 >              MPI_INT, 0, MPI_COMM_WORLD);
202  
203 <    MPI::COMM_WORLD.Bcast(&AtomToProcMap, mpiPlug->nAtomsGlobal,
204 <                          MPI_INT, 0);
203 >    MPI_Bcast(AtomToProcMap, mpiPlug->nAtomsGlobal,
204 >              MPI_INT, 0, MPI_COMM_WORLD);
205  
206 <    MPI::COMM_WORLD.Bcast(&MolComponentType, mpiPlug->nMolGlobal,
207 <                          MPI_INT, 0);
206 >    MPI_Bcast(MolComponentType, mpiPlug->nMolGlobal,
207 >              MPI_INT, 0, MPI_COMM_WORLD);
208  
209 <    MPI::COMM_WORLD.Bcast(&AtomsPerProc, mpiPlug->numberProcessors,
210 <                          MPI_INT, 0);    
209 >    MPI_Bcast(AtomsPerProc, mpiPlug->numberProcessors,
210 >              MPI_INT, 0, MPI_COMM_WORLD);    
211    } else {
212  
213      // Listen to your marching orders from processor 0:
214      
215 <    MPI::COMM_WORLD.Bcast(&MolToProcMap, mpiPlug->nMolGlobal,
216 <                          MPI_INT, 0);
215 >    MPI_Bcast(MolToProcMap, mpiPlug->nMolGlobal,
216 >              MPI_INT, 0, MPI_COMM_WORLD);
217      
218 <    MPI::COMM_WORLD.Bcast(&AtomToProcMap, mpiPlug->nAtomsGlobal,
219 <                          MPI_INT, 0);
218 >    MPI_Bcast(AtomToProcMap, mpiPlug->nAtomsGlobal,
219 >              MPI_INT, 0, MPI_COMM_WORLD);
220  
221 <    MPI::COMM_WORLD.Bcast(&MolComponentType, mpiPlug->nMolGlobal,
222 <                          MPI_INT, 0);
221 >    MPI_Bcast(MolComponentType, mpiPlug->nMolGlobal,
222 >              MPI_INT, 0, MPI_COMM_WORLD);
223      
224 <    MPI::COMM_WORLD.Bcast(&AtomsPerProc, mpiPlug->numberProcessors,
225 <                          MPI_INT, 0);
224 >    MPI_Bcast(AtomsPerProc, mpiPlug->numberProcessors,
225 >              MPI_INT, 0, MPI_COMM_WORLD);
226 >
227 >
228    }
229  
230  
# Line 245 | Line 244 | int* mpiSimulation::divideLabor( void ){
244      }
245    }
246  
247 <  MPI::COMM_WORLD.Allreduce(&nmol_local,&nmol_global,1,MPI_INT,MPI_SUM);
248 <  MPI::COMM_WORLD.Allreduce(&natoms_local,&natoms_global,1,MPI_INT,MPI_SUM);
247 >  MPI_Allreduce(&nmol_local,&nmol_global,1,MPI_INT,MPI_SUM,
248 >                MPI_COMM_WORLD);
249 >  MPI_Allreduce(&natoms_local,&natoms_global,1,MPI_INT,
250 >                MPI_SUM, MPI_COMM_WORLD);
251    
252    if( nmol_global != entryPlug->n_mol ){
253      sprintf( painCave.errMsg,
# Line 277 | Line 278 | int* mpiSimulation::divideLabor( void ){
278    local_index = 0;
279    for (i = 0; i < mpiPlug->nAtomsGlobal; i++) {
280      if (AtomToProcMap[i] == mpiPlug->myNode) {
280      local_index++;
281        globalIndex[local_index] = i;
282 +      local_index++;
283      }
284    }
285    
286 <
286 <
287 <
288 <   index = mpiPlug->myAtomStart;
289 < //   for( i=0; i<mpiPlug->myNlocal; i++){
290 < //     globalIndex[i] = index;
291 < //     index++;
292 < //   }
293 <
294 < //   return globalIndex;
286 >  return globalIndex;
287   }
288  
289  
# Line 300 | Line 292 | void mpiSimulation::mpiRefresh( void ){
292    int isError, i;
293    int *globalIndex = new int[mpiPlug->myNlocal];
294  
295 <  for(i=0; i<mpiPlug->myNlocal; i++) globalIndex[i] = entryPlug->atoms[i]->getGlobalIndex();
295 >  // Fortran indexing needs to be increased by 1 in order to get the 2 languages to
296 >  // not barf
297  
298 +  for(i=0; i<mpiPlug->myNlocal; i++) globalIndex[i] = entryPlug->atoms[i]->getGlobalIndex()+1;
299 +
300    
301    isError = 0;
302    setFsimParallel( mpiPlug, &(entryPlug->n_atoms), globalIndex, &isError );

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines