OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SequenceGen.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef SEQUENCE_GEN_H
12 #define SEQUENCE_GEN_H
13 
14 /***********************************************************************
15  *
16  * class SequenceGen
17  *
18  * SequenceGen generates a sequence of doubles from some algorithm, such
19  * a uniform or gaussian random number generator. It is templated on
20  * a class which actually generates the number sequence. SequenceGen
21  * provides an expression-template-aware wrapper around this generator.
22  *
23  * A SequenceGen is created by giving it a reference to an instance
24  * of the specific generator it should use. It keeps a copy of this
25  * generator, so the classes used as template parameters for SequenceGen
26  * must have a copy constructor. It should also have a default constructor.
27  * Also, it uses the () operator to get
28  * the next number in the sequence. The particular generator itself
29  * may be accessed through the 'getGenerator' method.
30  *
31  * The generator class used as the template parameter must also provide
32  * a typedef indicating the return type of the generator; this typedef
33  * must be called Return_t.
34  *
35  ***********************************************************************/
36 
37 // include files
38 #include "PETE/IpplExpressions.h"
39 
40 // some macro definitions to set up basic binary math operations
41 
42 #define RNG_OPERATOR_WITH_SCALAR(GEN,SCA,OP,APP) \
43  \
44 inline PETEBinaryReturn<GEN,SCA,APP>::type \
45 OP(const GEN& lhs, SCA sca) \
46 { \
47  return PETE_apply( APP(), lhs(), sca ); \
48 } \
49  \
50 inline PETEBinaryReturn<SCA,GEN,APP>::type \
51 OP(SCA sca, const GEN& rhs) \
52 { \
53  return PETE_apply( APP(), sca, rhs() ); \
54 }
55 
56 #define RNG_OPERATOR(GEN,OP,APP) \
57  \
58 RNG_OPERATOR_WITH_SCALAR(GEN,short,OP,APP) \
59 RNG_OPERATOR_WITH_SCALAR(GEN,int,OP,APP) \
60 RNG_OPERATOR_WITH_SCALAR(GEN,long,OP,APP) \
61 RNG_OPERATOR_WITH_SCALAR(GEN,float,OP,APP) \
62 RNG_OPERATOR_WITH_SCALAR(GEN,double,OP,APP) \
63 RNG_OPERATOR_WITH_SCALAR(GEN,std::complex<double>,OP,APP)
64 
65 #define RNG_BASIC_MATH(GEN) \
66  \
67 RNG_OPERATOR(GEN,operator+,OpAdd) \
68 RNG_OPERATOR(GEN,operator-,OpSubtract) \
69 RNG_OPERATOR(GEN,operator*,OpMultipply) \
70 RNG_OPERATOR(GEN,operator/,OpDivide)
71 
72 
73 // the guts of the sequence generator
74 template <class GT>
75 class SequenceGen : public PETE_Expr< SequenceGen<GT> >
76 {
77 
78 public:
80  SequenceGen(const GT& gen) : Gen(gen) { }
81  // Interface for PETE.
82  enum { IsExpr = 1 }; // Treat SequenceGen as a PETE_Expr in expressions.
84  typedef typename GT::Return_t PETE_Return_t;
85  const PETE_Expr_t& MakeExpression() const { return *this; }
86  PETE_Expr_t& MakeExpression() { return *this; }
87 
88  // typedefs for functions below
89  typedef typename GT::Return_t Return_t;
90 
91  // return access to the generator
92  GT& getGenerator() { return Gen; }
93  const GT& getGenerator() const { return Gen; }
94 
95  // get the next value in the sequence
96  inline Return_t operator()(void) const { return Gen(); }
97 
98 private:
99  // the number generator
100  GT Gen;
101 };
102 
103 // define for_each operations for SequenceGen objects
104 #include "Utility/RNGAssignDefs.h"
105 
106 #endif // SEQUENCE_GEN_H
107 
108 /***************************************************************************
109  * $RCSfile: SequenceGen.h,v $ $Author: adelmann $
110  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
111  * IPPL_VERSION_ID: $Id: SequenceGen.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
112  ***************************************************************************/
Definition: PETE.h:77
const GT & getGenerator() const
Definition: SequenceGen.h:93
SequenceGen< GT > PETE_Expr_t
Definition: SequenceGen.h:83
GT::Return_t PETE_Return_t
Definition: SequenceGen.h:84
SequenceGen(const GT &gen)
Definition: SequenceGen.h:80
GT::Return_t Return_t
Definition: SequenceGen.h:89
Return_t operator()(void) const
Definition: SequenceGen.h:96
const PETE_Expr_t & MakeExpression() const
Definition: SequenceGen.h:85
GT & getGenerator()
Definition: SequenceGen.h:92
PETE_Expr_t & MakeExpression()
Definition: SequenceGen.h:86