[b5ebb5] | 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 |
|
---|
[35f587] | 26 | FragmentResult::ptr FragmentQueue::NoResult( new FragmentResult(-1) );
|
---|
| 27 | FragmentResult::ptr FragmentQueue::NoResultQueued( new FragmentResult(-2) );
|
---|
| 28 | FragmentResult::ptr FragmentQueue::ResultDelivered( new FragmentResult(-3) );
|
---|
[b5ebb5] | 29 |
|
---|
| 30 | /** Constructor for class FragmentQueue.
|
---|
| 31 | *
|
---|
| 32 | */
|
---|
| 33 | FragmentQueue::FragmentQueue()
|
---|
| 34 | {}
|
---|
| 35 |
|
---|
| 36 | /** Destructor for class FragmentQueue.
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 | FragmentQueue::~FragmentQueue()
|
---|
[78ad7d] | 40 | {
|
---|
| 41 | jobs.clear();
|
---|
| 42 | results.clear();
|
---|
| 43 | }
|
---|
[b5ebb5] | 44 |
|
---|
[12d15a] | 45 | /** Checks whether there are jobs in the queue at all.
|
---|
| 46 | * \return true - jobs present, false - queue is empty
|
---|
| 47 | */
|
---|
| 48 | bool FragmentQueue::isJobPresent() const
|
---|
| 49 | {
|
---|
| 50 | return !jobs.empty();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[b5ebb5] | 53 | /** Pushes a FragmentJob into the internal queue for delivery to server.
|
---|
| 54 | *
|
---|
| 55 | * \note we throw assertion when jobid has already been used.
|
---|
| 56 | *
|
---|
| 57 | * \param job job to enter into queue
|
---|
| 58 | */
|
---|
[78ad7d] | 59 | void FragmentQueue::pushJob(FragmentJob::ptr job)
|
---|
[b5ebb5] | 60 | {
|
---|
[78ad7d] | 61 | ASSERT(job->getId() != JobId::IllegalJob,
|
---|
[9875cc] | 62 | "FragmentQueue::pushJob() - job to push has IllegalJob id.");
|
---|
[78ad7d] | 63 | ASSERT(!results.count(job->getId()),
|
---|
| 64 | "FragmentQueue::pushJob() - job id "+toString(job->getId())+" has already been used.");
|
---|
| 65 | results.insert( std::make_pair(job->getId(), NoResult));
|
---|
[12d15a] | 66 | jobs.push_back(job);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[9875cc] | 69 | /** Pushes a bunch of FragmentJob's into the internal queue for delivery to server.
|
---|
| 70 | *
|
---|
| 71 | * \note we throw assertion when jobid has already been used.
|
---|
| 72 | *
|
---|
| 73 | * \sa pushJob()
|
---|
| 74 | *
|
---|
| 75 | * \param _jobs jobs to enter into queue
|
---|
| 76 | */
|
---|
[78ad7d] | 77 | void FragmentQueue::pushJobs(std::vector<FragmentJob::ptr> &_jobs)
|
---|
[9875cc] | 78 | {
|
---|
[78ad7d] | 79 | for (std::vector<FragmentJob::ptr>::iterator iter = _jobs.begin();
|
---|
[9875cc] | 80 | iter != _jobs.end(); ++iter)
|
---|
| 81 | pushJob(*iter);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[12d15a] | 84 | /** Pops top-most FragmentJob from internal queue.
|
---|
| 85 | *
|
---|
| 86 | * From here on, we expect a result in FragmentQueue::results.
|
---|
| 87 | *
|
---|
| 88 | * \return job topmost job from queue
|
---|
| 89 | */
|
---|
[78ad7d] | 90 | FragmentJob::ptr FragmentQueue::popJob()
|
---|
[12d15a] | 91 | {
|
---|
| 92 | ASSERT(jobs.size(),
|
---|
| 93 | "FragmentQueue::popJob() - there are no jobs on the queue.");
|
---|
[78ad7d] | 94 | FragmentJob::ptr job = jobs.front();
|
---|
| 95 | ResultMap::iterator iter = results.find(job->getId());
|
---|
[12d15a] | 96 | ASSERT(iter != results.end(),
|
---|
[78ad7d] | 97 | "FragmentQueue::popJob() - for job "+toString(job->getId())+" no result place has been stored.");
|
---|
[12d15a] | 98 | iter->second = NoResultQueued;
|
---|
| 99 | jobs.pop_front();
|
---|
| 100 | return job;
|
---|
[b5ebb5] | 101 | }
|
---|
| 102 |
|
---|
[b9c486] | 103 | /** Internal function to check whether result is not one of static entities.
|
---|
| 104 | *
|
---|
| 105 | * @param result result to check against
|
---|
| 106 | * @return true - result is a present, valid result, false - result is one of the statics
|
---|
| 107 | */
|
---|
[35f587] | 108 | bool FragmentQueue::isPresentResult(const FragmentResult::ptr result) const
|
---|
[b9c486] | 109 | {
|
---|
[35f587] | 110 | return (*result != *NoResult)
|
---|
| 111 | && (*result != *NoResultQueued)
|
---|
| 112 | && (*result != *ResultDelivered);
|
---|
[b9c486] | 113 | }
|
---|
| 114 |
|
---|
[b5ebb5] | 115 | /** Queries whether a job has already been finished and the result is present.
|
---|
| 116 | *
|
---|
| 117 | * \param jobid id of job to query
|
---|
| 118 | * \return true - result is present, false - result is not present
|
---|
| 119 | */
|
---|
| 120 | bool FragmentQueue::isResultPresent(JobId_t jobid) const
|
---|
| 121 | {
|
---|
| 122 | ResultMap::const_iterator iter = results.find(jobid);
|
---|
| 123 | return ((iter != results.end())
|
---|
[b9c486] | 124 | && isPresentResult(iter->second));
|
---|
[b5ebb5] | 125 | }
|
---|
| 126 |
|
---|
[8ee5ac] | 127 | /** Counts the number of jobs for which we have a calculated result present.
|
---|
| 128 | *
|
---|
| 129 | * \return number of calculated results
|
---|
| 130 | */
|
---|
| 131 | size_t FragmentQueue::getDoneJobs() const
|
---|
| 132 | {
|
---|
| 133 | size_t doneJobs = 0;
|
---|
| 134 | for (ResultMap::const_iterator iter = results.begin();
|
---|
| 135 | iter != results.end(); ++iter)
|
---|
[35f587] | 136 | if (isPresentResult(iter->second))
|
---|
[8ee5ac] | 137 | ++doneJobs;
|
---|
[bf56f6] | 138 | return doneJobs;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Counts the number of jobs for which still have to be calculated.
|
---|
| 142 | *
|
---|
| 143 | * \return number of jobs to be calculated
|
---|
| 144 | */
|
---|
| 145 | size_t FragmentQueue::getPresentJobs() const
|
---|
| 146 | {
|
---|
| 147 | const size_t presentJobs = jobs.size();
|
---|
| 148 | return presentJobs;
|
---|
[8ee5ac] | 149 | }
|
---|
| 150 |
|
---|
[b5ebb5] | 151 | /** Delivers result for a finished job.
|
---|
| 152 | *
|
---|
| 153 | * \note we throw assertion if not present
|
---|
| 154 | *
|
---|
| 155 | * \param jobid id of job
|
---|
| 156 | * \return result for job of given \a jobid
|
---|
| 157 | */
|
---|
[35f587] | 158 | FragmentResult::ptr FragmentQueue::getResult(JobId_t jobid)
|
---|
[b5ebb5] | 159 | {
|
---|
| 160 | ResultMap::iterator iter = results.find(jobid);
|
---|
| 161 | ASSERT(iter != results.end(),
|
---|
| 162 | "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
|
---|
[35f587] | 163 | ASSERT(*iter->second != *NoResult,
|
---|
[12d15a] | 164 | "FragmentQueue::pushResult() - job "+toString(jobid)+" has not been request for calculation yet.");
|
---|
[35f587] | 165 | ASSERT(*iter->second != *NoResultQueued,
|
---|
[12d15a] | 166 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s calculation is underway but not result has arrived yet.");
|
---|
[35f587] | 167 | ASSERT(*iter->second != *ResultDelivered,
|
---|
[b5ebb5] | 168 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
|
---|
| 169 | /// store result
|
---|
[35f587] | 170 | FragmentResult::ptr _result = iter->second;
|
---|
[b5ebb5] | 171 | /// mark as delivered in map
|
---|
| 172 | iter->second = ResultDelivered;
|
---|
| 173 | /// and return result
|
---|
[b9c486] | 174 | return _result;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[35f587] | 177 | std::vector<FragmentResult::ptr> FragmentQueue::getAllResults()
|
---|
[b9c486] | 178 | {
|
---|
[35f587] | 179 | std::vector<FragmentResult::ptr> returnresults;
|
---|
[b9c486] | 180 | for (ResultMap::iterator iter = results.begin();
|
---|
| 181 | iter != results.end(); ++iter) {
|
---|
| 182 | if (isPresentResult(iter->second)) {
|
---|
| 183 | returnresults.push_back(getResult(iter->first));
|
---|
| 184 | iter = results.begin();
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return returnresults;
|
---|
[b5ebb5] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /** Pushes a result for a finished job.
|
---|
| 192 | *
|
---|
| 193 | * \note we throw assertion if job already has result or is not known.
|
---|
| 194 | *
|
---|
| 195 | * \param result result of job to store
|
---|
| 196 | */
|
---|
[35f587] | 197 | void FragmentQueue::pushResult(FragmentResult::ptr &_result)
|
---|
[b5ebb5] | 198 | {
|
---|
| 199 | /// check for presence
|
---|
[35f587] | 200 | ResultMap::iterator iter = results.find(_result->getId());
|
---|
[b5ebb5] | 201 | ASSERT(iter != results.end(),
|
---|
[35f587] | 202 | "FragmentQueue::pushResult() - job "+toString(_result->getId())+" is not known to us.");
|
---|
| 203 | ASSERT(*iter->second == *NoResultQueued,
|
---|
| 204 | "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(_result->getId())+".");
|
---|
[b5ebb5] | 205 | /// and overwrite NoResult in found entry
|
---|
[b9c486] | 206 | iter->second = _result;
|
---|
[b5ebb5] | 207 | }
|
---|
[b9c486] | 208 |
|
---|