Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_UNIVERSE_SIZE now initialized to the total amount of hosts in the platform
[simgrid.git] / src / smpi / internals / smpi_shared.cpp
index e66a618..42108cd 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -33,6 +33,7 @@
  *                                                                    \ |  |
  *                                                                      ----
  */
+#include <algorithm>
 #include <array>
 #include <cstring>
 #include <map>
 
 #include <cerrno>
 
-#include <sys/types.h>
-#ifndef WIN32
-#include <sys/mman.h>
-#endif
+#include "smpi_utils.hpp"
 #include <stdlib.h>
+#include <sys/mman.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include "smpi_utils.hpp"
 #ifndef MAP_ANONYMOUS
 #define MAP_ANONYMOUS MAP_ANON
 #endif
@@ -61,7 +59,7 @@
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_shared, smpi, "Logging specific to SMPI (shared memory macros)");
 
-namespace{
+namespace {
 /** Some location in the source code
  *
  *  This information is used by SMPI_SHARED_MALLOC to allocate  some shared memory for all simulated processes.
@@ -94,12 +92,10 @@ struct shared_metadata_t {
 std::map<const void*, shared_metadata_t> allocs_metadata;
 std::map<std::string, void*, std::less<>> calls;
 
-#ifndef WIN32
 int smpi_shared_malloc_bogusfile           = -1;
 int smpi_shared_malloc_bogusfile_huge_page = -1;
 unsigned long smpi_shared_malloc_blocksize = 1UL << 20;
-#endif
-}
+} // namespace
 
 void smpi_shared_destroy()
 {
@@ -108,7 +104,6 @@ void smpi_shared_destroy()
   calls.clear();
 }
 
-#ifndef WIN32
 static void* shm_map(int fd, size_t size, shared_data_key_type* data)
 {
   void* mem = smpi_temp_shm_mmap(fd, size);
@@ -126,9 +121,8 @@ static void *smpi_shared_malloc_local(size_t size, const char *file, int line)
 {
   void* mem;
   smpi_source_location loc(file, line);
-  auto res = allocs.insert(std::make_pair(loc, shared_data_t()));
-  auto data = res.first;
-  if (res.second) {
+  auto [data, inserted] = allocs.try_emplace(loc);
+  if (inserted) {
     // The new element was inserted.
     int fd             = smpi_temp_shm_get();
     data->second.fd    = fd;
@@ -400,9 +394,9 @@ std::vector<std::pair<size_t, size_t>> shift_and_frame_private_blocks(const std:
                                                                       size_t offset, size_t buff_size)
 {
   std::vector<std::pair<size_t, size_t>> result;
-  for (auto const& block : vec) {
-    auto new_block = std::make_pair(std::min(std::max((size_t)0, block.first - offset), buff_size),
-                                    std::min(std::max((size_t)0, block.second - offset), buff_size));
+  for (auto const& [block_begin, block_end] : vec) {
+    auto new_block = std::make_pair(std::clamp(block_begin - offset, (size_t)0, buff_size),
+                                    std::clamp(block_end - offset, (size_t)0, buff_size));
     if (new_block.second > 0 && new_block.first < buff_size)
       result.push_back(new_block);
   }
@@ -453,7 +447,7 @@ void smpi_shared_free(void *ptr)
     if (data->count <= 0) {
       close(data->fd);
       allocs.erase(allocs.find(meta->second.data->first));
-      allocs_metadata.erase(ptr);
+      allocs_metadata.erase(meta);
       XBT_DEBUG("Shared free - Local - with removal - of %p", ptr);
     } else {
       XBT_DEBUG("Shared free - Local - no removal - of %p, count = %d", ptr, data->count);
@@ -467,7 +461,7 @@ void smpi_shared_free(void *ptr)
       munmap(ptr, meta->second.size);
       if(meta->second.data->second.count==0){
         delete meta->second.data;
-        allocs_metadata.erase(ptr);
+        allocs_metadata.erase(meta);
       }
     }else{
       xbt_free(ptr);
@@ -479,7 +473,6 @@ void smpi_shared_free(void *ptr)
     xbt_free(ptr);
   }
 }
-#endif
 
 int smpi_shared_known_call(const char* func, const char* input)
 {