Repository navigation
Expand file tree
/
Copy pathmatrix_mult.cpp
More file actions
219 lines (178 loc) · 7.23 KB
/
Copy pathmatrix_mult.cpp
File metadata and controls
219 lines (178 loc) · 7.23 KB
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
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/python.hpp>
#include <vector>
#include <chrono>
#include <iostream>
#include <omp.h>
#define ENABLE_BOOST_PRECISION_TIMER 0
// Convert Python list to std::vector
std::vector<std::vector<double>> py_list_to_vector(const boost::python::list& py_list) {
std::vector<std::vector<double>> vec;
for (int i = 0; i < boost::python::len(py_list); ++i) {
boost::python::list sublist = boost::python::extract<boost::python::list>(py_list[i]);
std::vector<double> subvec;
for (int j = 0; j < boost::python::len(sublist); ++j) {
subvec.push_back(boost::python::extract<double>(sublist[j]));
}
vec.push_back(subvec);
}
return vec;
}
// Convert std::vector to Python list
boost::python::list vector_to_py_list(const std::vector<std::vector<double>>& vec) {
boost::python::list py_list;
for (const auto& subvec : vec) {
boost::python::list sublist;
for (double val : subvec) { // Fix type to double
sublist.append(val);
}
py_list.append(sublist);
}
return py_list;
}
void matrix_mult_threading(const std::vector<std::vector<double>>& A, const std::vector<std::vector<double>>& B, std::vector<std::vector<double>>& C,
const double& alpha, const double& beta, const unsigned int& number_of_core) {
uint64_t rows_A = A.size();
// Setting the maxinum of threads to half the number of rows if dimensions are greater than 128
// In this way a single thread has to worry about only 2 rows of the result matrix
if (rows_A <= 64)
{
omp_set_num_threads(2);
}
else
{
omp_set_num_threads(8);
}
// omp_set_num_threads(number_of_core);
uint64_t cols_A = A[0].size();
uint64_t cols_B = B[0].size();
std::vector<std::vector<double>> result(rows_A, std::vector<double>(cols_B, 0));
#pragma omp parallel
{
int T_ID = omp_get_thread_num();
auto max_threads = omp_get_num_threads();
// std::cout << "max threads = " << max_threads << std::endl;
auto range = rows_A / max_threads;
// Perform matrix multiplication and scaling
for (auto i = range * T_ID; i < (T_ID == max_threads - 1 ? rows_A : range * (T_ID + 1)); ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
for (uint64_t l = 0; l < cols_A; ++l) {
result[i][j] += A[i][l] * B[l][j];
}
result[i][j] *= alpha; // Scale the result by alpha
}
}
// Scale matrix C by beta and add to the result
#pragma omp barrier
for (auto i = range * T_ID; i < (T_ID == max_threads - 1 ? rows_A : range * (T_ID + 1)); ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
C[i][j] = beta * C[i][j] + result[i][j];
}
}
}
}
void matrix_mult_threading_b(const std::vector<std::vector<double>>& A, const std::vector<std::vector<double>>& B, std::vector<std::vector<double>>& C,
const double& alpha, const double& beta, const unsigned int& number_of_core) {
// omp_set_num_threads(20);
omp_set_num_threads(omp_get_max_threads());
uint64_t rows_A = A.size();
uint64_t cols_A = A[0].size();
uint64_t cols_B = B[0].size();
// Result matrix
std::vector<std::vector<double>> result(rows_A, std::vector<double>(cols_B, 0));
// Set the number of threads to be used
omp_set_num_threads(omp_get_max_threads());
// omp_set_num_threads(number_of_core);
#pragma omp parallel for collapse(2) schedule(dynamic)
for (uint64_t i = 0; i < rows_A; ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
for (uint64_t l = 0; l < cols_A; ++l) {
result[i][j] += A[i][l] * B[l][j];
}
result[i][j] *= alpha; // Scale the result by alpha
}
}
// Scale matrix C by beta and add to the result
#pragma omp parallel for collapse(2) schedule(dynamic)
for (uint64_t i = 0; i < rows_A; ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
C[i][j] = beta * C[i][j] + result[i][j];
}
}
}
// Standard GEMM formula is:
// C = α⋅A⋅B + β⋅C
// Matrix multiplication function matrix_mult(vec_A, vec_B, vec_C, alpha, beta);
void matrix_mult(const std::vector<std::vector<double>>& A, const std::vector<std::vector<double>>& B, std::vector<std::vector<double>>& C,
const double& alpha, const double& beta) {
uint64_t rows_A = A.size();
uint64_t cols_A = A[0].size();
uint64_t rows_B = B.size();
uint64_t cols_B = B[0].size();
uint64_t rows_C = C.size();
uint64_t cols_C = C[0].size();
if ((cols_A != rows_B) || (rows_A != rows_C || cols_B != cols_C)) {
throw std::invalid_argument("Number of columns in A must be equal to number of rows in B");
}
std::vector<std::vector<double>> result(rows_A, std::vector<double>(cols_B, 0));
#if ENABLE_BOOST_PRECISION_TIMER
auto start = std::chrono::high_resolution_clock::now(); // Start timing
#endif
// Perform matrix multiplication and scaling
for (uint64_t i = 0; i < rows_A; ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
for (uint64_t l = 0; l < cols_A; ++l) {
result[i][j] += A[i][l] * B[l][j];
}
result[i][j] *= alpha;
}
}
// Scale matrix C by beta and add to the result
for (uint64_t i = 0; i < rows_A; ++i) {
for (uint64_t j = 0; j < cols_B; ++j) {
C[i][j] = beta * C[i][j] + result[i][j];
}
}
#if ENABLE_BOOST_PRECISION_TIMER
auto end = std::chrono::high_resolution_clock::now(); // End timing
std::chrono::duration<double> elapsed = end - start; // Calculate elapsed time
std::cout << "Matrix multiplication took " << elapsed.count() << " seconds.\n"; // Print elapsed time
#endif
}
// Wrapper function to handle Python list inputs and outputs
boost::python::list matrix_mult_py(const boost::python::list& A, const boost::python::list& B, const boost::python::list& C,
const double& alp, const double& be, const unsigned int& optimization_num, const unsigned int& number_of_core) {
std::vector<std::vector<double>> vec_A = py_list_to_vector(A);
std::vector<std::vector<double>> vec_B = py_list_to_vector(B);
std::vector<std::vector<double>> vec_C = py_list_to_vector(C);
double alpha = alp;
double beta = be;
switch (optimization_num)
{
case 0:
matrix_mult(vec_A, vec_B, vec_C, alpha, beta);
break;
<<<<<<< HEAD
case 1:
matrix_mult_threading(vec_A, vec_B, vec_C, alpha, beta);
break;
case 2:
matrix_mult_threading_b(vec_A, vec_B, vec_C, alpha, beta);
=======
case 1: // WIP (Murtaza)
matrix_mult_threading(vec_A, vec_B, vec_C, alpha, beta, number_of_core);
break;
case 2:
matrix_mult_threading_b(vec_A, vec_B, vec_C, alpha, beta, number_of_core);
// WIP
>>>>>>> 0741f04 (results)
break;
default:
throw std::invalid_argument("Choose a correct optimization number");
}
return vector_to_py_list(vec_C); // vec_C will store the result
}
BOOST_PYTHON_MODULE(matrix_mult) {
using namespace boost::python;
def("matrix_mult", matrix_mult_py);
}