Esempio n. 1
0
/*! \brief Prints the Kernel functions of this Program. */
void ocl::Program::print(std::ostream& out) const
{
#if 1
	//	out << "Printing program:" << std::endl;
	for(const auto &k : _kernels)
	{
		const ocl::Kernel &kernel = *k;
		out << kernel.toString() << std::endl;
	}
	out << std::endl;
#else
	checkConstraints();

	auto it = commonCodeBlocks_.begin();

	for ( auto& k : _kernels )
	{
		assert( k != nullptr );
		assert( it != commonCodeBlocks_.end() );

		out << *it++;
		out << k->toString();
		out << '\n';
	}

	if ( it != commonCodeBlocks_.end() )
	{
		out << *it++;
	}
#endif
}
Esempio n. 2
0
/*! \brief Instantiates this Program for a given Context and CompileOption.
	*
	* This Program is not yet created, only initialized with the given Context and CompileOptions.
	* In order to create it, load Kernel objects into this Program and call the appropriate create function.
	* It is assumed that the Kernel functions are not templated. Thus no Types must be provided.
	*
	* \param context The Context for which this Program will be created.
	* \param options defines a valid CompileOption for build process.
*/
ocl::Program::Program(ocl::Context& ctxt, const ocl::CompileOption &options) :
	_id(NULL), _context(&ctxt), _kernels(), _types(), _options(options), commonCodeBlocks_( 1u, std::string() )
{
	_context->insert(this);

	checkConstraints();
}
Esempio n. 3
0
//_________________________________________________________________________________________________
void Opnd::assignMemLocation(MemOpndKind k, Opnd * _base, Opnd * _index, Opnd * _scale, Opnd * _displacement)
{
    assert(_base||_index||_displacement);
    assert(!_scale||_index);

    constraints[ConstraintKind_Location]=Constraint(OpndKind_Mem, constraints[ConstraintKind_Initial].getSize());
    memOpndKind=k;

    checkConstraints();

    if (_base) 
        setMemOpndSubOpnd(MemOpndSubOpndKind_Base, _base);
    else 
        memOpndSubOpnds[MemOpndSubOpndKind_Base]=NULL;
    if (_index) 
        setMemOpndSubOpnd(MemOpndSubOpndKind_Index, _index);
    else 
        memOpndSubOpnds[MemOpndSubOpndKind_Index]=NULL;
    if (_scale) 
        setMemOpndSubOpnd(MemOpndSubOpndKind_Scale, _scale);
    else 
        memOpndSubOpnds[MemOpndSubOpndKind_Scale]=NULL;
    if (_displacement) 
        setMemOpndSubOpnd(MemOpndSubOpndKind_Displacement, _displacement);
    else 
        memOpndSubOpnds[MemOpndSubOpndKind_Displacement]=NULL;

}
Esempio n. 4
0
bool MeshTopologyTests::testConstraintRelaxation()
{
  bool success = true;

  // tests to confirm that constraints are appropriately relaxed when refinements render neighbors compatible.

  // make two simple meshes
  MeshTopologyPtr mesh2D = makeRectMesh(0.0, 0.0, 2.0, 1.0,
                                        2, 1); // 2 initial elements
  MeshTopologyPtr mesh3D = makeHexMesh(0.0, 0.0, 0.0, 2.0, 4.0, 3.0,
                                       2, 2, 1); // 4 initial elements

  mesh2D->refineCell(0, RefinementPattern::regularRefinementPatternQuad(), mesh2D->cellCount());
  mesh2D->refineCell(1, RefinementPattern::regularRefinementPatternQuad(), mesh2D->cellCount());

  mesh3D->refineCell(0, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());
  mesh3D->refineCell(1, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());
  mesh3D->refineCell(2, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());
  mesh3D->refineCell(3, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());

  // empty containers:
  map<unsigned,pair<IndexType,unsigned> > expectedEdgeConstraints2D;
  map<unsigned,pair<IndexType,unsigned> > expectedFaceConstraints3D;
  map<unsigned,pair<IndexType,unsigned> > expectedEdgeConstraints3D;

  int edgeDim = 1, faceDim = 2;

  if (! checkConstraints(mesh2D, edgeDim, expectedEdgeConstraints2D, "compatible 2D mesh") )
  {
    cout << "Failed edge constraint check for compatible 2D mesh." << endl;
    success = false;
  }

  if (! checkConstraints(mesh3D, edgeDim, expectedEdgeConstraints3D, "compatible 3D mesh") )
  {
    cout << "Failed edge constraint check for compatible 3D mesh." << endl;
    success = false;
  }

  if (! checkConstraints(mesh3D, faceDim, expectedFaceConstraints3D, "compatible 3D mesh") )
  {
    cout << "Failed face constraint check for compatible 3D mesh." << endl;
    success = false;
  }

  return success;
}
Esempio n. 5
0
/*! \brief Instantiates this Program for a given Context, predefined Types and CompileOption.
	*
	* This Program is not yet created, only initialized with the given Context, Types and CompileOptions.
	* In order to create it, load Kernel objects into this Program and call the appropriate create function.
	* Templated Kernel functions are then build for the given Types with the CompileOption.
	* The Kernel function name will be changed to kernel_<type>.
	*
	* \param context The Context for which this Program will be created.
	* \param types Types which consist of valid Type objects.
	* \param options defines a valid CompileOption for build process.
*/
ocl::Program::Program(ocl::Context& ctxt, const utl::Types &types, const ocl::CompileOption &options) :
	_id(NULL), _context(&ctxt), _kernels(), _types(types), _options(options), commonCodeBlocks_( 1u, std::string() )
{
	if(_types.empty()) throw std::runtime_error( "no types selected.");
	_context->insert(this);

	checkConstraints();
}
Esempio n. 6
0
/*! \brief Removes all kernels created with this Program.
  *
  * Kernel objects are also deleted and not accessible any more from elsewhere.
*/
void ocl::Program::removeKernels()
{
	/*while(!_kernels.empty()){
		delete  _kernels.begin()->second;
		_kernels.erase( _kernels.begin());
	}*/
	_kernels.clear();

	commonCodeBlocks_.resize( 1u );
	commonCodeBlocks_[0].clear();

	checkConstraints();
}
Esempio n. 7
0
/*! \brief Destroys the Kernel specified by its function name. */
void ocl::Program::deleteKernel(const std::string &name)
{
	iterator it = std::find_if( _kernels.begin(), _kernels.end(), [&name]( std::unique_ptr< Kernel > const& k ){
			return k->name() == name;
});

	if(it == _kernels.end()) throw std::runtime_error( "Kernel " + name + " does not exist yet");
	/*const Kernel *__k = it->second;
	delete __k;*/
	_kernels.erase(it);

	checkConstraints();
}
Esempio n. 8
0
SudokuFile& SudokuBoardGenerator::generateBoard(int N, int p, int q, int numAssignments, long timeout) {
    //given a SudokuFile with N, P, Q, creates a board with the given params
    //and assigns it to the board of the SudokuFile.
    //timeout represents the time in ms allowed to created the SudokuFile
    SudokuFile sf = SudokuFile(N, p, q);
    std::vector<std::vector<int>> tempBoard = sf.getBoard();
    if (numAssignments > sf.getN()*sf.getN())
    {
        std::cout << "Number of assignments exceeds available spaces in board. Returning SudokuFile with an empty board" << std::endl;
        return sf;
    }
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis1(0, sf.getN() - 1);
    std::uniform_int_distribution<> dis2(1, sf.getN());

    auto startTime = std::chrono::high_resolution_clock::now();

    for (int i = 0; i < numAssignments; i++)
    {
        int randomRow = dis1(gen); //0 to N-1
        int randomColumn = dis1(gen); //0 to N-1
        int randomAssignment = dis2(gen); //1 to N
        if (tempBoard[randomRow][randomColumn] == 0 && checkConstraints(randomRow, randomColumn, randomAssignment, sf, tempBoard))
        {
            tempBoard[randomRow][randomColumn] = randomAssignment;
        }
        else
        {
            i--;

            auto currentTime = std::chrono::high_resolution_clock::now();
            if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() > timeout)
            {
                std::cout << "Timeout at " << i << " elements" << std::endl;
                std::vector<std::vector<int>> tempboard;
                break;
            }
        }
    }
    sf.setBoard(tempBoard);
    retBoard = tempBoard;
    return sf;
}
Esempio n. 9
0
void SudokuBoardSolver::makeConstraintMap(SudokuFile &sF)
{
	std::vector<std::vector<int>> board = sF.getBoard();
	std::map<std::tuple<int, int>, std::vector<int>> constraint;
	for (int row = 0; row < sF.getN(); row++)
	{
		for (int col = 0; col < sF.getN(); col++)
		{
			for (int k = 1; k < sF.getN() + 1; k++)
			{
				if (board[row][col] == 0 && checkConstraints(row, col, k, sF, board))
				{
					constraint[std::make_tuple(row, col)].push_back(k);
				}
			}
		}
	}
	constraintBoard = constraint;
}
Esempio n. 10
0
  void DDLIndexPopulator::addColumnData(const execplan::ColumnResult* cr, const erydbSystemCatalog::ColType colType, int added)
  {
    WriteEngine::IdxTupleList  tupleList;
    WriteEngine::IdxTuple tuple; 

    for(int i=0;i < cr->dataCount(); ++i)
    {	
	
	WriteEngine::IdxTuple tuple ; 
	convertColData( cr, i,  colType, tuple);

	if (checkConstraints( tuple, colType, i, added))
	{
	    tupleList.push_back(tuple);
	    if (! added )
	      fRidList.push_back(cr->GetRid(i));
	}
	else
	  break;
    }
    if (tupleList.size())
    	fIdxValueList.push_back(tupleList);
  }
