Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Change raw for-loops to range for-loops.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 26 Feb 2020 21:12:26 +0000 (22:12 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 26 Feb 2020 21:14:32 +0000 (22:14 +0100)
examples/s4u/platform-properties/s4u-platform-properties.cpp
src/kernel/routing/FatTreeZone.cpp
src/plugins/host_energy.cpp
src/surf/network_ib.cpp
teshsuite/s4u/activity-lifecycle/testing_comm.cpp
teshsuite/xbt/parallel_log_crashtest/parallel_log_crashtest.cpp

index 1720303..57d47cc 100644 (file)
@@ -119,8 +119,8 @@ int main(int argc, char* argv[])
 
   XBT_INFO("There are %zu hosts in the environment", totalHosts);
   std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
 
   XBT_INFO("There are %zu hosts in the environment", totalHosts);
   std::vector<simgrid::s4u::Host*> hosts = e.get_all_hosts();
-  for (unsigned int i = 0; i < hosts.size(); i++)
-    XBT_INFO("Host '%s' runs at %.0f flops/s", hosts[i]->get_cname(), hosts[i]->get_speed());
+  for (simgrid::s4u::Host const* host : hosts)
+    XBT_INFO("Host '%s' runs at %.0f flops/s", host->get_cname(), host->get_speed());
 
   e.load_deployment(argv[2]);
   e.run();
 
   e.load_deployment(argv[2]);
   e.run();
index 46d7449..820dfba 100644 (file)
@@ -30,11 +30,11 @@ FatTreeZone::FatTreeZone(NetZoneImpl* father, const std::string& name, resource:
 
 FatTreeZone::~FatTreeZone()
 {
 
 FatTreeZone::~FatTreeZone()
 {
-  for (unsigned int i = 0; i < this->nodes_.size(); i++) {
-    delete this->nodes_[i];
+  for (FatTreeNode const* node : this->nodes_) {
+    delete node;
   }
   }
-  for (unsigned int i = 0; i < this->links_.size(); i++) {
-    delete this->links_[i];
+  for (FatTreeLink const* link : this->links_) {
+    delete link;
   }
 }
 
   }
 }
 
@@ -149,8 +149,8 @@ void FatTreeZone::seal()
     msgBuffer.str("");
     msgBuffer << "Nodes are : ";
 
     msgBuffer.str("");
     msgBuffer << "Nodes are : ";
 
-    for (unsigned int i = 0; i < this->nodes_.size(); i++) {
-      msgBuffer << this->nodes_[i]->id << "(" << this->nodes_[i]->level << "," << this->nodes_[i]->position << ") ";
+    for (FatTreeNode const* node : this->nodes_) {
+      msgBuffer << node->id << "(" << node->level << "," << node->position << ") ";
     }
     XBT_DEBUG("%s", msgBuffer.str().c_str());
   }
     }
     XBT_DEBUG("%s", msgBuffer.str().c_str());
   }
@@ -169,8 +169,8 @@ void FatTreeZone::seal()
   if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
     std::stringstream msgBuffer;
     msgBuffer << "Links are : ";
   if (XBT_LOG_ISENABLED(surf_route_fat_tree, xbt_log_priority_debug)) {
     std::stringstream msgBuffer;
     msgBuffer << "Links are : ";
-    for (unsigned int i = 0; i < this->links_.size(); i++) {
-      msgBuffer << "(" << this->links_[i]->up_node_->id << "," << this->links_[i]->down_node_->id << ") ";
+    for (FatTreeLink const* link : this->links_) {
+      msgBuffer << "(" << link->up_node_->id << "," << link->down_node_->id << ") ";
     }
     XBT_DEBUG("%s", msgBuffer.str().c_str());
   }
     }
     XBT_DEBUG("%s", msgBuffer.str().c_str());
   }
@@ -380,11 +380,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster)
                                                      " levels but the child count vector (the first one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
 
                                                      " levels but the child count vector (the first one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
 
-  for (size_t i = 0; i < tmp.size(); i++) {
+  for (std::string const& level : tmp) {
     try {
     try {
-      this->num_children_per_node_.push_back(std::stoi(tmp[i]));
+      this->num_children_per_node_.push_back(std::stoi(level));
     } catch (const std::invalid_argument&) {
     } catch (const std::invalid_argument&) {
-      surf_parse_error(std::string("Invalid child count: ") + tmp[i]);
+      surf_parse_error(std::string("Invalid child count: ") + level);
     }
   }
 
     }
   }
 
