Skip to content

Commit

Permalink
Onlly allocate using alloc_bind and pass worker id to all task proper…
Browse files Browse the repository at this point in the history
…ties
  • Loading branch information
ikbuibui committed May 30, 2024
1 parent de96be4 commit 6a4d4ef
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 93 deletions.
7 changes: 4 additions & 3 deletions examples/1_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ int main(int, char*[])
redGrapes::ResourceUser<TTask> user1(
{a.read(), // complete resource
a.write().area({0}, {10}), // write only indices 0 to 10
b.write()});
b.write()},
0);

redGrapes::ResourceUser<TTask> user2({b.read()});
redGrapes::ResourceUser<TTask> user2({b.read()}, 0);

redGrapes::ResourceUser<TTask> user3({b.read(), c.write()});
redGrapes::ResourceUser<TTask> user3({b.read(), c.write()}, 0);

std::cout << "is_serial(user1,user1) = " << is_serial(user1, user1) << std::endl;
std::cout << "is_serial(user1,user2) = " << is_serial(user1, user2) << std::endl;
Expand Down
7 changes: 7 additions & 0 deletions redGrapes/TaskFreeCtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace redGrapes
{

using WorkerId = uint8_t;
using ResourceId = uint16_t;

/** WorkerID of parser to wake it up
* ID 0,1,2... are used for worker threads
Expand Down Expand Up @@ -50,6 +51,12 @@ namespace redGrapes
static inline WorkerAllocPool worker_alloc_pool;
static inline CondVar cv{0};

static inline ResourceId create_resource_uid()
{
static std::atomic<ResourceId> id = 0;
return id++;
}

static inline std::function<void()> idle = []
{
SPDLOG_TRACE("Parser::idle()");
Expand Down
3 changes: 2 additions & 1 deletion redGrapes/dispatch/mpi/request_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ namespace redGrapes
* yields until the request is done. While waiting
* for this request, other tasks will be executed.
*
* Must be called inside a running task
* @param request The MPI request to wait for
* @return the resulting MPI status of the request
*/
MPI_Status get_status(MPI_Request request)
{
auto status = memory::alloc_shared<MPI_Status>();
auto status = memory::alloc_shared_bind<MPI_Status>((*TaskCtx<TTask>::current_task)->worker_id);
auto event = *TaskCtx<TTask>::create_event();

// SPDLOG_TRACE("MPI RequestPool: status event = {}", (void*)event.get());
Expand Down
6 changes: 3 additions & 3 deletions redGrapes/redGrapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ namespace redGrapes
template<typename Container, typename... Args>
auto createFieldResource(Args&&... args) -> FieldResource<Container, RGTask>
{
return FieldResource<Container, RGTask>(args...);
return FieldResource<Container, RGTask>(std::forward<Args>(args)...);
}

template<typename T>
Expand All @@ -200,13 +200,13 @@ namespace redGrapes
template<typename T, typename... Args>
auto createIOResource(Args&&... args) -> IOResource<T, RGTask>
{
return IOResource<T, RGTask>(args...);
return IOResource<T, RGTask>(std::forward<Args>(args)...);
}

template<typename AccessPolicy>
auto createResource() -> Resource<RGTask, AccessPolicy>
{
return Resource<RGTask, AccessPolicy>();
return Resource<RGTask, AccessPolicy>(TaskFreeCtx::create_resource_uid());
}

private:
Expand Down
34 changes: 28 additions & 6 deletions redGrapes/resource/fieldresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma once

#include "redGrapes/TaskFreeCtx.hpp"
#include "redGrapes/resource/access/field.hpp"
#include "redGrapes/resource/resource.hpp"

Expand Down Expand Up @@ -89,8 +90,14 @@ namespace redGrapes
}

protected:
AreaGuard(std::shared_ptr<Container> obj)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(obj)
AreaGuard(ResourceId id, std::shared_ptr<Container> const& obj)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(id, obj)
{
}

template<typename... Args>
AreaGuard(ResourceId id, Args&&... args)
: SharedResourceObject<Container, TTask, access::FieldAccess<dim>>(id, std::forward<Args>(args)...)
{
}

Expand Down Expand Up @@ -168,7 +175,12 @@ namespace redGrapes
{
}

ReadGuard(std::shared_ptr<Container> obj) : AreaGuard<Container, TTask>(obj)
ReadGuard(ResourceId id, std::shared_ptr<Container> const& obj) : AreaGuard<Container, TTask>(id, obj)
{
}

template<typename... Args>
ReadGuard(ResourceId id, Args&&... args) : AreaGuard<Container, TTask>(id, std::forward<Args>(args)...)
{
}
};
Expand Down Expand Up @@ -219,7 +231,12 @@ namespace redGrapes
{
}

