Zoltan2
Zoltan2_MultiJagged_ReductionOps.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #ifndef _ZOLTAN2_MultiJagged_ReductionOps_HPP_
00051 #define _ZOLTAN2_MultiJagged_ReductionOps_HPP_
00052 
00053 #include <Teuchos_ReductionOp.hpp>
00054 
00055 namespace Teuchos{
00056 template <typename Ordinal, typename T>
00057 class MultiJaggedCombinedReductionOp  : public ValueTypeReductionOp<Ordinal,T>
00058 {
00059 private:
00060     Ordinal numSum_0, numMin_1, numMin_2;
00061     std::vector <Ordinal> *partVector;
00062     Ordinal vectorBegin;
00063     Ordinal k;
00064     int reductionType;
00065 
00066 public:
00069     MultiJaggedCombinedReductionOp ():numSum_0(0), numMin_1(0),
00070         numMin_2(0), k(0), partVector(NULL), vectorBegin(0), reductionType(0){}
00071 
00078     MultiJaggedCombinedReductionOp (Ordinal nsum, Ordinal nmin1, Ordinal nmin2,
00079      Ordinal k_):
00080         numSum_0(nsum), numMin_1(nmin1), numMin_2(nmin2), partVector(NULL),
00081         vectorBegin(0), k(k_), reductionType(0){}
00082 
00083 
00084     MultiJaggedCombinedReductionOp (std::vector <Ordinal> *pVector, Ordinal vBegin,
00085      Ordinal k_):
00086         numSum_0(0), numMin_1(0), numMin_2(0), partVector(pVector),
00087         vectorBegin(vBegin), k(k_), reductionType(1){}
00088 
00089 
00092     void reduce( const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
00093     {
00094         if (reductionType == 0){
00095             Ordinal next=0;
00096             for(Ordinal ii = 0; ii < k ; ++ii){
00097                 for (Ordinal i=0; i < numSum_0; i++, next++)
00098                     inoutBuffer[next] += inBuffer[next];
00099 
00100                 for (Ordinal i=0; i < numMin_1; i++, next++)
00101                     if (inoutBuffer[next] < inBuffer[next])
00102                         inoutBuffer[next] = inBuffer[next];
00103 
00104                 for (Ordinal i=0; i < numMin_2; i++, next++)
00105                     if (inoutBuffer[next] > inBuffer[next])
00106                         inoutBuffer[next] = inBuffer[next];
00107             }
00108         }
00109         else {
00110             Ordinal next=0;
00111             for(Ordinal ii = 0; ii < k ; ++ii){
00112                 Ordinal partPartition = (*partVector)[ii + vectorBegin];
00113                 Ordinal tnumSum_ = 2 * partPartition - 1;
00114                 Ordinal tnumMin_1 = partPartition - 1;
00115                 Ordinal tnumMin_2 = tnumMin_1 ;
00116                 for (Ordinal i=0; i < tnumSum_; i++, next++)
00117                     inoutBuffer[next] += inBuffer[next];
00118 
00119                 for (Ordinal i=0; i < tnumMin_1; i++, next++)
00120                     if (inoutBuffer[next] < inBuffer[next])
00121                         inoutBuffer[next] = inBuffer[next];
00122 
00123                 for (Ordinal i=0; i < tnumMin_2; i++, next++)
00124                     if (inoutBuffer[next] > inBuffer[next])
00125                         inoutBuffer[next] = inBuffer[next];
00126             }
00127         }
00128     }
00129 };
00130 
00131 
00132 template <typename Ordinal, typename T>
00133 class MultiJaggedCombinedMinMaxTotalReductionOp :
00134      public ValueTypeReductionOp<Ordinal,T>
00135 {
00136 private:
00137     Ordinal numMin, numMax, numTotal;
00138 
00139 public:
00142     MultiJaggedCombinedMinMaxTotalReductionOp ():numMin(0), numMax(0), numTotal(0)
00143     {}
00144 
00151     MultiJaggedCombinedMinMaxTotalReductionOp (Ordinal nmin, Ordinal nmax, Ordinal
00152      nTotal): numMin(nmin), numMax(nmax), numTotal(nTotal){}
00153 
00156     void reduce( const Ordinal count, const T inBuffer[], T inoutBuffer[]) const
00157     {
00158         Ordinal next=0;
00159 
00160         for (Ordinal i=0; i < numMin; i++, next++)
00161             if (inoutBuffer[next] > inBuffer[next])
00162                 inoutBuffer[next] = inBuffer[next];
00163 
00164         for (Ordinal i=0; i < numMax; i++, next++)
00165             if (inoutBuffer[next] < inBuffer[next])
00166                 inoutBuffer[next] = inBuffer[next];
00167 
00168 
00169         for (Ordinal i=0; i < numTotal; i++, next++)
00170             inoutBuffer[next] += inBuffer[next];
00171     }
00172 };
00173 } // namespace Teuchos
00174 
00175 #endif //_ZOLTAN2_MultiJagged_ReductionOps_HPP_