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