Skip to content

Commit

Permalink
Added construct in rdmaAllocator.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mou authored and mou committed Apr 6, 2023
1 parent 980aa50 commit 1bfd936
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
29 changes: 24 additions & 5 deletions benchmarks/warm_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,34 @@ int main(int argc, char ** argv)
// 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>* in = allocator_in.allocate(opts.input_size);
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>* out = allocator_out.allocate(opts.input_size);
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(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);
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::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];



// TODO: Since the for loop writes a value of 1 to each byte of the in buffer,
Expand Down Expand Up @@ -132,5 +147,9 @@ int main(int argc, char ** argv)
printf("%d ", ((char*)out->data())[i]);
printf("\n");

// std::free(&v_in);
// std::free(&v_out);
// v_in.get_allocator().deallocate(&v_in[0],opts.input_size);
// v_out.get_allocator().deallocate(&v_out[0],opts.input_size);
return 0;
}
28 changes: 20 additions & 8 deletions rfaas/include/rfaas/rdma_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,33 @@ namespace rfaas {
inline explicit RdmaAllocator(RdmaInfo& info) noexcept: _info(info) {}

template<class U>
constexpr RdmaAllocator(const RdmaAllocator<U> &) noexcept {}
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();

// 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;
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) {
::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();
Expand All @@ -54,8 +66,8 @@ namespace rfaas {
private:
const RdmaInfo &_info;

void report(T *p, std::size_t n, bool alloc = true) const {
std::cout << (alloc ? "Alloc: " : "Dealloc: ") << sizeof(T) * n
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';
}
Expand Down

0 comments on commit 1bfd936

Please sign in to comment.