@@ -393,11 +393,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster)
   surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
                                                      " levels but the parent count vector (the second one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
   surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
                                                      " levels but the parent count vector (the second one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
-  for (size_t i = 0; i < tmp.size(); i++) {
+  for (std::string const& parent : tmp) {
     try {
     try {
-      this->num_parents_per_node_.push_back(std::stoi(tmp[i]));
+      this->num_parents_per_node_.push_back(std::stoi(parent));
     } catch (const std::invalid_argument&) {
     } catch (const std::invalid_argument&) {
-      surf_parse_error(std::string("Invalid parent count: ") + tmp[i]);
+      surf_parse_error(std::string("Invalid parent count: ") + parent);
     }
   }
 
     }
   }
 
@@ -406,11 +406,11 @@ void FatTreeZone::parse_specific_arguments(ClusterCreationArgs* cluster)
   surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
                                                      " levels but the port count vector (the third one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
   surf_parse_assert(tmp.size() == this->levels_, std::string("You specified ") + std::to_string(this->levels_) +
                                                      " levels but the port count vector (the third one) contains " +
                                                      std::to_string(tmp.size()) + " levels.");
-  for (size_t i = 0; i < tmp.size(); i++) {
+  for (std::string const& port : tmp) {
     try {
     try {
-      this->num_port_lower_level_.push_back(std::stoi(tmp[i]));
+      this->num_port_lower_level_.push_back(std::stoi(port));
     } catch (const std::invalid_argument&) {
     } catch (const std::invalid_argument&) {
-      throw std::invalid_argument(std::string("Invalid lower level port number:") + tmp[i]);
+      throw std::invalid_argument(std::string("Invalid lower level port number:") + port);
     }
   }
   this->cluster_ = cluster;
     }
   }
   this->cluster_ = cluster;
@@ -423,16 +423,16 @@ void FatTreeZone::generate_dot_file(const std::string& filename) const
   xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
 
   file << "graph AsClusterFatTree {\n";
   xbt_assert(file.is_open(), "Unable to open file %s", filename.c_str());
 
   file << "graph AsClusterFatTree {\n";
-  for (unsigned int i = 0; i < this->nodes_.size(); i++) {
-    file << this->nodes_[i]->id;
-    if (this->nodes_[i]->id < 0)
+  for (FatTreeNode const* node : this->nodes_) {
+    file << node->id;
+    if (node->id < 0)
       file << " [shape=circle];\n";
     else
       file << " [shape=hexagon];\n";
   }
 
       file << " [shape=circle];\n";
     else
       file << " [shape=hexagon];\n";
   }
 
-  for (unsigned int i = 0; i < this->links_.size(); i++) {
-    file << this->links_[i]->down_node_->id << " -- " << this->links_[i]->up_node_->id << ";\n";
+  for (FatTreeLink const* link : this->links_) {
+    file << link->down_node_->id << " -- " << link->up_node_->id << ";\n";
   }
   file << "}";
   file.close();
   }
   file << "}";
   file.close();
