Newer
Older
#include "distributedmatrix.hpp"
#include "matrix.hpp"
#include "mlp_sgd_distributed.cpp"
#include <mpi.h>
#include <iostream>
#include <cassert>
#include <cmath>
#include <functional>
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
// Helper function to test if two doubles are approximately equal
bool approxEqual(double a, double b, double epsilon = 1e-10) {
return std::abs(a - b) < epsilon;
}
// Helper function to test if two matrices are approximately equal
bool matricesEqual(const Matrix& a, const Matrix& b, double epsilon = 1e-10) {
if (a.numRows() != b.numRows() || a.numCols() != b.numCols()) {
return false;
}
for (int i = 0; i < a.numRows(); i++) {
for (int j = 0; j < a.numCols(); j++) {
if (!approxEqual(a.get(i, j), b.get(i, j), epsilon)) {
return false;
}
}
}
return true;
}
// Test constructor and basic properties
void testConstructorAndBasics() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create a test matrix
Matrix testMatrix(3, 4);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
testMatrix.set(i, j, i * 10 + j);
}
}
// Create distributed matrix
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix distMatrix(testMatrix, numProcs);
// Check dimensions
assert(distMatrix.numRows() == 3);
assert(distMatrix.numCols() == 4);
// Gather and check equality with original
Matrix gathered = distMatrix.gather();
assert(matricesEqual(gathered, testMatrix));
if (rank == 0) {
std::cout << "Constructor and basic properties test passed!" << std::endl;
}
}
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
// Test column distribution
void testColumnDistribution() {
int rank, numProcs;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
// Create a test matrix with more columns to better test distribution
int cols = numProcs * 2 + 1; // Ensure some remainder for testing
Matrix testMatrix(3, cols);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < cols; j++) {
testMatrix.set(i, j, i * 100 + j);
}
}
// Create distributed matrix
DistributedMatrix distMatrix(testMatrix, numProcs);
// Check local data dimensions
const Matrix& localData = distMatrix.getLocalData();
// Calculate expected column distribution
int baseCols = cols / numProcs;
int remainder = cols % numProcs;
int expectedLocalCols = baseCols + (rank < remainder ? 1 : 0);
assert(localData.numRows() == 3);
assert(localData.numCols() == expectedLocalCols);
// Check global/local column index conversion
for (int j = 0; j < localData.numCols(); j++) {
int globalJ = distMatrix.globalColIndex(j);
assert(distMatrix.localColIndex(globalJ) == j);
assert(distMatrix.ownerProcess(globalJ) == rank);
}
if (rank == 0) {
std::cout << "Column distribution test passed!" << std::endl;
}
}
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
// Test gather function
void testGather() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create a test matrix
Matrix testMatrix(4, 6);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 6; j++) {
testMatrix.set(i, j, i * 10 + j);
}
}
// Create distributed matrix
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix distMatrix(testMatrix, numProcs);
// Gather and check equality with original
Matrix gathered = distMatrix.gather();
assert(matricesEqual(gathered, testMatrix));
if (rank == 0) {
std::cout << "Gather function test passed!" << std::endl;
}
}
// Test the get and set functions (may throw exceptions)
void testGetAndSet() {
int rank, numProcs;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
// Skip this test if only one process
if (numProcs == 1) {
if (rank == 0) {
std::cout << "Get/Set test skipped (requires multiple processes)" << std::endl;
}
return;
}
// Create a test matrix
Matrix testMatrix(2, numProcs);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < numProcs; j++) {
testMatrix.set(i, j, i * numProcs + j);
}
}
// Create distributed matrix - each process gets exactly one column
DistributedMatrix distMatrix(testMatrix, numProcs);
// Test local access - should work
bool localAccessWorks = true;
try {
double val = distMatrix.get(1, rank);
assert(approxEqual(val, 1 * numProcs + rank));
distMatrix.set(1, rank, 99.0);
val = distMatrix.get(1, rank);
assert(approxEqual(val, 99.0));
} catch (std::exception& e) {
localAccessWorks = false;
}
assert(localAccessWorks);
// Test remote access - should throw
if (numProcs > 1) {
bool remoteAccessFails = false;
int remoteRank = (rank + 1) % numProcs;
try {
(void)distMatrix.get(1, remoteRank);
} catch (std::exception& e) {
remoteAccessFails = true;
}
assert(remoteAccessFails);
remoteAccessFails = false;
try {
distMatrix.set(1, remoteRank, 100.0);
} catch (std::exception& e) {
remoteAccessFails = true;
}
assert(remoteAccessFails);
}
if (rank == 0) {
std::cout << "Get/Set function test passed!" << std::endl;
}
}
// Test copy constructor
void testCopyConstructor() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create a test matrix
Matrix testMatrix(3, 5);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
testMatrix.set(i, j, i * 5 + j);
}
}
// Create distributed matrix
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix original(testMatrix, numProcs);
// Create a copy
DistributedMatrix copy(original);
// Check basic properties
assert(copy.numRows() == original.numRows());
assert(copy.numCols() == original.numCols());
// Check local data
const Matrix& originalLocal = original.getLocalData();
const Matrix& copyLocal = copy.getLocalData();
assert(matricesEqual(originalLocal, copyLocal));
// Modify copy and check independence
auto doubleFunc = [](double x) { return 2 * x; };
DistributedMatrix modifiedCopy = copy.apply(doubleFunc);
Matrix originalGathered = original.gather();
Matrix modifiedGathered = modifiedCopy.gather();
assert(!matricesEqual(originalGathered, modifiedGathered));
if (rank == 0) {
std::cout << "Copy constructor test passed!" << std::endl;
}
}
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
// Test apply function
void testApply() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create a test matrix
Matrix testMatrix(2, 5);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
testMatrix.set(i, j, i + j);
}
}
// Create distributed matrix
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix distMatrix(testMatrix, numProcs);
// Apply a function to square each element
auto squareFunc = [](double x) { return x * x; };
DistributedMatrix squaredMatrix = distMatrix.apply(squareFunc);
// Create expected result
Matrix expectedMatrix = testMatrix.apply(squareFunc);
// Gather and check
Matrix gathered = squaredMatrix.gather();
assert(matricesEqual(gathered, expectedMatrix));
if (rank == 0) {
std::cout << "Apply function test passed!" << std::endl;
}
}
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
// Test applyBinary function
void testApplyBinary() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create test matrices
Matrix testMatrix1(3, 4);
Matrix testMatrix2(3, 4);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
testMatrix1.set(i, j, i + j);
testMatrix2.set(i, j, i * j);
}
}
// Create distributed matrices
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix distMatrix1(testMatrix1, numProcs);
DistributedMatrix distMatrix2(testMatrix2, numProcs);
// Apply binary function (add)
auto addFunc = [](double a, double b) { return a + b; };
DistributedMatrix resultMatrix = DistributedMatrix::applyBinary(distMatrix1, distMatrix2, addFunc);
// Create expected result
Matrix expectedMatrix(3, 4);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
expectedMatrix.set(i, j, testMatrix1.get(i, j) + testMatrix2.get(i, j));
}
}
// Gather and check
Matrix gathered = resultMatrix.gather();
assert(matricesEqual(gathered, expectedMatrix));
if (rank == 0) {
std::cout << "ApplyBinary function test passed!" << std::endl;
}
}
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
void testSum() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create test matrix
Matrix matrixFull(3, 5);
double total = 0.0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
matrixFull.set(i, j, i * 5 + j + 1);
total += i * 5 + j + 1;
}
}
// Create a distributed matrix
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix matrix(matrixFull, numProcs);
// Compute the sum
double result = matrix.sum();
// Check
assert(approxEqual(result, total, 1e-8));
if (rank == 0) {
std::cout << "Sum test passed!" << std::endl;
}
}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Test matrix multiplication (Matrix * DistributedMatrix)
void testMultiply() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create test matrices
Matrix leftMatrix(2, 3);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
leftMatrix.set(i, j, i * 3 + j + 1); // 1-based values for better testing
}
}
Matrix rightMatrixFull(3, 4);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
rightMatrixFull.set(i, j, i * 4 + j + 1); // 1-based values
}
}
// Create distributed matrix for right operand
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix rightMatrix(rightMatrixFull, numProcs);
// Multiply
DistributedMatrix resultMatrix = multiply(leftMatrix, rightMatrix);
// Compute expected result
Matrix expectedMatrix = leftMatrix * rightMatrixFull;
// Gather and check
Matrix gathered = resultMatrix.gather();
assert(matricesEqual(gathered, expectedMatrix, 1e-8)); // Use larger epsilon for multiplication
if (rank == 0) {
std::cout << "Matrix multiplication test passed!" << std::endl;
}
}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Test multiplyTransposed
void testMultiplyTransposed() {
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create test matrices
Matrix matrix1Full(3, 5);
Matrix matrix2Full(4, 5);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
matrix1Full.set(i, j, i * 5 + j + 1);
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
matrix2Full.set(i, j, i * 5 + j + 2);
}
}
// Create distributed matrices
int numProcs;
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
DistributedMatrix matrix1(matrix1Full, numProcs);
DistributedMatrix matrix2(matrix2Full, numProcs);
// Compute expected result
Matrix expectedMatrix = matrix1Full * matrix2Full.transpose();
if (rank == 0) { // pour éviter que tous les processus n'impriment
std::cout << "Résultat espere:" << std::endl;
for (int i = 0; i < expectedMatrix.numRows(); ++i) {
for (int j = 0; j < expectedMatrix.numCols(); ++j) {
std::cout << expectedMatrix.get(i, j) << " ";
}
std::cout << std::endl;
}
}
// Compute A * B^T
Matrix result = matrix1.multiplyTransposed(matrix2);
// Check
assert(matricesEqual(result, expectedMatrix, 1e-8));
if (rank == 0) {
std::cout << "MultiplyTransposed test passed!" << std::endl;
}
}
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
void test_distributed_mlp_training()
{
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Print info about the MPI environment
if (rank == 0) {
std::cout << "Running with " << size << " MPI processes." << std::endl;
}
// Create a simple XOR dataset
// Create data
Matrix X(3, 4);
Matrix Y(1, 4);
// 0
X.set(0, 0, 0.0);
X.set(1, 0, 0.0);
X.set(2, 0, 1.0);
Y.set(0, 0, 0.0);
// 1
X.set(0, 1, 0.0);
X.set(1, 1, 1.0);
X.set(2, 1, 1.0);
Y.set(0, 1, 1.0);
// 2
X.set(0, 2, 1.0);
X.set(1, 2, 0.0);
X.set(2, 2, 1.0);
Y.set(0, 2, 1.0);
// 3
X.set(0, 3, 1.0);
X.set(1, 3, 1.0);
X.set(2, 3, 1.0);
Y.set(0, 3, 0.0);
// Distribute the data
Dataset data = Dataset(DistributedMatrix(X, size), DistributedMatrix(Y, size));
// Create and train the model
MLP model(3, 128, 1, 0.1);
if (rank == 0) {
std::cout << "Training distributed MLP for XOR problem..." << std::endl;
}
model.train(data, 5000);
if (rank==0) {
std::cout << "Distributed MLP training test completed." << std::endl;
}
}
int main(int argc, char** argv) {
// Initialize MPI
int initialized;
MPI_Initialized(&initialized);
if (!initialized) {
MPI_Init(&argc, &argv);
}
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
std::cout << "Starting DistributedMatrix tests..." << std::endl;
}
try {
// Run tests
if (rank == 0) {
std::cout << "All tests passed successfully!" << std::endl;
}
}
catch (std::exception& e) {
if (rank == 0) {
std::cerr << "Test failed with exception: " << e.what() << std::endl;
}
MPI_Abort(MPI_COMM_WORLD, 1);
}
// Finalize MPI if we initialized it
// int finalized;
// MPI_Finalized(&finalized);
// if (!finalized && initialized) {
// MPI_Finalize();
// }
MPI_Finalize();
return 0;
}