-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
142 lines (132 loc) · 4.97 KB
/
utils.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
//
// Created by WYF on 2021/12/7.
//
#include "utils.h"
#include <mpi.h>
#include <memory>
#include <functional>
#include <iostream>
#include <random>
#include <cmath>
#include <cstring>
template<typename T>
using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)>>;
static void MPI_Datatype_Deleter(MPI_Datatype *p) {
MPI_Type_free(p);
}
void matrix_gather(MPI_Comm comm, const double M[], const int n_row, const int n_col, double result[], int root) {
int rank, size, pr, pc;
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
if (rank == root) {
int dims[2], periods[2], my_coords[2];
MPI_Cart_get(comm, 2, dims, periods, my_coords);
pr = dims[0];
pc = dims[1];
MPI_Datatype subarray, resized_subarray;
int sizes[] = {pr * n_row, pc * n_col};
int sub_sizes[] = {n_row, n_col};
int starts[] = {0, 0};
MPI_Type_create_subarray(2, sizes, sub_sizes, starts, MPI_ORDER_C, MPI_DOUBLE, &subarray);
MPI_Type_commit(&subarray);
deleted_unique_ptr<MPI_Datatype> subarray_owner(&subarray, MPI_Datatype_Deleter);
MPI_Type_create_resized(subarray, 0, sizeof(double) * n_col, &resized_subarray);
MPI_Type_commit(&resized_subarray);
deleted_unique_ptr<MPI_Datatype> resized_subarray_owner(&resized_subarray, MPI_Datatype_Deleter);
int displs[size];
int recvcounts[size];
for (int i = 0; i < size; ++i) {
int coords[2];
MPI_Cart_coords(comm, i, 2, coords);
displs[i] = coords[0] * pc * n_row + coords[1];
recvcounts[i] = 1;
}
MPI_Gatherv(M, n_col * n_row, MPI_DOUBLE, result, recvcounts, displs, resized_subarray, root, comm);
// MPI_Type_free(&resized_subarray);
// MPI_Type_free(&subarray);
} else {
MPI_Gatherv(M, n_col * n_row, MPI_DOUBLE, nullptr, nullptr, nullptr, MPI_DOUBLE, root, comm);
}
}
void matrix_scatter(MPI_Comm comm, const double M[], const int n_row, const int n_col, double result[], int root) {
int rank, size, pr, pc;
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
if (rank == root) {
int dims[2], periods[2], my_coords[2];
MPI_Cart_get(comm, 2, dims, periods, my_coords);
pr = dims[0];
pc = dims[1];
MPI_Datatype subarray, resized_subarray;
int sizes[] = {pr * n_row, pc * n_col};
int sub_sizes[] = {n_row, n_col};
int starts[] = {0, 0};
MPI_Type_create_subarray(2, sizes, sub_sizes, starts, MPI_ORDER_C, MPI_DOUBLE, &subarray);
MPI_Type_commit(&subarray);
deleted_unique_ptr<MPI_Datatype> subarray_owner(&subarray, MPI_Datatype_Deleter);
MPI_Type_create_resized(subarray, 0, sizeof(double) * n_col, &resized_subarray);
MPI_Type_commit(&resized_subarray);
deleted_unique_ptr<MPI_Datatype> resized_subarray_owner(&resized_subarray, MPI_Datatype_Deleter);
int displs[size];
int sendcounts[size];
for (int i = 0; i < size; ++i) {
int coords[2];
MPI_Cart_coords(comm, i, 2, coords);
displs[i] = coords[0] * pc * n_row + coords[1];
sendcounts[i] = 1;
}
MPI_Scatterv(M, sendcounts, displs, resized_subarray, result, n_row * n_col, MPI_DOUBLE, root, comm);
// MPI_Type_free(&resized_subarray);
// MPI_Type_free(&subarray);
} else {
MPI_Scatterv(nullptr, nullptr, nullptr, MPI_UNDEFINED, result, n_col * n_row, MPI_DOUBLE, root, comm);
}
}
void print_matrix(const double M[], const int n_row, const int n_col) {
for (int i = 0; i < n_row; ++i) {
for (int j = 0; j < n_col; ++j) {
std::cout << M[i * n_col + j] << '\t';
}
std::cout << std::endl;
}
}
void init_matrix(double M[], const int n_row, const int n_col) {
#ifdef DEBUG
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
std::default_random_engine re((std::random_device()) ());
std::uniform_real_distribution<double> dist(-1.0, 1.0);
double n = 1;
#endif
for (int i = 0; i < n_row; ++i) {
for (int j = 0; j < n_col; ++j) {
#ifdef DEBUG
M[i * n_col + j] = rank;
#else
M[i * n_col + j] = dist(re);
#endif
}
}
}
double validate_matrix(const double M[], const double N[], const int n_row, const int n_col) {
double max_err = 0.0;
for (int i = 0; i < n_row; ++i) {
for (int j = 0; j < n_col; ++j) {
size_t idx = i * n_col + j;
double err = std::abs(M[idx] - N[idx]);
if (err > max_err) max_err = err;
}
}
return max_err;
}
void matmul(const double A[], const double B[], double C[], const int row_A, const int col_A, const int col_B) {
memset(C, 0, row_A * col_B * sizeof(double));
for (int i = 0; i < row_A; ++i) {
for (int k = 0; k < col_A; ++k) {
for (int j = 0; j < col_B; ++j) {
C[i * col_B + j] += A[i * col_A + k] * B[k * col_B + j];
}
}
}
}