title: NBody problem parallelization gives different results for the same input
body: <p>This an MPI version of the NBody problem. I already have an OpenMP version and its results are the same as the nbody version with one thread, but the MPI results differ, above all at the last interactions. At the first interactions, the outputs are quite similar but at the end, the outputs differ a lot.</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;cstring&gt;
  #include &lt;vector&gt;
  #include &lt;cstdlib&gt;
  #include &lt;chrono&gt; 
  #include &lt;stdio.h&gt; 
  #include &lt;string.h&gt; 
  #include &lt;omp.h&gt;
  #include &lt;mpi.h&gt;
  #include &quot;vector2.h&quot;
  
  /*
   * Constant definitions for field dimensions, and particle masses
   */
  const int fieldWidth = 1000;
  const int fieldHalfWidth = fieldWidth &gt;&gt; 1;
  const int fieldHeight = 1000;
  const int fieldHalfHeight = fieldHeight &gt;&gt; 1;
  
  const float minBodyMass = 2.5f;
  const float maxBodyMassVariance = 5.f;
  
  /*
   * Particle structure
   */
  struct Particle
  {
      float PositionX;
    float PositionY;
      float VelocityX;
    float VelocityY;
      float   Mass;
  
      Particle(float mass, float x, float y):
          PositionX( x )
      , PositionY( y )
          , VelocityX( 0.f )
      , VelocityY( 0.f )
          , Mass ( mass )
      { }
      
  };
  
  /*
   * Compute forces of particles exerted on one another
   */
  void ComputeForces(std::vector&lt;Particle&gt; &amp;p_bodies, float p_gravitationalTerm, float p_deltaT)
  {
      Vector2 direction,
          force, acceleration;
  
    Vector2 position1, position2,
      velocity1, velocity2;
  
      float distance;
  
      for (size_t j = 0; j &lt; p_bodies.size(); ++j)
      {
      position1 = 0.f, position2 = 0.f;
          
      Particle &amp;p1 = p_bodies[j];
        position1 = Vector2(p1.PositionX, p1.PositionY);
      
          force = 0.f, acceleration = 0.f;
          for (size_t k = 0; k &lt; p_bodies.size(); ++k)
          {
              if (k == j) continue;
          
              Particle &amp;p2 = p_bodies[k];
              position2 = Vector2(p2.PositionX, p2.PositionY);
        
              // Compute direction vector
              direction = position2 - position1;
              
              // Limit distance term to avoid singularities
              distance = std::max&lt;float&gt;( 0.5f * (p2.Mass + p1.Mass), fabs(direction.Length()) );
              
              // Accumulate force
              force += direction / (distance * distance * distance) * p2.Mass; 
          }
          // Compute acceleration for body 
          acceleration = force * p_gravitationalTerm;
          
          // Integrate velocity (m/s)
          p1.VelocityX += acceleration[0] * p_deltaT;
      p1.VelocityY += acceleration[1] * p_deltaT;
      }
  }
  
  /*
   * Update particle positions
   */
  void MoveBodies(std::vector&lt;Particle&gt; &amp;p_bodies, float p_deltaT)
  {
      for (size_t j = 0; j &lt; p_bodies.size(); ++j)
      {
          p_bodies[j].PositionX += p_bodies[j].VelocityX * p_deltaT;
       p_bodies[j].PositionY += p_bodies[j].VelocityY * p_deltaT;
      }
  }
  
  /*
   * Commit particle masses and positions to file in CSV format
   */
  void PersistPositions(const std::string &amp;p_strFilename, std::vector&lt;Particle&gt; &amp;p_bodies)
  {
      std::cout &lt;&lt; &quot;Writing to file: &quot; &lt;&lt; p_strFilename &lt;&lt; std::endl;
      std::string path = &quot;./NBodyOutputMPI/&quot; + p_strFilename;
      std::ofstream output(path);
      
      if (output.is_open())
      {   
          for (int j = 0; j &lt; p_bodies.size(); j++)
          {
              output &lt;&lt;   p_bodies[j].Mass &lt;&lt; &quot;, &quot; &lt;&lt;
                  p_bodies[j].PositionX &lt;&lt; &quot;, &quot; &lt;&lt;
                  p_bodies[j].PositionY &lt;&lt; std::endl;
          }
          
          output.close();
      }
      else
          std::cerr &lt;&lt; &quot;Unable to persist data to file:&quot; &lt;&lt; p_strFilename &lt;&lt; std::endl;
  
  }
  
  
  
  int main(int argc, char** argv)
  {
      int particleCount = 1024;
      int maxIteration = 1000;
      float deltaT = 0.05f;
      float gTerm = 1.f;
      bool enableOutput = true; //Enable the output files or not
      bool randomParticles = true; //If particles are built randomly or not
  
      std::ifstream fileInput;
      std::stringstream fileOutput;
      std::vector&lt;Particle&gt; bodies;
      
      std::string line;
      std::string argument;
      std::string fileName;
      
      const char delimiter = ','; //To split the numbers.
    
    //Variables for the struct.
    MPI_Datatype particletype, oldtypes[1]; 
    MPI_Aint offsets[1];
    int blockcounts[1];
    
    //Start MPI.
    int rc = MPI_Init(&amp;argc, &amp;argv); 
    
    //Variables for MPI.
    int rank, numtasks;
    
    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &amp;numtasks);
    
    //Initialize variables for Type_struct.
    offsets[0] = 0;
    oldtypes[0] = MPI_FLOAT;
    blockcounts[0] = 5;
    
    //Define the struct.
    MPI_Type_create_struct(1, blockcounts, offsets, oldtypes, &amp;particletype);
    MPI_Type_commit(&amp;particletype);
      
  //Introduce inputs
  
  for(int i = 0; i &lt; argc; i++){ //Walk through the arguments in the command line.
          argument = argv[i]; //Argument in the command line.       
        if(argument == &quot;-f&quot;){
          while(fileInput) //While the file is opened.
            {
              std::vector&lt;float&gt; vector (3);
                while(std::getline(fileInput,line)){ //Process each line.
                      std::stringstream splitlines;
                    splitlines &lt;&lt; line;
                    std::string word;
                      int counter = 0;
  
                      while(std::getline(splitlines,word,delimiter)){ //Process each word in each line.
                      float number = std::stof(word);
                      vector[counter] = number;
                      counter++;
                      }   
                      
                    bodies.push_back(Particle(vector[0],vector[1],vector[2])); //Add particle to the vector.
                }         
        }
      }       
  
  
  
    std::vector&lt;Particle&gt; particles;
    std::vector&lt;Particle&gt; final;
    
    if (rank == 0)
      final.resize(bodies.size());
      
    particles.resize(bodies.size() / numtasks);
    
    /*if(bodies.size()%numtasks!=0){
      MPI_Abort(MPI_COMM_WORLD, MPI_ERR_SIZE);
    }*/
    
    //Start the timer.
      double start = MPI_Wtime();
      for (int iteration = 0; iteration &lt; maxIteration; ++iteration)
      {
      MPI_Scatter(bodies.data(), particles.size(), particletype, particles.data(), particles.size(), particletype, 0, MPI_COMM_WORLD);
  
        ComputeForces(particles, gTerm, deltaT);
        MoveBodies(particles, deltaT);
        
      MPI_Gather(particles.data(), particles.size(), particletype, final.data(), particles.size(), particletype, 0, MPI_COMM_WORLD);
  
        if(enableOutput &amp;&amp; rank == 0){
              fileOutput.str(std::string());
              fileOutput &lt;&lt; &quot;nbody_&quot; &lt;&lt; iteration &lt;&lt; &quot;.txt&quot;;
              PersistPositions(fileOutput.str(), final);
          }
      }
    //Finish the timer.
      double finish = MPI_Wtime();
  
    //Calculate and show the time.
    if(rank == 0){
        double elapsed = finish - start;
        std::cout &lt;&lt; &quot;Elapsed time: &quot; &lt;&lt; elapsed &lt;&lt; &quot; s\n&quot;;
    }
      
    //End MPI.
    MPI_Finalize();
   
      return 0;
  }
  </code></pre>