00001 /* @HEADER@ */ 00002 // ************************************************************************ 00003 // 00004 // Sundance 00005 // Copyright (2005) Sandia Corporation 00006 // 00007 // Copyright (year first published) Sandia Corporation. Under the terms 00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 00009 // retains certain rights in this software. 00010 // 00011 // This library is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Lesser General Public License as 00013 // published by the Free Software Foundation; either version 2.1 of the 00014 // License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, but 00017 // WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // Lesser General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License along with this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00024 // USA 00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 00026 // Sandia National Laboratories, Livermore, California, USA 00027 // 00028 // ************************************************************************ 00029 /* @HEADER@ */ 00030 00031 #ifndef SUNDANCE_TEMPSTACK_H 00032 #define SUNDANCE_TEMPSTACK_H 00033 00034 #include "SundanceDefs.hpp" 00035 #include "SundanceEvalVector.hpp" 00036 #include "SundanceNoncopyable.hpp" 00037 #include <stack> 00038 00039 namespace Sundance 00040 { 00041 using namespace Sundance; 00042 /** 00043 * TempStack provides a stack of temporary variables for use during 00044 * evaluation. 00045 * 00046 * During the course of evaluating an expression, it is often necessary 00047 * to create temporary variables. For example, in evaluating 00048 * \code 00049 * a += b*(c+d) 00050 * \endcode 00051 * it is required to create three temporaries (ignoring for 00052 * explanatory purposes any copies made in cases where one of 00053 * the vectors will be used elsewhere). We can see this 00054 * by breaking the operation 00055 * down into the following steps: 00056 * \code 00057 * 1. Create a temporary variable t1 00058 * 2. Evaluate expression b into t1 00059 * 3. Create a temporary variable t2 00060 * 4. Evaluate expression c into t2 00061 * 3. Create a temporary variable t3 00062 * 4. Evaluate expression d into t3 00063 * 5. Carry out t2 += t3 00064 * 6. Carry out t1 *= t2 00065 * 7. Carry out a += t1 00066 * \endcode 00067 * The number of temporaries required for a given expression 00068 * will depend on the graph of the expression. In general, we want to 00069 * create exactly as many temporaries as are needed, and reuse any 00070 * temporaries that are no longer needed. This is a well-known problem 00071 * in compiler design, and can be accomplished by maintaining a 00072 * stack of temporaries. When a new temporary is needed, it is popped 00073 * from the stack; if the stack is empty, a new temporary is allocated. 00074 * When a step of a calculation is done, any temporaries used are 00075 * put back on the stack for further use. 00076 */ 00077 class TempStack : public Noncopyable 00078 { 00079 public: 00080 /** Empty ctor */ 00081 TempStack(); 00082 00083 /** Construct with an initial vector size */ 00084 TempStack(int vecSize); 00085 00086 /** Push vector data onto the stack */ 00087 void pushVectorData(const RCP<Array<double> >& vecData) ; 00088 00089 /** Pop vector data from the stack */ 00090 RCP<Array<double> > popVectorData() ; 00091 00092 /** Get a new vector (which will often reuse stack data) */ 00093 RCP<EvalVector> popVector() 00094 {return rcp(new EvalVector(this));} 00095 00096 /** */ 00097 void setVecSize(int vecSize) {vecSize_ = vecSize;} 00098 00099 /** */ 00100 void resetCounter() ; 00101 00102 /** */ 00103 int numVecsAccessed() const {return numVecsAccessed_;} 00104 00105 /** */ 00106 int numVecsAllocated() const {return numVecsAllocated_;} 00107 00108 /** */ 00109 int vecSize() const {return vecSize_;} 00110 00111 private: 00112 00113 int vecSize_; 00114 00115 std::stack<RCP<Array<double> > > stack_; 00116 00117 int numVecsAllocated_; 00118 00119 int numVecsAccessed_; 00120 }; 00121 } 00122 00123 #endif