Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make shared pointer std atomic #83

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions redGrapes/TaskFreeCtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <vector>

Expand All @@ -36,10 +37,10 @@ namespace redGrapes
inline memory::ChunkedBumpAlloc<memory::HwlocAlloc>& get_alloc(WorkerId worker_id)
{
assert(worker_id < allocs.size());
return allocs[worker_id];
return *allocs[worker_id];
}

std::vector<memory::ChunkedBumpAlloc<memory::HwlocAlloc>> allocs;
std::vector<std::unique_ptr<memory::ChunkedBumpAlloc<memory::HwlocAlloc>>> allocs;
};

struct TaskFreeCtx
Expand Down
9 changes: 6 additions & 3 deletions redGrapes/dispatch/thread/worker_pool.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "redGrapes/memory/hwloc_alloc.hpp"
#include "redGrapes/util/trace.hpp"

#include <memory>

namespace redGrapes
{
namespace dispatch
Expand Down Expand Up @@ -45,9 +47,10 @@ namespace redGrapes
WorkerId pu_id = worker_id % TaskFreeCtx::n_pus;
// allocate worker with id `i` on arena `i`,
hwloc_obj_t obj = hwloc_get_obj_by_type(TaskFreeCtx::hwloc_ctx.topology, HWLOC_OBJ_PU, pu_id);
TaskFreeCtx::worker_alloc_pool.allocs.emplace_back(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE);
TaskFreeCtx::worker_alloc_pool.allocs.push_back(
std::make_unique<memory::ChunkedBumpAlloc<memory::HwlocAlloc>>(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE));

auto worker = memory::alloc_shared_bind<WorkerThread<Worker>>(worker_id, obj, worker_id, *this);
workers.emplace_back(worker);
Expand Down
6 changes: 0 additions & 6 deletions redGrapes/memory/chunked_bump_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ namespace redGrapes
{
}

ChunkedBumpAlloc(ChunkedBumpAlloc&& other)
: chunk_size(other.chunk_size)
, bump_allocators(other.bump_allocators)
{
}

static inline size_t roundup_to_poweroftwo(size_t s)
{
s--;
Expand Down
5 changes: 3 additions & 2 deletions redGrapes/scheduler/thread_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ namespace redGrapes
// allocate worker with id `i` on arena `i`,
hwloc_obj_t obj = hwloc_get_obj_by_type(TaskFreeCtx::hwloc_ctx.topology, HWLOC_OBJ_PU, pu_id);
TaskFreeCtx::worker_alloc_pool.allocs.emplace_back(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE);
std::make_unique<memory::ChunkedBumpAlloc<memory::HwlocAlloc>>(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE));

m_worker_thread
= memory::alloc_shared_bind<dispatch::thread::WorkerThread<Worker>>(m_base_id, obj, m_base_id);
Expand Down
24 changes: 12 additions & 12 deletions redGrapes/util/atomic_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ namespace redGrapes
struct ItemControlBlock
{
bool volatile deleted;
std::shared_ptr<ItemControlBlock> prev;
std::atomic<std::shared_ptr<ItemControlBlock>> prev;
uintptr_t item_data_ptr;

ItemControlBlock(memory::Block blk) : deleted(false), item_data_ptr(blk.ptr)
ItemControlBlock(memory::Block blk) : deleted(false), prev(nullptr), item_data_ptr(blk.ptr)
{
/* put Item at front and initialize it
* with the remaining memory region
Expand Down Expand Up @@ -76,11 +76,11 @@ namespace redGrapes
*/
void skip_deleted_prev()
{
std::shared_ptr<ItemControlBlock> p = std::atomic_load(&prev);
std::shared_ptr<ItemControlBlock> p = prev.load();
while(p && p->deleted)
p = std::atomic_load(&p->prev);
p = (p->prev).load();

std::atomic_store(&prev, p);
prev.store(p);
}

Item* get() const
Expand All @@ -90,7 +90,7 @@ namespace redGrapes
};

Allocator alloc;
std::shared_ptr<ItemControlBlock> head;
std::atomic<std::shared_ptr<ItemControlBlock>> head;
size_t const chunk_capacity;

/* keeps a single, predefined pointer
Expand Down Expand Up @@ -256,7 +256,7 @@ namespace redGrapes
*/
MutBackwardIterator rbegin() const
{
return MutBackwardIterator{std::atomic_load(&head)};
return MutBackwardIterator{head.load()};
}

MutBackwardIterator rend() const
Expand All @@ -266,7 +266,7 @@ namespace redGrapes

ConstBackwardIterator crbegin() const
{
return ConstBackwardIterator{std::atomic_load(&head)};
return ConstBackwardIterator{head.load()};
}

ConstBackwardIterator crend() const
Expand Down Expand Up @@ -297,9 +297,9 @@ namespace redGrapes
bool append_successful = false;
while(!append_successful)
{
old_head = std::atomic_load(&head);
std::atomic_store(&new_head->prev, old_head);
append_successful = std::atomic_compare_exchange_strong(&head, &old_head, new_head);
old_head = head.load();
(new_head->prev).store(old_head);
append_successful = head.compare_exchange_strong(old_head, new_head);
}

return MutBackwardIterator{old_head};
Expand All @@ -312,7 +312,7 @@ namespace redGrapes

std::shared_ptr<ItemControlBlock> expected(nullptr);
std::shared_ptr<ItemControlBlock> const& desired = new_head;
return std::atomic_compare_exchange_strong(&head, &expected, desired);
return head.compare_exchange_strong(expected, desired);
}
};

Expand Down
Loading