Skip to content

Commit 751d06a

Browse files
committed
[sonar] follow recommandations to improve code maintainability
1 parent 895f14c commit 751d06a

File tree

9 files changed

+43
-31
lines changed

9 files changed

+43
-31
lines changed

include/dtlmod/Metadata.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ class Variable;
1919

2020
/// \cond EXCLUDE_FROM_DOCUMENTATION
2121
class Metadata {
22+
friend Variable;
2223
Variable* variable_;
2324

24-
public:
25-
explicit Metadata(Variable* variable) : variable_(variable) {}
25+
std::map<unsigned int, // Transaction id
26+
std::map<std::pair<std::vector<size_t>, std::vector<size_t>>, // starts and counts
27+
std::pair<std::string, sg4::ActorPtr>, // filename and publisher
28+
std::less<>>,
29+
std::less<>> transaction_infos_;
2630

27-
std::map<unsigned int, // Transaction id
28-
std::map<std::pair<std::vector<size_t>, std::vector<size_t>>, // starts and counts
29-
std::pair<std::string, sg4::ActorPtr>, // filename and publisher
30-
std::less<>>,
31-
std::less<>>
32-
transaction_infos_;
31+
protected:
32+
const std::map<std::pair<std::vector<size_t>, std::vector<size_t>>,
33+
std::pair<std::string, sg4::ActorPtr>, std::less<>>& get_blocks_for_transaction(int id)
34+
{ return transaction_infos_[id]; }
35+
void add_transaction(int id, const std::vector<size_t>& start, const std::vector<size_t>& count,
36+
const std::string& filename, sg4::ActorPtr publisher);
3337

38+
public:
39+
explicit Metadata(Variable* variable) : variable_(variable) {}
3440
int get_current_transaction() const { return transaction_infos_.empty() ? -1 : (transaction_infos_.rbegin())->first; }
3541
void export_to_file(std::ofstream& ostream) const;
3642
};

include/dtlmod/StagingTransport.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class StagingTransport : public Transport {
1616
using Transport::Transport;
1717
friend StagingEngine;
1818
std::unordered_map<std::string, sg4::MessageQueue*> publisher_put_requests_mq_;
19+
std::unordered_map<std::string, sg4::ActivitySet> pending_put_requests_;
1920

2021
protected:
2122
void add_publisher(unsigned long publisher_id) override;
@@ -26,9 +27,12 @@ class StagingTransport : public Transport {
2627
// Create a message queue to receive request for variable pieces from subscribers
2728
void set_publisher_put_requests_mq(const std::string& publisher_name);
2829
[[nodiscard]] sg4::MessageQueue* get_publisher_put_requests_mq(const std::string& publisher_name) const;
29-
30-
public:
31-
std::unordered_map<std::string, sg4::ActivitySet> pending_put_requests;
30+
[[nodiscard]] bool pending_put_requests_exist_for(const std::string& pub_name)
31+
{ return not pending_put_requests_[pub_name].empty(); }
32+
[[nodiscard]] sg4::ActivityPtr wait_any_pending_put_request_for(const std::string& pub_name)
33+
{ return pending_put_requests_[pub_name].wait_any(); }
34+
35+
public:
3236
~StagingTransport() override = default;
3337
void put(std::shared_ptr<Variable> var, size_t /*simulated_size_in_bytes*/) override;
3438
void get(std::shared_ptr<Variable> var) override;

src/DTL.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "dtlmod/DTL.hpp"
1010
#include "dtlmod/DTLException.hpp"
11-
#define JSON_USE_IMPLICIT_CONVERSIONS 0
1211
#include <fstream>
1312
#include <nlohmann/json.hpp>
1413

src/FileTransport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void FileTransport::put(std::shared_ptr<Variable> var, size_t size)
4141
var->add_transaction_metadata(tid, self, file->get_path());
4242

4343
XBT_DEBUG("Actor '%s' is writing %lu bytes into file '%s'", self->get_cname(), size, file->get_path().c_str());
44-
to_write_in_transaction_[self].emplace_back(std::make_pair(file, size));
44+
to_write_in_transaction_[self].emplace_back(file, size);
4545
}
4646

4747
void FileTransport::close_pub_files() const
@@ -71,7 +71,7 @@ void FileTransport::get(std::shared_ptr<Variable> var)
7171
XBT_DEBUG("Actor '%s' is opening file '%s'", self->get_cname(), filename.c_str());
7272
auto file = fs->open(filename, "r");
7373
// Keep track of what to read from this file for this get
74-
to_read_in_transaction_[self].emplace_back(std::make_pair(file, size));
74+
to_read_in_transaction_[self].emplace_back(file, size);
7575
}
7676
}
7777
}

src/Metadata.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(dtlmod);
1111

