Commit 7d1be819 authored by Matthieu Dorier's avatar Matthieu Dorier

done with the argobots wrappers

parent ce1fb848
/*
* Copyright (c) 2017 UChicago Argonne, LLC
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __THALLIUM_ABT_ERRORS_HPP
#define __THALLIUM_ABT_ERRORS_HPP
......
......@@ -104,27 +104,27 @@ class pool {
}
static ABT_unit u_create_from_thread(ABT_thread t) {
auto uu = unit_allocator.allocate(1);
unit_allocator.construct(uu, thread(t));
auto uu = std::allocator_traits<Ualloc>::allocate(unit_allocator,1);
std::allocator_traits<Ualloc>::construct(unit_allocator, uu, thread(t));
return static_cast<ABT_unit>(uu);
}
static ABT_unit u_create_from_task(ABT_task t) {
auto uu = unit_allocator.allocate(1);
unit_allocator.construct(uu, task(t));
auto uu = std::allocator_traits<Ualloc>::allocate(unit_allocator,1);
std::allocator_traits<Ualloc>::construct(unit_allocator, uu, task(t));
return static_cast<ABT_unit>(uu);
}
static void u_free(ABT_unit* u) {
auto uu = static_cast<U*>(*u);
uu->~U();
unit_allocator.deallocate(uu, 1);
std::allocator_traits<Ualloc>::destroy(unit_allocator, uu);
std::allocator_traits<Ualloc>::deallocate(unit_allocator, uu, 1);
*u = nullptr;
}
static int p_init(ABT_pool p, ABT_pool_config cfg) {
P* impl = pool_allocator.allocate(1);
pool_allocator.construct(impl);
P* impl = std::allocator_traits<Palloc>::allocate(pool_allocator, 1);
std::allocator_traits<Palloc>::construct(pool_allocator, impl);
int ret = ABT_pool_set_data(p, static_cast<void*>(impl));
return ret;
}
......@@ -162,9 +162,12 @@ class pool {
static int p_free(ABT_pool p) {
void* data;
int ret = ABT_pool_get_data(p, &data);
if(ret != ABT_SUCCESS)
return ret;
auto impl = static_cast<P*>(data);
impl->~P();
pool_allocator.deallocate(impl, 1);
std::allocator_traits<Palloc>::destroy(pool_allocator, impl);
std::allocator_traits<Palloc>::deallocate(pool_allocator, impl, 1);
return ret;
}
};
......@@ -446,6 +449,21 @@ class pool {
return thread::create_on_pool(m_pool, forward_work_unit, static_cast<void*>(fp));
}
/**
* @brief Create a thread running the specified function and push it
* into the pool.
*
* @tparam F type of function to run as a task. Must have operator()().
* @param f Function to run as a task.
* @param attr Thread attributes.
*
* @return a thread object managing the created thread.
*/
template<typename F>
managed<thread> make_thread(F&& f, const thread::attribute& attr) {
auto fp = new std::function<void(void)>(std::forward<F>(f));
return thread::create_on_pool(m_pool, forward_work_unit, static_cast<void*>(fp), attr);
}
};
template<typename P, typename U, typename Palloc, typename Ualloc>
......
......@@ -75,8 +75,8 @@ class scheduler {
public:
static int init(ABT_sched s, ABT_sched_config) {
auto ss = scheduler_allocator.allocate(1);
scheduler_allocator.construct(ss,s);
auto ss = std::allocator_traits<Salloc>::allocate(scheduler_allocator, 1);
std::allocator_traits<Salloc>::construct(scheduler_allocator,ss,s);
return ABT_sched_set_data(s, static_cast<void*>(ss));
}
......@@ -89,10 +89,13 @@ class scheduler {
static int free(ABT_sched s) {
void* data;
ABT_sched_get_data(s, &data);
int ret = ABT_sched_get_data(s, &data);
if(ret != ABT_SUCCESS)
return ret;
S* impl = static_cast<S*>(data);
impl->~S();
scheduler_allocator.deallocate(impl,1);
std::allocator_traits<Salloc>::destroy(scheduler_allocator, impl);
std::allocator_traits<Salloc>::deallocate(scheduler_allocator,impl,1);
return ret;
}
static ABT_pool get_migr_pool(ABT_sched s) {
......
......@@ -49,10 +49,6 @@ enum class thread_state : std::int32_t {
terminated = ABT_THREAD_STATE_TERMINATED
};
/* MISSING:
ABT_thread_get_attr
*/
/**
* @brief The thread class wraps an Argobot's ABT_thread resource.
* Note that the task object does NOT manage the internal ABT_thread.
......@@ -67,6 +63,133 @@ class thread {
friend class xstream;
friend class managed<thread>;
public:
class attribute {
friend class thread;
ABT_thread_attr m_attr;
/**
* @brief Makes an attribute object from an ABT_thread_attr.
* Used by thread::get_attribute().
*
* @param attr ABT_thread_attr handle.
*/
explicit attribute(ABT_thread_attr attr)
: m_attr(attr) {}
public:
/**
* @brief Native handle type.
*/
typedef ABT_thread_attr native_handle_type;
/**
* @brief Constructor.
*/
attribute() {
TL_THREAD_ASSERT(ABT_thread_attr_create(&m_attr));
}
/**
* @brief Copy-constructor is deleted.
*/
attribute(const attribute&) = delete;
/**
* @brief Move constructor.
*/
attribute(attribute&& other)
: m_attr(other.m_attr) {
other.m_attr = ABT_THREAD_ATTR_NULL;
}
/**
* @brief Copy-assignment operator is deleted.
*/
attribute& operator=(const attribute&) = delete;
/**
* @brief Move-assignment operator.
*/
attribute& operator=(attribute&& other) {
if(this == &other)
return *this;
if(m_attr != ABT_THREAD_ATTR_NULL) {
TL_THREAD_ASSERT(ABT_thread_attr_free(&m_attr));
}
m_attr = other.m_attr;
other.m_attr = ABT_THREAD_ATTR_NULL;
return *this;
}
/**
* @brief Destructor.
*/
~attribute() {
if(m_attr != ABT_THREAD_ATTR_NULL) {
ABT_thread_attr_free(&m_attr);
}
}
/**
* @brief Get the underlying native handle.
*
* @return the native handle.
*/
native_handle_type native_handle() const {
return m_attr;
}
/**
* @brief Sets the address and size of the stack to use.
*
* @param addr address of the stack.
* @param size size of the stack.
*/
void set_stack(void* addr, size_t size) {
TL_THREAD_ASSERT(ABT_thread_attr_set_stack(m_attr, addr, size));
}
/**
* @brief Gets the address of the stack.
*
* @return the address of the stack.
*/
void* get_stack_address() const {
void* addr;
size_t size;
TL_THREAD_ASSERT(ABT_thread_attr_get_stack(m_attr, &addr, &size));
return addr;
}
/**
* @brief Get the size of the stack.
*
* @return the size of the stack.
*/
size_t get_stack_size() const {
void* addr;
size_t size;
TL_THREAD_ASSERT(ABT_thread_attr_get_stack(m_attr, &addr, &size));
return size;
}
/**
* @brief Set the migratability of the stack.
*/
void set_migratable(bool migratable) {
ABT_bool flag = migratable ? ABT_TRUE : ABT_FALSE;
TL_THREAD_ASSERT(ABT_thread_attr_set_migratable(m_attr, flag));
}
};
private:
ABT_thread m_thread;
thread(ABT_thread t)
......@@ -84,6 +207,17 @@ class thread {
return managed<thread>(t);
}
static managed<thread> create_on_xstream(ABT_xstream es, void(*f)(void*), void* arg, const attribute& attr) {
ABT_thread t;
TL_THREAD_ASSERT(ABT_thread_create_on_xstream(es, f, arg, attr.native_handle(), &t));
return managed<thread>(t);
}
static managed<thread> create_on_pool(ABT_pool p, void(*f)(void*), void* arg, const attribute& attr) {
ABT_thread t;
TL_THREAD_ASSERT(ABT_thread_create(p, f, arg, attr.native_handle(), &t));
return managed<thread>(t);
}
void destroy() {
if(m_thread != ABT_THREAD_NULL)
ABT_thread_free(&m_thread);
......@@ -196,6 +330,12 @@ class thread {
return s;
}
attribute get_attribute() const {
ABT_thread_attr attr;
TL_THREAD_ASSERT(ABT_thread_get_attr(m_thread, &attr));
return attribute(attr);
}
/**
* @brief Sets the thread's migratability. By default, all threads
* are migratable. If flag is true, the thread becomes migratable.
......
......@@ -446,6 +446,22 @@ class xstream {
return thread::create_on_xstream(m_xstream, forward_work_unit, static_cast<void*>(fp));
}
/**
* @brief Create a thread running the specified function and push it
* into the pool.
*
* @tparam F type of function to run as a thread. Must have operator()().
* @param f Function to run as a thread.
* @param attr Thread attributes.
*
* @return a managed<thread> object managing the created thread.
*/
template<typename F>
managed<thread> make_thread(F&& f, const thread::attribute& attr) {
auto fp = new std::function<void(void)>(std::forward<F>(f));
return thread::create_on_xstream(m_xstream, forward_work_unit, static_cast<void*>(fp), attr);
}
/**
* @brief Create a task running the specified function and push it
* into the pool.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment