PSkel
src/PSkelStencilTiling.h
00001 //-------------------------------------------------------------------------------
00002 // Copyright (c) 2015, ICEI - PUC Minas
00003 // All rights reserved.
00004 // 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 // 
00008 // 1. Redistributions of source code must retain the above copyright notice, this
00009 // list of conditions and the following disclaimer.
00010 // 
00011 // 2. Redistributions in binary form must reproduce the above copyright notice,
00012 // this list of conditions and the following disclaimer in the documentation
00013 // and/or other materials provided with the distribution.
00014 // 
00015 // 3. Neither the name of the copyright holder nor the names of its contributors
00016 // may be used to endorse or promote products derived from this software without
00017 // specific prior written permission.
00018 // 
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00023 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //-------------------------------------------------------------------------------
00030 
00031 #ifndef PSKEL_STENCIL_TILING_H
00032 #define PSKEL_STENCIL_TILING_H
00033 
00034 namespace PSkel{
00035 
00039 template<class Array, class Mask>
00040 class StencilTiling {
00041 public:
00042         Mask mask;
00043         Array input;
00044         Array output;
00045         
00046         size_t iterations;
00047 
00048         //global offsets that define the tiling
00049         //necessary for the given iterations,
00050         //i.e., including the halo region (neighbourhood margin)
00051         size_t widthOffset;
00052         size_t heightOffset;
00053         size_t depthOffset;
00054         size_t width;
00055         size_t height;
00056         size_t depth;
00057 
00058         //core offsets represent the inner margin
00059         //that consist of the halo region (neighbourhood) for 
00060         //the given iterations
00061         size_t coreWidthOffset;
00062         size_t coreHeightOffset;
00063         size_t coreDepthOffset;
00064         size_t coreWidth;
00065         size_t coreHeight;
00066         size_t coreDepth;
00067 
00068         StencilTiling(Array in, Array out, Mask mask){
00069                 this->input = in;
00070                 this->output = out;
00071                 this->mask = mask;
00072         }
00073 
00084         void tile(size_t iterations, size_t widthOffset, size_t heightOffset, size_t depthOffset, size_t width, size_t height, size_t depth){
00085                 this->iterations = iterations;
00086                 //check for unaligned tiling
00087                 if((widthOffset+width)>this->input.getWidth())
00088                         width = this->input.getWidth()-widthOffset;
00089                 if((heightOffset+height)>this->input.getHeight())
00090                         height = this->input.getHeight()-heightOffset;
00091                 if((depthOffset+depth)>this->input.getDepth())
00092                         depth = this->input.getDepth()-depthOffset;
00093 
00094                 this->coreWidthOffset = widthOffset; //temporary value of core width offset
00095                 this->coreHeightOffset = heightOffset; //temporary value of core height offset
00096                 this->coreDepthOffset = depthOffset; //temporary value of core depth offset
00097                 this->coreWidth = width; //set the width for the logical tile region
00098                 this->coreHeight = height; //set the height for the logical tile region
00099                 this->coreDepth = depth; //set the depth for the logical tile region
00100                 
00101                 size_t maskRange = mask.getRange()*iterations;
00102 
00103                 int widthExtra = 0;
00104                 int heightExtra = 0;
00105                 int depthExtra = 0;
00106 
00107                 if(widthOffset>=maskRange){
00108                         widthOffset=widthOffset-maskRange;
00109                 }else{
00110                         widthOffset = 0;
00111                         widthExtra = maskRange-widthOffset;
00112                 }
00113                 width = width+(maskRange*2)-widthExtra;
00114                 if((widthOffset+width)>this->input.getWidth())
00115                         width = this->input.getWidth()-widthOffset;
00116 
00117                 if(heightOffset>=maskRange){
00118                         heightOffset=heightOffset-maskRange;
00119                 }else{
00120                         heightOffset = 0;
00121                         heightExtra = maskRange-heightOffset;
00122                 }
00123                 height = height+(maskRange*2)-heightExtra;
00124                 if((heightOffset+height)>this->input.getHeight())
00125                         height = this->input.getHeight()-heightOffset;
00126 
00127                 if(depthOffset>=maskRange){
00128                         depthOffset=depthOffset-maskRange;
00129                 }else{
00130                         depthOffset = 0;
00131                         depthExtra = maskRange-depthOffset;
00132                 }
00133                 depth = depth+(maskRange*2)-depthExtra;
00134                 if((depthOffset+depth)>this->input.getDepth())
00135                         depth = this->input.getDepth()-depthOffset;
00136                 
00137                 this->coreWidthOffset -= widthOffset; //final value of core width offset
00138                 this->coreHeightOffset -= heightOffset; //final value of core height offset
00139                 this->coreDepthOffset -= depthOffset; //final value of core depth offset
00140                 
00141                 this->widthOffset = widthOffset;
00142                 this->heightOffset = heightOffset;
00143                 this->depthOffset = depthOffset;
00144                 this->width = width;
00145                 this->height = height;
00146                 this->depth = depth;
00147         }
00148 };
00149 
00150 }
00151 
00152 #endif
 All Classes Files Functions