ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/mpi_implementation/mpiSimulation.cpp
Revision: 215
Committed: Thu Dec 19 21:59:51 2002 UTC (21 years, 6 months ago) by chuckv
File size: 3904 byte(s)
Log Message:
+ added lennard-jones force module and corresponding class.
+ created forceFactory directory.

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 chuckv 215 mpiSimulation* mpiSim;
9 chuckv 194
10     mpiSimulation::mpiSimulation(SimInfo* the_entryPlug)
11 chuckv 122 {
12 chuckv 195 entryPlug = the_entryPlug;
13 chuckv 215 mpiPlug = new MpiSimData;
14 chuckv 195
15 chuckv 215 mpiPlug->numberProcessors = MPI::COMM_WORLD.Get_size();
16     mpiPlug->myNode = worldRank;
17 chuckv 195
18 chuckv 215
19 chuckv 122 }
20    
21    
22 chuckv 194 mpiSimulation::~mpiSimulation(){
23 chuckv 195
24 chuckv 215 delete mpiPlug;
25     // perhaps we should let fortran know the party is over.
26 chuckv 195
27 chuckv 194 }
28    
29    
30 mmeineke 200 void mpiSimulation::divideLabor( void ){
31    
32     int nComponents;
33     MoleculeStamp** compStamps;
34     int* componentsNmol;
35    
36 chuckv 195 double numerator;
37     double denominator;
38     double precast;
39    
40     int nTarget;
41     int molIndex, atomIndex, compIndex, compStart;
42     int done;
43 chuckv 196 int nLocal, molLocal;
44 chuckv 195 int i;
45     int smallDiff, bigDiff;
46    
47     int testSum;
48    
49 mmeineke 200 nComponents = entryPlug->nComponents;
50     compStamps = entryPlug->compStamps;
51     componentsNmol = entryPlug->componentsNmol;
52    
53 chuckv 215 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 mmeineke 202
60 chuckv 195 numerator = (double) entryPlug->n_atoms;
61 chuckv 215 denominator = (double) mpiPlug->numberProcessors;
62 chuckv 195 precast = numerator / denominator;
63     nTarget = (int)( precast + 0.5 );
64    
65     molIndex = 0;
66     atomIndex = 0;
67     compIndex = 0;
68     compStart = 0;
69 chuckv 215 for( i=0; i<(mpiPlug->numberProcessors-1); i++){
70 chuckv 195
71     done = 0;
72     nLocal = 0;
73 chuckv 196 molLocal = 0;
74 chuckv 195
75 chuckv 215 if( i == mpiPlug->myNode ){
76     mpiPlug->myMolStart = molIndex;
77     mpiPlug->myAtomStart = atomIndex;
78 chuckv 195 }
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 chuckv 196 molLocal++;
92 chuckv 195
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 chuckv 196 molLocal--;
105 chuckv 195 atomIndex -= compStamps[compIndex]->getNAtoms();
106     nLocal -= compStamps[compIndex]->getNAtoms();
107     done = 1;
108     }
109     }
110     }
111    
112 chuckv 215 if( i == mpiPlug->myNode ){
113     mpiPlug->myMolEnd = (molIndex - 1);
114     mpiPlug->myAtomEnd = (atomIndex - 1);
115     mpiPlug->myNlocal = nLocal;
116     mpiPlug->myMol = molLocal;
117 chuckv 195 }
118    
119     numerator = (double)( entryPlug->n_atoms - atomIndex );
120 chuckv 215 denominator = (double)( mpiPlug->numberProcessors - (i+1) );
121 chuckv 195 precast = numerator / denominator;
122     nTarget = (int)( precast + 0.5 );
123     }
124    
125 chuckv 215 if( mpiPlug->myNode == mpiPlug->numberProcessors-1 ){
126     mpiPlug->myMolStart = molIndex;
127     mpiPlug->myAtomStart = atomIndex;
128 chuckv 195
129     nLocal = 0;
130 chuckv 196 molLocal = 0;
131 chuckv 195 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 chuckv 196 molLocal++;
143 chuckv 195 }
144    
145 chuckv 215 mpiPlug->myMolEnd = (molIndex - 1);
146     mpiPlug->myAtomEnd = (atomIndex - 1);
147     mpiPlug->myNlocal = nLocal;
148     mpiPlug->myMol = molLocal;
149 chuckv 195 }
150    
151    
152 chuckv 215 MPI_Allreduce( &nLocal, &testSum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
153 chuckv 195
154 chuckv 215 if( mpiPlug->myNode == 0 ){
155 chuckv 195 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 chuckv 196
168     // lets create the identity array
169 chuckv 195 }