- unsigned int i;
- for (i = 0; i < size / smpi_shared_malloc_blocksize; i++) {
- void* pos = (void*)((unsigned long)mem + i * smpi_shared_malloc_blocksize);
- void* res = mmap(pos, smpi_shared_malloc_blocksize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED | MAP_POPULATE,
- smpi_shared_malloc_bogusfile, 0);
- xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
- "size of the mapped file using --cfg=smpi/shared-malloc-blocksize=newvalue (default 1048576) ?"
- "You can also try using the sysctl vm.max_map_count",
- strerror(errno));
- }
- if (size % smpi_shared_malloc_blocksize) {
- void* pos = (void*)((unsigned long)mem + i * smpi_shared_malloc_blocksize);
- void* res = mmap(pos, size % smpi_shared_malloc_blocksize, PROT_READ | PROT_WRITE,
- MAP_FIXED | MAP_SHARED | MAP_POPULATE, smpi_shared_malloc_bogusfile, 0);
- xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
- "size of the mapped file using --cfg=smpi/shared-malloc-blocksize=newvalue (default 1048576) ?"
- "You can also try using the sysctl vm.max_map_count",
- strerror(errno));
+ for(int i_block = 0; i_block < nb_shared_blocks; i_block ++) {
+ XBT_DEBUG("\tglobal shared allocation, mmap block %d/%d", i_block+1, nb_shared_blocks);
+ size_t start_offset = shared_block_offsets[2*i_block];
+ size_t stop_offset = shared_block_offsets[2*i_block+1];
+ xbt_assert(start_offset < stop_offset, "start_offset (%zu) should be lower than stop offset (%zu)", start_offset, stop_offset);
+ xbt_assert(stop_offset <= size, "stop_offset (%zu) should be lower than size (%zu)", stop_offset, size);
+ if(i_block < nb_shared_blocks-1)
+ xbt_assert(stop_offset < shared_block_offsets[2*i_block+2],
+ "stop_offset (%zu) should be lower than its successor start offset (%zu)", stop_offset, shared_block_offsets[2*i_block+2]);
+ size_t start_block_offset = ALIGN_UP(start_offset, smpi_shared_malloc_blocksize);
+ size_t stop_block_offset = ALIGN_DOWN(stop_offset, smpi_shared_malloc_blocksize);
+ for (unsigned block_id=0, i = start_block_offset / smpi_shared_malloc_blocksize; i < stop_block_offset / smpi_shared_malloc_blocksize; block_id++, i++) {
+ XBT_DEBUG("\t\tglobal shared allocation, mmap block offset %d", block_id);
+ void* pos = (void*)((unsigned long)mem + i * smpi_shared_malloc_blocksize);
+ void* res = mmap(pos, smpi_shared_malloc_blocksize, PROT_READ | PROT_WRITE, mmap_flag,
+ huge_fd, 0);
+ xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
+ "size of the mapped file using --cfg=smpi/shared-malloc-blocksize=newvalue (default 1048576) ? "
+ "You can also try using the sysctl vm.max_map_count. "
+ "If you are using huge pages, check that you have at least one huge page (/proc/sys/vm/nr_hugepages) "
+ "and that the directory you are passing is mounted correctly (mount /path/to/huge -t hugetlbfs -o rw,mode=0777).",
+ strerror(errno));
+ }
+ size_t low_page_start_offset = ALIGN_UP(start_offset, PAGE_SIZE);
+ size_t low_page_stop_offset = start_block_offset < ALIGN_DOWN(stop_offset, PAGE_SIZE) ? start_block_offset : ALIGN_DOWN(stop_offset, PAGE_SIZE);
+ if(low_page_start_offset < low_page_stop_offset) {
+ XBT_DEBUG("\t\tglobal shared allocation, mmap block start");
+ void* pos = (void*)((unsigned long)mem + low_page_start_offset);
+ void* res = mmap(pos, low_page_stop_offset-low_page_start_offset, PROT_READ | PROT_WRITE, mmap_base_flag, // not a full huge page
+ smpi_shared_malloc_bogusfile, 0);
+ xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
+ "size of the mapped file using --cfg=smpi/shared-malloc-blocksize=newvalue (default 1048576) ?"
+ "You can also try using the sysctl vm.max_map_count",
+ strerror(errno));
+ }
+ if(low_page_stop_offset <= stop_block_offset) {
+ XBT_DEBUG("\t\tglobal shared allocation, mmap block stop");
+ size_t high_page_stop_offset = stop_offset == size ? size : ALIGN_DOWN(stop_offset, PAGE_SIZE);
+ if(high_page_stop_offset > stop_block_offset) {
+ void* pos = (void*)((unsigned long)mem + stop_block_offset);
+ void* res = mmap(pos, high_page_stop_offset-stop_block_offset, PROT_READ | PROT_WRITE, mmap_base_flag, // not a full huge page
+ smpi_shared_malloc_bogusfile, 0);
+ xbt_assert(res == pos, "Could not map folded virtual memory (%s). Do you perhaps need to increase the "
+ "size of the mapped file using --cfg=smpi/shared-malloc-blocksize=newvalue (default 1048576) ?"
+ "You can also try using the sysctl vm.max_map_count",
+ strerror(errno));
+ }
+ }