00001 /* @HEADER@ */ 00002 // ************************************************************************ 00003 // 00004 // Sundance 00005 // Copyright 2011 Sandia Corporation 00006 // 00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00008 // the U.S. Government retains certain rights in this software. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu) 00038 // 00039 00040 /* @HEADER@ */ 00041 00042 #ifndef SUNDANCE_TEMPSTACK_H 00043 #define SUNDANCE_TEMPSTACK_H 00044 00045 #include "SundanceDefs.hpp" 00046 #include "SundanceEvalVector.hpp" 00047 #include "SundanceNoncopyable.hpp" 00048 #include <stack> 00049 00050 namespace Sundance 00051 { 00052 using namespace Sundance; 00053 /** 00054 * TempStack provides a stack of temporary variables for use during 00055 * evaluation. 00056 * 00057 * During the course of evaluating an expression, it is often necessary 00058 * to create temporary variables. For example, in evaluating 00059 * \code 00060 * a += b*(c+d) 00061 * \endcode 00062 * it is required to create three temporaries (ignoring for 00063 * explanatory purposes any copies made in cases where one of 00064 * the vectors will be used elsewhere). We can see this 00065 * by breaking the operation 00066 * down into the following steps: 00067 * \code 00068 * 1. Create a temporary variable t1 00069 * 2. Evaluate expression b into t1 00070 * 3. Create a temporary variable t2 00071 * 4. Evaluate expression c into t2 00072 * 3. Create a temporary variable t3 00073 * 4. Evaluate expression d into t3 00074 * 5. Carry out t2 += t3 00075 * 6. Carry out t1 *= t2 00076 * 7. Carry out a += t1 00077 * \endcode 00078 * The number of temporaries required for a given expression 00079 * will depend on the graph of the expression. In general, we want to 00080 * create exactly as many temporaries as are needed, and reuse any 00081 * temporaries that are no longer needed. This is a well-known problem 00082 * in compiler design, and can be accomplished by maintaining a 00083 * stack of temporaries. When a new temporary is needed, it is popped 00084 * from the stack; if the stack is empty, a new temporary is allocated. 00085 * When a step of a calculation is done, any temporaries used are 00086 * put back on the stack for further use. 00087 */ 00088 class TempStack : public Noncopyable 00089 { 00090 public: 00091 /** Empty ctor */ 00092 TempStack(); 00093 00094 /** Construct with an initial vector size */ 00095 TempStack(int vecSize); 00096 00097 /** Push vector data onto the stack */ 00098 void pushVectorData(const RCP<Array<double> >& vecData) ; 00099 00100 /** Pop vector data from the stack */ 00101 RCP<Array<double> > popVectorData() ; 00102 00103 /** Get a new vector (which will often reuse stack data) */ 00104 RCP<EvalVector> popVector() 00105 {return rcp(new EvalVector(this));} 00106 00107 /** */ 00108 void setVecSize(int vecSize) {vecSize_ = vecSize;} 00109 00110 /** */ 00111 void resetCounter() ; 00112 00113 /** */ 00114 int numVecsAccessed() const {return numVecsAccessed_;} 00115 00116 /** */ 00117 int numVecsAllocated() const {return numVecsAllocated_;} 00118 00119 /** */ 00120 int vecSize() const {return vecSize_;} 00121 00122 private: 00123 00124 int vecSize_; 00125 00126 std::stack<RCP<Array<double> > > stack_; 00127 00128 int numVecsAllocated_; 00129 00130 int numVecsAccessed_; 00131 }; 00132 } 00133 00134 #endif