-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlasMpi.cpp
245 lines (196 loc) · 7.43 KB
/
BlasMpi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*!
* @file BlasMpi.cpp
* @internal source of class BlasMpi
* @author Abal-Kassim Cheik Ahamed, Frédéric Magoulès, Sonia Toubaline
* @date Tue Nov 24 16:16:48 CET 2015
* @version 1.0
* @remarks
*/
// basic packages
// project packages
#include "BlasMpi.hpp"
#include "Vector.hpp"
#include "MatrixDense.hpp"
#include "DataTopology.hpp"
// third-party packages
//! @namespace BlasMpi
namespace BlasMpi {
________________________________________________________________________________
//! @internal compute matrix-vector product
int MatrixVectorProductBandRow (
Vector<double,int>& y_local,
const MatrixDense<double,int>& A_local,
const Vector<double,int>& x_local,
MPI_Comm& mpi_comm ) {
// -- number of processors
int numb_procs;
// -- get number of processes
MPI_Comm_size( mpi_comm, &numb_procs );
// -- size of the vector
int size_local = x_local.GetSize( );
// -- get global size (band-row splitting)
int size_global = 0;
MPI_Allreduce( &size_local, &size_global, 1, MPI_INT, MPI_SUM, mpi_comm );
// -- global vector
Vector<double,int> x_global( size_global );
// -- delete band list position and size
int* band_list_start = NULL;
int* band_list_size = NULL;
DataTopology::BandTopology( band_list_start, band_list_size,
size_global, numb_procs );
// MPI_Allgatherv - MPICH
// MPI_Allgatherv
// Gathers data from all tasks and deliver the combined data to all tasks
// Synopsis
// int MPI_Allgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
// void *recvbuf, const int *recvcounts, const int *displs,
// MPI_Datatype recvtype, MPI_Comm comm)
// Input Parameters
// sendbuf
// starting address of send buffer (choice)
// sendcount
// number of elements in send buffer (integer)
// sendtype
// data type of send buffer elements (handle)
// recvcounts
// integer array (of length group size) containing the number of elements that are to be received from each process
// displs
// integer array (of length group size). Entry i specifies the displacement (relative to recvbuf ) at which to place the incoming data from process i
// recvtype
// data type of receive buffer elements (handle)
// comm
// communicator (handle)
//
// Output Parameters
// recvbuf
// address of receive buffer (choice)
// -- assemble local vector to 'root', upon processors
MPI_Allgatherv( x_local.GetCoef( ), size_local, MPI_DOUBLE,
x_global.GetCoef( ), band_list_size, band_list_start,
MPI_DOUBLE, mpi_comm );
// -- delete band list position and size
delete [] band_list_start;
delete [] band_list_size;
// -- perform local matrix vector product: A_local * x_global
A_local.MatrixVectorProduct( y_local, x_global );
return 0;
}
________________________________________________________________________________
//! @internal compute matrix-vector product
int MatrixVectorProductBandColumn (
Vector<double,int>& y_global,
const MatrixDense<double,int>& A_local,
const Vector<double,int>& x_local,
const int root,
MPI_Comm& mpi_comm ) {
// -- local matrix: number of rows
int numb_rows_global = A_local.GetNumbRows( );
// -- global vector
Vector<double,int> y_global_tmp( numb_rows_global );
// -- perform local matrix vector product: A_local * x_local
A_local.MatrixVectorProduct( y_global_tmp, x_local );
// -- allocate global result
y_global.Allocate( numb_rows_global );
// reduce to root proc
MPI_Reduce( y_global_tmp.GetCoef( ), y_global.GetCoef( ),
numb_rows_global, MPI_DOUBLE, MPI_SUM, root, mpi_comm );
return 0;
}
________________________________________________________________________________
//! @internal compute matrix-vector product y_local:= A_local *x_local
int MatrixVectorProductBlock (
Vector<double,int>& y_local,
const MatrixDense<double,int>& A_local,
const Vector<double,int>& x_local,
const int root,
MPI_Comm& mpi_comm_rows,
MPI_Comm& mpi_comm_columns ) {
// -- local matrix: number of rows
int numb_rows = A_local.GetNumbRows( );
// -- tmp vector
Vector<double,int> y_local_tmp( numb_rows );
// -- perform local subdomain matrix vector product: A_local * x_local_global
A_local.MatrixVectorProduct( y_local_tmp, x_local );
// -- every row reduces its share of y_local
MPI_Reduce( y_local_tmp.GetCoef( ), y_local.GetCoef( ),
numb_rows, MPI_DOUBLE, MPI_SUM, root, mpi_comm_rows );
return 0;
}
________________________________________________________________________________
//! @internal compute matrix-matrix product C := A_local * B
//! @note number of processor: P = P_I * P_J
//! page 60, livre "Calcul Scientifique Parallèle"
//! for K = 1 to P_j
//! if J = K then
//! B_Temp = B
//! end if
//! MPI_Bcast(B_temp, longueur, type, K-1, mpi_comm_rows)
//! if I = K then
//! C_Temp = C
//! end if
//! MPI_Bcast(C_temp, longueur, type, K-1, mpi_comm_columns)
//! A_local = A_local + B_temp * C_temp
//!
int MatrixMatrixProductBlock (
MatrixDense<double,int>& C,
const MatrixDense<double,int>& A_local,
const MatrixDense<double,int>& B,
const int root,
MPI_Comm& mpi_comm_rows,
MPI_Comm& mpi_comm_columns ) {
// -- number of processors (i-)
int numb_procs_i;
// -- process number (process proc_numb_i)
int proc_numb_i;
// -- get number of processes
MPI_Comm_size( mpi_comm_rows, &numb_procs_i );
// -- get current process proc_numb_i
MPI_Comm_rank( mpi_comm_rows, &proc_numb_i );
// -- number of processors (j-)
int numb_procs_j;
// -- process number (process proc_numb_j)
int proc_numb_j;
// -- get number of processes
MPI_Comm_size( mpi_comm_columns, &numb_procs_j );
// -- get current process proc_numb_j
MPI_Comm_rank( mpi_comm_columns, &proc_numb_j );
// -- local matrix: number of rows
int A_local_numb_rows = A_local.GetNumbRows( );
// -- local matrix: number of columns
int A_local_numb_columns = A_local.GetNumbColumns( );
// -- local matrix: number of rows
int B_numb_rows = B.GetNumbRows( );
// -- local matrix: number of columns
int B_numb_columns = B.GetNumbColumns( );
// -- result local matrix: C
C.Allocate( A_local_numb_rows, B_numb_columns );
// **************** optimize, declaration A_local et B TODO
// -- temporary local matrix: A_local
MatrixDense<double,int> A_local_temp;
A_local_temp.Allocate( A_local_numb_rows, A_local_numb_columns );
// -- temporary local matrix: B
MatrixDense<double,int> B_temp;
B_temp.Allocate( B_numb_rows, B_numb_columns );
// -- initialize C to 0
C.Initialize( 0 );
// --
for ( int k = 0; k < numb_procs_j; k++ ) {
// -- A_local block communication
if ( proc_numb_i == k ) {
A_local_temp = A_local;
}
MPI_Bcast( A_local_temp.GetCoef( ), A_local_numb_rows*A_local_numb_columns,
MPI_DOUBLE, k, mpi_comm_rows );
// -- B block communication
if ( proc_numb_j == k ) {
B_temp = B;
}
MPI_Bcast( B_temp.GetCoef( ), B_numb_rows*B_numb_columns,
MPI_DOUBLE, k, mpi_comm_columns );
// -- local block multiplication: C = C + A_local_temp * B_temp
A_local_temp.MatrixMatrixProductAdd( C, B_temp );
}
return 0;
}
________________________________________________________________________________
} // namespace BlasMpi {