22 : shm_name_(shm_name), size_(
size), own_(own), fd_(init_shmem_fd()) {}
30 : shm_name_(std::
move(pm.shm_name_)), size_(pm.size_), own_(pm.own_), fd_(pm.fd_) { pm.fd_ = -1; }
41 std::size_t
size()
const {
return size_; }
43 void map_to_vm(
void* addr, std::size_t
size, std::size_t offset)
const {
50 void* ret = mmap(addr,
size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd_, offset);
51 if (ret == MAP_FAILED) {
53 die(
"[ityr::common::physical_mem] mmap(%p, %lu, ...) failed", addr,
size);
61 if (own_ && shm_unlink(shm_name_.c_str()) == -1) {
63 die(
"[ityr::common::physical_mem] shm_unlink() failed");
68 int init_shmem_fd()
const {
70 if (own_) oflag |= O_CREAT | O_TRUNC;
72 int fd = shm_open(shm_name_.c_str(), oflag, S_IRUSR | S_IWUSR);
75 die(
"[ityr::common::physical_mem] shm_open() failed");
78 if (own_ && ftruncate(fd, size_) == -1) {
80 die(
"[ityr::common::physical_mem] ftruncate(%d, %lu) failed", fd, size_);
86 std::string shm_name_;
92 ITYR_TEST_CASE(
"[ityr::common::physical_mem] map physical memory to two different virtual addresses") {
94 singleton_initializer<topology::instance> topo;
101 std::size_t alloc_size = 16 * pagesize;
103 physical_mem pm(ss.str(), alloc_size,
true);
105 virtual_mem vm1(alloc_size);
106 virtual_mem vm2(alloc_size);
113 b1 =
reinterpret_cast<int*
>(vm1.addr());
114 b2 =
reinterpret_cast<int*
>(vm2.addr());
115 pm.map_to_vm(b1, alloc_size, 0);
116 pm.map_to_vm(b2, alloc_size, 0);
120 std::size_t offset = 3 * pagesize;
121 b1 =
reinterpret_cast<int*
>(
reinterpret_cast<std::byte*
>(vm1.addr()) + offset);
122 b2 =
reinterpret_cast<int*
>(
reinterpret_cast<std::byte*
>(vm2.addr()) + offset);
123 pm.map_to_vm(b1, pagesize, offset);
124 pm.map_to_vm(b2, pagesize, offset);
Definition: physical_mem.hpp:18
physical_mem()
Definition: physical_mem.hpp:20
physical_mem & operator=(const physical_mem &)=delete
physical_mem(physical_mem &&pm)
Definition: physical_mem.hpp:29
physical_mem & operator=(physical_mem &&pm)
Definition: physical_mem.hpp:31
physical_mem(const physical_mem &)=delete
physical_mem(const std::string &shm_name, std::size_t size, bool own)
Definition: physical_mem.hpp:21
std::size_t size() const
Definition: physical_mem.hpp:41
~physical_mem()
Definition: physical_mem.hpp:24
void map_to_vm(void *addr, std::size_t size, std::size_t offset) const
Definition: physical_mem.hpp:43
#define ITYR_SUBCASE(name)
Definition: util.hpp:41
#define ITYR_CHECK(cond)
Definition: util.hpp:48
rank_t my_rank()
Definition: topology.hpp:207
Definition: allocator.hpp:16
std::size_t get_page_size()
Definition: util.hpp:170
ForwardIteratorD move(const ExecutionPolicy &policy, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIteratorD first_d)
Move a range to another.
Definition: parallel_loop.hpp:934