#include // MPI #include #include using namespace std; void DebugVector(vector const& xin, MPI_Comm const& icomm) { int myrank, numprocs; MPI_Comm_rank(icomm, &myrank); MPI_Comm_size(icomm, &numprocs); int selectedProcess = 0; while(selectedProcess >= 0 && selectedProcess < numprocs) { if(myrank == 0) { cout << "Enter process number: " << endl; cin >> selectedProcess; } MPI_Bcast(&selectedProcess, 1, MPI_INT, 0, icomm); MPI_Barrier(icomm); if(myrank == selectedProcess ) { for(double x : xin) { cout << x << " "; } cout << endl; } MPI_Barrier(icomm); } } double par_scalar(const vector& x, const vector& y, MPI_Comm icomm) { double local = 0.0; for (unsigned int i = 0; i < x.size(); ++i) { local += x[i] * y[i]; } double sum = 0.0; MPI_Allreduce(&local, &sum, 1, MPI_DOUBLE, MPI_SUM, icomm); return sum; } void E7(const vector& x, MPI_Comm comm, double& min, double& max) { double local_min = x[0]; double local_max = x[0]; for (double x_i : x) { if (x_i < local_min) { local_min = x_i; } if (x_i> local_max) { local_max = x_i; } } MPI_Allreduce(&local_min, &min, 1, MPI_DOUBLE, MPI_MIN, comm); MPI_Allreduce(&local_max, &max, 1, MPI_DOUBLE, MPI_MAX, comm); } int main(int argc, char *argv[]) { MPI_Comm icomm = MPI_COMM_WORLD; MPI_Init(&argc, &argv); int myrank, numprocs; //numprocs = 1; // delete this line when uncommenting the next line MPI_Comm_rank(icomm, &myrank); // my MPI-rank MPI_Comm_size(icomm, &numprocs); if (0==myrank) { cout << "\n There are " << numprocs << " processes running.\n \n"; } if (0==myrank) cout << endl; unsigned int n=20; vector x(n); vector y(n); double x_i; for(unsigned int i=0; i