コード例 #1
0
ファイル: IndividualBag.cpp プロジェクト: AngelGate/beagle
/*!
 *  \brief Read individuals of a bag.
 *  \param inIter XML iterator to read the individuals from.
 *  \param ioContext Evolutionary context.
 */
void IndividualBag::readIndividuals(PACC::XML::ConstIterator inIter, Context& ioContext)
{
	Beagle_StackTraceBeginM();

	clear();
	Individual::Handle lPrevIndivHandle = ioContext.getIndividualHandle();
	unsigned int lPrevIndivIndex = ioContext.getIndividualIndex();

	for(PACC::XML::ConstIterator lIter=inIter; lIter; ++lIter) {
		if(lIter->getType() != PACC::XML::eData) continue;
		if(lIter->getValue() == "NullHandle") {
			push_back(NULL);
			continue;
		} else if(lIter->getValue() != "Individual") continue;

		Individual::Alloc::Handle lIndivAlloc = NULL;
		std::string lIndivTypeName = lIter->getAttribute("type");
		const Factory& lFactory = ioContext.getSystem().getFactory();
		if(lIndivTypeName.empty()) {
			lIndivAlloc =
			    castHandleT<Individual::Alloc>(lFactory.getConceptAllocator("Individual"));
		} else {
			lIndivAlloc =
			    castHandleT<Individual::Alloc>(lFactory.getAllocator(lIndivTypeName));
		}
		Individual::Handle lReadIndiv = castHandleT<Individual>(lIndivAlloc->allocate());
		const unsigned int lBackIndex = size();
		push_back(lReadIndiv);
		ioContext.setIndividualHandle(lReadIndiv);
		ioContext.setIndividualIndex(lBackIndex);
		lReadIndiv->readWithContext(lIter, ioContext);
	}

	ioContext.setIndividualIndex(lPrevIndivIndex);
	ioContext.setIndividualHandle(lPrevIndivHandle);

	Beagle_StackTraceEndM();
}
void Beagle::MPI::EvaluationOp::evaluatorOperate(Deme& ioDeme, Context& ioContext) {
	try {
		//char lMessage[4096];
		int lMessageSize;
		MPI_Status lStatus;
		int lSource;

		bool lDone = false;
		while(!lDone) {
			//Receive an individual to evaluate
			MPI_Recv(&lMessageSize, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &lStatus);
			lSource = lStatus.MPI_SOURCE;
			if(lStatus.MPI_TAG == eEvolutionEnd) {
				Beagle_LogDetailedM(
								   ioContext.getSystem().getLogger(),
								   "evaluation", "Beagle::MPIEvaluationOp",
								   std::string("End of evolution received from process ") + int2str(lSource)
								   );
				lDone = true;
			} else {
				char *lMessage = new char[lMessageSize];
				unsigned int lGeneration;
				MPI_Recv(lMessage, lMessageSize, MPI_CHAR, lSource, MPI_ANY_TAG, MPI_COMM_WORLD, &lStatus);
				MPI_Recv(&lGeneration, 1, MPI_INT, lSource, MPI_ANY_TAG, MPI_COMM_WORLD, &lStatus);
				ioContext.setGeneration(lGeneration);
				Beagle_LogTraceM(
								   ioContext.getSystem().getLogger(),
								   "evaluation", "Beagle::MPIEvaluationOp",
								   std::string("Evaluating individual send from process ") + int2str(lSource)
								   );
				
				//Parse the received individual
				std::istringstream lStreamIn(lMessage);

				PACC::XML::Document lXMLParser;
				lXMLParser.parse(lStreamIn);

				PACC::XML::ConstIterator lIndividualRootNode = lXMLParser.getFirstRoot(); 

				
				//Read the received individual
				ioContext.getDeme().resize(0);
				Individual::Handle lIndividual = castHandleT<Individual>(ioContext.getDeme().getTypeAlloc()->allocate());
				lIndividual->readWithContext(lIndividualRootNode,ioContext);
				ioContext.setIndividualHandle(lIndividual);
				ioContext.setIndividualIndex(0);
				
//				Beagle_LogDebugM(
//								 ioContext.getSystem().getLogger(),
//								 "evaluation", "Beagle::MPIEvaluationOp",
//								 std::string("Individual received: ") + lIndividual->serialize()
//								 );
				
				//Free message string
				delete [] lMessage;
				
				//Evaluated the fitness of the received individual
				Fitness::Handle lFitness = evaluate(*lIndividual, ioContext);
			
				//Send back the fitness
				std::ostringstream lStreamOut;
				PACC::XML::Streamer lXMLStream(lStreamOut);

				lFitness->write(lXMLStream);
				//std::cout << "Sending fitness of size " << lStreamOut.str().size()+1 << ":" << std::endl << lStreamOut.str() << std::endl;
				lMessageSize = lStreamOut.str().size()+1;
				
				Beagle_LogTraceM(
									ioContext.getSystem().getLogger(),
									"evaluation", "Beagle::MPIEvaluationOp",
									std::string("Sending back fitness")
									);
				
				MPI_Send(&lMessageSize, 1, MPI_INT, lSource, eMessageSize, MPI_COMM_WORLD);
				MPI_Send(const_cast<char*>(lStreamOut.str().data()), lMessageSize, MPI_CHAR, lSource, eFitness, MPI_COMM_WORLD);
			}
		}
	} catch(Exception& inException) {
		std::cerr << "Exception catched in evaluator:" << std::endl << std::flush;
		std::cerr << inException.what() << std::endl << std::flush;
		exit(1);
	}
	catch(std::exception& inException) {
		std::cerr << "Standard exception catched in evaluator:" << std::endl << std::flush;
		std::cerr << inException.what() << std::endl << std::flush;
		exit(1);
	}
}