PEXSI
 All Classes Namespaces Files Functions Variables Friends Pages
mpi_interf.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 The Regents of the University of California,
3  through Lawrence Berkeley National Laboratory.
4 
5  Author: Lin Lin
6 
7  This file is part of PEXSI. All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  (1) Redistributions of source code must retain the above copyright notice, this
13  list of conditions and the following disclaimer.
14  (2) Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  (3) Neither the name of the University of California, Lawrence Berkeley
18  National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
19  be used to endorse or promote products derived from this software without
20  specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33  You are under no obligation whatsoever to provide any bug fixes, patches, or
34  upgrades to the features, functionality or performance of the source code
35  ("Enhancements") to anyone; however, if you choose to make your Enhancements
36  available either publicly, or directly to Lawrence Berkeley National
37  Laboratory, without imposing a separate written license agreement for such
38  Enhancements, then you hereby grant the following license: a non-exclusive,
39  royalty-free perpetual license to install, use, modify, prepare derivative
40  works, incorporate into other computer software, distribute, and sublicense
41  such enhancements or derivative works thereof, in binary and source code form.
42 */
46 #ifndef _MPI_INTERF_HPP_
47 #define _MPI_INTERF_HPP_
48 
49 #include "environment.hpp"
50 
51 namespace PEXSI{
52 
56 namespace mpi{
57 
58 // *********************************************************************
59 // Allgatherv
60 //
61 // NOTE: The interface is quite preliminary.
62 // *********************************************************************
63 void Allgatherv(
64  std::vector<Int>& localVec,
65  std::vector<Int>& allVec,
66  MPI_Comm comm );
67 
68 
69 
70 
71 
72 template <typename T>
73 void
74 Allgatherv (
75  std::vector<T>& localVec,
76  std::vector<T>& allVec,
77  MPI_Comm comm )
78 {
79 #ifndef _RELEASE_
80  PushCallStack("mpi::Allgatherv");
81 #endif
82  Int mpirank, mpisize;
83  MPI_Comm_rank( comm, &mpirank );
84  MPI_Comm_size( comm, &mpisize );
85 
86  Int localSize = localVec.size()*sizeof(T);
87  std::vector<Int> localSizeVec( mpisize );
88  std::vector<Int> localSizeDispls( mpisize );
89  MPI_Allgather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT, comm );
90  localSizeDispls[0] = 0;
91  for( Int ip = 1; ip < mpisize; ip++ ){
92  localSizeDispls[ip] = (localSizeDispls[ip-1] + localSizeVec[ip-1])*sizeof(T);
93  }
94  Int totalSize = (localSizeDispls[mpisize-1] + localSizeVec[mpisize-1])*sizeof(T);
95 
96  allVec.clear();
97  allVec.resize( totalSize/sizeof(T) );
98 
99  MPI_Allgatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
100  &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, comm );
101 
102 #ifndef _RELEASE_
103  PopCallStack();
104 #endif
105 
106  return ;
107 }; // ----- end of function Allgatherv -----
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 // *********************************************************************
123 // Send / Recv for stringstream
124 //
125 // Isend / Irecv is not here because the size and content has to be
126 // communicated separately for non-blocking communication.
127 // *********************************************************************
128 
129 void Send( std::stringstream& sstm, Int dest, Int tagSize, Int tagContent,
130  MPI_Comm comm );
131 
132 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
133  MPI_Comm comm, MPI_Status& statSize, MPI_Status& statContent );
134 
135 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
136  MPI_Comm comm );
137 
138 // *********************************************************************
139 // Waitall
140 // *********************************************************************
141 
142 void
143 Wait ( MPI_Request& req );
144 
145 void
146 Waitall ( std::vector<MPI_Request>& reqs, std::vector<MPI_Status>& stats );
147 
148 void
149 Waitall ( std::vector<MPI_Request>& reqs );
150 
151 // *********************************************************************
152 // Reduce
153 // *********************************************************************
154 
155 void
156 Reduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
157 
158 void
159 Reduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
160 
161 
162 #ifdef _USE_MPI3_
163 void
164 Ireduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
165 
166 void
167 Ireduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
168 #endif
169 
170 void
171 Allreduce ( Int* sendbuf, Int* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
172 
173 void
174 Allreduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
175 
176 void
177 Allreduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
178 
179 // *********************************************************************
180 // Alltoall
181 // *********************************************************************
182 
183 void
184 Alltoallv ( Int *bufSend, Int *sizeSend, Int *displsSend,
185  Int *bufRecv, Int *sizeRecv,
186  Int *displsRecv, MPI_Comm comm );
187 
188 void
189 Alltoallv ( Real *bufSend, Int *sizeSend, Int *displsSend,
190  Real *bufRecv, Int *sizeRecv,
191  Int *displsRecv, MPI_Comm comm );
192 
193 void
194 Alltoallv ( Complex *bufSend, Int *sizeSend, Int *displsSend,
195  Complex *bufRecv, Int *sizeRecv,
196  Int *displsRecv, MPI_Comm comm );
197 
198 } // namespace mpi
199 
200 
201 } // namespace PEXSI
202 
203 
204 
205 #endif // _MPI_INTERF_HPP_
206 
Environmental variables.