/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include "parallel_doc_term_architect.h"
#include "Config.h"

/**
  * Function flushing connection
  * \param myRank Rank of node
  * \param stat MPI_Status for communication
  */

void antiGridError(int myRank,MPI_Status* stat)
{
	int message;
	int numOfNodes;
	MPI_Comm_size(MPI_COMM_WORLD,&numOfNodes);
	if(myRank==0)
	{
		for(int i=1;i<numOfNodes;i++)
		{
			MPI_Recv(&message,1,MPI_INT,i,0,MPI_COMM_WORLD,stat);
			//                std::cout<<"Sent message to node "<<i<<std::endl;
		}

	}
	else
	{
		message=1;
		MPI_Send(&message,1,MPI_INT,0,0,MPI_COMM_WORLD);
		//            std::cout<<"Received "<<message<<" on node X\n";
	}
}

int main(int argc, char* argv[])
{
	MPI_Init(&argc,&argv);
	int myRank;
	MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
	MPI_Status stat;
	antiGridError(myRank,&stat);

	std::cout << "Testujemy budowanie macierzy...\n";
	doc_term_architect::Config::Initalize(argc, argv);

	if(myRank==0)
	{
		doc_term_architect::DocTermArchitectMaster master;
		master.Start();
		master.Finalize();
	}
	else
	{
		doc_term_architect::DocTermArchitectSlave slave;
		slave.Start();
	}
	MPI_Finalize();

	return 0;
}
