Skip to content

Commit

Permalink
Modify RdmaAllocator to have a structure similar to the ["C++ named…
Browse files Browse the repository at this point in the history
… requirements example for the Allocator"](https://en.cppreference.com/w/cpp/named_req/Allocator).

Signed-off-by: mou <William-Mou>
  • Loading branch information
mou authored and mou committed Apr 3, 2023
1 parent 4105069 commit 889333e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 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 -std=c++17")
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra")

###
# Mandatory: devices configuration file.
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/warm_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ int main(int argc, char ** argv)
// FIXME: move me to a memory allocator

rfaas::RdmaAllocator<rdmalib::Buffer<char> > rdmaAllocator(executor);
auto in = rdmaAllocator.allocate(opts.input_size, IBV_ACCESS_LOCAL_WRITE,
rdmalib::Buffer<char>* in = rdmaAllocator.allocate(opts.input_size, IBV_ACCESS_LOCAL_WRITE,
rdmalib::functions::Submission::DATA_HEADER_SIZE);
auto out = rdmaAllocator.allocate(opts.input_size, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
rdmalib::Buffer<char>* out = rdmaAllocator.allocate(opts.input_size, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
// 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);
Expand Down
46 changes: 33 additions & 13 deletions rfaas/include/rfaas/rdma_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,47 @@
namespace rfaas {
template<typename T>
class RdmaAllocator {
private:
const executor &_executor;

public:
typedef T value_type;

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

// inline T *allocate(const std::size_t &, const int &, int = 0);
inline T *allocate(const std::size_t &size, const int &access, int header=0) {
if (size > std::size_t(-1) / sizeof(T))
throw std::bad_alloc();
template<class U>
constexpr RdmaAllocator(const RdmaAllocator<U> &) noexcept {}

[[nodiscard]] inline T *allocate(const std::size_t &size, const int &access, int header = 0) {
if (size > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_array_new_length();

// Maybe we could directly call the memset function here
if (auto buffer = new rdmalib::Buffer<char>(size, header)) {
report(buffer, size);
buffer->register_memory(_executor._state.pd(), access);
return buffer;
}
throw std::bad_alloc();
}

auto buffer = new rdmalib::Buffer<char>(size, header);
buffer->register_memory(_executor._state.pd(), access);
std::cout << "allocate memory by RdmaAllocator" << std::endl;
return buffer;
inline void deallocate(T *p, std::size_t size) noexcept {
report(p, size, 0);
std::free(p);
}

inline void deallocate(T *p, std::size_t n) noexcept {
operator delete(p);
private:
const executor &_executor;

void report(T *p, std::size_t n, bool alloc = true) const {
std::cout << (alloc ? "Alloc: " : "Dealloc: ") << sizeof(T) * n
<< " bytes at " << std::hex << std::showbase
<< reinterpret_cast<void *>(p) << std::dec << '\n';
}
};

template<class T, class U>
bool operator==(const RdmaAllocator<T> &, const RdmaAllocator<U> &) { return true; }

template<class T, class U>
bool operator!=(const RdmaAllocator<T> &, const RdmaAllocator<U> &) { return false; }
}

#endif //__RFAAS_RDMA_ALLOCATOR_HPP__

0 comments on commit 889333e

Please sign in to comment.