MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DiagonalMsg.cpp
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 #include "header.h"
11 #include "DiagonalMsg.h"
12 
13 // Static field declaration
15 vector< DiagonalMsg* > DiagonalMsg::msg_;
16 
17 DiagonalMsg::DiagonalMsg( Element* e1, Element* e2, unsigned int msgIndex )
18  : Msg( ObjId( managerId_, (msgIndex != 0) ? msgIndex: msg_.size() ),
19  e1, e2 ),
20  stride_( 1 )
21 {
22  if ( msgIndex == 0 ) {
23  msg_.push_back( this );
24  } else {
25  if ( msg_.size() <= msgIndex )
26  msg_.resize( msgIndex + 1 );
27  msg_[ msgIndex ] = this;
28  }
29 }
30 
32 {
33  assert( mid_.dataIndex < msg_.size() );
34  msg_[ mid_.dataIndex ] = 0; // ensure deleted ptr isn't reused.
35 }
36 
37 Eref DiagonalMsg::firstTgt( const Eref& src ) const
38 {
39  if ( src.element() == e1_ ) {
40  unsigned int nextData = src.dataIndex() + stride_;
41  return Eref( e2_, nextData );
42  }
43  else if ( src.element() == e2_ ) {
44  unsigned int nextData = src.dataIndex() - stride_;
45  return Eref( e1_, nextData );
46  }
47  return Eref( 0, 0 );
48 }
49 
50 void DiagonalMsg::sources( vector< vector < Eref > >& v ) const
51 {
52  v.clear();
53  v.resize( e2_->numData() );
54  int j = -stride_;
55  int numData1 = e1_->numData();
56  for ( unsigned int i = 0; i < e2_->numData(); ++i ) {
57  if ( j >= 0 && j < numData1 )
58  v[i].resize( 1, Eref( e1_, j ) );
59  j++;
60  }
61 }
62 
63 void DiagonalMsg::targets( vector< vector< Eref > >& v ) const
64 {
65  v.clear();
66  v.resize( e1_->numData() );
67  int j = stride_;
68  int numData2 = e2_->numData();
69  for ( unsigned int i = 0; i < e1_->numData(); ++i ) {
70  if ( j >= 0 && j < numData2 )
71  v[i].resize( 1, Eref( e2_, j ) );
72  j++;
73  }
74 }
75 
77 {
79 }
80 
81 void DiagonalMsg::setStride( int stride )
82 {
83  stride_ = stride;
84  e1()->markRewired();
85  e2()->markRewired();
86 }
87 
89 {
90  return stride_;
91 }
92 
94 {
95  if ( f.id.element() == e1() ) {
96  int i2 = f.dataIndex + stride_;
97  if ( i2 >= 0 ) {
98  unsigned int ui2 = i2;
99  if ( ui2 < e2()->numData() )
100  return ObjId( e2()->id(), DataId( ui2 ) );
101  }
102  } else if ( f.id.element() == e2() ) {
103  int i1 = f.dataIndex - stride_;
104  if ( i1 >= 0 ) {
105  unsigned int ui1 = i1;
106  if ( ui1 < e1()->numData() )
107  return ObjId( e1()->id(), DataId( ui1 ));
108  }
109  }
110  return ObjId( 0, BADINDEX );
111 }
112 
113 Msg* DiagonalMsg::copy( Id origSrc, Id newSrc, Id newTgt,
114  FuncId fid, unsigned int b, unsigned int n ) const
115 {
116  const Element* orig = origSrc.element();
117  if ( n <= 1 ) {
118  DiagonalMsg* ret = 0;
119  if ( orig == e1() ) {
120  ret = new DiagonalMsg( newSrc.element(), newTgt.element(), 0 );
121  ret->e1()->addMsgAndFunc( ret->mid(), fid, b );
122  } else if ( orig == e2() ) {
123  ret = new DiagonalMsg( newTgt.element(), newSrc.element(), 0 );
124  ret->e2()->addMsgAndFunc( ret->mid(), fid, b );
125  } else {
126  assert( 0 );
127  }
128  ret->setStride( stride_ );
129  return ret;
130  } else {
131  // Here we need a SliceMsg which goes from one 2-d array to another.
132  cout << "Error: DiagonalMsg::copy: DiagonalSliceMsg not yet implemented\n";
133  return 0;
134  }
135 }
136 
138 unsigned int DiagonalMsg::numMsg()
139 {
140  return msg_.size();
141 }
142 
144 char* DiagonalMsg::lookupMsg( unsigned int index )
145 {
146  assert( index < msg_.size() );
147  return reinterpret_cast< char* >( msg_[index] );
148 }
149 
151 // Here we set up the MsgManager portion of the class.
153 
155 {
157  // Field definitions.
159  static ValueFinfo< DiagonalMsg, int > stride(
160  "stride",
161  "The stride is the increment to the src DataId that gives the"
162  "dest DataId. "
163  "It can be positive or negative, but bounds checking"
164  "takes place and it does not wrap around.",
167  );
168 
169  static Finfo* msgFinfos[] = {
170  &stride, // value
171  };
172 
173  static Dinfo< short > dinfo;
174  static Cinfo msgCinfo (
175  "DiagonalMsg", // name
176  Msg::initCinfo(), // base class
177  msgFinfos,
178  sizeof( msgFinfos ) / sizeof( Finfo* ), // num Fields
179  &dinfo
180  );
181 
182  return &msgCinfo;
183 }
184 
void markRewired()
Definition: Element.cpp:706
Element * e2() const
Definition: Msg.h:68
Id managerId() const
Definition: DiagonalMsg.cpp:76
ObjId mid() const
Definition: Msg.h:106
static char * lookupMsg(unsigned int index)
Static function for Msg access.
Msg * copy(Id origSrc, Id newSrc, Id newTgt, FuncId fid, unsigned int b, unsigned int n) const
void sources(vector< vector< Eref > > &v) const
Definition: DiagonalMsg.cpp:50
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
Definition: Dinfo.h:60
Id id
Definition: ObjId.h:98
unsigned int dataIndex() const
Definition: Eref.h:50
void addMsgAndFunc(ObjId mid, FuncId fid, BindIndex bindIndex)
Definition: Element.cpp:128
Eref firstTgt(const Eref &src) const
Definition: DiagonalMsg.cpp:37
Definition: ObjId.h:20
Element * element() const
Definition: Eref.h:42
ObjId findOtherEnd(ObjId end) const
Definition: DiagonalMsg.cpp:93
Id id() const
Definition: Element.cpp:71
void setStride(int stride)
Definition: DiagonalMsg.cpp:81
void targets(vector< vector< Eref > > &v) const
Definition: DiagonalMsg.cpp:63
static unsigned int numMsg()
Msg lookup functions.
static const Cinfo * msgCinfo
Definition: Msg.cpp:233
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
Definition: Eref.h:26
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
Definition: Msg.h:18
Element * e1_
Index of this Msg on the msg_ vector.
Definition: Msg.h:180
ObjId mid_
Definition: Msg.h:178
Element * e1() const
Definition: Msg.h:61
static const Cinfo * assignmentMsgCinfo
int getStride() const
Definition: DiagonalMsg.cpp:88
Definition: Id.h:17
static vector< DiagonalMsg * > msg_
Definition: DiagonalMsg.h:72
DiagonalMsg(Element *e1, Element *e2, unsigned int msgIndex)
Definition: DiagonalMsg.cpp:17
Element * e2_
Element 1 attached to Msg.
Definition: Msg.h:181
static Id managerId_
Definition: DiagonalMsg.h:71
unsigned int FuncId
Definition: header.h:42
const unsigned int BADINDEX
Used by ObjId and Eref.
Definition: consts.cpp:25
unsigned int DataId
Definition: header.h:47
Definition: Cinfo.h:18
static const Cinfo * initCinfo()
Definition: Msg.cpp:165
unsigned int dataIndex
Definition: ObjId.h:99
Definition: Finfo.h:12