MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OpFuncBase.h
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #ifndef _OPFUNCBASE_H
11 #define _OPFUNCBASE_H
12 
13 #include "SrcFinfo.h"
14 
15 extern const unsigned char MooseSendHop;
16 extern const unsigned char MooseSetHop;
17 extern const unsigned char MooseSetVecHop;
18 extern const unsigned char MooseGetHop;
19 extern const unsigned char MooseGetVecHop;
20 extern const unsigned char MooseReturnHop;
21 extern const unsigned char MooseTestHop;
22 
23 class HopIndex
24 {
25  public:
26  HopIndex( unsigned short bindIndex,
27  unsigned char hopType = MooseSendHop)
28  : bindIndex_( bindIndex ),
29  hopType_( hopType )
30  {;}
31 
32  unsigned short bindIndex() const {
33  return bindIndex_;
34  }
35 
36  unsigned char hopType() const {
37  return hopType_;
38  }
39  private:
40  unsigned short bindIndex_;
41  unsigned char hopType_;
42 };
43 
44 class OpFunc
45 {
46  public:
47  OpFunc();
48 
49  virtual ~OpFunc()
50  {;}
51  virtual bool checkFinfo( const Finfo* s) const = 0;
52 
53  virtual string rttiType() const = 0;
54 
55  virtual const OpFunc* makeHopFunc( HopIndex hopIndex) const =0;
56 
58  virtual void opBuffer( const Eref& e, double* buf ) const = 0;
59 
61  virtual void opVecBuffer( const Eref& e, double* buf ) const
62  {;}
63 
64  static const OpFunc* lookop( unsigned int opIndex );
65 
66  unsigned int opIndex() const {
67  return opIndex_;
68  }
69 
71  bool setIndex( unsigned int i );
73  static unsigned int rebuildOpIndex();
74  private:
75  unsigned int opIndex_;
76  static vector< OpFunc* >& ops();
77 };
78 
79 class OpFunc0Base: public OpFunc
80 {
81  public:
82  bool checkFinfo( const Finfo* s ) const {
83  return dynamic_cast< const SrcFinfo0* >( s );
84  }
85 
86  virtual void op( const Eref& e ) const = 0;
87 
88  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
89 
90  void opBuffer( const Eref& e, double* buf ) const;
91 
92  string rttiType() const {
93  return "void";
94  }
95 };
96 
97 template< class A > class OpFunc1Base: public OpFunc
98 {
99  public:
100 
101  bool checkFinfo( const Finfo* s ) const {
102  return dynamic_cast< const SrcFinfo1< A >* >( s );
103  }
104 
105  virtual void op( const Eref& e, A arg ) const = 0;
106 
107  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
108 
109  void opBuffer( const Eref& e, double* buf ) const {
110  op( e, Conv< A >::buf2val( &buf ) );
111  }
112 
113  void opVecBuffer( const Eref& e, double* buf ) const {
114  vector< A > temp = Conv< vector< A > >::buf2val( &buf );
115  Element* elm = e.element();
116  if ( elm->hasFields() ) { // Assignment is to field array.
117  unsigned int di = e.dataIndex();
118  unsigned int nf = elm->numField( di -
119  elm->localDataStart() );
120  for ( unsigned int i = 0; i < nf; ++i) {
121  Eref er( elm, di, i );
122  op( er, temp[ i % temp.size() ] );
123  }
124  } else { // Assignment is to data entries.
125  unsigned int k = 0;
126  unsigned int start = elm->localDataStart();
127  unsigned int end = start + elm->numLocalData();
128  for ( unsigned int i = start; i < end; ++i) {
129  Eref er( elm, i, 0 );
130  op( er, temp[ k % temp.size() ] );
131  ++k;
132  }
133  }
134  }
135 
136  virtual void opVec( const Eref& e, const vector< A >& arg,
137  const OpFunc1Base< A >* op ) const
138  { ; } // overridden in HopFuncs.
139 
140  string rttiType() const {
141  return Conv< A >::rttiType();
142  }
143 };
144 
145 template< class A1, class A2 > class OpFunc2Base: public OpFunc
146 {
147  public:
148  bool checkFinfo( const Finfo* s ) const {
149  return dynamic_cast< const SrcFinfo2< A1, A2 >* >( s );
150  }
151 
152  virtual void op( const Eref& e, A1 arg1, A2 arg2 )
153  const = 0;
154 
155  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
156 
157  void opBuffer( const Eref& e, double* buf ) const {
158  const A1& arg1 = Conv< A1 >::buf2val( &buf );
159  op( e,
160  arg1, Conv< A2 >::buf2val( &buf ) );
161  }
162 
163  void opVecBuffer( const Eref& e, double* buf ) const {
164  vector< A1 > temp1 = Conv< vector< A1 > >::buf2val( &buf );
165  vector< A2 > temp2 = Conv< vector< A2 > >::buf2val( &buf );
166  Element* elm = e.element();
167  assert( temp1.size() >= elm->numLocalData() );
168  unsigned int k = 0;
169  unsigned int start = elm->localDataStart();
170  unsigned int end = start + elm->numLocalData();
171  for ( unsigned int i = start; i < end; ++i) {
172  unsigned int nf = elm->numField( i - start );
173  for ( unsigned int j = 0; j < nf; ++j) {
174  Eref er( elm, i, j );
175  op( er, temp1[ k % temp1.size() ],
176  temp2[ k % temp2.size() ] );
177  ++k;
178  }
179  }
180  }
181 
182  virtual void opVec( const Eref& e,
183  const vector< A1 >& arg1,
184  const vector< A2 >& arg2,
185  const OpFunc2Base< A1, A2 >* op ) const
186  { ; } // overridden in HopFuncs.
187 
188  string rttiType() const {
189  return Conv< A1 >::rttiType() + "," + Conv< A2 >::rttiType();
190  }
191 };
192 
193 template< class A1, class A2, class A3 > class OpFunc3Base:
194  public OpFunc
195 {
196  public:
197  bool checkFinfo( const Finfo* s ) const {
198  return dynamic_cast< const SrcFinfo3< A1, A2, A3 >* >( s );
199  }
200 
201  virtual void op( const Eref& e, A1 arg1, A2 arg2, A3 arg3 )
202  const = 0;
203 
204  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
205 
206  void opBuffer( const Eref& e, double* buf ) const {
207  const A1& arg1 = Conv< A1 >::buf2val( &buf );
208  const A2& arg2 = Conv< A2 >::buf2val( &buf );
209  op( e,
210  arg1, arg2, Conv< A3 >::buf2val( &buf ) );
211  }
212 
213  string rttiType() const {
214  return Conv< A1 >::rttiType() + "," + Conv< A2 >::rttiType() +
215  "," + Conv< A3 >::rttiType();
216  }
217 };
218 
219 template< class A1, class A2, class A3, class A4 >
220  class OpFunc4Base: public OpFunc
221 {
222  public:
223  bool checkFinfo( const Finfo* s ) const {
224  return dynamic_cast< const SrcFinfo4< A1, A2, A3, A4 >* >( s );
225  }
226 
227  virtual void op( const Eref& e,
228  A1 arg1, A2 arg2, A3 arg3, A4 arg4 ) const = 0;
229 
230  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
231 
232  void opBuffer( const Eref& e, double* buf ) const {
233  const A1& arg1 = Conv< A1 >::buf2val( &buf );
234  const A2& arg2 = Conv< A2 >::buf2val( &buf );
235  const A3& arg3 = Conv< A3 >::buf2val( &buf );
236  op( e,
237  arg1, arg2, arg3, Conv< A4 >::buf2val( &buf ) );
238  }
239 
240  string rttiType() const {
241  return Conv< A1 >::rttiType() + "," + Conv< A2 >::rttiType() +
242  "," + Conv<A3>::rttiType() + "," + Conv<A4>::rttiType();
243  }
244 };
245 
246 template< class A1, class A2, class A3, class A4, class A5 >
247  class OpFunc5Base: public OpFunc
248 {
249  public:
250  bool checkFinfo( const Finfo* s ) const {
251  return dynamic_cast< const SrcFinfo5< A1, A2, A3, A4, A5 >* >( s );
252  }
253 
254  virtual void op( const Eref& e,
255  A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5 ) const = 0;
256 
257  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
258 
259  void opBuffer( const Eref& e, double* buf ) const {
260  const A1& arg1 = Conv< A1 >::buf2val( &buf );
261  const A2& arg2 = Conv< A2 >::buf2val( &buf );
262  const A3& arg3 = Conv< A3 >::buf2val( &buf );
263  const A4& arg4 = Conv< A4 >::buf2val( &buf );
264  op( e,
265  arg1, arg2, arg3, arg4, Conv< A5 >::buf2val( &buf ) );
266  }
267 
268  string rttiType() const {
269  return Conv< A1 >::rttiType() + "," + Conv< A2 >::rttiType() +
270  "," + Conv<A3>::rttiType() + "," + Conv<A4>::rttiType() +
271  "," + Conv<A5>::rttiType();
272  }
273 };
274 
275 template< class A1, class A2, class A3, class A4, class A5, class A6 >
276  class OpFunc6Base: public OpFunc
277 {
278  public:
279  bool checkFinfo( const Finfo* s ) const {
280  return dynamic_cast< const SrcFinfo6< A1, A2, A3, A4, A5, A6 >* >( s );
281  }
282 
283  virtual void op( const Eref& e, A1 arg1, A2 arg2, A3 arg3, A4 arg4,
284  A5 arg5, A6 arg6 ) const = 0;
285 
286  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
287 
288  void opBuffer( const Eref& e, double* buf ) const {
289  const A1& arg1 = Conv< A1 >::buf2val( &buf );
290  const A2& arg2 = Conv< A2 >::buf2val( &buf );
291  const A3& arg3 = Conv< A3 >::buf2val( &buf );
292  const A4& arg4 = Conv< A4 >::buf2val( &buf );
293  const A5& arg5 = Conv< A5 >::buf2val( &buf );
294  op( e,
295  arg1, arg2, arg3, arg4, arg5, Conv< A6 >::buf2val( &buf ) );
296  }
297 
298  string rttiType() const {
299  return Conv< A1 >::rttiType() + "," + Conv< A2 >::rttiType() +
300  "," + Conv<A3>::rttiType() + "," + Conv<A4>::rttiType() +
301  "," + Conv<A5>::rttiType() + "," + Conv<A6>::rttiType();
302  }
303 };
304 
308 template< class A > class GetOpFuncBase: public OpFunc1Base< vector< A >* >
309 {
310  public:
311  /*
312  bool checkFinfo( const Finfo* s ) const {
313  return ( dynamic_cast< const SrcFinfo1< A >* >( s )
314  || dynamic_cast< const SrcFinfo1< FuncId >* >( s ) );
315  }
316  */
317 
318  virtual A returnOp( const Eref& e ) const = 0;
319 
320  // This returns an OpFunc1< A* > so we can pass back the arg A
321  const OpFunc* makeHopFunc( HopIndex hopIndex) const;
322 
323  // This is called on the target node when a remoteGet happens.
324  // It needs to do the 'get' function and stuff the data into the
325  // buffer for sending back.
326  void opBuffer( const Eref& e, double* buf ) const {
327  A ret = returnOp( e );
328  buf[0] = Conv<A>::size( ret );
329  buf++;
330  Conv< A >::val2buf( ret, &buf );
331  }
332 
333  /*
334  string rttiType() const {
335  return Conv< A >::rttiType();
336  }
337  */
338 };
339 /*
340 template< class A > class GetOpFuncBase: public OpFunc
341 {
342  public:
343  bool checkFinfo( const Finfo* s ) const {
344  return ( dynamic_cast< const SrcFinfo1< A >* >( s )
345  || dynamic_cast< const SrcFinfo1< FuncId >* >( s ) );
346  }
347 
348  virtual void op( const Eref& e, ObjId recipient, FuncId fid )
349  const = 0;
350 
351  virtual A returnOp( const Eref& e ) const = 0;
352 
353  string rttiType() const {
354  return Conv< A >::rttiType();
355  }
356 };
357 */
358 
362 template< class L, class A > class LookupGetOpFuncBase: public OpFunc
363 {
364  public:
365  bool checkFinfo( const Finfo* s ) const {
366  return ( dynamic_cast< const SrcFinfo1< A >* >( s )
367  || dynamic_cast< const SrcFinfo2< FuncId, L >* >( s ) );
368  }
369 
370  virtual void op( const Eref& e, L index,
371  ObjId recipient, FuncId fid ) const = 0;
372 
373  virtual A returnOp( const Eref& e, const L& index ) const = 0;
374 
375  const OpFunc* makeHopFunc( HopIndex hopIndex) const
376  {
377  // Perhaps later we can put in something for x-node gets.
378  return 0;
379  }
380 
381  const OpFunc* makeHopFunc( HopIndex hopIndex, const L& index ) const
382  {
383  // We need to convert the index and pass it into the HopFunc
384  // to pass on to target node.
385  return 0;
386  }
387 
388  void opBuffer( const Eref& e, double* buf ) const {
389  // Later figure out how to handle.
390  }
391 
392  string rttiType() const {
393  return Conv< A >::rttiType();
394  }
395 };
396 
397 #endif // _OPFUNCBASE_H
const unsigned char MooseGetHop
Definition: OpFuncBase.cpp:15
static void val2buf(const T &val, double **buf)
Definition: Conv.h:56
static const OpFunc * lookop(unsigned int opIndex)
Definition: OpFuncBase.cpp:42
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: OpFuncBase.h:375
virtual void op(const Eref &e, A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5, A6 arg6) const =0
virtual void opVec(const Eref &e, const vector< A > &arg, const OpFunc1Base< A > *op) const
Definition: OpFuncBase.h:136
virtual void op(const Eref &e, A1 arg1, A2 arg2, A3 arg3, A4 arg4) const =0
void opVecBuffer(const Eref &e, double *buf) const
Executes the OpFunc for all data by converting a vector of args.
Definition: OpFuncBase.h:113
virtual A returnOp(const Eref &e) const =0
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:250
const unsigned char MooseSetHop
Definition: OpFuncBase.cpp:13
unsigned char hopType_
Definition: OpFuncBase.h:41
virtual void op(const Eref &e, A arg) const =0
const unsigned char MooseGetVecHop
Definition: OpFuncBase.cpp:16
unsigned short bindIndex() const
Definition: OpFuncBase.h:32
virtual A returnOp(const Eref &e, const L &index) const =0
void opVecBuffer(const Eref &e, double *buf) const
Executes the OpFunc for all data by converting a vector of args.
Definition: OpFuncBase.h:163
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:330
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:299
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:183
bool setIndex(unsigned int i)
Used when rebuilding the Fid->OpFunc mapping.
Definition: OpFuncBase.cpp:58
unsigned int dataIndex() const
Definition: Eref.h:50
string rttiType() const
Definition: OpFuncBase.h:298
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:259
Definition: ObjId.h:20
string rttiType() const
Definition: OpFuncBase.h:188
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:101
Element * element() const
Definition: Eref.h:42
virtual void op(const Eref &e, A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5) const =0
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:232
string rttiType() const
Definition: OpFuncBase.h:213
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:223
virtual void opVec(const Eref &e, const vector< A1 > &arg1, const vector< A2 > &arg2, const OpFunc2Base< A1, A2 > *op) const
Definition: OpFuncBase.h:182
virtual void opBuffer(const Eref &e, double *buf) const =0
Executes the OpFunc by converting args.
const unsigned char MooseSetVecHop
Definition: OpFuncBase.cpp:14
virtual void op(const Eref &e) const =0
virtual bool checkFinfo(const Finfo *s) const =0
Definition: Conv.h:30
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:388
virtual void opVecBuffer(const Eref &e, double *buf) const
Executes the OpFunc for all data by converting a vector of args.
Definition: OpFuncBase.h:61
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:326
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:82
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:399
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:279
HopIndex(unsigned short bindIndex, unsigned char hopType=MooseSendHop)
Definition: OpFuncBase.h:26
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:157
virtual void op(const Eref &e, A1 arg1, A2 arg2) const =0
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:148
static unsigned int size(const T &val)
Definition: Conv.h:38
unsigned char hopType() const
Definition: OpFuncBase.h:36
virtual unsigned int numField(unsigned int rawIndex) const =0
Returns number of field entries for specified data.
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:206
virtual const OpFunc * makeHopFunc(HopIndex hopIndex) const =0
string rttiType() const
Definition: OpFuncBase.h:240
unsigned int opIndex_
Definition: OpFuncBase.h:75
string rttiType() const
Definition: OpFuncBase.h:392
static vector< OpFunc * > & ops()
Definition: OpFuncBase.cpp:20
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:197
string rttiType() const
Definition: OpFuncBase.h:92
virtual bool hasFields() const =0
Definition: Eref.h:26
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:513
virtual void op(const Eref &e, A1 arg1, A2 arg2, A3 arg3) const =0
bool checkFinfo(const Finfo *s) const
Definition: OpFuncBase.h:365
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:364
const unsigned char MooseReturnHop
Definition: OpFuncBase.cpp:17
virtual unsigned int localDataStart() const =0
Returns index of first data entry on this node.
virtual unsigned int numLocalData() const =0
Returns number of local data entries on this node.
const OpFunc * makeHopFunc(HopIndex hopIndex, const L &index) const
Definition: OpFuncBase.h:381
string rttiType() const
Definition: OpFuncBase.h:140
virtual void op(const Eref &e, L index, ObjId recipient, FuncId fid) const =0
virtual ~OpFunc()
Definition: OpFuncBase.h:49
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:288
static const T & buf2val(double **buf)
Definition: Conv.h:44
static string rttiType()
Definition: Conv.h:82
virtual string rttiType() const =0
static unsigned int rebuildOpIndex()
cleans out the entire Ops vector. Returns size of orig vector.
Definition: OpFuncBase.cpp:49
const unsigned char MooseTestHop
Definition: OpFuncBase.cpp:18
unsigned int FuncId
Definition: header.h:42
string rttiType() const
Definition: OpFuncBase.h:268
const unsigned char MooseSendHop
Definition: OpFuncBase.cpp:12
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.cpp:37
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: HopFunc.h:269
unsigned short bindIndex_
Definition: OpFuncBase.h:40
const OpFunc * makeHopFunc(HopIndex hopIndex) const
Definition: OpFuncBase.cpp:32
Definition: Finfo.h:12
void opBuffer(const Eref &e, double *buf) const
Executes the OpFunc by converting args.
Definition: OpFuncBase.h:109
unsigned int opIndex() const
Definition: OpFuncBase.h:66