1212
namespace dtlmod {
1313
/// \cond EXCLUDE_FROM_DOCUMENTATION
14+
void Metadata::add_transaction(int id, const std::vector<size_t>& start, const std::vector<size_t>& count,
15+
const std::string& location, sg4::ActorPtr publisher)
16+
{
17+
auto start_and_count = std::make_pair(start, count);
18+
transaction_infos_[id][start_and_count] = std::make_pair(location, publisher);
19+
}
20+
1421
void Metadata::export_to_file(std::ofstream& ostream) const
1522
{
1623
XBT_DEBUG("Variable %s:", variable_->get_cname());

src/StagingMboxTransport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ void StagingMboxTransport::get_requests_and_do_put(sg4::ActorPtr publisher)
3131
auto pub_name = publisher->get_name();
3232
// Wait for the reception of the messages. If something is requested, post a put in the mailbox for the
3333
// corresponding publisher-subscriber couple
34-
while (not pending_put_requests[pub_name].empty()) {
35-
auto request = boost::static_pointer_cast<sg4::Mess>(pending_put_requests[pub_name].wait_any());
34+
while (pending_put_requests_exist_for(pub_name)) {
35+
auto request = boost::static_pointer_cast<sg4::Mess>(wait_any_pending_put_request_for(pub_name));
3636
const auto* subscriber = request->get_sender();
3737
auto* req_size = static_cast<size_t*>(request->get_payload());
3838
if (*req_size > 0) {

src/StagingMqTransport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ void StagingMqTransport::get_requests_and_do_put(sg4::ActorPtr publisher)
3030
auto pub_name = publisher->get_name();
3131
// Wait for the reception of the messages. If something is requested, post a put in the message queue for the
3232
// corresponding publisher-subscriber couple
33-
while (not pending_put_requests[pub_name].empty()) {
34-
auto request = boost::static_pointer_cast<sg4::Mess>(pending_put_requests[pub_name].wait_any());
33+
while (pending_put_requests_exist_for(pub_name)) {
34+
auto request = boost::static_pointer_cast<sg4::Mess>(wait_any_pending_put_request_for(pub_name));
3535
const auto* subscriber = request->get_sender();
3636
auto* req_size = static_cast<size_t*>(request->get_payload());
3737
if (*req_size > 0) {

src/StagingTransport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ void StagingTransport::put(std::shared_ptr<Variable> var, size_t simulated_size_
4444
// Each Subscriber will send a put request to each publisher in the Stream. They can request for a certain size if
4545
// they need something from this publisher or 0 otherwise.
4646
// Start with posting all asynchronous gets and creating an ActivitySet.
47-
for (unsigned int i = 0; i < e->get_num_subscribers(); i++)
48-
pending_put_requests[pub_name].push(get_publisher_put_requests_mq(pub_name)->get_async());
47+
for (size_t i = 0; i < e->get_num_subscribers(); i++)
48+
pending_put_requests_[pub_name].push(get_publisher_put_requests_mq(pub_name)->get_async());
4949
}
5050

5151
void StagingTransport::get(std::shared_ptr<Variable> var)

src/Variable.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ const std::pair<unsigned int, unsigned int>& Variable::get_subscriber_transactio
7474
void Variable::add_transaction_metadata(unsigned int transaction_id, sg4::ActorPtr publisher,
7575
const std::string& location)
7676
{
77-
auto start_and_count = std::make_pair(local_start_[publisher], local_count_[publisher]);
78-
metadata_->transaction_infos_[transaction_id][start_and_count] = std::make_pair(location, publisher);
77+
metadata_->add_transaction(transaction_id, local_start_[publisher], local_count_[publisher], location, publisher);
7978
}
8079

8180
std::vector<std::pair<std::string, sg_size_t>> Variable::get_sizes_to_get_per_block(unsigned int transaction_id,
@@ -84,7 +83,7 @@ std::vector<std::pair<std::string, sg_size_t>> Variable::get_sizes_to_get_per_bl
8483
{
8584
std::vector<std::pair<std::string, sg_size_t>> get_sizes_per_block;
8685
// TODO add sanity checks
87-
auto blocks = metadata_->transaction_infos_[transaction_id];
86+
auto blocks = metadata_->get_blocks_for_transaction(transaction_id);
8887
XBT_DEBUG("%zu block(s) to check for transaction %u", blocks.size(), transaction_id);
8988
for (const auto& [block_info, location] : blocks) {
9089
size_t size_to_get = element_size_;
@@ -98,18 +97,15 @@ std::vector<std::pair<std::string, sg_size_t>> Variable::get_sizes_to_get_per_bl
9897
block_count[i]);
9998
size_t size_in_dim = 0;
10099
bool element_found = false;
101-
if (start[i] <= block_start[i]) {
102-
if ((block_start[i] - start[i]) <= count[i]) {
100+
if (start[i] <= block_start[i] && (block_start[i] - start[i]) <= count[i]) {
103101
size_in_dim = std::min(block_count[i], count[i] - (block_start[i] - start[i]));
104102
element_found = true;
105-
}
106-
} else {
107-
if ((start[i] - block_start[i]) <= block_count[i]) {
103+
}
104+
if (start[i] > block_start[i] && (start[i] - block_start[i]) <= block_count[i]) {
108105
size_in_dim = std::min(count[i], block_count[i] - (start[i] - block_start[i]));
109106
element_found = true;
110-
}
111107
}
112-
108+
113109
if (element_found && size_in_dim > 0) {
114110
something_to_get[i] = true;
115111
XBT_DEBUG("Mutiply size to read by %zu elements", size_in_dim);
@@ -119,7 +115,7 @@ std::vector<std::pair<std::string, sg_size_t>> Variable::get_sizes_to_get_per_bl
119115

120116
if (std::all_of(something_to_get.begin(), something_to_get.end(), [](bool v) { return v; })) {
121117
XBT_DEBUG("Total size to read from %s: %zu)", where.c_str(), size_to_get);
122-
get_sizes_per_block.emplace_back(std::make_pair(where, size_to_get));
118+
get_sizes_per_block.emplace_back(where, size_to_get);
123119
}
124120
}
125121
return get_sizes_per_block;

0 commit comments

Comments
 (0)