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 1737ea7..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;
 
@@ -352,7 +353,8 @@ int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks,
       return 0;
     low --;
     if (ptr < (char*)low->first + low->second.size) {
-      *offset = ((uint8_t*) low->first) - ((uint8_t*)ptr);
+      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;
     }
@@ -362,23 +364,15 @@ int smpi_is_shared(void* ptr, std::vector<std::pair<int, int>> &private_blocks,
   }
 }
 
-std::vector<std::pair<int, int>> shift_private_blocks(const std::vector<std::pair<int, int>> vec, int offset) {
-  std::vector<std::pair<int, int>> result;
-  for(auto block: vec) {
-    auto new_block = std::make_pair(std::max(0, block.first-offset), std::max(0, block.second-offset));
-    if(new_block.second > 0)
-      result.push_back(new_block);
-  }
-  return result;
-}
-
-void append_or_merge_block(std::vector<std::pair<int, int>> &vec, std::pair<int, int> &block) {
-  if(vec.size() > 0 && block.first <= vec.back().second) { // overlapping with the last block inserted
-    vec.back().second = std::max(vec.back().second, block.second);
-  }
-  else { // not overlapping, we insert a new block
-    vec.push_back(block);
-  }
+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) {
@@ -386,21 +380,21 @@ std::vector<std::pair<int, int>> merge_private_blocks(std::vector<std::pair<int,
   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].first < dst[i_dst].first) {
-      block = src[i_src];
-      i_src ++;
+    if(src[i_src].second <= dst[i_dst].first) {
+        i_src++;
     }
-    else {
-      block = dst[i_dst];
-      i_dst ++;
+    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 ++;
     }
-    append_or_merge_block(result, block);
-  }
-  for(; i_src < src.size(); i_src++) {
-    append_or_merge_block(result, src[i_src]);
-  }
-  for(; i_dst < dst.size(); i_dst++) {
-    append_or_merge_block(result, dst[i_dst]);
   }
   return result;
 }