source: src/Fragmentation/Automation/FragmentQueue.cpp@ b9c486

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
Last change on this file since b9c486 was b9c486, checked in by Frederik Heber <heber@…>, 13 years ago

Added getAllResults() to FragmentQueue.

  • added convenience function ::IsPresentResult().
  • this is to replace FragmentSchedulder::getResults() which relies on jobs being filled still.
  • added unit test on getAllResults().
  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2011 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * FragmentQueue.cpp
10 *
11 * Created on: Oct 19, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "FragmentQueue.hpp"
23
24#include "CodePatterns/Assert.hpp"
25
26FragmentResult FragmentQueue::NoResult(-1);
27FragmentResult FragmentQueue::NoResultQueued(-2);
28FragmentResult FragmentQueue::ResultDelivered(-3);
29
30/** Constructor for class FragmentQueue.
31 *
32 */
33FragmentQueue::FragmentQueue()
34{}
35
36/** Destructor for class FragmentQueue.
37 *
38 */
39FragmentQueue::~FragmentQueue()
40{}
41
42/** Checks whether there are jobs in the queue at all.
43 * \return true - jobs present, false - queue is empty
44 */
45bool FragmentQueue::isJobPresent() const
46{
47 return !jobs.empty();
48}
49
50/** Pushes a FragmentJob into the internal queue for delivery to server.
51 *
52 * \note we throw assertion when jobid has already been used.
53 *
54 * \param job job to enter into queue
55 */
56void FragmentQueue::pushJob(FragmentJob &job)
57{
58 ASSERT(job.getId() != JobId::IllegalJob,
59 "FragmentQueue::pushJob() - job to push has IllegalJob id.");
60 ASSERT(!results.count(job.getId()),
61 "FragmentQueue::pushJob() - job id "+toString(job.getId())+" has already been used.");
62 results.insert( std::make_pair(job.getId(), NoResult));
63 jobs.push_back(job);
64}
65
66/** Pushes a bunch of FragmentJob's into the internal queue for delivery to server.
67 *
68 * \note we throw assertion when jobid has already been used.
69 *
70 * \sa pushJob()
71 *
72 * \param _jobs jobs to enter into queue
73 */
74void FragmentQueue::pushJobs(std::vector<FragmentJob> &_jobs)
75{
76 for (std::vector<FragmentJob>::iterator iter = _jobs.begin();
77 iter != _jobs.end(); ++iter)
78 pushJob(*iter);
79}
80
81/** Pops top-most FragmentJob from internal queue.
82 *
83 * From here on, we expect a result in FragmentQueue::results.
84 *
85 * \return job topmost job from queue
86 */
87FragmentJob FragmentQueue::popJob()
88{
89 ASSERT(jobs.size(),
90 "FragmentQueue::popJob() - there are no jobs on the queue.");
91 FragmentJob job = jobs.front();
92 ResultMap::iterator iter = results.find(job.getId());
93 ASSERT(iter != results.end(),
94 "FragmentQueue::popJob() - for job "+toString(job.getId())+" no result place has been stored.");
95 iter->second = NoResultQueued;
96 jobs.pop_front();
97 return job;
98}
99
100/** Internal function to check whether result is not one of static entities.
101 *
102 * @param result result to check against
103 * @return true - result is a present, valid result, false - result is one of the statics
104 */
105bool FragmentQueue::isPresentResult(const FragmentResult &_result) const
106{
107 return (_result != NoResult)
108 && (_result != NoResultQueued)
109 && (_result != ResultDelivered);
110}
111
112/** Queries whether a job has already been finished and the result is present.
113 *
114 * \param jobid id of job to query
115 * \return true - result is present, false - result is not present
116 */
117bool FragmentQueue::isResultPresent(JobId_t jobid) const
118{
119 ResultMap::const_iterator iter = results.find(jobid);
120 return ((iter != results.end())
121 && isPresentResult(iter->second));
122}
123
124/** Counts the number of jobs for which we have a calculated result present.
125 *
126 * \return number of calculated results
127 */
128size_t FragmentQueue::getDoneJobs() const
129{
130 size_t doneJobs = 0;
131 for (ResultMap::const_iterator iter = results.begin();
132 iter != results.end(); ++iter)
133 if ((iter->second != NoResult)
134 && (iter->second != NoResultQueued)
135 && (iter->second != ResultDelivered))
136 ++doneJobs;
137 return doneJobs;
138}
139
140/** Delivers result for a finished job.
141 *
142 * \note we throw assertion if not present
143 *
144 * \param jobid id of job
145 * \return result for job of given \a jobid
146 */
147FragmentResult FragmentQueue::getResult(JobId_t jobid)
148{
149 ResultMap::iterator iter = results.find(jobid);
150 ASSERT(iter != results.end(),
151 "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
152 ASSERT(iter->second != NoResult,
153 "FragmentQueue::pushResult() - job "+toString(jobid)+" has not been request for calculation yet.");
154 ASSERT(iter->second != NoResultQueued,
155 "FragmentQueue::pushResult() - job "+toString(jobid)+"'s calculation is underway but not result has arrived yet.");
156 ASSERT(iter->second != ResultDelivered,
157 "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
158 /// store result
159 FragmentResult _result = iter->second;
160 /// mark as delivered in map
161 iter->second = ResultDelivered;
162 /// and return result
163 return _result;
164}
165
166std::vector<FragmentResult> FragmentQueue::getAllResults()
167{
168 std::vector<FragmentResult> returnresults;
169 for (ResultMap::iterator iter = results.begin();
170 iter != results.end(); ++iter) {
171 if (isPresentResult(iter->second)) {
172 returnresults.push_back(getResult(iter->first));
173 iter = results.begin();
174 }
175 }
176
177 return returnresults;
178}
179
180/** Pushes a result for a finished job.
181 *
182 * \note we throw assertion if job already has result or is not known.
183 *
184 * \param result result of job to store
185 */
186void FragmentQueue::pushResult(FragmentResult &_result)
187{
188 /// check for presence
189 ResultMap::iterator iter = results.find(_result.getId());
190 ASSERT(iter != results.end(),
191 "FragmentQueue::pushResult() - job "+toString(_result.getId())+" is not known to us.");
192 ASSERT(iter->second == NoResultQueued,
193 "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(_result.getId())+".");
194 /// and overwrite NoResult in found entry
195 iter->second = _result;
196}
197
Note: See TracBrowser for help on using the repository browser.