index 1f4ba65..b68edc8 100644 (file)
@@ -512,15 +512,13 @@ static void on_host_destruction(simgrid::s4u::Host const& host)
 
 static void on_simulation_end()
 {
 
 static void on_simulation_end()
 {
-  std::vector<simgrid::s4u::Host*> hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts();
-
   double total_energy      = 0.0; // Total energy consumption (whole platform)
   double used_hosts_energy = 0.0; // Energy consumed by hosts that computed something
   double total_energy      = 0.0; // Total energy consumption (whole platform)
   double used_hosts_energy = 0.0; // Energy consumed by hosts that computed something
-  for (size_t i = 0; i < hosts.size(); i++) {
-    if (dynamic_cast<simgrid::s4u::VirtualMachine*>(hosts[i]) == nullptr) { // Ignore virtual machines
-      double energy      = hosts[i]->extension<HostEnergy>()->get_consumed_energy();
+  for (simgrid::s4u::Host const* host : simgrid::s4u::Engine::get_instance()->get_all_hosts()) {
+    if (dynamic_cast<const simgrid::s4u::VirtualMachine*>(host) == nullptr) { // Ignore virtual machines
+      double energy = host->extension<HostEnergy>()->get_consumed_energy();
       total_energy += energy;
       total_energy += energy;
-      if (hosts[i]->extension<HostEnergy>()->host_was_used_)
+      if (host->extension<HostEnergy>()->host_was_used_)
         used_hosts_energy += energy;
     }
   }
         used_hosts_energy += energy;
     }
   }
index 70782d1..90cb7da 100644 (file)
@@ -116,11 +116,11 @@ void NetworkIBModel::computeIBfactors(IBNode* root)
   double num_comm_out    = root->ActiveCommsUp.size();
   double max_penalty_out = 0.0;
   // first, compute all outbound penalties to get their max
   double num_comm_out    = root->ActiveCommsUp.size();
   double max_penalty_out = 0.0;
   // first, compute all outbound penalties to get their max
-  for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
+  for (ActiveComm const* comm : root->ActiveCommsUp) {
     double my_penalty_out = 1.0;
 
     if (num_comm_out != 1) {
     double my_penalty_out = 1.0;
 
     if (num_comm_out != 1) {
-      if ((*it)->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
+      if (comm->destination->nbActiveCommsDown > 2) // number of comms sent to the receiving node
         my_penalty_out = num_comm_out * Bs * ys;
       else
         my_penalty_out = num_comm_out * Bs;
         my_penalty_out = num_comm_out * Bs * ys;
       else
         my_penalty_out = num_comm_out * Bs;
@@ -129,30 +129,30 @@ void NetworkIBModel::computeIBfactors(IBNode* root)
     max_penalty_out = std::max(max_penalty_out, my_penalty_out);
   }
 
     max_penalty_out = std::max(max_penalty_out, my_penalty_out);
   }
 
-  for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
+  for (ActiveComm* comm : root->ActiveCommsUp) {
     // compute inbound penalty
     double my_penalty_in = 1.0;
     // compute inbound penalty
     double my_penalty_in = 1.0;
-    int nb_comms         = (*it)->destination->nbActiveCommsDown; // total number of incoming comms
+    int nb_comms         = comm->destination->nbActiveCommsDown; // total number of incoming comms
     if (nb_comms != 1)
     if (nb_comms != 1)
-      my_penalty_in = ((*it)->destination->ActiveCommsDown)[root]        // number of comm sent to dest by root node
-                      * Be * (*it)->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
+      my_penalty_in = (comm->destination->ActiveCommsDown)[root]        // number of comm sent to dest by root node
+                      * Be * comm->destination->ActiveCommsDown.size(); // number of different nodes sending to dest
 
     double penalty = std::max(my_penalty_in, max_penalty_out);
 
 
     double penalty = std::max(my_penalty_in, max_penalty_out);
 
-    double rate_before_update = (*it)->action->get_bound();
+    double rate_before_update = comm->action->get_bound();
     // save initial rate of the action
     // save initial rate of the action
-    if ((*it)->init_rate == -1)
-      (*it)->init_rate = rate_before_update;
+    if (comm->init_rate == -1)
+      comm->init_rate = rate_before_update;
 
 
-    double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate;
+    double penalized_bw = num_comm_out ? comm->init_rate / penalty : comm->init_rate;
 
     if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
       XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
 
     if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
       XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,
-                (*it)->destination->id, (*it)->action, penalized_bw, (*it)->action->get_bound(), (*it)->init_rate);
-      get_maxmin_system()->update_variable_bound((*it)->action->get_variable(), penalized_bw);
+                comm->destination->id, comm->action, penalized_bw, comm->action->get_bound(), comm->init_rate);
+      get_maxmin_system()->update_variable_bound(comm->action->get_variable(), penalized_bw);
     } else {
     } else {
-      XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, (*it)->destination->id,
-                (*it)->action, penalized_bw, (*it)->init_rate);
+      XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id, comm->destination->id,
+                comm->action, penalized_bw, comm->init_rate);
     }
   }
   XBT_DEBUG("Finished computing IB penalties");
     }
   }
   XBT_DEBUG("Finished computing IB penalties");
