-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented custom byte_vector for local storage #245
Open
ryan-dozier
wants to merge
17
commits into
LLNL:v0.7-dev
Choose a base branch
from
ryan-dozier:feature-vector-irecv
base: v0.7-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6ae32e0
changed from byte[] to vector on recv side to mirror sends
ryan-dozier 560644f
temp modifying number of communicators in test_comm2
ryan-dozier 192daa2
large set of changes, added in byte vector for send/reciever
ryan-dozier 2e3028d
bug fixes, only failing csv_reader
ryan-dozier 8f5490b
bug fixes, added bytevector test for ci
ryan-dozier 7d58d21
introduced a minor bug
ryan-dozier 23c0679
fixed typo
ryan-dozier b45970c
changed from byte[] to vector on recv side to mirror sends
ryan-dozier 8c0a301
temp modifying number of communicators in test_comm2
ryan-dozier b1cbb2b
large set of changes, added in byte vector for send/reciever
ryan-dozier 70262ff
bug fixes, only failing csv_reader
ryan-dozier 6eb093e
bug fixes, added bytevector test for ci
ryan-dozier ae3a705
introduced a minor bug
ryan-dozier e7651bb
fixed typo
ryan-dozier d547df0
MTEST_WITH_SLURMerge branch 'feature-vector-irecv' of https://github.…
ryan-dozier 28b76c0
updated to newest 0.7-dev changes
ryan-dozier 7b008c4
addressing trevor comments
ryan-dozier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
#pragma once | ||
|
||
#include <cstring> | ||
#include <string> | ||
#include <cstddef> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include <sys/mman.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <vector> | ||
|
||
namespace ygm::detail { | ||
class byte_vector { | ||
using value_type = std::byte; | ||
using pointer = std::byte*; | ||
using reference = std::byte&; | ||
using difference_type = std::ptrdiff_t; | ||
public: | ||
class Byte_Iterator { | ||
public: | ||
|
||
Byte_Iterator() : m_bv(nullptr), i(0) {} | ||
Byte_Iterator(byte_vector* v, size_t i) : m_bv(v), i(i) {} | ||
|
||
reference operator*() { return (*m_bv)[i]; } | ||
const reference operator*() const { return (*m_bv)[i]; } | ||
pointer operator->() { return &(*m_bv)[i]; } | ||
const pointer operator->() const { return &(*m_bv)[i]; } | ||
reference operator[](int n) { return (*m_bv)[i + n]; } | ||
const reference operator[](int n) const { return (*m_bv)[i + n]; } | ||
|
||
Byte_Iterator& operator++() { ++i; return *this; } | ||
Byte_Iterator& operator--() { --i; return *this; } | ||
Byte_Iterator operator++(int) { Byte_Iterator r(*this); ++i; return r; } | ||
Byte_Iterator operator--(int) { Byte_Iterator r(*this); --i; return r; } | ||
|
||
Byte_Iterator& operator+=(int n) { i += n; return *this; } | ||
Byte_Iterator& operator-=(int n) { i -= n; return *this; } | ||
|
||
difference_type operator-(Byte_Iterator const& r) const { return i - r.i; } | ||
|
||
bool operator<(Byte_Iterator const& r) const { return i < r.i; } | ||
bool operator<=(Byte_Iterator const& r) const { return i <= r.i; } | ||
bool operator>(Byte_Iterator const& r) const { return i > r.i; } | ||
bool operator>=(Byte_Iterator const& r) const { return i >= r.i; } | ||
bool operator!=(Byte_Iterator const& r) const { return i != r.i; } | ||
bool operator==(Byte_Iterator const& r) const { return i == r.i; } | ||
|
||
private: | ||
const byte_vector* m_bv; | ||
size_t i; | ||
}; | ||
|
||
byte_vector() : m_data(nullptr), m_size(0), m_capacity(0) {} | ||
|
||
byte_vector(size_t set_capacity) : m_size(0) { | ||
m_capacity = get_page_aligned_size(set_capacity); | ||
// now that the shm_file is the correct size we can memory map to it. | ||
m_data = (pointer) mmap(NULL, m_capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); | ||
if (m_data == MAP_FAILED) { | ||
std::cerr << strerror(errno) << std::endl; | ||
throw std::runtime_error("mmap failed"); | ||
} | ||
} | ||
|
||
~byte_vector() { | ||
munmap(m_data, m_capacity); | ||
} | ||
|
||
byte_vector(byte_vector&) = default; | ||
byte_vector(const byte_vector&) = default; | ||
byte_vector(byte_vector&&) = default; | ||
|
||
const reference operator[](int i) const { return m_data[i]; } | ||
reference operator[](int i) { return m_data[i]; } | ||
|
||
pointer data() const { return m_data; } | ||
bool empty() const { return m_size == 0; } | ||
void clear() { m_size = 0; } | ||
size_t size() const { return m_size; } | ||
size_t capacity() const { return m_capacity; } | ||
|
||
Byte_Iterator begin() { return Byte_Iterator(this, 0); } | ||
Byte_Iterator end() { return Byte_Iterator(this, m_size); } | ||
|
||
void swap(byte_vector& other) { | ||
std::swap(m_data, other.m_data); | ||
std::swap(m_size, other.m_size); | ||
std::swap(m_capacity, other.m_capacity); | ||
} | ||
|
||
void reserve(size_t cap) { | ||
size_t new_capacity = get_page_aligned_size(cap); | ||
if(m_data == nullptr) { | ||
m_capacity = new_capacity; | ||
// now that the shm_file is the correct size we can memory map to it. | ||
m_data = (pointer) mmap(NULL, m_capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); | ||
if (m_data == MAP_FAILED) { | ||
throw std::runtime_error("mmap failed"); | ||
} | ||
return; | ||
} | ||
// if max osx handler | ||
#if __APPLE__ | ||
pointer temp = (pointer) mmap(NULL, new_capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); | ||
if (temp == MAP_FAILED) { | ||
std::cerr << strerror(errno) << std::endl; | ||
throw std::runtime_error("mmap failed"); | ||
} | ||
memcpy(temp, m_data, m_size); | ||
munmap(m_data, m_capacity); | ||
m_data = temp; | ||
#else | ||
m_data = (pointer) mremap(m_data, m_capacity, new_capacity, MREMAP_MAYMOVE); | ||
if(m_data == MAP_FAILED) { | ||
std::cerr << m_capacity << "," << cap << "," << new_capacity << ", " << strerror(errno) << std::endl; | ||
throw std::runtime_error("mremap failed"); | ||
} | ||
#endif | ||
m_capacity = new_capacity; | ||
} | ||
|
||
void resize(size_t s) { | ||
if(s > m_capacity) this->reserve(s); | ||
m_size = s; | ||
} | ||
|
||
void push_bytes(const void* d, size_t s) { | ||
if(s > m_capacity - m_size) { | ||
size_t new_capacity = std::max(m_capacity * 2, m_size + s); | ||
this->reserve(new_capacity); | ||
} | ||
memcpy(m_data + m_size, d, s); | ||
m_size += s; | ||
} | ||
|
||
void push_bytes(void* d, size_t s) { | ||
if(s > m_capacity - m_size) { | ||
size_t new_capacity = std::max(m_capacity * 2, m_size + s); | ||
this->reserve(new_capacity); | ||
} | ||
memcpy(m_data + m_size, d, s); | ||
m_size += s; | ||
} | ||
|
||
private: | ||
size_t get_page_aligned_size(size_t s) const { | ||
auto pagesize = getpagesize(); | ||
auto num_pages = s / pagesize; | ||
if (s % pagesize != 0) num_pages++; | ||
return num_pages * pagesize; | ||
} | ||
|
||
pointer m_data; | ||
size_t m_size; | ||
size_t m_capacity; | ||
}; | ||
|
||
} // end ygm namespace |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth implementing an exponentially growing size to get the number of calls to
mremap
to be logarithmic in the buffer size rather than linear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be experimented with before implementing.