MASA-Core
AbstractBlockAligner.hpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #ifndef ABSTRACTBLOCKALIGNER_HPP_
00023 #define ABSTRACTBLOCKALIGNER_HPP_
00024 
00025 #include "AbstractAligner.hpp"
00026 #include "../parameters/BlockAlignerParameters.hpp"
00027 #include "../processors/AbstractBlockProcessor.hpp"
00028 #include "../pruning/BlockPruningGenericN2.hpp"
00029 
00030 /**
00031  * @brief Abstract class that processes blocks individually considering
00032  * some schedule mechanism.
00033  *
00034  * This class implements some common behavior to simplify the implementation
00035  * of block aligners. These aligners process blocks individually using
00036  * a customized schedule mechanism that processes block through an
00037  * AbstractBlockProcessor object.
00038  *
00039  * Use the AbstractBlockProcessor if
00040  * you want to create a CPU block scheduler that processes a single block
00041  * in the specific hardware/software architecture.
00042  *
00043  * In order to extend an AbstractDiagonalAligner, the class must implement
00044  * two methods.
00045  *
00046  * <ul>
00047  *  <li>scheduleBlocks: schedules the block executions using a customized
00048  *      mechanism.
00049  *  <li>alignBlock: responsible to receive/dispatch data from/to MASA-Core
00050  *      and call the processBlock for each block. These procedures must
00051  *      be aware of the schedule mechanism in order to avoid unsafe calls
00052  *      to MASA-Core.
00053  * </ul>
00054  *
00055  */
00056 class AbstractBlockAligner : public AbstractAligner {
00057 public:
00058         /**
00059          * Constructor
00060          *
00061          * @param blockProcessor the block processor to be used. If NULL, the
00062          * default processor will be used.
00063          * @param params the aligner parameters. If NULL, the default param
00064          * class will be used.
00065          */
00066         AbstractBlockAligner(AbstractBlockProcessor* blockProcessor = NULL, BlockAlignerParameters* params = NULL);
00067 
00068         /**
00069          * Destructor.
00070          */
00071         virtual ~AbstractBlockAligner();
00072 
00073         /* Implementation of virtual methods from IAligner */
00074 
00075         virtual void initialize();
00076         virtual void alignPartition(Partition partition);
00077         virtual void finalize();
00078 
00079         virtual aligner_capabilities_t getCapabilities();
00080         virtual const score_params_t* getScoreParameters();
00081         virtual IAlignerParameters* getParameters();
00082 
00083         virtual void setSequences(const char* seq0, const char* seq1, int seq0_len, int seq1_len);
00084         virtual void unsetSequences();
00085 
00086         virtual void clearStatistics();
00087         virtual void printInitialStatistics(FILE* file);
00088         virtual void printStageStatistics(FILE* file);
00089         virtual void printFinalStatistics(FILE* file);
00090         virtual void printStatistics(FILE* file);
00091         virtual const char* getProgressString() const;
00092         virtual long long getProcessedCells();
00093 
00094 
00095 protected:
00096         /** Chunk of rows used to pass cells from up to bottom blocks */
00097         cell_t** row;
00098         /** Chunk of columns used to pass cells from left to right blocks */
00099         cell_t** col;
00100 
00101         /* Virtual methods that must be implemented by subclasses */
00102 
00103         /**
00104          * Schedules all the blocks for execution. As soon as one block is
00105          * ready to be executed, this method must call the
00106          * AbstractBlockAligner::alignBlock(int,int) function in order to prepare
00107          * this block for real execution.
00108          *
00109          * @param grid_width width of the grid in blocks.
00110          * @param grid_height height of the grid in blocks.
00111          */
00112         virtual void scheduleBlocks(int grid_width, int grid_height) = 0;
00113 
00114         /**
00115          * This method is called by AbstractBlockAligner::alignBlock(int,int)
00116          * with additional information about block coordinates. Then, this method
00117          * must receive/dispatch rows and columns from/to MASA-Core and call
00118          * the processBlock(int,int,int,int,int,int) method.
00119          *
00120          * @param bx horizontal block coordinate
00121          * @param by vertical block coordinate
00122          * @param i0 vertical first row of the block
00123          * @param j0 horizontal first column of the block
00124          * @param i1 vertical last row of the block
00125          * @param j1 horizontal last column of the block
00126          */
00127         virtual void alignBlock(int bx, int by, int i0, int j0, int i1, int j1) = 0;
00128 
00129         /**
00130          * Ignore the computation of a block. This method must be called
00131          * for statistical purposes only.
00132          *
00133          * @param bx horizontal block coordinate
00134          * @param by vertical block coordinate
00135          */
00136         void ignoreBlock(int bx, int by);
00137 
00138         /**
00139          * Check for pruned blocks
00140          * @param bx horizontal block coordinate
00141          * @param by vertical block coordinate
00142          * @return true if the block can be pruned
00143          */
00144         bool isBlockPruned(int bx, int by) const;
00145 
00146         /**
00147          * Increased statistics about block processing.
00148          * @param pruned indicates if the block was pruned.
00149          */
00150         virtual void increaseBlockStat(const bool pruned);
00151 
00152         /* Other protected methods*/
00153 
00154         void alignBlock(int bx, int by);
00155         bool processBlock(int bx, int by, int i0, int j0, int i1, int j1);
00156         bool isSpecialRow(int by);
00157         bool isSpecialColumn(int by);
00158 
00159 
00160 
00161         /**
00162          * Defines the preferred size of a block and the preferred number of blocks
00163          * in the grid. This values are used as a hint for the automatic grid/block
00164          * configuration.
00165          *
00166          * @param preferredBlockSize the preferred maximum size of a block. Must be
00167          * greater than 0.
00168          * @param preferredGridSize the preferred minimum grid size. Must be
00169          * greater than 0.
00170          */
00171         void setPreferredSizes(int preferredBlockSize, int preferredGridSize);
00172 
00173 
00174         /* memory related methods */
00175 
00176         virtual void allocateStructures();
00177         virtual void deallocateStructures();
00178 
00179 
00180 private:
00181         /** Parameter used in this aligner */
00182         BlockAlignerParameters* params;
00183 
00184         /** Stores the score of each grid */
00185         score_t** grid_scores;
00186 
00187         /** Processor that computes a single block */
00188         AbstractBlockProcessor* blockProcessor;
00189 
00190         /** Block Pruner object */
00191         BlockPruningGenericN2* blockPruner;
00192 
00193         /** Preferred maximum size of a block. */
00194         int preferredBlockSize;
00195 
00196         /** Preferred minimum grid size. */
00197         int preferredGridSize;
00198 
00199         /** Maintains the minimum blockWidth used. */
00200         int statMinBlockWidth;
00201         /** Maintains the maximum blockWidth used. */
00202         int statMaxBlockWidth;
00203         /** Maintains the minimum blockHeight used. */
00204         int statMinBlockHeight;
00205         /** Maintains the maximum blockHeight used. */
00206         int statMaxBlockHeight;
00207         /** Maintains the minimum gridWidth used. */
00208         int statMinGridWidth;
00209         /** Maintains the maximum gridWidth used. */
00210         int statMaxGridWidth;
00211         /** Maintains the minimum gridHeight used. */
00212         int statMinGridHeight;
00213         /** Maintains the maximum gridHeight used. */
00214         int statMaxGridHeight;
00215 
00216         /** Total number of blocks processed in the grid */
00217         int statTotalBlocks;
00218         /** Number of pruned blocks */
00219         int statPrunedBlocks;
00220 
00221 
00222         /** Score parameters */
00223         score_params_t score_params;
00224 
00225         /* memory related methods */
00226 
00227         Grid* configureGrid(Partition partition);
00228 
00229         /* Other methods */
00230 
00231         void pruningUpdate(int bx, int by, int score);
00232 };
00233 
00234 #endif /* ABSTRACTBLOCKALIGNER_HPP_ */