Skip to content

Commit

Permalink
Enable allocation with std::vector
Browse files Browse the repository at this point in the history
- Add a `construct` method in `rdmaAllocator.hpp` to enable allocation with std::vector.
- Test demonstrating standard memory allocation practices.
- Test demonstrating allocation with our custom allocator.
- Test demonstrating allocation with std::vector.
- Improve coding style and adhering to clang-tidy standards.

Signed-off-by: mou <William-Mou>
  • Loading branch information
mou authored and mou committed Apr 6, 2023
1 parent 1bfd936 commit 5273fcf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
34 changes: 18 additions & 16 deletions benchmarks/warm_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,40 @@ int main(int argc, char ** argv)
// FIXME: move me to a memory allocator

// 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);


// 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>* in0 = allocator_in.allocate(opts.input_size);
allocator_in.construct(in0, opts.input_size, rdmalib::functions::Submission::DATA_HEADER_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>* out0 = allocator_out.allocate(opts.input_size);
allocator_out.construct(out0, opts.input_size);
// 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>* in0 = allocator_in.allocate(opts.input_size);
// allocator_in.construct(in0, opts.input_size, rdmalib::functions::Submission::DATA_HEADER_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>* out0 = allocator_out.allocate(opts.input_size);
// allocator_out.construct(out0, opts.input_size);


// Sample: test demonstrating allocation with std::vector.
rfaas::RdmaInfo info_v_in(executor,IBV_ACCESS_LOCAL_WRITE, rdmalib::functions::Submission::DATA_HEADER_SIZE);

rfaas::RdmaInfo info_v_in(executor, IBV_ACCESS_LOCAL_WRITE, rdmalib::functions::Submission::DATA_HEADER_SIZE);
rfaas::RdmaAllocator<rdmalib::Buffer<char>> allocator_v_in{info_v_in};
std::vector<rdmalib::Buffer<char>, rfaas::RdmaAllocator<rdmalib::Buffer<char>>> v_in(allocator_v_in);

rfaas::RdmaInfo info_v_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE));
rfaas::RdmaInfo info_v_out(executor, (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
rfaas::RdmaAllocator<rdmalib::Buffer<char>> allocator_v_out{info_v_out};
std::vector<rdmalib::Buffer<char>, rfaas::RdmaAllocator<rdmalib::Buffer<char>>> v_out(allocator_v_out);
// allocator_out.construct(out, opts.input_size);

v_in.push_back({static_cast<size_t>(opts.input_size),rdmalib::functions::Submission::DATA_HEADER_SIZE});
v_out.push_back({static_cast<size_t>(opts.input_size)});

rdmalib::Buffer<char>* in = &v_in[0];
rdmalib::Buffer<char>* out = &v_out[0];
v_in.emplace_back(static_cast<size_t>(opts.input_size), rdmalib::functions::Submission::DATA_HEADER_SIZE);
v_out.emplace_back(static_cast<size_t>(opts.input_size));

rdmalib::Buffer<char> *in = &v_in[0];
rdmalib::Buffer<char> *out = &v_out[0];


// TODO: Since the for loop writes a value of 1 to each byte of the in buffer,
Expand Down
44 changes: 21 additions & 23 deletions rfaas/include/rfaas/rdma_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ namespace rfaas {
struct RdmaInfo {

public:
RdmaInfo(executor& executor, const int access, const int header_size=0)
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;

const executor &executor;
const int &access;
const int &header_size = 0;
};

template<typename T>
Expand All @@ -28,56 +29,53 @@ namespace rfaas {
public:
typedef T value_type;

inline explicit RdmaAllocator(RdmaInfo& info) noexcept: _info(info) {}
inline constexpr explicit RdmaAllocator(RdmaInfo &info) noexcept: _info(info) {}

template<class U>
constexpr explicit RdmaAllocator(const RdmaAllocator<U> &) noexcept {}
inline constexpr explicit RdmaAllocator(const RdmaAllocator<U> &) noexcept {}

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

if (auto p = static_cast<T*>(std::malloc(size * sizeof(T) + _info.header_size)))
{
report(p, size * sizeof(T) + _info.header_size);
if (auto p = static_cast<T *>(std::malloc(size * sizeof(T) + _info.header_size))) {
report(p, size * sizeof(T) + _info.header_size);
return p;
}
// // Maybe we could directly call the memset function here
// 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 <typename U, typename... Args>
void construct(U* p, Args&&... args) {
template<typename U, typename... Args>
inline void construct(U *p, Args &&... args) {
::new(p) U(std::forward<Args>(args)...);
// ::new(static_cast<void*>(p)) U(std::forward<Args>(args)...);
p->register_memory(_info.executor._state.pd(), _info.access);
}

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

template<typename U>
struct rebind {
using other = RdmaAllocator<U>;
};

private:
const RdmaInfo &_info;

void report(T *p, std::size_t size, bool alloc = true) const {
inline void report(T *p, std::size_t size, bool alloc = true) const {
std::cout << (alloc ? "Alloc: " : "Dealloc: ") << size
<< " 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; }
inline 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; }
inline bool operator!=(const RdmaAllocator<T> &, const RdmaAllocator<U> &) { return false; }
}

#endif //__RFAAS_RDMA_ALLOCATOR_HPP__
#endif //__RFAAS_RDMA_ALLOCATOR_HPP__

0 comments on commit 5273fcf

Please sign in to comment.