Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take the intersection of the private blocks instead of the union.
[simgrid.git] / src / smpi / smpi_shared.cpp
index 731f4ae..aa61be5 100644 (file)
@@ -295,11 +295,12 @@ void *smpi_shared_malloc_global__(size_t size, const char *file, int line, int *
   if(shared_block_offsets[0] > 0) {
     newmeta.private_blocks.push_back(std::make_pair(0, shared_block_offsets[0]));
   }
-  for(int i_block = 0; i_block < nb_shared_blocks-1; i_block ++) {
+  int i_block;
+  for(i_block = 0; i_block < nb_shared_blocks-1; i_block ++) {
     newmeta.private_blocks.push_back(std::make_pair(shared_block_offsets[2*i_block+1], shared_block_offsets[2*i_block+2]));
   }
-  if(shared_block_offsets[nb_shared_blocks-1] < size) {
-    newmeta.private_blocks.push_back(std::make_pair(shared_block_offsets[nb_shared_blocks-1], size));
+  if(shared_block_offsets[2*i_block+1] < size) {
+    newmeta.private_blocks.push_back(std::make_pair(shared_block_offsets[2*i_block+1], size));
   }
   allocs_metadata[mem] = newmeta;
 
@@ -337,7 +338,7 @@ void *smpi_shared_malloc(size_t size, const char *file, int line) {
   return mem;
 }
 
-int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks){
+int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks, int *offset){
   private_blocks.clear(); // being paranoid
   if (allocs_metadata.empty())
     return 0;
@@ -345,12 +346,15 @@ int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks){
     auto low = allocs_metadata.lower_bound(ptr);
     if (low->first==ptr) {
       private_blocks = low->second.private_blocks;
+      *offset = 0;
       return 1;
     }
     if (low == allocs_metadata.begin())
       return 0;
     low --;
     if (ptr < (char*)low->first + low->second.size) {
+      xbt_assert(ptr > (char*)low->first, "Oops, there seems to be a bug in the shared memory metadata.");
+      *offset = ((uint8_t*)ptr) - ((uint8_t*) low->first);
       private_blocks = low->second.private_blocks;
       return 1;
     }
@@ -360,6 +364,41 @@ int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks){
   }
 }
 
+std::vector<std::pair<int, int>> shift_and_frame_private_blocks(const std::vector<std::pair<int, int>> vec, int offset, int buff_size) {
+    std::vector<std::pair<int, int>> result;
+    for(auto block: vec) {
+        auto new_block = std::make_pair(std::min(std::max(0, block.first-offset), buff_size),
+                                        std::min(std::max(0, block.second-offset), buff_size));
+        if(new_block.second > 0 && new_block.first < buff_size)
+            result.push_back(new_block);
+    }
+    return result;
+}
+
+std::vector<std::pair<int, int>> merge_private_blocks(std::vector<std::pair<int, int>> src, std::vector<std::pair<int, int>> dst) {
+  std::vector<std::pair<int, int>> result;
+  unsigned i_src=0, i_dst=0;
+  while(i_src < src.size() && i_dst < dst.size()) {
+    std::pair<int, int> block;
+    if(src[i_src].second <= dst[i_dst].first) {
+        i_src++;
+    }
+    else if(dst[i_dst].second <= src[i_src].first) {
+        i_dst++;
+    }
+    else { // src.second > dst.first && dst.second > src.first → the blocks are overlapping
+      block = std::make_pair(std::max(src[i_src].first, dst[i_dst].first),
+                             std::min(src[i_src].second, dst[i_dst].second));
+      result.push_back(block);
+      if(src[i_src].second < dst[i_dst].second)
+          i_src ++;
+      else
+          i_dst ++;
+    }
+  }
+  return result;
+}
+
 void smpi_shared_free(void *ptr)
 {
   if (smpi_cfg_shared_malloc == shmalloc_local) {