Beispiel #1
0
int main(int argc,char **argv)
{
  PetscErrorCode ierr;           /* used to check for functions returning nonzeros */
  Vec            x, f;               /* solution, function */
  Tao            tao;                /* Tao solver context */
  AppCtx         user;               /* user-defined work context */

   /* Initialize TAO and PETSc */
  PetscInitialize(&argc,&argv,(char *)0,help);

  MPI_Comm_size(MPI_COMM_WORLD,&user.size);
  MPI_Comm_rank(MPI_COMM_WORLD,&user.rank);
  ierr = InitializeData(&user);CHKERRQ(ierr);

  /* Run optimization on rank 0 */
  if (user.rank == 0) {
    /* Allocate vectors */
    ierr = VecCreateSeq(PETSC_COMM_SELF,NPARAMETERS,&x);CHKERRQ(ierr);
    ierr = VecCreateSeq(PETSC_COMM_SELF,NOBSERVATIONS,&f);CHKERRQ(ierr);

    /* TAO code begins here */

    /* Create TAO solver and set desired solution method */
    ierr = TaoCreate(PETSC_COMM_SELF,&tao);CHKERRQ(ierr);
    ierr = TaoSetType(tao,TAOPOUNDERS);CHKERRQ(ierr);

    /* Set the function and Jacobian routines. */
    ierr = FormStartingPoint(x);CHKERRQ(ierr);
    ierr = TaoSetInitialVector(tao,x);CHKERRQ(ierr);
    ierr = TaoSetSeparableObjectiveRoutine(tao,f,EvaluateFunction,(void*)&user);CHKERRQ(ierr);

    /* Check for any TAO command line arguments */
    ierr = TaoSetFromOptions(tao);CHKERRQ(ierr);

    /* Perform the Solve */
    ierr = TaoSolve(tao);CHKERRQ(ierr);

    /* Free TAO data structures */
    ierr = TaoDestroy(&tao);CHKERRQ(ierr);

    /* Free PETSc data structures */
    ierr = VecDestroy(&x);CHKERRQ(ierr);
    ierr = VecDestroy(&f);CHKERRQ(ierr);
    StopWorkers(&user);
  } else {
    TaskWorker(&user);
  }
  PetscFinalize();
  return 0;
}
void Puzzle::WorkerGenerator()
{
	m_generation = 0;
	CreatePopulation(m_populationSize);

	m_generation = 1;

	std::ofstream file;
	file.open(m_saveFileName);

	while(true)
	{
		std::cout << "Starting Generation " << m_generation << std::endl;
		try
		{
			//Check to see if the main thread has signled us to stop
			boost::this_thread::interruption_point();
		}
		catch(boost::thread_interrupted&)
		{
			//If we have been signled to stop then shutdown all worker threads and wait
			StopWorkers();
			file.close();
			return;
		}

		boost::this_thread::disable_interruption disableInterruption;

		int lastSize = m_polulation->size();

		while (m_polulation->size() >= 2)
		{
			Creature* c1;
			Creature* c2;

			{
				boost::unique_lock<boost::recursive_mutex> lock(m_populationLock);

				unsigned int creature1Index = 0, creature2Index = 0;

				SelectNextParents(creature1Index, creature2Index);

				c1 = (*m_polulation)[creature1Index];
				c2 = (*m_polulation)[creature2Index];

				if (c1 == c2)
					break;

				m_polulation->erase(m_polulation->begin() + creature1Index);
				if (creature1Index < creature2Index)
					creature2Index -= 1;
				m_polulation->erase(m_polulation->begin() + creature2Index);
			}


			{
				boost::unique_lock<boost::mutex> pairLock(m_pairsAccess);
				boost::unique_lock<boost::recursive_mutex> jobCountLock(m_jobCountAccess);

				m_pairs.push(std::pair<Creature*, Creature*>(c1, c2));
				m_jobCount++;
			}

			m_workerTrigger.notify_one();
		}

		while(m_jobCount != 0)
		{
			boost::this_thread::yield();
		}

		SwapPopulations();
#ifdef USE_ELITE
		Elite();
#elif !USE_BASELINE
		Cull();
#endif

		if(m_generation % 5 == 0)
		{
			int half = m_polulation->size() / 2;
			auto& popRef = *m_polulation;

			Creature* best = popRef[0];
			Creature* worst = popRef[popRef.size() - 1];
			Creature* med = popRef[half];

			file << m_generation << "," << best->GetScore() << "," << worst->GetScore() << "," << med->GetScore() << std::endl;
		}

		m_generation++;

		boost::this_thread::restore_interruption enableInterruption(disableInterruption);
	}
}
FOOTPRINT_LIST_IMPL::~FOOTPRINT_LIST_IMPL()
{
    StopWorkers();
}