WriteGuard(std::shared_ptr<Container> obj) : ReadGuard<Container, TTask>(obj)
WriteGuard(ResourceId id, std::shared_ptr<Container> const& obj) : ReadGuard<Container, TTask>(id, obj)
{
}

template<typename... Args>
WriteGuard(ResourceId id, Args&&... args) : ReadGuard<Container, TTask>(id, std::forward<Args>(args)...)
{
}
};
Expand All @@ -231,13 +248,18 @@ namespace redGrapes
{
static constexpr size_t dim = trait::Field<Container>::dim;

FieldResource(Container* c) : fieldresource::WriteGuard<Container, TTask>(std::shared_ptr<Container>(c))
FieldResource(Container* c)
: fieldresource::WriteGuard<Container, TTask>(
TaskFreeCtx::create_resource_uid(),
std::shared_ptr<Container>(c))
{
}

template<typename... Args>
FieldResource(Args&&... args)
: fieldresource::WriteGuard<Container, TTask>(memory::alloc_shared<Container>(std::forward<Args>(args)...))
: fieldresource::WriteGuard<Container, TTask>(
TaskFreeCtx::create_resource_uid(),
std::forward<Args>(args)...)
{
}
};
Expand Down
26 changes: 20 additions & 6 deletions redGrapes/resource/ioresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma once

#include "redGrapes/TaskFreeCtx.hpp"
#include "redGrapes/resource/access/io.hpp"
#include "redGrapes/resource/resource.hpp"

Expand Down Expand Up @@ -48,7 +49,14 @@ namespace redGrapes
}

protected:
ReadGuard(std::shared_ptr<T> obj) : SharedResourceObject<T, TTask, access::IOAccess>(obj)
ReadGuard(ResourceId id, std::shared_ptr<T> const& obj)
: SharedResourceObject<T, TTask, access::IOAccess>(id, obj)
{
}

template<typename... Args>
ReadGuard(ResourceId id, Args&&... args)
: SharedResourceObject<T, TTask, access::IOAccess>(id, std::forward<Args>(args)...)
{
}
};
Expand Down Expand Up @@ -82,7 +90,12 @@ namespace redGrapes
}

protected:
WriteGuard(std::shared_ptr<T> obj) : ReadGuard<T, TTask>(obj)
WriteGuard(ResourceId id, std::shared_ptr<T> const& obj) : ReadGuard<T, TTask>(id, obj)
{
}

template<typename... Args>
WriteGuard(ResourceId id, Args&&... args) : ReadGuard<T, TTask>(id, std::forward<Args>(args)...)
{
}
};
Expand All @@ -92,13 +105,14 @@ namespace redGrapes
template<typename T, typename TTask>
struct IOResource : public ioresource::WriteGuard<T, TTask>
{
template<typename... Args>
IOResource(Args&&... args)
: ioresource::WriteGuard<T, TTask>(memory::alloc_shared<T>(std::forward<Args>(args)...))
IOResource(std::shared_ptr<T> const& o)
: ioresource::WriteGuard<T, TTask>(TaskFreeCtx::create_resource_uid(), o)
{
}

IOResource(std::shared_ptr<T> o) : ioresource::WriteGuard<T, TTask>(o)
template<typename... Args>
IOResource(Args&&... args)
: ioresource::WriteGuard<T, TTask>(TaskFreeCtx::create_resource_uid(), std::forward<Args>(args)...)
{
}

Expand Down
71 changes: 45 additions & 26 deletions redGrapes/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,52 @@
namespace redGrapes
{

using ResourceID = uint16_t;
namespace mapping
{
template<typename MappingFunc>
struct MapResourceIdToWorker
{
MappingFunc mappingFunc;

WorkerId operator()(ResourceId resourceId)
{
return mappingFunc(resourceId);
}
};

struct ModuloMapping
{
WorkerId operator()(ResourceId resourceId)
{
return resourceId % TaskFreeCtx::n_workers;
}
};

static MapResourceIdToWorker<ModuloMapping> map_resource_to_worker{};

} // namespace mapping

template<typename TTask, typename AccessPolicy>
class Resource;

template<typename TTask>
class ResourceBase
{
protected:
static ResourceID generateID()
{
static std::atomic<ResourceID> id_counter;
return id_counter.fetch_add(1);
}

public:
ChunkedList<TTask*, REDGRAPES_RUL_CHUNKSIZE> users;
SpinLock users_mutex;
ResourceID id;
ResourceId id;
uint8_t scope_level;

/**
* Create a new resource with an unused ID.
*/
ResourceBase()
: users(memory::Allocator(get_arena_id()))
, id(generateID())
ResourceBase(ResourceId id)
: users(memory::Allocator(mapping::map_resource_to_worker(id)))
, id(id)
, scope_level(TaskCtx<TTask>::scope_depth())
{
}

WorkerId get_arena_id() const
{
return id % TaskFreeCtx::n_workers;
}
};

template<typename TTask>
Expand Down Expand Up @@ -159,7 +170,7 @@ namespace redGrapes
return this->obj->resource->scope_level;
}

ResourceID resource_id() const
ResourceId resource_id() const
{
return this->obj->resource->id;
}
Expand Down Expand Up @@ -321,12 +332,10 @@ namespace redGrapes
}

