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 "communicator_mpi.h"
15 #include "layout.h"
16 #include "channel.h"
17 
25 Channel *Communicator_impl::send_init(int count, int idir, int ipm)
26 {
27  LOG;
28 
29  assert(ipm == 1 || ipm == -1);
30  assert(Layout::m_ndim <= max_dimension);
31 
32  Channel *ch = new Channel(count);
33  if (!ch) {
34 #ifdef DEBUG
35  printf("%s: allocate channel failed.\n", __func__);
36 #endif
37  abort();
38  }
39 
40  int dest = (ipm == Forward) ? Layout::m_ipe_up[idir] : Layout::m_ipe_dn[idir];
41  int tag = idir + max_dimension * (((ipm == Forward) ? 0 : 1) + 2 * m_grid_rank);
42 
43  int retv = MPI_Send_init((void *)&ch->m_buf[0], sizeof(Channel::element_type) * count, MPI_BYTE, dest, tag, m_comm, &ch->m_request);
44 
45  if (retv != MPI_SUCCESS) {
46  return (Channel *)0;
47  }
48 
49  return ch;
50 }
51 
52 
60 Channel *Communicator_impl::recv_init(int count, int idir, int ipm)
61 {
62  LOG;
63 
64  assert(ipm == 1 || ipm == -1);
65  assert(Layout::m_ndim <= max_dimension);
66 
67  Channel *ch = new Channel(count);
68  if (!ch) {
69 #ifdef DEBUG
70  printf("%s: allocate channel failed.\n", __func__);
71 #endif
72  abort();
73  }
74 
75  int src = (ipm == Forward) ? Layout::m_ipe_up[idir] : Layout::m_ipe_dn[idir];
76  int tag = idir + max_dimension * (((ipm == Forward) ? 1 : 0) + 2 * src);
77 
78  int retv = MPI_Recv_init((void *)&ch->m_buf[0], sizeof(Channel::element_type) * count, MPI_BYTE, src, tag, m_comm, &ch->m_request);
79 
80  if (retv != MPI_SUCCESS) {
81  return (Channel *)0;
82  }
83 
84  return ch;
85 }
86 
87 
88 // class Channel
89 Channel::Channel() : m_buf(0)
90 {
91  LOG;
92 }
93 
94 
95 Channel::Channel(const int count) : m_buf(count)
96 {
97  LOG;
98 }
99 
100 
102 {
103  LOG;
104 }
105 
106 
107 int Channel::start()
108 {
109  LOG;
110  return MPI_Start(&m_request);
111 }
112 
113 
114 int Channel::wait()
115 {
116  LOG;
117  return MPI_Wait(&m_request, &m_status);
118 }
119 
120 
121 // class ChannelSet
122 ChannelSet::ChannelSet(int count) : m_array(count), m_nreq(0)
123 {
124  LOG;
125 }
126 
127 
129 {
130  LOG;
131 
132  if (m_nreq >= m_array.size()) {
133  return MPI_ERR_BUFFER;
134  }
135  m_array[m_nreq++] = p->m_request; //* need grant access to private data of channel class.
136 
137  return MPI_SUCCESS;
138 }
139 
140 
142 {
143  LOG;
144  return MPI_Startall(m_nreq, &m_array[0]);
145 }
146 
147 
149 {
150  LOG;
151  return MPI_Waitall(m_nreq, &m_array[0], (MPI_Status *)0);
152 }
int wait()
Definition: channel.cpp:220
static const int max_dimension
Definition: channel.h:14
MPI_Status m_status
handler to MPI status information
Definition: channel.h:74
int start()
collective start
Definition: channel.cpp:141
#define LOG
Definition: defs.h:8
Definition: defs.h:22
static int m_ndim
number of dimensions.
Definition: layout.h:35
int start()
Definition: channel.cpp:196
int append(Channel *const p)
append channel to the set. there is no way to remove a channel.
Definition: channel.cpp:128
Channel class for asynchronous communication.
Definition: channel.h:16
MPI_Request m_request
handler to MPI persistent communication
Definition: channel.h:73
Channel()
Definition: channel.cpp:89
static int m_comm
instead of MPI_Comm m_comm;
unsigned int m_nreq
number of channels to hold.
Definition: channel.h:97
int wait()
collective wait
Definition: channel.cpp:148
std::valarray< MPI_Request > m_array
a collection of MPI request held in channels.
Definition: channel.h:96
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
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
virtual ~Channel()
Definition: channel.cpp:189
static int * m_ipe_dn
rank of downward neighbour in directions.
Definition: layout.h:43
ChannelSet(int nchannel=8)
constructor. default number of channels is 8 for upward and downward in 4 dimensions.
Definition: channel.cpp:122