-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransposition.cpp
More file actions
35 lines (29 loc) · 1.01 KB
/
transposition.cpp
File metadata and controls
35 lines (29 loc) · 1.01 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
#include "transposition.h"
#include <boost/thread/future.hpp>
#include <algorithm>
class TransposeJob {
const TMatrix& A;
TMatrix& Result;
const size_t StartRow;
const size_t EndRow;
public:
TransposeJob(const TMatrix& a, TMatrix& result, const size_t stRow, const size_t endRow)
: A(a), Result(result), StartRow(stRow), EndRow(endRow)
{}
void operator()() {
for (size_t i = StartRow; i < EndRow; ++i) {
Result[i].resize(A.size());
for (size_t j = 0; j < A.size(); ++j)
Result[i][j] = A[j][i];
}
}
};
void TransposeMatrixParallel(const TMatrix& a, TMatrix& result) {
result.resize(a[0].size());
const size_t RowsPerJob = (result.size() + NPROC - 1) / NPROC;
std::vector<boost::unique_future<void>> hndls(NPROC);
for (size_t i = 1; i < NPROC; ++i)
hndls[i] = boost::async(TransposeJob(a, result, i * RowsPerJob, std::min((i+1)*RowsPerJob, result.size())));
TransposeJob(a, result, 0, RowsPerJob)();
boost::wait_for_all(hndls.begin() + 1, hndls.end());
}