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