#include "mpi.h"
#include <stdio.h>
int main ( int argc, char *argv[] ) {
	MPI_Status status;
	int ProcNum, ProcRank;
	int dim = 1000;
	double *A, *B;
	double tmp = 0, C = 0;
	MPI_Init ( &argc, &argv );
	MPI_Comm_size ( MPI_COMM_WORLD, &ProcNum);
	MPI_Comm_rank ( MPI_COMM_WORLD, &ProcRank);

	if(ProcRank == 0){
		A = new double[dim];
		B = new double[dim];
		for(int i = 0; i < dim; i++){
			A[i] = 1.0;
			B[i] = 1.0;
		}
	}else{
		A = new double[dim];
		B = new double[dim];
	}

	MPI_Bcast(A,dim,MPI_DOUBLE,0,MPI_COMM_WORLD);
	MPI_Bcast(B,dim,MPI_DOUBLE,0,MPI_COMM_WORLD);

	if(ProcRank != 0){
		for(int i = (ProcRank - 1)*dim/4;i < ProcRank*dim/4; i++){
			tmp += A[i]*B[i];
		}

	}

	MPI_Reduce(&tmp,&C,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
	if(ProcRank == 0){
		printf("C = %lf \n", C);

	}
	MPI_Finalize();
	delete [] A;
	delete [] B;
	A = NULL;
	B = NULL;
	return 0;
}
