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