Skip to content

Commit

Permalink
Fixed Allocator to CPP standard version
Browse files Browse the repository at this point in the history
  • Loading branch information
mou authored and mou committed Apr 6, 2023
1 parent eb1fe09 commit 980aa50
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -g -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG ")
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG ")
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra")
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra ")

###
# Mandatory: devices configuration file.
Expand Down
22 changes: 15 additions & 7 deletions benchmarks/warm_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,25 @@ int main(int argc, char ** argv)

// FIXME: move me to a memory allocator

rfaas::RdmaAllocator<rdmalib::Buffer<char> > rdmaAllocator(executor);
rdmalib::Buffer<char>* in = rdmaAllocator.allocate(opts.input_size);
rdmaAllocator.construct(in, IBV_ACCESS_LOCAL_WRITE);
rdmalib::Buffer<char>* out = rdmaAllocator.allocate(opts.input_size);
rdmaAllocator.construct(out, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
// Sample: test demonstrating standard memory allocation.
// rdmalib::Buffer<char> in(opts.input_size, rdmalib::functions::Submission::DATA_HEADER_SIZE), out(opts.input_size);
// in.register_memory(executor._state.pd(), IBV_ACCESS_LOCAL_WRITE);
// out.register_memory(executor._state.pd(), IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);

// const rfaas::RdmaAllocator<rdmalib::Buffer<char> > &rdmaAllocator1 = rdmaAllocator;
std::vector<rdmalib::Buffer<char>, rfaas::RdmaAllocator<rdmalib::Buffer<char>>> v(8, rdmaAllocator);
// Sample: test demonstrating allocation with our custom allocator.
rfaas::RdmaInfo info_in(executor,IBV_ACCESS_LOCAL_WRITE,rdmalib::functions::Submission::DATA_HEADER_SIZE);
rfaas::RdmaAllocator<rdmalib::Buffer<char>> allocator_in{info_in};
rdmalib::Buffer<char>* in = allocator_in.allocate(opts.input_size);

rfaas::RdmaInfo info_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE));
rfaas::RdmaAllocator<rdmalib::Buffer<char>> allocator_out{info_out};
rdmalib::Buffer<char>* out = allocator_out.allocate(opts.input_size);

// Sample: test demonstrating allocation with std::vector.
// rfaas::RdmaInfo info_v(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE));
// rfaas::RdmaAllocator<rdmalib::Buffer<char>> allocator_v{info_v};
// std::vector<rdmalib::Buffer<char>, rfaas::RdmaAllocator<rdmalib::Buffer<char>>> v(allocator_v);


// TODO: Since the for loop writes a value of 1 to each byte of the in buffer,
// it overwrites all bytes previously set to 0 by the memset() function.
Expand Down
37 changes: 17 additions & 20 deletions rfaas/include/rfaas/rdma_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@
#include <rfaas/executor.hpp>

namespace rfaas {

struct RdmaInfo {

public:
RdmaInfo(executor& executor, const int access, const int header_size=0)
: executor(executor), access(access), header_size(header_size) {}
const executor& executor;
const int access;
const int header_size = 0;
};

template<typename T>
class RdmaAllocator {

public:
typedef T value_type;

inline explicit RdmaAllocator(const executor &executor) noexcept: _executor(executor) {}
inline explicit RdmaAllocator(RdmaInfo& info) noexcept: _info(info) {}

template<class U>
constexpr RdmaAllocator(const RdmaAllocator<U> &) noexcept {}
Expand All @@ -26,36 +38,21 @@ namespace rfaas {
throw std::bad_array_new_length();

// Maybe we could directly call the memset function here
mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (auto buffer = static_cast<T*>(std::malloc(size * sizeof(T)))){
if (auto buffer = new rdmalib::Buffer<char>(size, _info.header_size)) {
report(buffer, size);
buffer->register_memory(_info.executor._state.pd(), _info.access);
return buffer;
}
throw std::bad_alloc();
}

template <class U, class arg1>
void construct (U* p, arg1 access)
{
std::cout << "constructor" << std::endl;
p->register_memory(_executor._state.pd(), access);
}

template <class U, class arg1 , class arg2>
void construct (U* p, arg1 access, arg2 head)
{
std::cout << "constructor" << std::endl;
p->register_memory(_executor._state.pd(), access, head);
}
// [[nodiscard]] inline T *construct(const std::size_t &size, const int &access, int header = 0) {

inline void deallocate(T *p, std::size_t size) noexcept {
report(p, size, 0);
std::free(p);
p->~T();
}

private:
const executor &_executor;
const RdmaInfo &_info;

void report(T *p, std::size_t n, bool alloc = true) const {
std::cout << (alloc ? "Alloc: " : "Dealloc: ") << sizeof(T) * n
Expand Down

0 comments on commit 980aa50

Please sign in to comment.