Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
channel.cpp
Go to the documentation of this file.
1 
14 #include "configure.h"
15 #include "defs.h"
16 
17 #include "commonParameters.h"
18 #include "bridgeIO.h"
19 using Bridge::vout;
20 
21 #include "communicator.h"
22 #include "communicator_bgnet.h"
23 #include "layout.h"
24 #include "channel.h"
25 
26 // BGNET
27 #include "bgnet.h"
28 
29 // class Channel
30 // class Communicator
31 #define MAX_BUFFER_DIM 8 // this value should be twice dimension.
32 //====================================================================
33 // define static members
34 int Channel::m_set_id = 0;
35 
36 //std::vector<Channel::container_type> Channel::m_buf_send(MAX_BUFFER_DIM);
37 //std::vector<Channel::container_type> Channel::m_buf_recv(MAX_BUFFER_DIM);
38 std::vector<Channel::container_type> Channel::m_buf(2 * MAX_BUFFER_DIM);
39 //====================================================================
40 void Channel::set_thread(int Ntask, const std::valarray<int>& destid,
41  const std::valarray<int>& offset,
42  const std::valarray<int>& datasize)
43 {
44  m_bgnet_ids_thread.resize(Ntask);
45  assert(destid.size() == Ntask);
46  assert(offset.size() == Ntask);
47  for (int itask = 0; itask < Ntask; itask++) {
48  m_bgnet_ids_thread[itask].fifoID = itask;
49  m_bgnet_ids_thread[itask].sendBufID = m_bgnet_ids.sendBufID;
50  m_bgnet_ids_thread[itask].sendOffset = sizeof(element_type) * offset[itask];
51  m_bgnet_ids_thread[itask].size = datasize[itask];
52  m_bgnet_ids_thread[itask].destRank = m_bgnet_ids.destRank;
53  m_bgnet_ids_thread[itask].groupID = destid[itask];
54  m_bgnet_ids_thread[itask].recvBufID = m_bgnet_ids.recvBufID;
55  m_bgnet_ids_thread[itask].recvOffset = sizeof(element_type) * offset[itask];
56  m_bgnet_ids_thread[itask].counterID = m_bgnet_ids.counterID;
57  }
58 }
59 
60 
61 // class Communicator
62 //====================================================================
63 Channel *Communicator_impl::send_init(int count, int idir, int ipm)
64 {
65  LOG;
66 
67  assert(ipm == 1 || ipm == -1);
68  assert(Layout::m_ndim <= max_dimension);
69 
70  int dest = (ipm == Forward) ? Layout::m_ipe_up[idir] : Layout::m_ipe_dn[idir];
71  int tag = idir + max_dimension * ((ipm == Forward) ? 0 : 1);
72 
73 
74  Channel *ch = new Channel(count, tag, Channel::SEND);
75 
76  if (!ch) {
77 #ifdef DEBUG
78  printf("%s: allocate channel failed.\n", __func__);
79 #endif
80  abort();
81  }
82 
83  ch->m_bgnet_ids.fifoID = 0;
85  ch->m_bgnet_ids.sendOffset = 0;
86  ch->m_bgnet_ids.size = sizeof(Channel::element_type) * count;
87  ch->m_bgnet_ids.destRank = dest;
88  ch->m_bgnet_ids.groupID = 0;
89  ch->m_bgnet_ids.recvBufID = tag;
90  ch->m_bgnet_ids.recvOffset = 0;
91  ch->m_bgnet_ids.counterID = tag;
92 
94 
95  return ch;
96 }
97 
98 
99 //====================================================================
100 Channel *Communicator_impl::recv_init(int count, int idir, int ipm)
101 {
102  LOG;
103 
104  assert(ipm == 1 || ipm == -1);
105  assert(Layout::m_ndim <= max_dimension);
106 
107  int src = (ipm == Forward) ? Layout::m_ipe_up[idir] : Layout::m_ipe_dn[idir];
108  int tag = idir + max_dimension * ((ipm == Forward) ? 1 : 0);
109 
110  Channel *ch = new Channel(count, tag, Channel::RECV);
111 
112  if (!ch) {
113 #ifdef DEBUG
114  printf("%s: allocate channel failed.\n", __func__);
115 #endif
116  abort();
117  }
118 
119  ch->m_bgnet_ids.fifoID = 0;
121  ch->m_bgnet_ids.sendOffset = 0;
122  ch->m_bgnet_ids.size = sizeof(Channel::element_type) * count;
123  ch->m_bgnet_ids.destRank = -1;
124  ch->m_bgnet_ids.groupID = 0;
125  ch->m_bgnet_ids.recvBufID = tag;
126  ch->m_bgnet_ids.recvOffset = 0;
127  ch->m_bgnet_ids.counterID = tag;
128 
130 
131  return ch;
132 }
133 
134 
135 // class Channel
136 //====================================================================
137 Channel::Channel(const int count, const int tag, const channel_mode mode)
138 {
139  LOG;
140  m_count = count;
141 
143  // Bridge::VerboseLevel vl = Bridge::PARANOIAC;
144 
145  if (tag >= MAX_BUFFER_DIM) {
146  vout.crucial(vl, "%s: too large value of tag.\n", __func__);
147  abort();
148  }
149 
150  if (mode == SEND) {
151  m_ibuf = tag;
152 
153  if (count > m_buf[m_ibuf].size()) {
154  vout.general(vl, "%s: buffer size reset: ibuf = %d count = %d.\n",
155  __func__, tag, count);
156  m_buf[m_ibuf].resize(count);
157  // m_buf = &(m_buf_send[tag][0]);
158  BGNET_SetSendBuffer((void *)&m_buf[m_ibuf][0], tag,
159  sizeof(element_type) * count);
160  vout.paranoiac(vl, "pointer to m_buf[%d] = %x.\n", m_ibuf, &m_buf[m_ibuf]);
161  } else {
162  vout.paranoiac(vl, "%s: buffer size unchanged: ibuf = %d count = %d.\n",
163  __func__, m_ibuf, count);
164  vout.paranoiac(vl, "pointer to m_buf[%d] = %x.\n", m_ibuf, &m_buf[m_ibuf]);
165  }
166  } else if (mode == RECV) {
167  m_ibuf = tag + MAX_BUFFER_DIM;
168 
169  if (count > m_buf[m_ibuf].size()) {
170  vout.general(vl, "%s: buffer size reset: ibuf = %d count = %d.\n",
171  __func__, tag, count);
172  m_buf[m_ibuf].resize(count);
173  BGNET_SetRecvBuffer((void *)&m_buf[m_ibuf][0], tag,
174  sizeof(element_type) * count);
175  vout.paranoiac(vl, "pointer to m_buf[%d] = %x.\n", m_ibuf, &m_buf[m_ibuf]);
176  } else {
177  vout.paranoiac(vl, "%s: buffer size unchanged: ibuf = %d count = %d.\n",
178  __func__, m_ibuf, count);
179  vout.paranoiac(vl, "pointer to m_buf[%d] = %x.\n", m_ibuf, &m_buf[m_ibuf]);
180  }
181  } else {
182  vout.crucial(vl, "%s: irrelevant input mode.\n", __func__);
183  abort();
184  }
185 }
186 
187 
188 //====================================================================
190 {
191  LOG;
192 }
193 
194 
195 //====================================================================
197 {
198  LOG;
199 
200  int ret;
201  if (m_bgnet_ids.destRank != -1) {
202  ret = BGNET_Put(m_bgnet_ids.fifoID,
211  } else {
212  ret = 0;
213  }
214 
215  return ret;
216 }
217 
218 
219 //====================================================================
221 {
222  LOG;
223 
224  int ret;
225  if (m_bgnet_ids.destRank == -1) {
226  ret = BGNET_WaitForRecv(m_bgnet_ids.groupID,
228  m_bgnet_ids.size);
229  } else {
230  ret = 0;
231  }
232 
233  return ret;
234 }
235 
236 
237 //====================================================================
238 int Channel::start_thread(int itask)
239 {
240  LOG;
241 
242  int ret;
243  if ((m_bgnet_ids_thread[itask].destRank != -1) &&
244  (m_bgnet_ids_thread[itask].size != 0)) {
245  ret = BGNET_Put(m_bgnet_ids_thread[itask].fifoID,
246  m_bgnet_ids_thread[itask].sendBufID,
247  m_bgnet_ids_thread[itask].sendOffset,
248  m_bgnet_ids_thread[itask].size,
249  m_bgnet_ids_thread[itask].destRank,
250  m_bgnet_ids_thread[itask].groupID,
251  m_bgnet_ids_thread[itask].recvBufID,
252  m_bgnet_ids_thread[itask].recvOffset,
253  m_bgnet_ids_thread[itask].counterID);
254  } else {
255  ret = 0;
256  }
257 
258  return ret;
259 }
260 
261 
262 //====================================================================
263 int Channel::wait_thread(int itask)
264 {
265  LOG;
266 
267  int ret;
268  if ((m_bgnet_ids_thread[itask].destRank == -1) &&
269  (m_bgnet_ids_thread[itask].size != 0)) {
270  ret = BGNET_WaitForRecv(itask,
271  m_bgnet_ids_thread[itask].counterID,
272  m_bgnet_ids_thread[itask].size);
273  } else {
274  ret = 0;
275  }
276 
277  return ret;
278 }
279 
280 
281 //====================================================================
282 //============================================================END=====
std::valarray< bgnet_IDs > m_bgnet_ids_thread
Definition: channel.h:47
BridgeIO vout
Definition: bridgeIO.cpp:207
int wait()
Definition: channel.cpp:220
bgnet_IDs m_bgnet_ids
Definition: channel.h:46
static const int max_dimension
Definition: channel.h:14
void set_thread(int, const std::valarray< int > &, const std::valarray< int > &, const std::valarray< int > &)
Definition: channel.cpp:40
void general(const char *format,...)
Definition: bridgeIO.cpp:38
#define LOG
Definition: defs.h:8
int m_count
Definition: channel.h:30
Definition: defs.h:22
static int m_ndim
number of dimensions.
Definition: layout.h:35
static Bridge::VerboseLevel Vlevel()
int start()
Definition: channel.cpp:196
int start_thread(int)
Definition: channel.cpp:238
Channel class for asynchronous communication.
Definition: channel.h:16
int wait_thread(int)
Definition: channel.cpp:263
Channel()
Definition: channel.cpp:89
static int * m_ipe_up
rank of upward neighbour in directions.
Definition: layout.h:42
static std::vector< container_type > m_buf
Definition: channel.h:29
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:62
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
char element_type
Definition: channel.h:18
static Channel * recv_init(int count, int idir, int ipm)
Definition: channel.cpp:100
static Channel * send_init(int count, int idir, int ipm)
async communication
Definition: channel.cpp:63
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:25
virtual ~Channel()
Definition: channel.cpp:189
static int * m_ipe_dn
rank of downward neighbour in directions.
Definition: layout.h:43
static int m_set_id
identifies set of channels.
Definition: channel.h:49
static int sync()
synchronize within small world.
#define MAX_BUFFER_DIM
Definition: channel.cpp:31
channel_mode
Definition: channel.h:20
int m_ibuf
Definition: channel.h:31