public:
Resource()
{
static ResourceID i = 0;
Resource(ResourceId id)
: base{redGrapes::memory::alloc_shared_bind<ResourceBase<TTask>>(mapping::map_resource_to_worker(id), id)}

i = i++ % TaskFreeCtx::n_workers;
base = redGrapes::memory::alloc_shared_bind<ResourceBase<TTask>>(i);
{
}

/**
Expand All @@ -338,7 +347,8 @@ namespace redGrapes
*/
ResourceAccess<TTask> make_access(AccessPolicy pol) const
{
auto a = redGrapes::memory::alloc_shared_bind<Access>(base->get_arena_id(), base, pol);
auto a
= redGrapes::memory::alloc_shared_bind<Access>(mapping::map_resource_to_worker(base->id), base, pol);
return ResourceAccess<TTask>(a);
}
}; // class Resource
Expand All @@ -349,11 +359,20 @@ namespace redGrapes
// protected:
std::shared_ptr<T> obj;

SharedResourceObject(std::shared_ptr<T> obj) : obj(obj)
SharedResourceObject(ResourceId id, std::shared_ptr<T> obj) : Resource<TTask, AccessPolicy>(id), obj(obj)
{
}

SharedResourceObject(ResourceId id, SharedResourceObject const& other)
: Resource<TTask, AccessPolicy>(id)
, obj(other.obj)
{
}

SharedResourceObject(SharedResourceObject const& other) : Resource<TTask, AccessPolicy>(other), obj(other.obj)
template<typename... Args>
SharedResourceObject(ResourceId id, Args&&... args)
: Resource<TTask, AccessPolicy>(id)
, obj{memory::alloc_shared_bind<T>(mapping::map_resource_to_worker(id), std::forward<Args>(args)...)}
{
}
}; // struct SharedResourceObject
Expand Down
14 changes: 11 additions & 3 deletions redGrapes/resource/resource_user.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ namespace redGrapes
template<typename TTask>
struct ResourceUser
{
ResourceUser();
ResourceUser(ResourceUser const& other);
ResourceUser(std::initializer_list<ResourceAccess<TTask>> list);
ResourceUser(WorkerId worker_id);
ResourceUser(ResourceUser const& other) = delete;

ResourceUser(ResourceUser<TTask> const& other, WorkerId worker_id)
: access_list(memory::Allocator(worker_id), other.access_list)
, unique_resources(memory::Allocator(worker_id), other.unique_resources)
, scope_level(other.scope_level)
{
}

ResourceUser(std::initializer_list<ResourceAccess<TTask>> list, WorkerId worker_id);

void add_resource_access(ResourceAccess<TTask> ra);
void rm_resource_access(ResourceAccess<TTask> ra);
Expand Down
20 changes: 6 additions & 14 deletions redGrapes/resource/resource_user.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,17 @@ namespace redGrapes
}

template<typename TTask>
ResourceUser<TTask>::ResourceUser()
: access_list(memory::Allocator())
, unique_resources(memory::Allocator())
ResourceUser<TTask>::ResourceUser(WorkerId worker_id)
: access_list(memory::Allocator(worker_id))
, unique_resources(memory::Allocator(worker_id))
, scope_level(TaskCtx<TTask>::scope_depth())
{
}

template<typename TTask>
ResourceUser<TTask>::ResourceUser(ResourceUser<TTask> const& other)
: access_list(memory::Allocator(), other.access_list)
, unique_resources(memory::Allocator(), other.unique_resources)
, scope_level(other.scope_level)
{
}

template<typename TTask>
ResourceUser<TTask>::ResourceUser(std::initializer_list<ResourceAccess<TTask>> list)
: access_list(memory::Allocator())
, unique_resources(memory::Allocator())
ResourceUser<TTask>::ResourceUser(std::initializer_list<ResourceAccess<TTask>> list, WorkerId worker_id)
: access_list(memory::Allocator(worker_id))
, unique_resources(memory::Allocator(worker_id))
, scope_level(TaskCtx<TTask>::scope_depth())
{
for(auto& ra : list)
Expand Down
Loading

0 comments on commit 6a4d4ef

Please sign in to comment.