MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OneToOneMsg.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-2010 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 "OneToOneMsg.h"
12 
13 // Initializing static variables
15 vector< OneToOneMsg* > OneToOneMsg::msg_;
16 
17 OneToOneMsg::OneToOneMsg( const Eref& e1, const Eref& e2,
18  unsigned int msgIndex )
19  : Msg( ObjId( managerId_, (msgIndex != 0) ? msgIndex: msg_.size() ),
20  e1.element(), e2.element() ),
21  i1_( e1.dataIndex() ),
22  i2_( e2.dataIndex() )
23 {
24  if ( msgIndex == 0 ) {
25  msg_.push_back( this );
26  } else {
27  if ( msg_.size() <= msgIndex )
28  msg_.resize( msgIndex + 1 );
29  msg_[ msgIndex ] = this;
30  }
31 }
32 
34 {
35  assert( mid_.dataIndex < msg_.size() );
36  msg_[ mid_.dataIndex ] = 0; // ensure deleted ptr isn't reused.
37 }
38 
46 Eref OneToOneMsg::firstTgt( const Eref& src ) const
47 {
48  if ( src.element() == e1_ ) {
49  if ( e2_->hasFields() )
50  return Eref( e2_, i2_, src.dataIndex() );
51  else
52  return Eref( e2_, src.dataIndex(), 0 );
53  } else if ( src.element() == e2_ ) {
54  return Eref( e1_, src.dataIndex() );
55  }
56  return Eref( 0, 0 );
57 }
58 
59 // We don't permit e1 to have fields at this point.
60 void OneToOneMsg::sources( vector< vector< Eref > > & v) const
61 {
62  v.resize( 0 );
63  unsigned int n = e1_->numData();
64  if ( e2_->hasFields() ) {
65  if ( Eref( e2_, i2_ ).isDataHere() ) {
66  assert( i2_ > e2_->localDataStart() );
67  unsigned int nf = e2_->numField( i2_ - e2_->localDataStart() );
68  if ( n > nf )
69  n = nf;
70  v.resize( n );
71  for ( unsigned int i = 0; i < n; ++i ) {
72  v[i].resize( 1, Eref( e1_, i ) );
73  }
74  }
75  } else {
76  if ( n > e2_->numData() )
77  n = e2_->numData();
78  v.resize( e2_->numData() );
79  for ( unsigned int i = 0; i < n; ++i ) {
80  v[i].resize( 1, Eref( e1_, i ) );
81  }
82  }
83 }
84 
85 void OneToOneMsg::targets( vector< vector< Eref > > & v) const
86 {
87  unsigned int n = e1_->numData();
88  v.resize( e1_->numData() );
89  if ( e2_->hasFields() ) {
90  if ( Eref( e2_, i2_ ).isDataHere() ) {
91  assert( i2_ > e2_->localDataStart() );
92  unsigned int nf = e2_->numField( i2_ - e2_->localDataStart() );
93  if ( n > nf )
94  n = nf;
95  for ( unsigned int i = 0; i < n; ++i )
96  v[i].resize( 1, Eref( e2_, i2_, i ) );
97  } else { // Don't know target node # of entries, so send all.
98  unsigned int start = e1_->localDataStart();
99  unsigned int end = start + e1_->numLocalData();
100  for ( unsigned int i = start; i < end; ++i ) {
101  v[i].resize( 1, Eref( e2_, i2_, i ) );
102  }
103  }
104  } else {
105  if ( n > e2_->numData() )
106  n = e2_->numData();
107  for ( unsigned int i = 0; i < n; ++i ) {
108  v[i].resize( 1, Eref( e2_, i ) );
109  }
110  }
111 }
112 
114 {
116 }
117 
119 {
120  if ( f.element() == e1() )
121  return ObjId( e2()->id(), f.dataIndex );
122  else if ( f.element() == e2() )
123  return ObjId( e1()->id(), f.dataIndex );
124 
125  return ObjId( 0, BADINDEX );
126 }
127 
128 Msg* OneToOneMsg::copy( Id origSrc, Id newSrc, Id newTgt,
129  FuncId fid, unsigned int b, unsigned int n ) const
130 {
131  const Element* orig = origSrc.element();
132  // This works both for 1-copy and for n-copies
133  OneToOneMsg* ret = 0;
134  if ( orig == e1() ) {
135  ret = new OneToOneMsg( newSrc.eref(), newTgt.eref(), 0 );
136  ret->e1()->addMsgAndFunc( ret->mid(), fid, b );
137  } else if ( orig == e2() ) {
138  ret = new OneToOneMsg( newTgt.eref(), newSrc.eref(), 0 );
139  ret->e2()->addMsgAndFunc( ret->mid(), fid, b );
140  } else
141  assert( 0 );
142  // ret->e1()->addMsgAndFunc( ret->mid(), fid, b );
143  return ret;
144 }
145 
147 unsigned int OneToOneMsg::numMsg()
148 {
149  return msg_.size();
150 }
151 
153 char* OneToOneMsg::lookupMsg( unsigned int index )
154 {
155  assert( index < msg_.size() );
156  return reinterpret_cast< char* >( msg_[index] );
157 }
158 
160 // Here we set up the MsgManager portion of the class.
162 
164 {
166  // Field definitions. Nothing here.
168 
169  static Dinfo< short > dinfo;
170  static Cinfo msgCinfo (
171  "OneToOneMsg", // name
172  Msg::initCinfo(), // base class
173  0, // Finfo array
174  0, // Num Fields
175  &dinfo
176  );
177 
178  return &msgCinfo;
179 }
180 
182 
Element * e2() const
Definition: Msg.h:68
static Id managerId_
Definition: OneToOneMsg.h:50
ObjId mid() const
Definition: Msg.h:106
bool isDataHere() const
Definition: Eref.cpp:47
Id managerId() const
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
Msg * copy(Id origSrc, Id newSrc, Id newTgt, FuncId fid, unsigned int b, unsigned int n) const
Definition: Dinfo.h:60
void sources(vector< vector< Eref > > &v) const
Definition: OneToOneMsg.cpp:60
unsigned int dataIndex() const
Definition: Eref.h:50
void addMsgAndFunc(ObjId mid, FuncId fid, BindIndex bindIndex)
Definition: Element.cpp:128
ObjId findOtherEnd(ObjId end) const
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
static const Cinfo * initCinfo()
Setup function for Element-style access to Msg fields.
Element * element() const
Definition: Eref.h:42
Id id() const
Definition: Element.cpp:71
static const Cinfo * oneToOneMsgCinfo
static vector< OneToOneMsg * > msg_
Definition: OneToOneMsg.h:51
static unsigned int numMsg()
Msg lookup functions.
DataId i2_
Definition: OneToOneMsg.h:49
static char * lookupMsg(unsigned int index)
Static function for Msg access.
static char dataIndex[]
Definition: mfield.cpp:406
virtual unsigned int numField(unsigned int rawIndex) const =0
Returns number of field entries for specified data.
static const Cinfo * msgCinfo
Definition: Msg.cpp:233
virtual bool hasFields() const =0
Definition: Eref.h:26
OneToOneMsg(const Eref &e1, const Eref &e2, unsigned int msgIndex)
Definition: OneToOneMsg.cpp:17
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
Definition: Msg.h:18
virtual unsigned int localDataStart() const =0
Returns index of first data entry on this node.
Element * e1_
Index of this Msg on the msg_ vector.
Definition: Msg.h:180
ObjId mid_
Definition: Msg.h:178
virtual unsigned int numLocalData() const =0
Returns number of local data entries on this node.
Element * e1() const
Definition: Msg.h:61
Element * element() const
Definition: ObjId.cpp:124
vector< vector< T > > resize(vector< vector< T > >table, unsigned int n, T init)
Definition: Id.h:17
Element * e2_
Element 1 attached to Msg.
Definition: Msg.h:181
Eref firstTgt(const Eref &src) const
Definition: OneToOneMsg.cpp:46
void targets(vector< vector< Eref > > &v) const
Definition: OneToOneMsg.cpp:85
unsigned int FuncId
Definition: header.h:42
const unsigned int BADINDEX
Used by ObjId and Eref.
Definition: consts.cpp:25
Definition: Cinfo.h:18
static const Cinfo * initCinfo()
Definition: Msg.cpp:165
unsigned int dataIndex
Definition: ObjId.h:99