From 1bfd93604013a45a3b451e8c10b042415cdb690d Mon Sep 17 00:00:00 2001 From: mou Date: Fri, 7 Apr 2023 03:55:37 +0800 Subject: [PATCH] Added construct in rdmaAllocator.hpp --- benchmarks/warm_benchmark.cpp | 29 +++++++++++++++++++++----- rfaas/include/rfaas/rdma_allocator.hpp | 28 ++++++++++++++++++------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/benchmarks/warm_benchmark.cpp b/benchmarks/warm_benchmark.cpp index d7a4a94..68ed242 100644 --- a/benchmarks/warm_benchmark.cpp +++ b/benchmarks/warm_benchmark.cpp @@ -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> allocator_in{info_in}; - rdmalib::Buffer* in = allocator_in.allocate(opts.input_size); + rdmalib::Buffer* 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> allocator_out{info_out}; - rdmalib::Buffer* out = allocator_out.allocate(opts.input_size); + rdmalib::Buffer* 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> allocator_v{info_v}; - // std::vector, rfaas::RdmaAllocator>> v(allocator_v); + rfaas::RdmaInfo info_v_in(executor,IBV_ACCESS_LOCAL_WRITE, rdmalib::functions::Submission::DATA_HEADER_SIZE); + rfaas::RdmaAllocator> allocator_v_in{info_v_in}; + std::vector, rfaas::RdmaAllocator>> v_in(allocator_v_in); + + rfaas::RdmaInfo info_v_out(executor,(IBV_ACCESS_LOCAL_WRITE| IBV_ACCESS_REMOTE_WRITE)); + rfaas::RdmaAllocator> allocator_v_out{info_v_out}; + std::vector, rfaas::RdmaAllocator>> v_out(allocator_v_out); + // allocator_out.construct(out, opts.input_size); + + v_in.push_back({static_cast(opts.input_size),rdmalib::functions::Submission::DATA_HEADER_SIZE}); + v_out.push_back({static_cast(opts.input_size)}); + + rdmalib::Buffer* in = &v_in[0]; + rdmalib::Buffer* out = &v_out[0]; + // TODO: Since the for loop writes a value of 1 to each byte of the in buffer, @@ -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; } diff --git a/rfaas/include/rfaas/rdma_allocator.hpp b/rfaas/include/rfaas/rdma_allocator.hpp index 1691b03..7d8a6aa 100644 --- a/rfaas/include/rfaas/rdma_allocator.hpp +++ b/rfaas/include/rfaas/rdma_allocator.hpp @@ -31,21 +31,33 @@ namespace rfaas { inline explicit RdmaAllocator(RdmaInfo& info) noexcept: _info(info) {} template - constexpr RdmaAllocator(const RdmaAllocator &) noexcept {} + constexpr explicit RdmaAllocator(const RdmaAllocator &) noexcept {} [[nodiscard]] inline T *allocate(const size_t &size) { if (size > std::numeric_limits::max() / sizeof(T)) throw std::bad_array_new_length(); - // Maybe we could directly call the memset function here - if (auto buffer = new rdmalib::Buffer(size, _info.header_size)) { - report(buffer, size); - buffer->register_memory(_info.executor._state.pd(), _info.access); - return buffer; + if (auto p = static_cast(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(size, _info.header_size)) { +// report(buffer, size); +// buffer->register_memory(_info.executor._state.pd(), _info.access); +// return buffer; +// } throw std::bad_alloc(); } + template + void construct(U* p, Args&&... args) { + ::new(p) U(std::forward(args)...); +// ::new(static_cast(p)) U(std::forward(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(); @@ -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(p) << std::dec << '\n'; }