Skip to content

Commit 9ce8ba4

Browse files
committed
Apply clang-tidy fixes and formatting
1 parent 559181f commit 9ce8ba4

File tree

5 files changed

+82
-76
lines changed

5 files changed

+82
-76
lines changed

tasks/klimov_m_torus/common/include/common.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace klimov_m_torus {
99

1010
struct TransferRequest {
11-
int sender;
12-
int receiver;
11+
int sender{};
12+
int receiver{};
1313
std::vector<int> data;
1414
};
1515

tasks/klimov_m_torus/mpi/include/ops_mpi.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class TorusMeshCommunicator : public BaseTask {
2222
bool RunImpl() override;
2323
bool PostProcessingImpl() override;
2424

25-
static std::pair<int, int> CalculateGridSize(int totalProcesses);
25+
static std::pair<int, int> CalculateGridSize(int total_processes);
2626
static int CombineCoordinates(int row, int col, int rows, int cols);
2727
static std::pair<int, int> SplitRank(int rank, int cols);
2828
static std::vector<int> BuildMessageRoute(int rows, int cols, int from, int to);
2929

3030
void DistributeSenderReceiver(int &src, int &dst);
3131
void DistributeDataLength(int src, int &len) const;
32-
std::vector<int> AssembleSendBuffer(int src, int len) const;
32+
[[nodiscard]] std::vector<int> AssembleSendBuffer(int src, int len) const;
3333
void RelayMessage(int src, int dst, const std::vector<int> &route, const std::vector<int> &buffer,
3434
std::vector<int> &output) const;
3535
void SaveFinalResult(int dst, const std::vector<int> &output, const std::vector<int> &route);

tasks/klimov_m_torus/mpi/src/ops_mpi.cpp

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,65 @@
1212

1313
namespace klimov_m_torus {
1414

15+
namespace {
16+
17+
// Вспомогательные функции, вынесенные в анонимный namespace для снижения сложности RelayMessage
18+
19+
void HandleSameNode(int current_rank, int src, const std::vector<int> &buffer, std::vector<int> &output) {
20+
if (current_rank == src) {
21+
output = buffer;
22+
}
23+
}
24+
25+
void HandleSourceNode(int current_rank, int src, const std::vector<int> &route, const std::vector<int> &buffer,
26+
std::vector<int> &output) {
27+
output = buffer;
28+
if (current_rank == src && route.size() > 1) {
29+
int next_hop = route[1];
30+
int send_len = static_cast<int>(buffer.size());
31+
MPI_Send(&send_len, 1, MPI_INT, next_hop, 0, MPI_COMM_WORLD);
32+
if (send_len > 0) {
33+
MPI_Send(output.data(), send_len, MPI_INT, next_hop, 1, MPI_COMM_WORLD);
34+
}
35+
}
36+
}
37+
38+
void HandleIntermediateNode(int current_rank, int dst, const std::vector<int> &route, int my_pos,
39+
std::vector<int> &output) {
40+
int prev_hop = route[my_pos - 1];
41+
int recv_len = 0;
42+
MPI_Recv(&recv_len, 1, MPI_INT, prev_hop, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
43+
output.resize(recv_len);
44+
if (recv_len > 0) {
45+
MPI_Recv(output.data(), recv_len, MPI_INT, prev_hop, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
46+
}
47+
48+
if (current_rank != dst && my_pos + 1 < static_cast<int>(route.size())) {
49+
int next_hop = route[my_pos + 1];
50+
MPI_Send(&recv_len, 1, MPI_INT, next_hop, 0, MPI_COMM_WORLD);
51+
if (recv_len > 0) {
52+
MPI_Send(output.data(), recv_len, MPI_INT, next_hop, 1, MPI_COMM_WORLD);
53+
}
54+
}
55+
}
56+
57+
} // namespace
58+
1559
TorusMeshCommunicator::TorusMeshCommunicator(const InType &in) {
1660
SetTypeOfTask(GetStaticTypeOfTask());
1761
GetInput() = in;
1862
GetOutput() = {};
1963
}
2064

21-
std::pair<int, int> TorusMeshCommunicator::CalculateGridSize(int totalProcesses) {
22-
int rows = static_cast<int>(std::sqrt(static_cast<double>(totalProcesses)));
23-
while (rows > 1 && (totalProcesses % rows != 0)) {
65+
std::pair<int, int> TorusMeshCommunicator::CalculateGridSize(int total_processes) {
66+
int rows = static_cast<int>(std::sqrt(static_cast<double>(total_processes)));
67+
while (rows > 1 && (total_processes % rows != 0)) {
2468
--rows;
2569
}
2670
if (rows <= 0) {
2771
rows = 1;
2872
}
29-
int cols = totalProcesses / rows;
73+
int cols = total_processes / rows;
3074
if (cols <= 0) {
3175
cols = 1;
3276
}
@@ -119,7 +163,8 @@ bool TorusMeshCommunicator::PreProcessingImpl() {
119163
}
120164

121165
bool TorusMeshCommunicator::RunImpl() {
122-
int sender = 0, receiver = 0;
166+
int sender = 0;
167+
int receiver = 0;
123168
DistributeSenderReceiver(sender, receiver);
124169

125170
int data_len = 0;
@@ -155,48 +200,23 @@ void TorusMeshCommunicator::DistributeDataLength(int src, int &len) const {
155200
std::vector<int> TorusMeshCommunicator::AssembleSendBuffer(int src, int len) const {
156201
std::vector<int> buffer(len);
157202
if (current_rank_ == src && len > 0) {
158-
std::copy(local_request_.data.begin(), local_request_.data.end(), buffer.begin());
203+
std::ranges::copy(local_request_.data, buffer.begin());
159204
}
160205
return buffer;
161206
}
162207

163208
void TorusMeshCommunicator::RelayMessage(int src, int dst, const std::vector<int> &route,
164209
const std::vector<int> &buffer, std::vector<int> &output) const {
165-
const int route_len = static_cast<int>(route.size());
166-
auto it = std::find(route.begin(), route.end(), current_rank_);
210+
auto it = std::ranges::find(route, current_rank_);
167211
bool on_route = (it != route.end());
168212
int my_pos = on_route ? static_cast<int>(std::distance(route.begin(), it)) : -1;
169213

170214
if (src == dst) {
171-
if (current_rank_ == src) {
172-
output = buffer;
173-
}
215+
HandleSameNode(current_rank_, src, buffer, output);
174216
} else if (current_rank_ == src) {
175-
output = buffer;
176-
if (route_len > 1) {
177-
int next_hop = route[1];
178-
int send_len = static_cast<int>(buffer.size());
179-
MPI_Send(&send_len, 1, MPI_INT, next_hop, 0, MPI_COMM_WORLD);
180-
if (send_len > 0) {
181-
MPI_Send(output.data(), send_len, MPI_INT, next_hop, 1, MPI_COMM_WORLD);
182-
}
183-
}
217+
HandleSourceNode(current_rank_, src, route, buffer, output);
184218
} else if (on_route) {
185-
int prev_hop = route[my_pos - 1];
186-
int recv_len = 0;
187-
MPI_Recv(&recv_len, 1, MPI_INT, prev_hop, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
188-
output.resize(recv_len);
189-
if (recv_len > 0) {
190-
MPI_Recv(output.data(), recv_len, MPI_INT, prev_hop, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
191-
}
192-
193-
if (current_rank_ != dst && my_pos + 1 < route_len) {
194-
int next_hop = route[my_pos + 1];
195-
MPI_Send(&recv_len, 1, MPI_INT, next_hop, 0, MPI_COMM_WORLD);
196-
if (recv_len > 0) {
197-
MPI_Send(output.data(), recv_len, MPI_INT, next_hop, 1, MPI_COMM_WORLD);
198-
}
199-
}
219+
HandleIntermediateNode(current_rank_, dst, route, my_pos, output);
200220
}
201221
}
202222

tasks/klimov_m_torus/tests/.clang-tidy

Lines changed: 0 additions & 12 deletions
This file was deleted.

tasks/klimov_m_torus/tests/performance/main.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,79 +12,77 @@ namespace klimov_m_torus {
1212

1313
class TorusPerformanceTest : public ppc::util::BaseRunPerfTests<InType, OutType> {
1414
protected:
15-
InType test_data_;
16-
bool data_ready_ = false;
17-
int world_size_ = 1;
18-
int rank_ = 0;
19-
bool is_seq_mode_ = false;
15+
InType test_data;
16+
bool data_ready = false;
17+
int world_size = 1;
18+
int rank = 0;
19+
bool is_seq_mode = false;
2020

2121
void SetUp() override {
2222
std::string task_name = std::get<1>(GetParam());
23-
is_seq_mode_ = (task_name.find("seq") != std::string::npos);
23+
is_seq_mode = (task_name.find("seq") != std::string::npos);
2424

2525
int mpi_initialized = 0;
2626
MPI_Initialized(&mpi_initialized);
2727
if (mpi_initialized != 0) {
28-
MPI_Comm_size(MPI_COMM_WORLD, &world_size_);
29-
MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
28+
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
29+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
3030
}
3131

3232
PrepareTestData();
3333
}
3434

3535
void PrepareTestData() {
36-
if (data_ready_) {
36+
if (data_ready) {
3737
return;
3838
}
3939

4040
const int data_size = 10000000;
4141

42-
test_data_.sender = 0;
43-
if (is_seq_mode_) {
44-
test_data_.receiver = 0;
42+
test_data.sender = 0;
43+
if (is_seq_mode) {
44+
test_data.receiver = 0;
4545
} else {
46-
test_data_.receiver = (world_size_ > 1) ? (world_size_ - 1) : 0;
46+
test_data.receiver = (world_size > 1) ? (world_size - 1) : 0;
4747
}
4848

49-
test_data_.data.resize(data_size);
49+
test_data.data.resize(data_size);
5050
for (int i = 0; i < data_size; ++i) {
51-
test_data_.data[i] = i + 1;
51+
test_data.data[i] = i + 1;
5252
}
5353

54-
data_ready_ = true;
54+
data_ready = true;
5555
}
5656

5757
InType GetTestInputData() override {
58-
return test_data_;
58+
return test_data;
5959
}
6060

6161
bool CheckTestOutputData(OutType &out) override {
6262
std::string task_name = std::get<1>(GetParam());
6363
bool is_mpi = (task_name.find("mpi") != std::string::npos);
6464

6565
if (is_mpi) {
66-
if (rank_ != test_data_.receiver) {
66+
if (rank != test_data.receiver) {
6767
return out.received_data.empty() && out.route.empty();
6868
}
69-
if (out.received_data.size() != test_data_.data.size()) {
69+
if (out.received_data.size() != test_data.data.size()) {
7070
return false;
7171
}
7272
if (out.received_data.empty()) {
7373
return true;
7474
}
75-
return (out.received_data.front() == test_data_.data.front() &&
76-
out.received_data.back() == test_data_.data.back());
75+
return (out.received_data.front() == test_data.data.front() && out.received_data.back() == test_data.data.back());
7776
}
7877

79-
if (rank_ == 0) {
80-
if (out.received_data.size() != test_data_.data.size()) {
78+
if (rank == 0) {
79+
if (out.received_data.size() != test_data.data.size()) {
8180
return false;
8281
}
8382
if (out.received_data.empty()) {
8483
return true;
8584
}
86-
return (out.received_data.front() == test_data_.data.front() &&
87-
out.received_data.back() == test_data_.data.back());
85+
return (out.received_data.front() == test_data.data.front() && out.received_data.back() == test_data.data.back());
8886
}
8987
return true;
9088
}

0 commit comments

Comments
 (0)