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 |
|
---|
26 | FragmentResult FragmentQueue::NoResult(-1);
|
---|
27 | FragmentResult FragmentQueue::NoResultQueued(-2);
|
---|
28 | FragmentResult FragmentQueue::ResultDelivered(-3);
|
---|
29 |
|
---|
30 | /** Constructor for class FragmentQueue.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | FragmentQueue::FragmentQueue()
|
---|
34 | {}
|
---|
35 |
|
---|
36 | /** Destructor for class FragmentQueue.
|
---|
37 | *
|
---|
38 | */
|
---|
39 | FragmentQueue::~FragmentQueue()
|
---|
40 | {}
|
---|
41 |
|
---|
42 | /** Checks whether there are jobs in the queue at all.
|
---|
43 | * \return true - jobs present, false - queue is empty
|
---|
44 | */
|
---|
45 | bool 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 | */
|
---|
56 | void 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 | */
|
---|
74 | void 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 | */
|
---|
87 | FragmentJob 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 | */
|
---|
105 | bool 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 | */
|
---|
117 | bool 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 | */
|
---|
128 | size_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 | */
|
---|
147 | FragmentResult 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 |
|
---|
166 | std::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 | */
|
---|
186 | void 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 |
|
---|