#include <iostream>
#include <mpi.h>
using namespace std;

int main(int argc, char** argv){
	MPI_Init(&argc, &argv);
	int world_size, world_rank;
	MPI_Comm_size(MPI_COMM_WORLD, &world_size);
	MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
	int multiplier;
	if(world_rank==0){
		multiplier=10;
		cout<<"[BOSS] Broadcasting the multiplier ("<<multiplier<<") to all nodes..."<<endl;
	}
	MPI_Bcast(&multiplier, 1, MPI_INT, 0, MPI_COMM_WORLD);
	int local_value=world_rank*multiplier;
	cout<<"  ->[NODE "<<world_rank<<"] My calculated local value is: "<<local_value<<endl;
	int total_sum=0;
	MPI_Reduce(&local_value, &total_sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
	if(world_rank==0){
		cout<<"\n[BOSS] All nodes have reported back."<<endl;
		cout<<"[BOSS] The total combined sum from the entire cluster is: "<<total_sum<<endl;
	}
	MPI_Finalize();
	return 0;
}
