#include <stdio.h>
#include "mpi.h"
#define DATASIZE 1000

int main(int argc, char *argv[] )
{
    int my_rank, num_procs, index, chunk, low, high, i, my_result;
	int data[DATASIZE];
	FILE *fp;
	double stime, etime;
    MPI_Init( &argc, &argv );
	index = 0;
	my_result = 0;
    MPI_Comm_rank( MPI_COMM_WORLD, &my_rank);
	MPI_Comm_size( MPI_COMM_WORLD, &num_procs);
    stime = MPI_Wtick();
	if (my_rank == 0) 
	{
		fp = fopen("data.dat","r");
		if (fp == NULL)
		{
			printf("Cannot open file");
			exit(0);			
		}
		while (!feof(fp))
		{
			fscanf(fp,"%d",&data[index]);
			index++;
		}
    }
	
	MPI_Bcast(&index,1, MPI_INT, 0, MPI_COMM_WORLD);
	
	MPI_Bcast(data,index, MPI_INT, 0, MPI_COMM_WORLD);
	
	chunk = index / num_procs;
	low = my_rank * chunk;
	high = low + chunk;
	printf(" process %d  chunk %d low %d high %d \n", my_rank, chunk, low, high);
	for ( i = low; i< high ; i++)
		my_result += data[i];
	
	printf(" The result from process %d is %d \n", my_rank, my_result);
	
		
	MPI_Finalize( );
    return 0;
}
