Skip to content

Commit

Permalink
#1899: util: use new memory pool
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander authored and thearusable committed Aug 25, 2022
1 parent 7e07313 commit ade8a7e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/vt/pool/static_sized/memory_pool_equal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "vt/config.h"
#include "vt/pool/static_sized/memory_pool_equal.h"
#include "vt/pool/header/pool_header.h"
#include "vt/runnable/runnable.h"

#include <vector>
#include <cstdint>
Expand Down Expand Up @@ -146,3 +147,9 @@ template struct MemoryPoolEqual<memory_size_small>;
template struct MemoryPoolEqual<memory_size_medium>;

}} //end namespace vt::pool

namespace vt { namespace pool {

template struct MemoryPoolEqual<vt::runnable::detail::runnable_context_max_size>;

}} /* end namespace vt::pool */
2 changes: 1 addition & 1 deletion src/vt/runnable/make_runnable.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct RunnableMaker {
*/
template <typename ElmT, typename IdxT = typename ElmT::IndexType>
RunnableMaker&& withCollection(ElmT* elm) {
impl_->template addContext<ctx::Collection<IdxT>>(elm);
impl_->template addContext<ctx::Collection>(elm);
set_handler_ = true;

if (handler_ != uninitialized_handler) {
Expand Down
26 changes: 16 additions & 10 deletions src/vt/runnable/runnable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "vt/configs/debug/debug_var_unused.h"
#include "vt/configs/arguments/app_config.h"
#include "vt/scheduler/thread_manager.h"
#include "vt/pool/static_sized/memory_pool_equal.h"

namespace vt { namespace runnable {

Expand Down Expand Up @@ -199,33 +200,38 @@ void RunnableNew::run() {
}

void RunnableNew::begin() {
for (auto&& ctx : contexts_) {
ctx->begin();
for (int i = 0; i < ci_; i++) {
contexts_[i]->begin();
}
}

void RunnableNew::end() {
for (auto&& ctx : contexts_) {
ctx->end();
for (int i = 0; i < ci_; i++) {
contexts_[i]->end();
}
}

void RunnableNew::suspend() {
for (auto&& ctx : contexts_) {
ctx->suspend();
for (int i = 0; i < ci_; i++) {
contexts_[i]->suspend();
}
}

void RunnableNew::resume() {
for (auto&& ctx : contexts_) {
ctx->resume();
for (int i = 0; i < ci_; i++) {
contexts_[i]->resume();
}
}

void RunnableNew::send(elm::ElementIDStruct elm, MsgSizeType bytes) {
for (auto&& ctx : contexts_) {
ctx->send(elm, bytes);
for (int i = 0; i < ci_; i++) {
contexts_[i]->send(elm, bytes);
}
}

/*static*/ std::unique_ptr<
pool::MemoryPoolEqual<detail::runnable_context_max_size>
> RunnableNew::up_pool =
std::make_unique<pool::MemoryPoolEqual<detail::runnable_context_max_size>>();

}} /* end namespace vt::runnable */
57 changes: 52 additions & 5 deletions src/vt/runnable/runnable.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@

#include "vt/messaging/message/smart_ptr.h"
#include "vt/context/runnable_context/base.h"
#include "vt/context/runnable_context/td.h"
#include "vt/context/runnable_context/trace.h"
#include "vt/context/runnable_context/from_node.h"
#include "vt/context/runnable_context/set_context.h"
#include "vt/context/runnable_context/collection.h"
#include "vt/context/runnable_context/lb_data.h"
#include "vt/context/runnable_context/continuation.h"
#include "vt/elm/elm_id.h"
#include "vt/utils/ptr/unique_fixed.h"

// fwd-declarations for the element types
namespace vt { namespace vrt {
Expand All @@ -61,14 +69,49 @@ struct UntypedCollection;

namespace vt { namespace runnable {

namespace detail {

template <typename T>
constexpr T& constexprMax(T& a, T& b) {
return a > b ? a : b;
}

template <typename T>
constexpr T& arrayMaxImpl(T* begin, T* end) {
return begin + 1 == end
? *begin
: constexprMax(*begin, arrayMaxImpl(begin + 1, end));
}

template <typename T, std::size_t N>
constexpr T& arrayMax(T(&arr)[N]) {
return arrayMaxImpl(arr, arr + N);
}

constexpr std::size_t runnable_context_array[] = {
#if vt_check_enabled(trace_enabled)
sizeof(ctx::Trace),
#endif
sizeof(ctx::Continuation),
sizeof(ctx::FromNode),
sizeof(ctx::LBData),
sizeof(ctx::SetContext),
sizeof(ctx::TD),
sizeof(ctx::Collection)
};

constexpr std::size_t runnable_context_max_size = arrayMax(runnable_context_array);

} /* end namespace detail */

/**
* \struct RunnableNew
*
* \brief Holds a runnable active handler along with all the context associated
* with it to run it independently of the where in the stack it was created.
*/
struct RunnableNew {
using CtxBasePtr = std::unique_ptr<ctx::Base>;
using CtxBasePtr = vt::util::ptr::unique_ptr_fixed<ctx::Base>;

template <typename... Args>
using FnParamType = void(*)(Args...);
Expand Down Expand Up @@ -107,9 +150,7 @@ struct RunnableNew {
* \c T
*/
template <typename T, typename... Args>
void addContext(Args&&... args) {
contexts_.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
}
void addContext(Args&&... args);

/**
* \brief Set up a handler to run on an collection object
Expand Down Expand Up @@ -241,10 +282,16 @@ struct RunnableNew {
task_ = task_in;
}

/// Memory pool for fixed sized unique pointer allocation
static std::unique_ptr<
pool::MemoryPoolEqual<detail::runnable_context_max_size>
> up_pool;

private:
MsgSharedPtr<BaseMsgType> msg_ = nullptr; /**< The associated message */
bool is_threaded_ = false; /**< Whether ULTs are supported */
std::vector<CtxBasePtr> contexts_; /**< Vector of contexts */
std::array<CtxBasePtr, 8> contexts_; /**< Array of contexts */
int ci_ = 0; /**< Current index of contexts */
ActionType task_ = nullptr; /**< The runnable's task */
bool done_ = false; /**< Whether task is complete */
bool suspended_ = false; /**< Whether task is suspended */
Expand Down
14 changes: 12 additions & 2 deletions src/vt/runnable/runnable.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,25 @@ namespace vt { namespace runnable {

template <typename T>
T* RunnableNew::get() {
for (auto&& ctx : contexts_) {
auto t = dynamic_cast<T*>(ctx.get());
for (int i = 0; i < ci_; i++) {
auto t = dynamic_cast<T*>(contexts_[i].get());
if (t) {
return t;
}
}
return nullptr;
}

template <typename T, typename... Args>
void RunnableNew::addContext(Args&&... args) {
auto c = vt::util::ptr::make_unique_fixed<
T, detail::runnable_context_max_size
>(*up_pool.get(), std::forward<Args>(args)...);
contexts_[ci_++] = vt::util::ptr::unique_fixed_to_base<ctx::Base, detail::runnable_context_max_size>(
*up_pool.get(), std::move(c)
);
}

}} /* end namespace vt::runnable */

#endif /*INCLUDED_VT_RUNNABLE_RUNNABLE_IMPL_H*/

0 comments on commit ade8a7e

Please sign in to comment.