/*! \file:  GetOwnershipBoundariesFromRange.cpp
 *  \brief from a local range on each cpu, we determine what 
 *  lower row and upper row from a matrix a cpu owns.
 */


#ifdef HAVE_CONFIG_H
	#include "config.h"
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include "stdio.h"
#include "../../../shared/shared.h"

#undef __FUNCT__
#define __FUNCT__ "GetOwnershipBoundariesFromRange"

void GetOwnershipBoundariesFromRange(int* plower_row,int* pupper_row,int range){

	/*externals :*/
	extern int my_rank;
	extern int num_procs;
	
	/*output: */
	int lower_row,upper_row;
	
	/*intermediary :*/
	int i;
	int* allranges=NULL;

	/*Gather all range values into allranges, for all nodes*/
	allranges=(int*)xmalloc(num_procs*sizeof(int));
	MPI_Allgather(&range,1,MPI_INT,allranges,1,MPI_INT,MPI_COMM_WORLD);

	/*From all ranges, get lower row and upper row*/
	lower_row=0;
	upper_row=lower_row+allranges[0];
	for (i=1;i<=my_rank;i++){
		lower_row=lower_row+allranges[i-1];
		upper_row=upper_row+allranges[i];
	}

	/*Assign output*/
	*plower_row=lower_row;
	*pupper_row=upper_row;

	/*Free ressources:*/
	xfree((void**)&allranges);

}
