| 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 | 
chuckv | 
195 | 
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; | 
| 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 | 
  | 
  | 
 | 
| 58 | 
  | 
  | 
    if( i == myNode ){ | 
| 59 | 
  | 
  | 
      myMolStart = molIndex; | 
| 60 | 
  | 
  | 
      myAtomStart = atomIndex; | 
| 61 | 
  | 
  | 
    } | 
| 62 | 
  | 
  | 
     | 
| 63 | 
  | 
  | 
    while( !done ){ | 
| 64 | 
  | 
  | 
       | 
| 65 | 
  | 
  | 
      if( (molIndex-compStart) >= componentsNmol[compIndex] ){ | 
| 66 | 
  | 
  | 
        compStart = molIndex; | 
| 67 | 
  | 
  | 
        compIndex++; | 
| 68 | 
  | 
  | 
        continue; | 
| 69 | 
  | 
  | 
      } | 
| 70 | 
  | 
  | 
 | 
| 71 | 
  | 
  | 
      nLocal += compStamps[compIndex]->getNAtoms(); | 
| 72 | 
  | 
  | 
      atomIndex += compStamps[compIndex]->getNAtoms(); | 
| 73 | 
  | 
  | 
      molIndex++; | 
| 74 | 
  | 
  | 
       | 
| 75 | 
  | 
  | 
      if ( nLocal == nTarget ) done = 1; | 
| 76 | 
  | 
  | 
       | 
| 77 | 
  | 
  | 
      else if( nLocal < nTarget ){ | 
| 78 | 
  | 
  | 
        smallDiff = nTarget - nLocal; | 
| 79 | 
  | 
  | 
      } | 
| 80 | 
  | 
  | 
      else if( nLocal > nTarget ){ | 
| 81 | 
  | 
  | 
        bigDiff = nLocal - nTarget; | 
| 82 | 
  | 
  | 
         | 
| 83 | 
  | 
  | 
        if( bigDiff < smallDiff ) done = 1; | 
| 84 | 
  | 
  | 
        else{ | 
| 85 | 
  | 
  | 
          molIndex--; | 
| 86 | 
  | 
  | 
          atomIndex -= compStamps[compIndex]->getNAtoms(); | 
| 87 | 
  | 
  | 
          nLocal -= compStamps[compIndex]->getNAtoms(); | 
| 88 | 
  | 
  | 
          done = 1; | 
| 89 | 
  | 
  | 
        } | 
| 90 | 
  | 
  | 
      } | 
| 91 | 
  | 
  | 
    } | 
| 92 | 
  | 
  | 
     | 
| 93 | 
  | 
  | 
    if( i == myNode ){ | 
| 94 | 
  | 
  | 
      myMolEnd = (molIndex - 1); | 
| 95 | 
  | 
  | 
      myAtomEnd = (atomIndex - 1); | 
| 96 | 
  | 
  | 
      myNlocal = nLocal; | 
| 97 | 
  | 
  | 
    } | 
| 98 | 
  | 
  | 
     | 
| 99 | 
  | 
  | 
    numerator = (double)( entryPlug->n_atoms - atomIndex ); | 
| 100 | 
  | 
  | 
    denominator = (double)( numberProcessors - (i+1) ); | 
| 101 | 
  | 
  | 
    precast = numerator / denominator; | 
| 102 | 
  | 
  | 
    nTarget = (int)( precast + 0.5 ); | 
| 103 | 
  | 
  | 
  } | 
| 104 | 
  | 
  | 
   | 
| 105 | 
  | 
  | 
  if( myNode == numberProcessors-1 ){ | 
| 106 | 
  | 
  | 
      myMolStart = molIndex; | 
| 107 | 
  | 
  | 
      myAtomStart = atomIndex; | 
| 108 | 
  | 
  | 
 | 
| 109 | 
  | 
  | 
      nLocal = 0; | 
| 110 | 
  | 
  | 
      while( compIndex < nComponents ){ | 
| 111 | 
  | 
  | 
         | 
| 112 | 
  | 
  | 
        if( (molIndex-compStart) >= componentsNmol[compIndex] ){ | 
| 113 | 
  | 
  | 
          compStart = molIndex; | 
| 114 | 
  | 
  | 
          compIndex++; | 
| 115 | 
  | 
  | 
          continue; | 
| 116 | 
  | 
  | 
        } | 
| 117 | 
  | 
  | 
 | 
| 118 | 
  | 
  | 
        nLocal += compStamps[compIndex]->getNAtoms(); | 
| 119 | 
  | 
  | 
        atomIndex += compStamps[compIndex]->getNAtoms(); | 
| 120 | 
  | 
  | 
        molIndex++; | 
| 121 | 
  | 
  | 
      } | 
| 122 | 
  | 
  | 
       | 
| 123 | 
  | 
  | 
      myMolEnd = (molIndex - 1); | 
| 124 | 
  | 
  | 
      myAtomEnd = (atomIndex - 1); | 
| 125 | 
  | 
  | 
      myNlocal = nLocal;    | 
| 126 | 
  | 
  | 
  } | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
 | 
| 129 | 
  | 
  | 
  MPI_Allreduce( &Nlocal, &testSum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD ); | 
| 130 | 
  | 
  | 
   | 
| 131 | 
  | 
  | 
  if( myNode == 0 ){ | 
| 132 | 
  | 
  | 
    if( testSum != entryPlug->n_atoms ){ | 
| 133 | 
  | 
  | 
      sprintf( painCave.errMsg, | 
| 134 | 
  | 
  | 
               "The summ of all nLocals, %d, did not equal the total number of atoms, %d.\n", | 
| 135 | 
  | 
  | 
               testSum, entryPlug->n_atoms ); | 
| 136 | 
  | 
  | 
      painCave.isFatal = 1; | 
| 137 | 
  | 
  | 
      simError(); | 
| 138 | 
  | 
  | 
    } | 
| 139 | 
  | 
  | 
  } | 
| 140 | 
  | 
  | 
 | 
| 141 | 
  | 
  | 
  sprintf( checkPointMsg, | 
| 142 | 
  | 
  | 
           "Successfully divided the molecules among the processors.\n" ); | 
| 143 | 
  | 
  | 
  MPIcheckPoint(); | 
| 144 | 
  | 
  | 
} |