Esempio n. 11
0
int mutation(ga * ga){
	

  	int i, j, mutAlloc, oldPeriod, oldSlice, newAllocationIndex;
	machine * indiv;
	vcore * vc;
	int oldPcoreIndex;
	pcore * oldPcore;
	pcore * newPcore;

	/*for (i = 0; i < POPULATION; i++) {
		for(j =0; j< VCORES; j++){
			printf("machine %d, vcore %d\n",i,ga->population[i].vm->vcores[j].id);
		} // end for

	}*/ // end for

	int satisfies, counter;
	//printf("IN MUTATION\n");
	for (i = 0; i < POPULATION; ++i) {
	  // satisfies = -1;
	 
	  indiv = &ga->population[i];
	 
	    /*	  oldTDF = indiv->vm->tdf;
		  if(rand() > MUTRATE){
		  newTdf = rand() % (ga->aConfig->maxTdf - ga->aConfig->minTdf) + ga->aConfig->minTdf;
		  //newTdf = indiv->vm->tdf + (rand()%2 -1);
		  if(newTdf>= MINTDF && newTdf<= MAXTDF)
		  indiv->vm->tdf = newTdf;
		  }
	    */
	    for (j = 0; j < VCORES; j++) {
	      satisfies = -1;
	      counter = 0;
	      mutAlloc = -1;
	      while (satisfies == -1 && counter < 100) {
                
                mutAlloc = -1;
		//	printf("inside mutation while, individual %d, vcore %d\n", i, j);
	     
		vc = &indiv->vm->vcores[j];
		oldPeriod = vc->period;
		oldSlice = vc->slice;
		
		//printf("IN MUTATION WHILE LOOP\n");
		if(rand() > MUTRATE){
		  vc->period =
		    rand() % (ga->aConfig->maxPeriod - ga->aConfig->minPeriod) + ga->aConfig->minPeriod;
		  //newPeriod = vc->period + (rand() % 2 - 1);
		  //if(newPeriod>= MINPERIOD && newPeriod<= MAXPERIOD) 
		  //vc->period = newPeriod;

		}
		if(rand() > MUTRATE){
		  vc->slice =
		    rand() % (ga->aConfig->maxSlice - ga->aConfig->minSlice) + ga->aConfig->minSlice;
		  //		  newSlice = vc->slice + (rand() % 2 - 1);
		  //if(newSlice>= MINSLICE && newSlice<= MAXSLICE)
		  //vc->slice = newSlice;
		}
		
		oldPcoreIndex = vc->pcore->id;
	        oldPcore = &indiv->pcores[oldPcoreIndex];
		
		if(rand() > MUTRATE){
		  
		  // We must add vc to the list of vcores of vc->pcore
		  // and remove it from the list of vcores of the old pcore and decrement the nrVcores in that pcore
		  mutAlloc = 0;
		  removeVcoreFromPcore(oldPcore, vc->id);   
		  newAllocationIndex = (rand() % indiv->nrPcores);
		  vc->pcore = &indiv->pcores[newAllocationIndex];
		  
		  vc->pcore->vcores[vc->pcore->nrVCores] = *vc;
		  indiv->pcores[newAllocationIndex].nrVCores++;
		  vc->pcore->utilization = calculateUtilization(vc->pcore);
                  newPcore = &indiv->pcores[newAllocationIndex];
		  
		} //end if
		satisfies = checkConstraints(vc->pcore, ga->population[i].vm);
		if (satisfies == -1){
		  vc->period = oldPeriod;
		  vc->slice = oldSlice;
		  if (mutAlloc == 0) {
         	    vc->pcore = oldPcore;	
		    oldPcore->vcores[oldPcore->nrVCores] = *vc;
		    oldPcore->nrVCores++;
		    newPcore->nrVCores--;
		    newPcore->utilization = calculateUtilization(newPcore);
		    oldPcore->utilization = calculateUtilization(oldPcore);
		  } // end if
		
		} // end if
		counter++;
	      } //end while
	    } //end for vcores
	} // end for population
	
	return 0;
}
bool PartResizerWidget::movePartition(qint64 newFirstSector)
{
    const qint64 originalLength = partition().length();
    const bool isLengthAligned = PartitionAlignment::isLengthAligned(device(), partition());

    if (maximumFirstSector(align()) > -1 && newFirstSector > maximumFirstSector(align()))
        newFirstSector = maximumFirstSector(align());

    if (minimumFirstSector(align()) > 0 && newFirstSector < minimumFirstSector(align()))
        newFirstSector = minimumFirstSector(align());

    if (align())
        newFirstSector = PartitionAlignment::alignedFirstSector(device(), partition(), newFirstSector, minimumFirstSector(align()), maximumFirstSector(align()), -1, -1);

    qint64 delta = newFirstSector - partition().firstSector();

    if (delta == 0)
        return false;

    qint64 newLastSector = partition().lastSector() + delta;

    if (minimumLastSector(align()) > -1 && newLastSector < minimumLastSector(align()))
    {
        const qint64 deltaLast = minimumLastSector(align()) - newLastSector;
        newFirstSector += deltaLast;
        newLastSector += deltaLast;
    }

    if (maximumLastSector(align()) > 0 && newLastSector > maximumLastSector(align()))
    {
        const qint64 deltaLast = newLastSector - maximumLastSector(align());
        newFirstSector -= deltaLast;
        newLastSector -= deltaLast;
    }

    if (align())
        newLastSector = PartitionAlignment::alignedLastSector(device(), partition(), newLastSector, minimumLastSector(align()), maximumLastSector(align()), -1, -1, originalLength, isLengthAligned);

    if (newLastSector == partition().lastSector())
        return false;

    if (isLengthAligned && newLastSector - newFirstSector + 1 != partition().length())
    {
        qDebug() << "length changes while trying to move partition " << partition().deviceNode() << ". new first: " << newFirstSector << ", new last: " << newLastSector << ", old length: " << partition().length() << ", new length: " << newLastSector - newFirstSector + 1;
        return false;
    }

    if (!checkConstraints(newFirstSector, newLastSector))
    {
        qDebug() << "constraints not satisfied while trying to move partition " << partition().deviceNode() << ". new first: " << newFirstSector << ", new last: " << newLastSector;
        return false;
    }

    if (align() && !PartitionAlignment::isAligned(device(), partition(), newFirstSector, newLastSector, true))
    {
        qDebug() << "partition " << partition().deviceNode() << " not aligned but supposed to be. new first: " << newFirstSector << " delta: " << PartitionAlignment::firstDelta(device(), partition(), newFirstSector) << ", new last: " << newLastSector << ", delta: " << PartitionAlignment::lastDelta(device(), partition(), newLastSector);
        return false;
    }

    if (partition().children().size() > 0 &&
            (!checkAlignment(*partition().children().first(), partition().firstSector() - newFirstSector) ||
             !checkAlignment(*partition().children().last(), partition().lastSector() - newLastSector)))
    {
        qDebug() << "cannot align children while trying to move partition " << partition().deviceNode();
        return false;
    }

    partition().setFirstSector(newFirstSector);
    partition().fileSystem().setFirstSector(newFirstSector);

    partition().setLastSector(newLastSector);
    partition().fileSystem().setLastSector(newLastSector);

    updatePositions();

    emit firstSectorChanged(partition().firstSector());
    emit lastSectorChanged(partition().lastSector());

    return true;
}
Esempio n. 13
0
//_________________________________________________________________________________________________
void Opnd::setCalculatedConstraint(Constraint c)
{
    constraints[ConstraintKind_Calculated]=c & constraints[ConstraintKind_Initial];
    checkConstraints();
    assert(!constraints[ConstraintKind_Calculated].isNull());
}
Esempio n. 14
0
int SudokuBoardSolver::backtrackingSearch(SudokuFile &sF, long timeout)
{
	if (countOnce == 0)
	{
		startTime = std::chrono::system_clock::now().time_since_epoch();

	}

	countOnce++;

	bool timedOut = false;

	if (isComplete(sF))
	{
		sf = sF;
		return 1;
	}
	else
	{
		std::vector<std::vector<int>> board = sF.getBoard();

		for (int i = 0; i < sF.getN(); i++)
		{

			for (int j = 0; j < sF.getN(); j++)
			{

				for (int k = 1; k < sF.getN() + 1; k++)
				{

					if (board[i][j] == 0)
					{
		
						if (checkConstraints(i, j, k, sF, board))
						{
		
							board[i][j] = k;
							++countNode;
							sF.setBoard(board);
				

							if (backtrackingSearch(sF, timeout) == 1)
							{
								++countNode;
								return 1;
							}
							else
							{
								auto currentTime = std::chrono::system_clock::now().time_since_epoch();
								if (std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count() >= timeout)
								{
									++countNode;
									return 3;
								}

								else
								{
									board[i][j] = 0;
									sF.setBoard(board);
		
									if (k == sF.getN())
									{
										++backtrack;
										return 2;
									}
								}
							}
						}
						else if (k == sF.getN())
						{
							++backtrack;
							return 2;
						}

						auto currentTime = std::chrono::system_clock::now().time_since_epoch();
						if (std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count() >= timeout)
						{
							++countNode;
							return 3;
						}
					}
				}
			}
		}
	}

	return 2;

}
Esempio n. 15
0
/*! \brief Reads kernel functions from a string into this Program.
  *
  * The string object can contain multiple OpenCL kernel
  * functions. It deletes all comments and if the kernels
  * are templated, it substitutes the template parameter
  * with the provided types. For each Kernel function
  * a Kernel object is built and stored within a map.
  * The map stores the name of the kernel function and
  * the corresponding function.
  * Note that DEFINES are not supported yet.
*/
ocl::Program& ocl::Program::operator << (const std::string &k)
{
	//   std::cout << "kernels: " << _kernels.size() << " blocks: " << commonCodeBlocks_.size() << std::endl;
	//   if ( _kernels.empty() )
	//   {
	//     commonCodeBlocks_.resize( 0 );
	//   }

	std::string kernels = k;

	//#if 0 // For now disable comment removal as we need it to play a trick on the AMD compiler.
	eraseComments(kernels);
	//#endif

	size_t pos = 0;
	while(pos < kernels.npos){
		const std::string next = nextKernel(kernels, pos);

		if(next.empty()) {
			commonCodeBlocks_.push_back( kernels.substr( pos ) );
			break;
		}
		pos += next.length() + commonCodeBlocks_.back().length();


//		std::cout << "COMMON CODE: " << commonCodeBlocks_.back();
//		std::cout << "KERNEL CODE: " << next << std::endl;

		if(_types.empty() || !ocl::Kernel::templated(next)){
			std::unique_ptr< ocl::Kernel > kernel( new ocl::Kernel(*this, next) );
			if(this->isBuilt()) kernel->create();

			auto t = std::find_if( _kernels.begin(), _kernels.end(), [&kernel]( std::unique_ptr< Kernel > const& k ){ return kernel->name() == k->name(); } );

			if ( t != _kernels.end() ) {
				t->swap( kernel );
			} else {
				_kernels.push_back( std::move( kernel ) );
			}

			continue;
		}


		for(utl::Types::const_iterator it = _types.begin(); it != _types.end(); ++it)
		{
			const utl::Type& type = **it;
			std::unique_ptr< ocl::Kernel > kernel( new ocl::Kernel(*this, next, type) );
			if(this->isBuilt()) {
				kernel->create();
			}
			Kernels::iterator t = std::find_if( _kernels.begin(), _kernels.end(), [&kernel]( std::unique_ptr< Kernel > const& k ){ return kernel->name() == k->name(); 		} );

			if ( t != _kernels.end() ) {
				t->swap( kernel );
			} else {
				_kernels.push_back( std::move( kernel ) );
			}

			// "Invent" common code blocks for kernels being generated by the type system.
			if ( /*std::distance(it, _types.begin()) > 1*/ it != _types.begin() ) {
				commonCodeBlocks_.push_back("\n");
			}
		}
	}


	commonCodeBlocks_.push_back( "" );

	//	std::cout << "Printing Program in StreamOperator:" << std::endl;
	//	this->print();
	//	std::cout << "-----------------------------------" << std::endl;


	checkConstraints();

	return *this;
}
Esempio n. 16
0
void DietWizardDialog::createDiet( void )
{
	KApplication::setOverrideCursor( Qt::WaitCursor );

	START_TIMER("Creating the diet");

	RecipeList rlist;
	dietRList->clear();

	// Get the whole list of recipes, detailed
	int flags = RecipeDB::Title | getNecessaryFlags();
	database->loadRecipes( &rlist, flags );

	// temporal iterator list so elements can be removed without reloading them again from the DB
	// this list prevents the same meal from showing up in the same day twice
	Q3ValueList <RecipeList::Iterator> tempRList;

	bool alert = false;

	for ( int day = 0;day < dayNumber;day++ )  // Create the diet for the number of days defined by the user
	{
		populateIteratorList( rlist, &tempRList ); // temporal iterator list so elements can be removed without reloading them again from the DB
		for ( int meal = 0;meal < mealNumber;meal++ )
		{
			int dishNo = ( ( MealInput* ) ( mealTabs->widget( meal ) ) ) ->dishNo();

			for ( int dish = 0;dish < dishNo;dish++ ) {
				bool found = false;
				Q3ValueList <RecipeList::Iterator> tempDishRList = tempRList;
				while ( ( !found ) && !tempDishRList.empty() ) {
					int random_index = ( int ) ( ( float ) ( KRandom::random() ) / ( float ) RAND_MAX * tempDishRList.count() );
					Q3ValueList<RecipeList::Iterator>::Iterator iit = tempDishRList.at( random_index ); // note that at() retrieves an iterator to the iterator list, so we need to use * in order to get the RecipeList::Iterator

					RecipeList::Iterator rit = *iit;
					if ( found = ( ( ( !categoryFiltering( meal, dish ) ) || checkCategories( *rit, meal, dish ) ) && checkConstraints( *rit, meal, dish ) ) )  // Check that the recipe is inside the constraint limits and in the categories specified
					{
						dietRList->append( *rit ); // Add recipe to the diet list
						tempRList.remove( tempRList.find(*iit) ); //can't just remove()... the iterator isn't from this list (its an iterator from tempDishRList)
					}
					else {
						tempDishRList.remove( iit ); // Remove this analized recipe from teh list
					}
				}
				if ( !found )
					alert = true;
			}
		}
	}

	if ( alert ) {
		KApplication::restoreOverrideCursor();
		KMessageBox::sorry( this, i18nc( "@info", "Given the constraints, a full diet list could not be constructed. Either the recipe list is too short or the constraints are too demanding. " ) );
	}

	else // show the resulting diet
	{

		// make a list of dishnumbers
		QList<int> dishNumbers;

		for ( int meal = 0;meal < mealNumber;meal++ ) {
			int dishNo = ( ( MealInput* ) ( mealTabs->widget( meal ) ) ) ->dishNo();
			dishNumbers << dishNo;
		}

		KApplication::restoreOverrideCursor();

		// display the list
		QPointer<DietViewDialog> dietDisplay = new DietViewDialog( this, *dietRList, dayNumber, mealNumber, dishNumbers );
		connect( dietDisplay, SIGNAL( signalOk() ), this, SLOT( createShoppingList() ) );
		dietDisplay->exec();
		delete dietDisplay;
	}

	END_TIMER();
}
Esempio n. 17
0
/*! \brief Instantiates this Program for a given Context and CompileOption.
	*
	* This Program is not yet created or initialized with the necessary Context.
	* Do not forget to provide at least a valid Context, Kernel objects or
	* functions and to build it.
*/
ocl::Program::Program() :
	_id(NULL), _context(), _kernels(), _types(), _options(), commonCodeBlocks_( 1u, std::string() )
{
	checkConstraints();
}
Esempio n. 18
0
int initGA(struct ga * ga){

  vcore * vc;
  int index = -1;

 // printf("Initializing general parameters\n");

  ga-> aConfig  =  (struct allocatorConfig *) malloc(sizeof(struct allocatorConfig));
 
  // CPU Speeds in Khz
  ga->aConfig->maxCPU = MAXCPU;   // Khz
  ga->aConfig->minCPU = MINCPU ;

  // Percentage of utilization
  ga->aConfig->maxU = MAXU;
  ga->aConfig->minU = MINU;

  // Time dilation Factor
  ga->aConfig->maxTdf = MAXTDF;
  ga->aConfig->minTdf = MINTDF;
  ga->populationSize = POPULATION;
  // Slices and periods in micro seconds
  
  ga->aConfig->maxPeriod = MAXPERIOD;
  ga->aConfig->minPeriod = MINPERIOD;
  ga->aConfig->maxSlice = MAXSLICE;
  ga->aConfig->minSlice = MINSLICE;
 
 
  ga->population = (machine *) malloc(ga->populationSize*sizeof(machine));

  // Configuration which is constant for each machine
  int nrPcores = PCORES;
  int nrVcores = VCORES;
  int i, j, k, satisfies;

  //printf("Initializing before for\n");

  for (i = 0; i < ga->populationSize; i++) {

 //   
    ga->population[i].id = i;
//printf("next machine %d\n", ga->population[i].id);
    pcore * pcores =  (pcore *) malloc(nrPcores*sizeof(pcore));
    vm * vm = (struct vm *) malloc(sizeof(struct vm));
    
    //printf("Initializing physical cores\n");
    
    for (j = 0; j < nrPcores; j++) {
      pcores[j].id = j;
      pcores[j].vcores = (vcore *) malloc(nrVcores*sizeof(vcore));
      pcores[j].nrVCores = 0;
      
      pcores[j].maxUtilization = rand() % (ga->aConfig->maxU - ga->aConfig->minU) + ga->aConfig->minU;
   //   printf("max utilization for pcore %d: %f\n", j, pcores[j].maxUtilization);
      pcores[j].speedKhz = rand() % (ga->aConfig->maxCPU - ga->aConfig->minCPU) + ga->aConfig->minCPU;
      
    }
    
    vm->vcores = (struct vcore *) malloc(nrVcores*sizeof(struct vcore));
    
    for (j = 0;  j < nrVcores; j++) {
      vm->vcores[j].speedKhz =
	(rand() % (ga->aConfig->maxCPU - ga->aConfig->minCPU)) + ga->aConfig->minCPU;
      vm->vcores[j].id = j;
    }
    ga->population[i].pcores = pcores;
    ga->population[i].vm = vm;
    ga->population[i].nrPcores = nrPcores;
    ga->population[i].vm->nrVcores = vm->nrVcores;
    
        for (k = 0; k < nrVcores; k++) {
		vc = &ga->population[i].vm->vcores[k];

        	satisfies = -1;
		index = -1;
    		vc->speedKhz = vm->vcores[k].speedKhz;
		vc->id = vm->vcores[k].id;

        	while (satisfies == -1) {
                  
		 if(index!=-1) {
                  removeVcoreFromPcore(&ga->population[i].pcores[index], vc->id);
                 }
		  ga->population[i].vm->tdf =
		    rand() % (ga->aConfig->maxTdf - ga->aConfig->minTdf) + ga->aConfig->minTdf;
		  vc->slice =
		    rand() % (ga->aConfig->maxSlice - ga->aConfig->minSlice) + ga->aConfig->minSlice;
		  
		  vc->period =
		    rand() % (ga->aConfig->maxPeriod - ga->aConfig->minPeriod) + ga->aConfig->minPeriod;
		  index = rand() % nrPcores;
		  vc->pcore =
		    &ga->population[i].pcores[index];
		  ga->population[i].pcores[index].
		    vcores[ga->population[i].pcores[index].nrVCores] = *vc;
		++ga->population[i].pcores[index].nrVCores;
//		printf("for machine %d\n", ga->population[i].id);
//		printf("max utilization for pcore %d: %f\n", index, ga->population[i].pcores[index].maxUtilization);
//printf("max utilization for pcore %d: %f\n", index, vc->pcore->maxUtilization);
		  satisfies = checkConstraints(vc->pcore, ga->population[i].vm);
		} // end while
		
		ga->population[i].vm->vcores[k] = *vc;
				
		ga->population[i].pcores[index].utilization = calculateUtilization(&(ga->population[i].pcores[index]));
        }

	

    }
  
	
  return 0;
}
Esempio n. 19
0
int crossover(ga * ga){
	
	int i, indexP1, indexP2, pcoreId;
	int crossCounter = 0;
	int nrNewPop = -1;
	int satisfies = -1;
	machine * newPopulation = (machine*) malloc(POPULATION*sizeof(machine));
	machine * p1;
	machine * p2;
	machine * ch1;
	machine * ch2;

	while(nrNewPop < POPULATION - 1){

		indexP1 = rand() % ga->populationSize;
		indexP2 = rand() % ga->populationSize;

		// Parent pointers

		p1 = &ga->population[indexP1];
		p2 = &ga->population[indexP2];
               
		// Children pointers

	        ch1 = (machine *) malloc(sizeof(machine));
	        ch2 = (machine *) malloc(sizeof(machine));


		// Set the information of the physical cores
		// (It is constant for all machines)

		ch1->id = p1->id;		
		ch1->nrPcores = p1->nrPcores;
		ch1->fitness = 0.0;
		ch1->pcores = (pcore *) malloc(ch1->nrPcores * sizeof(pcore));
		ch1->vm = (vm *) malloc(sizeof(vm));
		ch1->vm->vcores = (vcore *) malloc(VCORES*sizeof(vcore));

		ch2->id = p2->id;
		ch2->nrPcores = p2->nrPcores;
		ch2->fitness = 0.0;
		ch2->pcores = (pcore *) malloc(ch2->nrPcores * sizeof(pcore));
		ch2->vm = (vm *) malloc(sizeof(vm));
		ch2->vm->vcores = (vcore *) malloc(VCORES*sizeof(vcore));
                
		for (i = 0; i < ch1->nrPcores; i++) {
		    ch1->pcores[i].id = i;
		    ch1->pcores[i].maxUtilization = p1->pcores[i].maxUtilization;
		    ch1->pcores[i].speedKhz = p1->pcores[i].speedKhz;
		    ch1->pcores[i].utilization = 0; // Should be recalculated later
		    ch1->pcores[i].nrVCores = 0;
		    
		    ch2->pcores[i].id = i;
		    ch2->pcores[i].maxUtilization = p2->pcores[i].maxUtilization;
		    ch2->pcores[i].speedKhz = p2->pcores[i].speedKhz;
		    ch2->pcores[i].utilization = 0; // Should be recalculated later
		    ch2->pcores[i].nrVCores = 0;
			

		}

			
		for(i=0;i<VCORES;i++){
			ch1->vm->vcores[i].pcore =  (pcore *) malloc( sizeof(pcore));
			ch2->vm->vcores[i].pcore =  (pcore *) malloc( sizeof(pcore));
		} // end for

		/*for(i=0;i<ch1->nrPcores;i++){
			ch1->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
			ch2->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
					
		} // end for*/


		while (satisfies < 0 && crossCounter <100 ) {

			//printf("Inside crossover while\n");
			for(i=0;i<ch1->nrPcores;i++){
				ch1->pcores[i].nrVCores = 0;
				ch2->pcores[i].nrVCores = 0;
				if(ch1->pcores[i].vcores)
					free(ch1->pcores[i].vcores);
				if(ch1->pcores[i].vcores)
					free(ch2->pcores[i].vcores);
				ch1->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
				ch2->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
					
			} // end for

			int x = rand() % VCORES;// Crossover point

			if(rand()>CROSSRATE && x > 0){

				//printf("***** Doing crossover *****\n");

				ch1->vm->tdf = p1->vm->tdf;
				ch2->vm->tdf = p2->vm->tdf;
				for (i = 0; i < x; i++) {
					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	
				
			
				} // end for
				for (i = x; i < VCORES; i++) {


					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
				
					vcoreCh1->id = p2->vm->vcores[i].id;
					vcoreCh1->period = p2->vm->vcores[i].period;
					vcoreCh1->slice = p2->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	

					vcoreCh2->id = p1->vm->vcores[i].id;
					vcoreCh2->period = p1->vm->vcores[i].period;
					vcoreCh2->slice = p1->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p1->vm->vcores[i].speedKhz;

					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	

		
				} // end for
			} // end if
			else{
				//printf("***** No crossover just copying to next generation *****\n");
				
				for (i = 0; i < VCORES; i++) {

					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;
				
					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	


				} // end for

				} // end else
			
			
			for (i = 0; i < VCORES; i++) {
				satisfies = 0;
			   	satisfies += checkConstraints(ch1->vm->vcores[i].pcore, ch1->vm);
			   	satisfies += checkConstraints(ch2->vm->vcores[i].pcore, ch2->vm);
			} // end for
		crossCounter++;
		
		}  // end while satisfies
		if (crossCounter >= 100) {
			for (i = 0; i < VCORES; i++) {

					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	


				}
		}

		newPopulation[++nrNewPop] = *ch1;
		newPopulation[++nrNewPop] = *ch2;
		satisfies = -1;
		crossCounter = 0;
 		
   
	} // end while population

        freePopulation(ga->population, ga->populationSize);
	ga->population = newPopulation;
	return 0;

} // end crossover function
Esempio n. 20
0
bool MeshTopologyTests::testEntityConstraints()
{
  bool success = true;

  // make two simple meshes
  MeshTopologyPtr mesh2D = makeRectMesh(0.0, 0.0, 2.0, 1.0,
                                        2, 1);
  MeshTopologyPtr mesh3D = makeHexMesh(0.0, 0.0, 0.0, 2.0, 4.0, 3.0,
                                       2, 2, 1);

  unsigned vertexDim = 0;
  unsigned edgeDim = 1;
  unsigned faceDim = 2;

  // first, check that unconstrained edges and faces are unconstrained

  set< unsigned > boundaryEdges;
  set< unsigned > internalEdges;

  for (unsigned cellIndex=0; cellIndex<mesh2D->cellCount(); cellIndex++)
  {
    CellPtr cell = mesh2D->getCell(cellIndex);
    unsigned sideCount = cell->getSideCount();

    for (unsigned sideOrdinal=0; sideOrdinal<sideCount; sideOrdinal++)
    {
      unsigned edgeIndex = cell->entityIndex(edgeDim, sideOrdinal);
      unsigned numCells = mesh2D->getActiveCellCount(edgeDim,edgeIndex);
      if (numCells == 1)   // boundary edge
      {
        boundaryEdges.insert(edgeIndex);
      }
      else if (numCells == 2)
      {
        internalEdges.insert(edgeIndex);
      }
      else
      {
        success = false;
        cout << "testEntityConstraints: In initial 2D mesh, edge " << edgeIndex << " has active cell count of " << numCells << ".\n";
      }
    }
  }
  if (internalEdges.size() != 1)
  {
    success = false;
    cout << "testEntityConstraints: In initial 2D mesh, there are " << internalEdges.size() << " internal edges (expected 1).\n";
  }
  for (set<unsigned>::iterator edgeIt=internalEdges.begin(); edgeIt != internalEdges.end(); edgeIt++)
  {
    unsigned edgeIndex = *edgeIt;
    unsigned constrainingEntityIndex = mesh2D->getConstrainingEntity(edgeDim,edgeIndex).first;
    if (constrainingEntityIndex != edgeIndex)
    {
      success = false;
      cout << "testEntityConstraints: In initial 2D mesh, internal edge is constrained by a different edge.\n";
    }
  }

  set<unsigned> boundaryFaces;
  set<unsigned> internalFaces;
  map<unsigned, vector<unsigned> > faceToEdges;
  for (unsigned cellIndex=0; cellIndex<mesh3D->cellCount(); cellIndex++)
  {
    CellPtr cell = mesh3D->getCell(cellIndex);
    unsigned sideCount = cell->getSideCount();

    for (unsigned sideOrdinal=0; sideOrdinal<sideCount; sideOrdinal++)
    {
      unsigned faceIndex = cell->entityIndex(faceDim, sideOrdinal);
      unsigned numCells = mesh3D->getActiveCellCount(faceDim,faceIndex);
      if (numCells == 1)   // boundary face
      {
        boundaryFaces.insert(faceIndex);
      }
      else if (numCells == 2)
      {
        internalFaces.insert(faceIndex);
      }
      else
      {
        success = false;
        cout << "testEntityConstraints: In initial 3D mesh, face " << faceIndex << " has active cell count of " << numCells << ".\n";
      }

      if (faceToEdges.find(faceIndex) == faceToEdges.end())
      {
        CellTopoPtr faceTopo = cell->topology()->getSubcell(faceDim, sideOrdinal);
        unsigned numEdges = faceTopo->getSubcellCount(edgeDim);
        vector<unsigned> edgeIndices(numEdges);
        for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
        {
          edgeIndices[edgeOrdinal] = mesh3D->getFaceEdgeIndex(faceIndex, edgeOrdinal);
        }
      }
    }
  }

  if (internalFaces.size() != 4)
  {
    success = false;
    cout << "testEntityConstraints: In initial 3D mesh, there are " << internalFaces.size() << " internal faces (expected 4).\n";
  }
  for (set<unsigned>::iterator faceIt=internalFaces.begin(); faceIt != internalFaces.end(); faceIt++)
  {
    unsigned faceIndex = *faceIt;
    unsigned constrainingEntityIndex = mesh3D->getConstrainingEntity(faceDim,faceIndex).first;
    if (constrainingEntityIndex != faceIndex)
    {
      success = false;
      cout << "testEntityConstraints: In initial 3D mesh, internal face is constrained by a different face.\n";
    }
  }

  // now, make a single refinement in each mesh:
  unsigned cellToRefine2D = 0, cellToRefine3D = 3;
  mesh2D->refineCell(cellToRefine2D, RefinementPattern::regularRefinementPatternQuad(), mesh2D->cellCount());
  mesh3D->refineCell(cellToRefine3D, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());

//  printMeshInfo(mesh2D);

  // figure out which faces/edges were refined and add the corresponding

  map<unsigned,pair<IndexType,unsigned> > expectedEdgeConstraints2D;
  set<unsigned> refinedEdges;
  for (set<unsigned>::iterator edgeIt=boundaryEdges.begin(); edgeIt != boundaryEdges.end(); edgeIt++)
  {
    set<unsigned> children = mesh2D->getChildEntitiesSet(edgeDim, *edgeIt);
    if (children.size() > 0)
    {
      refinedEdges.insert(*edgeIt);
      boundaryEdges.insert(children.begin(), children.end());
    }
  }
  for (set<unsigned>::iterator edgeIt=internalEdges.begin(); edgeIt != internalEdges.end(); edgeIt++)
  {
    set<unsigned> children = mesh2D->getChildEntitiesSet(edgeDim, *edgeIt);
    if (children.size() > 0)
    {
      refinedEdges.insert(*edgeIt);
      internalEdges.insert(children.begin(), children.end());
      for (set<unsigned>::iterator childIt = children.begin(); childIt != children.end(); childIt++)
      {
        unsigned childIndex = *childIt;
        expectedEdgeConstraints2D[childIndex] = make_pair(*edgeIt, edgeDim);
      }
    }
  }
  // 1 quad refined: expect 4 refined edges
  if (refinedEdges.size() != 4)
  {
    success = false;
    cout << "After initial refinement, 2D mesh has " << refinedEdges.size() << " refined edges (expected 4).\n";
  }
  checkConstraints(mesh2D, edgeDim, expectedEdgeConstraints2D);

  set<unsigned> refinedFaces;
  map<unsigned,pair<IndexType,unsigned> > expectedFaceConstraints3D;
  map<unsigned,pair<IndexType,unsigned> > expectedEdgeConstraints3D;

  for (set<unsigned>::iterator faceIt=boundaryFaces.begin(); faceIt != boundaryFaces.end(); faceIt++)
  {
    set<unsigned> children = mesh3D->getChildEntitiesSet(faceDim, *faceIt);
    if (children.size() > 0)
    {
      refinedFaces.insert(*faceIt);
      boundaryFaces.insert(children.begin(), children.end());
    }
  }

  for (set<unsigned>::iterator faceIt=internalFaces.begin(); faceIt != internalFaces.end(); faceIt++)
  {
    vector<unsigned> children = mesh3D->getChildEntities(faceDim, *faceIt);
    if (children.size() > 0)
    {
      refinedFaces.insert(*faceIt);
      internalFaces.insert(children.begin(), children.end());
      for (unsigned childOrdinal = 0; childOrdinal < children.size(); childOrdinal++)
      {
        unsigned childIndex = children[childOrdinal];
        expectedFaceConstraints3D[childIndex] = make_pair(*faceIt, faceDim);
        unsigned numEdges = 4;
        unsigned internalEdgeCount = 0; // for each child of a quad, we expect to have 2 internal edges
        for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
        {
          unsigned edgeIndex = mesh3D->getFaceEdgeIndex(childIndex, edgeOrdinal);
          unsigned activeCellCount = mesh3D->getActiveCellCount(edgeDim, edgeIndex);
          if (activeCellCount==2)
          {
            internalEdgeCount++;
            expectedEdgeConstraints3D[edgeIndex] = make_pair(*faceIt, faceDim);
          }
          else if (activeCellCount==1)     // hanging edge
          {
            if (! mesh3D->entityHasParent(edgeDim, edgeIndex))
            {
              cout << "Hanging edge with edgeIndex " << edgeIndex << " (in face " << childIndex << ") does not have a parent edge.\n";
              cout << "Edge vertices:\n";
              mesh3D->printEntityVertices(edgeDim, edgeIndex);
              cout << "Face vertices:\n";
              mesh3D->printEntityVertices(faceDim, childIndex);
              success = false;
            }
            else
            {
              unsigned edgeParentIndex = mesh3D->getEntityParent(edgeDim, edgeIndex);
              expectedEdgeConstraints3D[edgeIndex] = make_pair(edgeParentIndex, edgeDim);
            }
          }
          else
          {
            cout << "Unexpected number of active cells: " << activeCellCount << endl;
          }
        }
        if (internalEdgeCount != 2)
        {
          cout << "Expected internalEdgeCount to be 2; was " << internalEdgeCount << endl;
          success = false;
        }
      }
    }
  }
  // 1 hex refined: expect 6 refined faces
  if (refinedFaces.size() != 6)
  {
    success = false;
    cout << "After initial refinement, 3D mesh has " << refinedFaces.size() << " refined faces (expected 6).\n";
  }
  if (! checkConstraints(mesh3D, faceDim, expectedFaceConstraints3D, "refined 3D mesh") )
  {
    cout << "Failed face constraint check for refined 3D mesh." << endl;
    success = false;
  }
  if (! checkConstraints(mesh3D, edgeDim, expectedEdgeConstraints3D, "refined 3D mesh") )
  {
    cout << "Failed edge constraint check for refined 3D mesh." << endl;
    success = false;
  }

  // now, we refine one of the children of the refined cells in each mesh, to produce a 2-level constraint
  set<unsigned> edgeChildren2D;
  set<unsigned> cellsForEdgeChildren2D;
  for (map<unsigned,pair<IndexType,unsigned> >::iterator edgeConstraint=expectedEdgeConstraints2D.begin();
       edgeConstraint != expectedEdgeConstraints2D.end(); edgeConstraint++)
  {
    edgeChildren2D.insert(edgeConstraint->first);
    unsigned cellIndex = mesh2D->getActiveCellIndices(edgeDim, edgeConstraint->first).begin()->first;
    cellsForEdgeChildren2D.insert(cellIndex);
//    cout << "cellsForEdgeChildren2D: " << cellIndex << endl;
  }

  // one of these has (1,0) as one of its vertices.  Let's figure out which one:
  unsigned vertexIndex;
  if (! mesh2D->getVertexIndex(makeVertex(1, 0), vertexIndex) )
  {
    cout << "Error: vertex not found.\n";
    success = false;
  }

  vector< pair<unsigned,unsigned> > cellsForVertex = mesh2D->getActiveCellIndices(vertexDim, vertexIndex);
  if (cellsForVertex.size() != 2)
  {
    cout << "cellsForVertex should have 2 entries; has " << cellsForVertex.size() << endl;
    success = false;
  }
  unsigned childCellForVertex, childCellConstrainedEdge;
  set<unsigned> childNewlyConstrainingEdges; // the two interior edges that we break
  for (vector< pair<unsigned,unsigned> >::iterator cellIt=cellsForVertex.begin(); cellIt != cellsForVertex.end(); cellIt++)
  {
//    cout << "cellsForVertex: " << cellIt->first << endl;
    if ( cellsForEdgeChildren2D.find( cellIt->first ) != cellsForEdgeChildren2D.end() )
    {
      // found match
      childCellForVertex = cellIt->first;
      // now, figure out which of the "edgeChildren2D" is shared by this cell:
      CellPtr cell = mesh2D->getCell(childCellForVertex);
      unsigned numEdges = cell->getSideCount();
      for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
      {
        unsigned edgeIndex = cell->entityIndex(edgeDim, edgeOrdinal);
        if (edgeChildren2D.find(edgeIndex) != edgeChildren2D.end())
        {
          childCellConstrainedEdge = edgeIndex;
        }
        else if ( mesh2D->getActiveCellCount(edgeDim, edgeIndex) == 2 )
        {
          childNewlyConstrainingEdges.insert(edgeIndex);
        }
      }
    }
  }
  if (childNewlyConstrainingEdges.size() != 2)
  {
    cout << "Expected 2 newly constraining edges after 2nd refinement of 2D mesh, but found " << childNewlyConstrainingEdges.size() << endl;
    success = false;
  }

  // refine the cell that matches (1,0):
  mesh2D->refineCell(childCellForVertex, RefinementPattern::regularRefinementPatternQuad(), mesh2D->cellCount());

  // now, fix the expected edge constraints, then check them...
  set<unsigned> childEdges = mesh2D->getChildEntitiesSet(edgeDim, childCellConstrainedEdge);
  if (childEdges.size() != 2)
  {
    cout << "Expected 2 child edges, but found " << childEdges.size() << ".\n";
    success = false;
  }
  for (set<unsigned>::iterator edgeIt = childEdges.begin(); edgeIt != childEdges.end(); edgeIt++)
  {
    expectedEdgeConstraints2D[*edgeIt] = expectedEdgeConstraints2D[childCellConstrainedEdge];
  }
  expectedEdgeConstraints2D.erase(childCellConstrainedEdge);
  for (set<unsigned>::iterator edgeIt = childNewlyConstrainingEdges.begin(); edgeIt != childNewlyConstrainingEdges.end(); edgeIt++)
  {
    set<unsigned> newChildEdges = mesh2D->getChildEntitiesSet(edgeDim, *edgeIt);
    for (set<unsigned>::iterator newEdgeIt = newChildEdges.begin(); newEdgeIt != newChildEdges.end(); newEdgeIt++)
    {
      expectedEdgeConstraints2D[*newEdgeIt] = make_pair(*edgeIt,edgeDim);
    }
  }

  if (! checkConstraints(mesh2D, edgeDim, expectedEdgeConstraints2D, "twice-refined 2D mesh") )
  {
    cout << "Failed constraint check for twice-refined 2D mesh." << endl;
    success = false;
  }

  // now, do a second level of refinement for 3D mesh
  // one of these has (1,2,0) as one of its vertices.  Let's figure out which one:
  if (! mesh3D->getVertexIndex(makeVertex(1, 2, 0), vertexIndex) )
  {
    cout << "Error: vertex not found.\n";
    success = false;
  }

  cellsForVertex = mesh3D->getActiveCellIndices(vertexDim, vertexIndex);
  if (cellsForVertex.size() != 4)
  {
    cout << "cellsForVertex should have 4 entries; has " << cellsForVertex.size() << endl;
    success = false;
  }

  vector<unsigned> justCellsForVertex;
  for (vector< pair<unsigned,unsigned> >::iterator entryIt = cellsForVertex.begin(); entryIt != cellsForVertex.end(); entryIt++)
  {
    justCellsForVertex.push_back(entryIt->first);
  }
  vector<unsigned> childCellIndices = mesh3D->getCell(cellToRefine3D)->getChildIndices(mesh3D);
  std::sort(childCellIndices.begin(), childCellIndices.end());
  vector<unsigned> matches(childCellIndices.size() + cellsForVertex.size());
  vector<unsigned>::iterator matchEnd = std::set_intersection(justCellsForVertex.begin(), justCellsForVertex.end(), childCellIndices.begin(), childCellIndices.end(), matches.begin());
  matches.resize(matchEnd-matches.begin());

  if (matches.size() != 1)
  {
    cout << "matches should have exactly one entry, but has " << matches.size();
    success = false;
  }
  unsigned childCellIndex = matches[0];
  CellPtr childCell = mesh3D->getCell(childCellIndex);
  set<unsigned> childInteriorUnconstrainedFaces;
  set<unsigned> childInteriorConstrainedFaces;
  unsigned faceCount = childCell->getSideCount();
  for (unsigned faceOrdinal=0; faceOrdinal<faceCount; faceOrdinal++)
  {
    unsigned faceIndex = childCell->entityIndex(faceDim, faceOrdinal);
    if (mesh3D->getActiveCellCount(faceDim, faceIndex) == 1)
    {
      // that's an interior constrained face, or a boundary face
      if (expectedFaceConstraints3D.find(faceIndex) != expectedFaceConstraints3D.end())
      {
        // constrained face
        childInteriorConstrainedFaces.insert(faceIndex);
      }
    }
    else if (mesh3D->getActiveCellCount(faceDim, faceIndex) == 2)
    {
      // an interior unconstrained face
      childInteriorUnconstrainedFaces.insert(faceIndex);
    }
    else
    {
      cout << "Error: unexpected active cell count.  Expected 1 or 2, but was " << mesh3D->getActiveCellCount(faceDim, faceIndex) << endl;
      success = false;
    }
  }
//  Camellia::print("childInteriorUnconstrainedFaces", childInteriorUnconstrainedFaces);
//  Camellia::print("childInteriorConstrainedFaces", childInteriorConstrainedFaces);

  mesh3D->refineCell(childCellIndex, RefinementPattern::regularRefinementPatternHexahedron(), mesh3D->cellCount());

  // update expected face and edge constraints
//  set<unsigned> edgeConstraintsToDrop;
  for (set<unsigned>::iterator faceIt=childInteriorConstrainedFaces.begin(); faceIt != childInteriorConstrainedFaces.end(); faceIt++)
  {
    unsigned faceIndex = *faceIt;
    set<unsigned> newChildFaces = mesh3D->getChildEntitiesSet(faceDim, faceIndex);
    for (set<unsigned>::iterator newChildIt=newChildFaces.begin(); newChildIt != newChildFaces.end(); newChildIt++)
    {
      unsigned newChildIndex = *newChildIt;
      expectedFaceConstraints3D[newChildIndex] = expectedFaceConstraints3D[faceIndex];
//      cout << "Expecting two-level face constraint: face " << newChildIndex << " constrained by face " << expectedFaceConstraints3D[newChildIndex].first << endl;
    }
    unsigned numEdges = mesh3D->getSubEntityCount(faceDim, faceIndex, edgeDim);
    set<IndexType> childEdgesOnParentBoundary;
    for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
    {
      unsigned edgeIndex = mesh3D->getSubEntityIndex(faceDim, faceIndex, edgeDim, edgeOrdinal);
      set<unsigned> newChildEdges = mesh3D->getChildEntitiesSet(edgeDim, edgeIndex);
      for (set<unsigned>::iterator newChildIt=newChildEdges.begin(); newChildIt != newChildEdges.end(); newChildIt++)
      {
        unsigned newChildIndex = *newChildIt;
        expectedEdgeConstraints3D[newChildIndex] = expectedEdgeConstraints3D[edgeIndex];
//        cout << "Expecting two-level edge constraint: edge " << newChildIndex << " constrained by ";
//        cout << typeString(expectedEdgeConstraints3D[newChildIndex].second) << " " << expectedEdgeConstraints3D[newChildIndex].first << endl;
        childEdgesOnParentBoundary.insert(newChildIndex);
//        edgeConstraintsToDrop.insert(edgeIndex);
      }
    }

    for (set<unsigned>::iterator newChildIt=newChildFaces.begin(); newChildIt != newChildFaces.end(); newChildIt++)
    {
      unsigned newChildFaceIndex = *newChildIt;
      int numEdges = mesh3D->getSubEntityCount(faceDim, newChildFaceIndex, edgeDim);
      for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
      {
        unsigned newChildEdgeIndex = mesh3D->getSubEntityIndex(faceDim, newChildFaceIndex, edgeDim, edgeOrdinal);
        if (childEdgesOnParentBoundary.find(newChildEdgeIndex) == childEdgesOnParentBoundary.end())
        {
          expectedEdgeConstraints3D[newChildEdgeIndex] = expectedFaceConstraints3D[faceIndex];
        }
      }
    }

    expectedFaceConstraints3D.erase(faceIndex);
  }
//  for (set<unsigned>::iterator edgeToDropIt=edgeConstraintsToDrop.begin(); edgeToDropIt != edgeConstraintsToDrop.end(); edgeToDropIt++) {
//    expectedEdgeConstraints3D.erase(*edgeToDropIt);
//  }
  for (set<unsigned>::iterator faceIt=childInteriorUnconstrainedFaces.begin(); faceIt != childInteriorUnconstrainedFaces.end(); faceIt++)
  {
    unsigned faceIndex = *faceIt;
    set<unsigned> newChildFaces = mesh3D->getChildEntitiesSet(faceDim, faceIndex);
    for (set<unsigned>::iterator newChildIt=newChildFaces.begin(); newChildIt != newChildFaces.end(); newChildIt++)
    {
      unsigned newChildIndex = *newChildIt;
      expectedFaceConstraints3D[newChildIndex] = make_pair(faceIndex, faceDim);
    }
    expectedFaceConstraints3D.erase(faceIndex);
    unsigned numEdges = mesh3D->getSubEntityCount(faceDim, faceIndex, edgeDim);
    set<IndexType> childEdgesOnParentBoundary;
    for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
    {
      unsigned edgeIndex = mesh3D->getSubEntityIndex(faceDim, faceIndex, edgeDim, edgeOrdinal);
      set<unsigned> newChildEdges = mesh3D->getChildEntitiesSet(edgeDim, edgeIndex);
      for (set<unsigned>::iterator newChildIt=newChildEdges.begin(); newChildIt != newChildEdges.end(); newChildIt++)
      {
        unsigned newChildIndex = *newChildIt;
        if (expectedEdgeConstraints3D.find(newChildIndex) == expectedEdgeConstraints3D.end())   // only impose edge constraint if there is not one already present
        {
          expectedEdgeConstraints3D[newChildIndex] = make_pair(edgeIndex,edgeDim);
        }
        childEdgesOnParentBoundary.insert(newChildIndex);
      }
    }
    for (set<unsigned>::iterator newChildIt=newChildFaces.begin(); newChildIt != newChildFaces.end(); newChildIt++)
    {
      unsigned newChildFaceIndex = *newChildIt;
      int numEdges = mesh3D->getSubEntityCount(faceDim, newChildFaceIndex, edgeDim);
      for (unsigned edgeOrdinal=0; edgeOrdinal<numEdges; edgeOrdinal++)
      {
        unsigned newChildEdgeIndex = mesh3D->getSubEntityIndex(faceDim, newChildFaceIndex, edgeDim, edgeOrdinal);
        if (childEdgesOnParentBoundary.find(newChildEdgeIndex) == childEdgesOnParentBoundary.end())
        {
          if (expectedEdgeConstraints3D.find(newChildEdgeIndex) == expectedEdgeConstraints3D.end())   // only impose edge constraint if there is not one already present
          {
            expectedEdgeConstraints3D[newChildEdgeIndex] = make_pair(faceIndex, faceDim);
          }
        }
      }
    }
  }

  if (! checkConstraints(mesh3D, edgeDim, expectedEdgeConstraints3D, "twice-refined 3D mesh") )
  {
    cout << "Failed edge constraint check for twice-refined 3D mesh." << endl;
    success = false;
  }

  if (! checkConstraints(mesh3D, faceDim, expectedFaceConstraints3D, "twice-refined 3D mesh") )
  {
    cout << "Failed face constraint check for twice-refined 3D mesh." << endl;
    success = false;
  }

  return success;
}