ppALIGN API documentation
src/checkpoint.hpp
00001 /****************************************************************************** 00002 Copyright 2009 Stefan Wolfsheimer & Gregory Nuel. 00003 00004 This file is part of ppALIGN 00005 00006 ppALIGN is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 ppALIGN is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with ppALIGN; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 *******************************************************************************/ 00020 00021 #ifndef _CHECKPOINT_HPP_ 00022 #define _CHECKPOINT_HPP_ 00023 #include <iostream> 00024 #include <vector> 00025 00026 #include "exceptions.hpp" 00027 00028 00049 class CheckpointDriver 00050 { 00051 public: 00054 virtual ~CheckpointDriver() {} 00055 00056 00059 virtual void FirstPassForward(long last_row, long current_row, long row_i) = 0; 00060 00063 virtual void Forward(long last_row, long current_row, long row_i) = 0; 00064 00069 virtual bool Backward(long current_row, long row_i) = 0; 00070 00074 virtual long SpaceAvail() = 0; 00075 00077 virtual void ForwardDone() {} 00078 00079 00084 virtual long StartBackward(); 00085 00086 00090 virtual long SpaceRequired() = 0; 00091 }; 00092 00093 00096 class DemoCheckpointDriver : public CheckpointDriver 00097 { 00098 public: 00105 DemoCheckpointDriver(long _M, long _N, long _start=-1, long end = -1); 00106 virtual void FirstPassForward(long last_row, long current_row, long row_i) ; 00107 virtual void Forward(long last_row, long current_row, long row_i) ; 00108 virtual bool Backward(long current_row, long row_i) ; 00109 virtual long SpaceAvail() ; 00110 virtual long SpaceRequired() ; 00111 virtual long StartBackward(); 00112 private: 00113 long M,N,start,end; 00114 std::vector<long> workspace; 00115 }; 00116 00117 00129 void LinearSpaceComputation(CheckpointDriver & d, bool forward_done_event=true); 00130 00134 void QuadraticSpaceComputation(CheckpointDriver & d); 00135 00143 void OptimalCheckpoint(CheckpointDriver & driver) ; 00144 00145 00149 template<typename TYPE> 00150 struct DynCell 00151 { 00153 TYPE M; 00154 00156 TYPE I; 00157 00159 TYPE D; 00160 00165 friend std::ostream & operator<< (std::ostream & ost, const DynCell & c) 00166 { 00167 ost << "M=" << c.M << ",I=" << c.I << ",D=" << c.D; 00168 return ost; 00169 }; 00170 }; 00171 00172 00177 class NotEnoughMemory : public ExceptionBase 00178 { 00179 public: 00184 NotEnoughMemory(size_t n_cells_required, size_t n_cells_avail) 00185 { 00186 std::stringstream str; 00187 str << "Not enough memory for operation " << n_cells_required << " cells provided " << n_cells_avail << " available!"; 00188 errMsg = str.str(); 00189 } 00190 }; 00191 00192 00193 00199 template<typename CELL> 00200 class Workspace 00201 : public std::vector<CELL> 00202 { 00203 public: 00210 typedef CELL cell_t; 00211 00214 typedef typename std::vector<cell_t> parent_t; 00216 00221 typedef typename std::vector<cell_t>::iterator iterator; 00222 00224 typedef typename std::vector<cell_t>::const_iterator const_iterator; 00225 00227 typedef typename std::vector<cell_t>::reverse_iterator reverse_iterator; 00228 00230 typedef typename std::vector<cell_t>::const_reverse_iterator const_reverse_iterator; 00232 00236 Workspace(size_t n_cells); 00237 00242 void SetupTable(size_t n_rows, 00243 size_t n_cols); 00244 00248 inline size_t Rows(); 00249 00253 inline size_t Cols(); 00254 00259 inline iterator RowBegin(size_t row); 00260 00261 private: 00262 size_t n_rows; 00263 size_t n_cols; 00264 }; 00265 00266 00267 00270 00271 template<typename CELL> 00272 Workspace<CELL>::Workspace(size_t n_cells) : parent_t(n_cells) 00273 { 00274 n_rows = 0; 00275 n_cols = 0; 00276 00277 } 00278 00279 template<typename CELL> 00280 void Workspace<CELL>::SetupTable(size_t _n_rows, size_t _n_cols) 00281 { 00282 n_cols = _n_cols; 00283 n_rows = parent_t::size() / n_cols; 00284 if(n_rows < _n_rows && n_rows < 2) 00285 { 00286 throw NotEnoughMemory(2 ,n_rows); 00287 } 00288 } 00289 00290 template<typename CELL> 00291 size_t Workspace<CELL>::Rows() 00292 { 00293 return n_rows; 00294 } 00295 00296 template<typename CELL> 00297 size_t Workspace<CELL>::Cols() 00298 { 00299 return n_cols; 00300 } 00301 00302 template<typename CELL> 00303 typename Workspace<CELL>::iterator Workspace<CELL>::RowBegin(size_t row) 00304 { 00305 return parent_t::begin() + row * n_cols; 00306 } 00307 00308 00309 #endif