@@ -164,13 +164,13 @@ void NetworkIBModel::updateIBfactors_rec(IBNode* root, std::vector<bool>& update
     XBT_DEBUG("IB - Updating rec %d", root->id);
     computeIBfactors(root);
     updatedlist[root->id] = true;
     XBT_DEBUG("IB - Updating rec %d", root->id);
     computeIBfactors(root);
     updatedlist[root->id] = true;
-    for (std::vector<ActiveComm*>::iterator it = root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
-      if (not updatedlist[(*it)->destination->id])
-        updateIBfactors_rec((*it)->destination, updatedlist);
+    for (ActiveComm const* comm : root->ActiveCommsUp) {
+      if (not updatedlist[comm->destination->id])
+        updateIBfactors_rec(comm->destination, updatedlist);
     }
     }
-    for (std::map<IBNode*, int>::iterator it = root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
-      if (not updatedlist[it->first->id])
-        updateIBfactors_rec(it->first, updatedlist);
+    for (std::map<IBNode*, int>::value_type const& comm : root->ActiveCommsDown) {
+      if (not updatedlist[comm.first->id])
+        updateIBfactors_rec(comm.first, updatedlist);
     }
   }
 }
     }
   }
 }
@@ -187,12 +187,12 @@ void NetworkIBModel::updateIBfactors(NetworkAction* action, IBNode* from, IBNode
       to->ActiveCommsDown[from] -= 1;
 
     to->nbActiveCommsDown--;
       to->ActiveCommsDown[from] -= 1;
 
     to->nbActiveCommsDown--;
-    for (std::vector<ActiveComm*>::iterator it = from->ActiveCommsUp.begin(); it != from->ActiveCommsUp.end(); ++it) {
-      if ((*it)->action == action) {
-        delete *it;
-        from->ActiveCommsUp.erase(it);
-        break;
-      }
+    std::vector<ActiveComm*>::iterator it =
+        std::find_if(begin(from->ActiveCommsUp), end(from->ActiveCommsUp),
+                     [action](const ActiveComm* comm) { return comm->action == action; });
+    if (it != std::end(from->ActiveCommsUp)) {
+      delete *it;
+      from->ActiveCommsUp.erase(it);
     }
     action->unref();
   } else {
     }
     action->unref();
   } else {
index 9d4dddd..c40a344 100644 (file)
@@ -24,8 +24,8 @@ static void test_link_off_helper(double delay)
   simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
     assert_exit(true, 9);
     double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
   simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], [&start]() {
     assert_exit(true, 9);
     double milestone[5] = {0.5, 3.5, 4.5, 7.5, 9.0};
-    for (int i = 0; i < 5; i++)
-      milestone[i] += start;
+    for (double& m : milestone)
+      m += start;
     for (int i = 0; i < 4; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
       REQUIRE_NETWORK_FAILURE({
     for (int i = 0; i < 4; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
       REQUIRE_NETWORK_FAILURE({
@@ -40,8 +40,8 @@ static void test_link_off_helper(double delay)
     assert_exit(true, 9);
     int data            = 42;
     double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
     assert_exit(true, 9);
     int data            = 42;
     double milestone[5] = {1.5, 2.5, 5.5, 6.5, 9.0};
-    for (int i = 0; i < 5; i++)
-      milestone[i] += start;
+    for (double& m : milestone)
+      m += start;
     for (int i = 0; i < 2; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
       XBT_VERB("dsend(%c)", 'A' + i);
     for (int i = 0; i < 2; i++) {
       simgrid::s4u::this_actor::sleep_until(milestone[i]);
       XBT_VERB("dsend(%c)", 'A' + i);
index b894023..1ea8857 100644 (file)
@@ -34,13 +34,13 @@ int main(int argc, char* argv[])
   std::thread crashers[crasher_amount];
 
   /* spawn threads */
   std::thread crashers[crasher_amount];
 
   /* spawn threads */
-  for (int i = 0; i < crasher_amount; i++) {
-    crashers[i] = std::thread(crasher_thread, i);
-  }
+  int id = 0;
+  for (std::thread& thr : crashers)
+    thr = std::thread(crasher_thread, id++);
 
   /* wait for them */
 
   /* wait for them */
-  for (int i = 0; i < crasher_amount; i++)
-    crashers[i].join();
+  for (std::thread& thr : crashers)
+    thr.join();
 
   return 0;
 }
 
   return 0;
 }