I am working on parallelizing my code using MPI. I have heat a brick wall not it is at a stand still. If someone can help with making this parallel I would be happy. I was getting bus 10, I changed the place where I put MPI_Send and MPI_Recv, and used MPI_Bcast.
#include <iostream>
#include <random>
#include <ctime>
#include <vector>
#include <fstream>
#include <iosfwd>
#include "particles.hpp"
#include <mpi.h>
using namespace std;
default_random_engine e(time(NULL));
vector<Particles>particle;
double getRan();
double getMass();
void calInfo(int currentBody, int body2);
void update();
int main(int argc, char* argv[]){
//
int rank, size;
MPI_Status stat;
// start mpi
MPI_Init(&argc, &argv);
//finds the rank of the process
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
//how big process
MPI_Comm_size(MPI_COMM_WORLD, &size);
ofstream resultFile;
resultFile.open("result.txt");
double startTime=MPI_Wtime();
//create bodies
for(int i = 0; i < Nbodies; i++){
particle.emplace_back(getRan(),getRan(),getRan(),getRan(),getRan(),getRan(),getMass());
}
//starts the system
for(int steps = 0; steps < deltaSteps; steps++){
for(int currentBody = 0; currentBody < Nbodies; currentBody++){
for(int body2 = 0; body2 < Nbodies; body2++){
if (rank == 0){
//MPI_Bcast(&particle,Nbodies,MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Send(&particle[body2], Nbodies, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
//cal infomation
calInfo(currentBody,body2);
MPI_Recv(&particle[body2], Nbodies,MPI_DOUBLE,1,0,MPI_COMM_WORLD,&stat);
}else{
MPI_Recv(&particle[body2], Nbodies,MPI_DOUBLE,1,0,MPI_COMM_WORLD,&stat);
calInfo(currentBody,body2);
MPI_Send(&particle[body2], Nbodies, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
}
}
}
//print info
for(int i = 0; i < Nbodies; i++){
resultFile << rank <<" "<< particle[i].retuMass()<<","<<particle[i].retuposX()<<","<<particle[i].retuposY()<<","<<particle[i].retuposZ()<<","<<particle[i].retuvelX()<<","<<particle[i].retuvelY()<<","<<particle[i].retuvelZ()<<","<<particle[i].retuaccX()<<","<<particle[i].retuaccY()<<","<<particle[i].retuaccY()<<","<<particle[i].retufroceX()<<","<<particle[i].retufroceY()<<","<<particle[i].retufroceZ()<<"n"<<"n"<<"n";
}
//update pos, acc, vel
update();
}
resultFile << "total time = " << MPI_Wtime()-startTime;
//stops MPI
MPI_Finalize();
retu(0);
}
double getRan(){
//genartate rand numbers bewtween -1e18-1e18 for pos and vel
double pos;
uniform_real_distribution<> d(-1e18,1e18);
pos= d(e);
retu pos;
}
double getMass(){
//genarate a mass number greater then 0
double pos;
uniform_real_distribution<> d(1e18,6e18 );
pos= d(e);
retu pos;
}
void calInfo(int currentBody, int body2){
if(currentBody != body2){
//call the method to calculate all the r's
particle[currentBody].calR(particle[body2]);
//calcuatle the froces in X,Y,Z
particle[currentBody].calFroceX(particle[body2]);
particle[currentBody].calFroceY(particle[body2]);
particle[currentBody].calFroceZ(particle[body2]);
}
}
void update(){
for(int updateBody = 0; updateBody < Nbodies; updateBody++){
//update the accelration, posstion, velocity
particle[updateBody].updateAccX();
particle[updateBody].updateAccY();
particle[updateBody].updateAccZ();
particle[updateBody].updatePosX();
particle[updateBody].updatePosY();
particle[updateBody].updatePosZ();
particle[updateBody].updateVelX();
particle[updateBody].updateVelY();
particle[updateBody].updateVelZ();
}
}
