Changeset 127a8e


Ignore:
Timestamp:
Jul 8, 2010, 4:04:11 PM (14 years ago)
Author:
Tillmann Crueger <crueger@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
4d7462
Parents:
40f928
Message:

Improved managing of Ids in the World

  • Improved algorithm for managing ids to use less time and space
  • Made the moleculeIds managed
Location:
src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/World.cpp

    r40f928 r127a8e  
    2525#include "Box.hpp"
    2626#include "Matrix.hpp"
     27#include "defs.hpp"
    2728
    2829#include "Patterns/Singleton_impl.hpp"
     
    121122  molecule *mol = NULL;
    122123  mol = NewMolecule();
    123   ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
    124   mol->setId(currMoleculeId++);
     124  moleculeId_t id = getNextMoleculeId();
     125  ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
     126  mol->setId(id);
    125127  // store the molecule by ID
    126128  molecules[mol->getId()] = mol;
     
    140142  DeleteMolecule(mol);
    141143  molecules.erase(id);
     144  releaseMoleculeId(id);
    142145}
    143146
     
    145148  OBSERVE;
    146149  atomId_t id = getNextAtomId();
     150  ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
    147151  atom *res = NewAtom(id);
    148152  res->setWorld(this);
     
    221225
    222226atomId_t World::getNextAtomId(){
    223   // see if we can reuse some Id
    224   if(atomIdPool.empty()){
    225     return currAtomId++;
    226   }
    227   else{
    228     // we give out the first ID from the pool
    229     atomId_t id = *(atomIdPool.begin());
    230     atomIdPool.erase(id);
     227  // try to find an Id in the pool;
     228  if(!atomIdPool.empty()){
     229    atomIdPool_t::iterator iter=atomIdPool.begin();
     230    atomId_t id = iter->first;
     231    pair<atomId_t,atomId_t> newRange = make_pair(id+1,iter->second);
     232    // we wont use this iterator anymore, so we don't care about invalidating
     233    atomIdPool.erase(iter);
     234    if(newRange.first<newRange.second){
     235      atomIdPool.insert(newRange);
     236    }
    231237    return id;
    232238  }
     239  // Nothing in the pool... we are out of luck
     240  return currAtomId++;
    233241}
    234242
    235243void World::releaseAtomId(atomId_t id){
    236   atomIdPool.insert(id);
    237   // defragmentation of the pool
    238   set<atomId_t>::reverse_iterator iter;
    239   // go through all Ids in the pool that lie immediately below the border
    240   while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
    241     atomIdPool.erase(--currAtomId);
    242   }
     244  atomIdPool.insert(make_pair(id,id+1));
     245  defragAtomIdPool();
    243246}
    244247
    245248bool World::reserveAtomId(atomId_t id){
    246249  if(id>=currAtomId ){
    247     // add all ids between the new one and current border as available
    248     for(atomId_t pos=currAtomId; pos<id; ++pos){
    249       atomIdPool.insert(pos);
     250    pair<atomId_t,atomId_t> newRange = make_pair(currAtomId,id);
     251    if(newRange.first<newRange.second){
     252      atomIdPool.insert(newRange);
    250253    }
    251254    currAtomId=id+1;
     255    defragAtomIdPool();
    252256    return true;
    253257  }
    254   else if(atomIdPool.count(id)){
    255     atomIdPool.erase(id);
     258  // look for a range that matches the request
     259  for(atomIdPool_t::iterator iter=atomIdPool.begin();iter!=atomIdPool.end();++iter){
     260    if(iter->first>id){
     261      // we have coverd all available ranges... nothing to be found here
     262      break;
     263    }
     264    // no need to check first, since it has to be <=id, since otherwise we would have broken out
     265    if(iter->second > id){
     266      // we found a matching range... get the id from this range
     267
     268      // split up this range at the point of id
     269      pair<atomId_t,atomId_t> bottomRange = make_pair(iter->first,id);
     270      pair<atomId_t,atomId_t> topRange = make_pair(id+1,iter->second);
     271      // remove this range
     272      atomIdPool.erase(iter);
     273      if(bottomRange.first<bottomRange.second){
     274        atomIdPool.insert(bottomRange);
     275      }
     276      if(topRange.first<topRange.second){
     277        atomIdPool.insert(topRange);
     278      }
     279      defragAtomIdPool();
     280      return true;
     281    }
     282  }
     283  // this ID could not be reserved
     284  return false;
     285}
     286
     287void World::defragAtomIdPool(){
     288  // check if the situation is bad enough to make defragging neccessary
     289  if((numAtomDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
     290     (atomIdPool.size()<lastAtomPoolSize+MAX_POOL_FRAGMENTATION)){
     291    ++numAtomDefragSkips;
     292    return;
     293  }
     294  for(atomIdPool_t::iterator iter = atomIdPool.begin();iter!=atomIdPool.end();){
     295    // see if this range is adjacent to the next one
     296    atomIdPool_t::iterator next = iter;
     297    next++;
     298    if(next!=atomIdPool.end() && (next->first==iter->second)){
     299      // merge the two ranges
     300      pair<atomId_t,atomId_t> newRange = make_pair(iter->first,next->second);
     301      atomIdPool.erase(iter);
     302      atomIdPool.erase(next);
     303      pair<atomIdPool_t::iterator,bool> res = atomIdPool.insert(newRange);
     304      ASSERT(res.second,"Id-Pool was confused");
     305      iter=res.first;
     306      continue;
     307    }
     308    ++iter;
     309  }
     310  if(!atomIdPool.empty()){
     311    // check if the last range is at the border
     312    atomIdPool_t::iterator iter = atomIdPool.end();
     313    iter--;
     314    if(iter->second==currAtomId){
     315      currAtomId=iter->first;
     316      atomIdPool.erase(iter);
     317    }
     318  }
     319  lastAtomPoolSize=atomIdPool.size();
     320  numAtomDefragSkips=0;
     321}
     322
     323// Molecules
     324
     325moleculeId_t World::getNextMoleculeId(){
     326  // try to find an Id in the pool;
     327  if(!moleculeIdPool.empty()){
     328    moleculeIdPool_t::iterator iter=moleculeIdPool.begin();
     329    moleculeId_t id = iter->first;
     330    pair<moleculeId_t,moleculeId_t> newRange = make_pair(id+1,iter->second);
     331    // we wont use this iterator anymore, so we don't care about invalidating
     332    moleculeIdPool.erase(iter);
     333    if(newRange.first<newRange.second){
     334      moleculeIdPool.insert(newRange);
     335    }
     336    return id;
     337  }
     338  // Nothing in the pool... we are out of luck
     339  return currMoleculeId++;
     340}
     341
     342void World::releaseMoleculeId(moleculeId_t id){
     343  moleculeIdPool.insert(make_pair(id,id+1));
     344  defragMoleculeIdPool();
     345}
     346
     347bool World::reserveMoleculeId(moleculeId_t id){
     348  if(id>=currMoleculeId ){
     349    pair<moleculeId_t,moleculeId_t> newRange = make_pair(currMoleculeId,id);
     350    if(newRange.first<newRange.second){
     351      moleculeIdPool.insert(newRange);
     352    }
     353    currMoleculeId=id+1;
     354    defragMoleculeIdPool();
    256355    return true;
    257356  }
    258   else{
    259     // this ID could not be reserved
    260     return false;
    261   }
    262 }
    263 
    264 // Molecules
     357  // look for a range that matches the request
     358  for(moleculeIdPool_t::iterator iter=moleculeIdPool.begin();iter!=moleculeIdPool.end();++iter){
     359    if(iter->first>id){
     360      // we have coverd all available ranges... nothing to be found here
     361      break;
     362    }
     363    // no need to check first, since it has to be <=id, since otherwise we would have broken out
     364    if(iter->second > id){
     365      // we found a matching range... get the id from this range
     366
     367      // split up this range at the point of id
     368      pair<moleculeId_t,moleculeId_t> bottomRange = make_pair(iter->first,id);
     369      pair<moleculeId_t,moleculeId_t> topRange = make_pair(id+1,iter->second);
     370      // remove this range
     371      moleculeIdPool.erase(iter);
     372      if(bottomRange.first<bottomRange.second){
     373        moleculeIdPool.insert(bottomRange);
     374      }
     375      if(topRange.first<topRange.second){
     376        moleculeIdPool.insert(topRange);
     377      }
     378      defragMoleculeIdPool();
     379      return true;
     380    }
     381  }
     382  // this ID could not be reserved
     383  return false;
     384}
     385
     386void World::defragMoleculeIdPool(){
     387  // check if the situation is bad enough to make defragging neccessary
     388  if((numMoleculeDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
     389     (moleculeIdPool.size()<lastMoleculePoolSize+MAX_POOL_FRAGMENTATION)){
     390    ++numMoleculeDefragSkips;
     391    return;
     392  }
     393  for(moleculeIdPool_t::iterator iter = moleculeIdPool.begin();iter!=moleculeIdPool.end();){
     394    // see if this range is adjacent to the next one
     395    moleculeIdPool_t::iterator next = iter;
     396    next++;
     397    if(next!=moleculeIdPool.end() && (next->first==iter->second)){
     398      // merge the two ranges
     399      pair<moleculeId_t,moleculeId_t> newRange = make_pair(iter->first,next->second);
     400      moleculeIdPool.erase(iter);
     401      moleculeIdPool.erase(next);
     402      pair<moleculeIdPool_t::iterator,bool> res = moleculeIdPool.insert(newRange);
     403      ASSERT(res.second,"Id-Pool was confused");
     404      iter=res.first;
     405      continue;
     406    }
     407    ++iter;
     408  }
     409  if(!moleculeIdPool.empty()){
     410    // check if the last range is at the border
     411    moleculeIdPool_t::iterator iter = moleculeIdPool.end();
     412    iter--;
     413    if(iter->second==currMoleculeId){
     414      currMoleculeId=iter->first;
     415      moleculeIdPool.erase(iter);
     416    }
     417  }
     418  lastMoleculePoolSize=moleculeIdPool.size();
     419  numMoleculeDefragSkips=0;
     420}
    265421
    266422/******************************* Iterators ********************************/
     
    322478    atoms(this),
    323479    currAtomId(0),
     480    lastAtomPoolSize(0),
     481    numAtomDefragSkips(0),
    324482    molecules(),
    325483    currMoleculeId(0),
  • src/World.hpp

    r40f928 r127a8e  
    288288  void releaseAtomId(atomId_t);
    289289  bool reserveAtomId(atomId_t);
     290  void defragAtomIdPool();
     291
     292  moleculeId_t getNextMoleculeId();
     293  void releaseMoleculeId(moleculeId_t);
     294  bool reserveMoleculeId(moleculeId_t);
     295  void defragMoleculeIdPool();
    290296
    291297  periodentafel *periode;
     
    298304  AtomSet atoms;
    299305private:
    300   std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
     306  typedef std::set<std::pair<atomId_t, atomId_t> > atomIdPool_t;
     307
     308  /**
     309   * stores the pool for all available AtomIds below currAtomId
     310   *
     311   * The pool contains ranges of free ids in the form [bottom,top).
     312   */
     313  atomIdPool_t atomIdPool;
    301314  atomId_t currAtomId; //!< stores the next available Id for atoms
     315  size_t lastAtomPoolSize; //!< size of the pool after last defrag, to skip some defrags
     316  unsigned int numAtomDefragSkips;
     317
    302318  MoleculeSet molecules;
     319  typedef std::set<std::pair<moleculeId_t, moleculeId_t> > moleculeIdPool_t;
     320  moleculeIdPool_t moleculeIdPool;
    303321  moleculeId_t currMoleculeId;
     322  size_t lastMoleculePoolSize; //!< size of the pool after last defrag, to skip some defrags
     323  unsigned int numMoleculeDefragSkips;
    304324private:
    305325  /**
  • src/defs.hpp

    r40f928 r127a8e  
    8484#define MOLECUILDER_NAME "Molecuilder"
    8585
     86const unsigned int MAX_POOL_FRAGMENTATION=20;
     87const unsigned int MAX_FRAGMENTATION_SKIPS=100;
     88
    8689#endif /*DEFS_HPP_*/
Note: See TracChangeset for help on using the changeset viewer.