-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_manipulation.cc
313 lines (279 loc) · 10.3 KB
/
matrix_manipulation.cc
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright 2007 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <float.h>
#include <cmath>
#include "matrix_manipulation.h"
#include "io.h"
#include "util.h"
#include "matrix.h"
#include "parallel_interface.h"
#include "document.h"
#include "kernel.h"
#include "pd_ipm_parm.h"
namespace psvm {
// Cholesky factorization: factorize matrix A into LL^T.
// "original" represents A, low_triangular represents L.
// The memory block of L is allocated in this function, which will be freed
// by calling Destory() (which is done in the destructor of LLMatrix).
bool MatrixManipulation::CF(const LLMatrix &original,
LLMatrix *low_triangular) {
CHECK(low_triangular);
int dim = original.GetDim();
low_triangular->Init(dim);
for (int i = 0; i < dim; ++i) {
for (int j = i; j < dim; ++j) {
double sum = original.Get(j, i);
for (int k = i-1; k >= 0; --k) {
sum -= low_triangular->Get(i, k) * low_triangular->Get(j, k);
}
if (i == j) {
if (sum <= 0) { // sum should be larger than 0
cerr << "Only symmetric positive definite matrix can perform"
" Cholesky factorization.";
return false;
}
low_triangular->Set(i, i, sqrt(sum));
} else {
low_triangular->Set(j, i, sum/low_triangular->Get(i, i));
}
}
}
return true;
}
// For the processor storing the pivot sample in local machine (here, 'doc'),
// *pivot_sample will be assigned to be the pointer to the pivot sample.
// For other processors, pivot sample data will be packed and broadcasted from
// the processor owning it, and a Sample '**pivot_sample' will be allocated to
// store pivot sample data.
//
// 'pivot_global_index' is used to verify whether the pivot sample is stored
// in current processor (in 'doc').
void MatrixManipulation::BroadcastPivotSample(const Document &doc,
int pivot_global_index,
Sample **pivot_sample) {
CHECK(pivot_sample);
ParallelInterface *mpi = ParallelInterface::GetParallelInterface();
int parallel_id = mpi->GetProcId();
int pnum = mpi->GetNumProcs();
int pivot_local_index = mpi->ComputeGlobalToLocal(pivot_global_index,
parallel_id);
int buff_size = 0;
char *buffer = NULL;
if (pivot_local_index != -1) {
// Pack the pivot sample data
*pivot_sample = const_cast<Sample*>(doc.GetLocalSample(pivot_local_index));
buff_size = Document::PackSample(buffer, **pivot_sample);
// Broadcast package size
mpi->Bcast(&buff_size, 1, MPI_INT, parallel_id, MPI_COMM_WORLD);
// Broadcast package
mpi->Bcast(buffer, buff_size, MPI_BYTE, parallel_id, MPI_COMM_WORLD);
} else {
int root = pivot_global_index % pnum;
// Broadcast package size
mpi->Bcast(&buff_size, 1, MPI_INT, root, MPI_COMM_WORLD);
// Prepare a suitable memory block to receive package.
buffer = new char[buff_size];
// Broadcast package
mpi->Bcast(buffer, buff_size, MPI_BYTE, root, MPI_COMM_WORLD);
Document::UnpackSample(*pivot_sample, buffer);
}
delete [] buffer;
}
void MatrixManipulation::FreePivotSample(Sample *pivot_sample,
int pivot_index) {
ParallelInterface *mpi = ParallelInterface::GetParallelInterface();
int parallel_id = mpi->GetProcId();
if (mpi->ComputeGlobalToLocal(pivot_index, parallel_id) == -1) {
delete pivot_sample;
}
}
void MatrixManipulation::ICF(const Document &doc, const Kernel& kernel,
int rows, int columns, double threshold,
ParallelMatrix *icf) {
CHECK(icf);
ParallelInterface *mpi = ParallelInterface::GetParallelInterface();
int parallel_id = mpi->GetProcId();
int pnum = mpi->GetNumProcs();
icf->Init(rows, columns);
int local_rows = mpi->ComputeNumLocal(rows);
int* pivot = new int[columns];
bool* pivot_selected = new bool[local_rows];
// diag1: the diagonal part of Q (the kernal matrix diagonal
// diag2: the quadratic sum of a row of the ICF matrix
double* diag1 = new double[local_rows];
double* diag2 = new double[local_rows];
for (int i = 0; i < local_rows; ++i) {
diag1[i] = kernel.CalcKernelWithLabel(*doc.GetLocalSample(i),
*doc.GetLocalSample(i));
diag2[i] = 0;
pivot_selected[i] = 0;
}
double* header_row = new double[columns];
for (int column = 0; column < columns; column++) {
// Get global trace
double local_trace = 0;
for (int i = 0; i < local_rows; i++) {
// If pivot_selected[i] == true, diag1[i] - diag2[i] == 0,
// summation is not needed.
if (pivot_selected[i] == false) local_trace += diag1[i] - diag2[i];
}
double global_trace = DBL_MAX;
mpi->AllReduce(&local_trace, &global_trace, 1, MPI_DOUBLE, MPI_SUM,
MPI_COMM_WORLD);
// Test stop criterion
if (global_trace < threshold) {
icf->SetNumCols(column);
if (parallel_id == 0) {
cout << "reset columns from " << columns
<< " to " << icf->GetNumCols();
}
break;
}
// Find local pivot
Pivot local_pivot;
local_pivot.pivot_value = -DBL_MAX;
local_pivot.pivot_index = -1;
for (int i = 0; i < local_rows; ++i) {
double tmp = diag1[i] - diag2[i];
if (pivot_selected[i] == false && tmp > local_pivot.pivot_value) {
local_pivot.pivot_index = mpi->ComputeLocalToGlobal(i, parallel_id);
local_pivot.pivot_value = tmp;
}
}
// Get global pivot (MPI_Reduce is used)
Pivot global_pivot;
mpi->AllReduce(&local_pivot, &global_pivot, 1,
MPI_DOUBLE_INT, MPI_MAXLOC, MPI_COMM_WORLD);
// Update pivot vector
pivot[column] = global_pivot.pivot_index;
// Broadcast pivot sample
Sample *pivot_sample = NULL;
BroadcastPivotSample(doc, global_pivot.pivot_index, &pivot_sample);
// Broadcast the row corresponds to pivot.
int header_row_size = column + 1;
int localRowID = mpi->ComputeGlobalToLocal(global_pivot.pivot_index,
parallel_id);
if (localRowID != -1) {
icf->Set(localRowID, column, sqrt(global_pivot.pivot_value));
for (int j = 0; j < header_row_size; ++j) {
header_row[j] = icf->Get(localRowID, j);
}
mpi->Bcast(header_row, header_row_size,
MPI_DOUBLE, parallel_id, MPI_COMM_WORLD);
// Update pivot flag vector
pivot_selected[localRowID] = true;
} else {
int root = global_pivot.pivot_index % pnum;
mpi->Bcast(header_row, header_row_size,
MPI_DOUBLE, root, MPI_COMM_WORLD);
}
// Calculate the column'th column
// Note: 1. This order can improve numerical accuracy.
// 2. Cache is used, will be faster too.
for (int i = 0; i < local_rows; ++i) {
if (pivot_selected[i] == false) {
icf->Set(i, column, 0);
}
}
for (int k = 0; k < column; ++k) {
for (int i = 0; i < local_rows; ++i) {
if (pivot_selected[i] == false) {
icf->Set(i, column, icf->Get(i, column) -
icf->Get(i, k) * header_row[k]);
}
}
}
for (int i = 0; i < local_rows; ++i) {
if (pivot_selected[i] == false) {
icf->Set(i, column, icf->Get(i, column) +
kernel.CalcKernelWithLabel(*doc.GetLocalSample(i), *pivot_sample));
}
}
for (int i = 0; i < local_rows; ++i) {
if (pivot_selected[i] == false) {
icf->Set(i, column, icf->Get(i, column)/header_row[column]);
}
}
// Free pivot sample
FreePivotSample(pivot_sample, global_pivot.pivot_index);
// Update diagonal
for (int i = 0; i < local_rows; ++i) {
diag2[i] += icf->Get(i, column) * icf->Get(i, column);
}
}
delete[] pivot;
delete[] pivot_selected;
delete[] diag1;
delete[] diag2;
delete[] header_row;
}
void MatrixManipulation::ProductMM(const ParallelMatrix &icf,
const double *diagonal,
LLMatrix *product) {
CHECK(product);
int row = icf.GetNumLocalRows();
int column = icf.GetNumCols();
double* buff = new double[max(row, (column + 1) * column / 2)];
double* result = new double[(column + 1) * column / 2];
int offset = 0;
for (int i = 0; i < column; ++i) {
offset += i;
for (int p = 0; p < row; ++p) {
buff[p] = icf.Get(p, i) * diagonal[p];
}
for (int j = 0; j <= i; ++j) {
double tmp = 0;
for (int p = 0; p < row; ++p) {
tmp += buff[p] * icf.Get(p, j);
}
result[offset+j] = tmp;
}
}
ParallelInterface *pinterface = ParallelInterface::GetParallelInterface();
// Sum reduce the column*column matrix on each computer to Computer of ID:0
pinterface->Reduce(result, buff, (column + 1) * column / 2,
MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (pinterface->GetProcId() == 0) {
int disp = 0;
for (int i = 0; i < column; ++i) {
for (int j = 0; j <= i; ++j) {
product->Set(i, j, buff[disp++] + (i == j ? 1 : 0));
}
}
}
delete[] buff;
delete[] result;
}
void MatrixManipulation::CholBackwardSub(const LLMatrix &a, const double *b,
double *x) {
int dim = a.GetDim();
for (int k = dim - 1; k >= 0; --k) {
double tmp = b[k];
for (int i = k + 1; i < dim; ++i) {
tmp -= x[i] * a.Get(i, k);
}
x[k] = tmp / a.Get(k, k);
}
}
void MatrixManipulation::CholForwardSub(const LLMatrix &a, const double *b,
double *x) {
int dim = a.GetDim();
for (int k = 0; k < dim; ++k) {
double tmp = b[k];
for (int i = 0; i < k; ++i) {
tmp -= x[i] * a.Get(k, i);
}
x[k] = tmp / a.Get(k, k);
}
}
} // namespace psvm