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

	int d = dim/ProcNum;
	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;
		}
	}
	A1 = new double[d];
	B1 = new double[d];

	MPI_Scatter(A,d,MPI_DOUBLE,A1,d,MPI_DOUBLE,0,MPI_COMM_WORLD);
	MPI_Scatter(B,d,MPI_DOUBLE,B1,d,MPI_DOUBLE,0,MPI_COMM_WORLD);
	
	for(int i = 0; i < d; i++){
		C += A1[i]*B1[i];
	}

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

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