Skip to content

Commit

Permalink
start using access pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ikbuibui committed Jul 15, 2024
1 parent 4f6133d commit c5d32d6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 64 deletions.
2 changes: 1 addition & 1 deletion redGrapes/redGrapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace redGrapes

SPDLOG_TRACE("emplace task to worker {}", worker_id);

using Impl = typename std::invoke_result_t<BindArgs<Callable, Args...>, Callable, Args...>;
using Impl = typename std::invoke_result_t<BindArgs<Callable, decltype(detail::forward_arg(std::forward<Args>(args)))...>, Callable, decltype(detail::forward_arg(std::forward<Args>(args)))...>;
// this is not set to nullptr. But it goes out of scope. Memory is managed by allocate
FunTask<Impl, RGTask>* task;
memory::Allocator alloc(worker_id);
Expand Down
4 changes: 2 additions & 2 deletions redGrapes/resource/fieldresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace redGrapes

operator ResourceAccess() const noexcept
{
return this->make_access(access::FieldAccess<dim>(access::IOAccess::read, this->m_area));
return this->res.make_access(access::FieldAccess<dim>(access::IOAccess::read, this->m_area));
}

protected:
Expand Down Expand Up @@ -249,7 +249,7 @@ namespace redGrapes

operator ResourceAccess() const noexcept
{
return this->make_access(access::FieldAccess<dim>(access::IOAccess::write, this->m_area));
return this->res.make_access(access::FieldAccess<dim>(access::IOAccess::write, this->m_area));
}

protected:
Expand Down
58 changes: 10 additions & 48 deletions redGrapes/resource/ioresource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ namespace redGrapes
{
operator ResourceAccess() const noexcept
{
return this->make_access(access::IOAccess::read);
return this->res.make_access(access::IOAccess::read);
}

ReadGuard read() const noexcept
auto read() const noexcept
{
return *this;
return ResourceAccessPair<std::shared_ptr<T> const>(
this->obj,
this->res.make_access(access::IOAccess::read));
}

T const& operator*() const noexcept
Expand All @@ -54,35 +56,15 @@ namespace redGrapes
}

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

template<typename... Args>
ReadGuard(ResourceId id, Args&&... args)
: SharedResourceObject<T, access::IOAccess>(id, std::forward<Args>(args)...)
{
}

ReadGuard(Resource< access::IOAccess> const& res, std::shared_ptr<T> const& obj)
: SharedResourceObject<T, access::IOAccess>(res, obj)
{
}

template<typename... Args>
ReadGuard(Resource< access::IOAccess> const& res, Args&&... args)
: SharedResourceObject<T, access::IOAccess>(res, std::forward<Args>(args)...)
{
}
using SharedResourceObject<T, access::IOAccess>::SharedResourceObject;
};

template<typename T>
struct WriteGuard : public ReadGuard<T>
{
operator ResourceAccess() const noexcept
{
return this->make_access(access::IOAccess::write);
return this->res.make_access(access::IOAccess::write);
}

WriteGuard write() const noexcept
Expand All @@ -106,34 +88,15 @@ namespace redGrapes
}

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

template<typename... Args>
WriteGuard(ResourceId id, Args&&... args) : ReadGuard<T>(id, std::forward<Args>(args)...)
{
}

WriteGuard(Resource< access::IOAccess> const& res, std::shared_ptr<T> const& obj)
: ReadGuard<T>(res, obj)
{
}

template<typename... Args>
WriteGuard(Resource< access::IOAccess> const& res, Args&&... args)
: ReadGuard<T>(res, std::forward<Args>(args)...)
{
}
using ReadGuard<T>::ReadGuard;
};

} // namespace ioresource

template<typename T>
struct IOResource : public ioresource::WriteGuard<T>
{
IOResource(std::shared_ptr<T> const& o)
: ioresource::WriteGuard<T>(TaskFreeCtx::create_resource_uid(), o)
IOResource(std::shared_ptr<T> const& o) : ioresource::WriteGuard<T>(TaskFreeCtx::create_resource_uid(), o)
{
}

Expand All @@ -147,8 +110,7 @@ namespace redGrapes
}

template<typename U>
IOResource(IOResource<U> const& res, std::shared_ptr<T> const& obj)
: ioresource::WriteGuard<T>(res, obj)
IOResource(IOResource<U> const& res, std::shared_ptr<T> const& obj) : ioresource::WriteGuard<T>(res, obj)
{
}

Expand Down
31 changes: 21 additions & 10 deletions redGrapes/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ namespace redGrapes
}
}; // class ResourceAccess

template<typename T>
struct ResourceAccessPair : public std::pair<T, ResourceAccess>
{
using std::pair<T, ResourceAccess>::pair;

operator ResourceAccess() const
{
return this->second;
}
};

namespace trait
{

Expand Down Expand Up @@ -352,37 +363,37 @@ namespace redGrapes
}; // class Resource

template<typename T, typename AccessPolicy>
struct SharedResourceObject : Resource<AccessPolicy>
struct SharedResourceObject
{
// protected:
std::shared_ptr<T> obj;

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

template<typename... Args>
SharedResourceObject(ResourceId id, Args&&... args)
: Resource<AccessPolicy>(id)
: res{id}
, obj{memory::alloc_shared_bind<T>(mapping::map_resource_to_worker(id), std::forward<Args>(args)...)}
{
}

SharedResourceObject(Resource<AccessPolicy> const& res, std::shared_ptr<T> const& obj)
: Resource<AccessPolicy>{res}
, obj{obj}
SharedResourceObject(Resource<AccessPolicy> const& res, std::shared_ptr<T> const& obj) : res{res}, obj{obj}
{
}

template<typename... Args>
SharedResourceObject(Resource<AccessPolicy> const& res, Args&&... args)
: Resource<AccessPolicy>{res}
: res{res}
, obj{memory::alloc_shared_bind<T>(
mapping::map_resource_to_worker(res.resource_id()),
std::forward<Args>(args)...)}
{
}

protected:
std::shared_ptr<T> obj;
Resource<AccessPolicy> res;


}; // struct SharedResourceObject

} // namespace redGrapes
Expand Down
34 changes: 31 additions & 3 deletions redGrapes/task/task_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <spdlog/spdlog.h>

#include <type_traits>
#include <utility>

namespace redGrapes
{
Expand All @@ -38,13 +39,38 @@ namespace redGrapes
}
};

namespace detail
{

// Helper function to conditionally forward the argument
template<typename T>
constexpr auto forward_arg(T&& arg)
{
return std::forward<T>(arg);
}

template<typename T>
constexpr auto forward_arg(ResourceAccessPair<T>&& arg)
{
return std::forward<T>(arg.first);
}

// decltype(detail::forward_arg(std::forward<Args>(args)))
// detail::forward_arg(std::forward<Args>(args))...));

} // namespace detail

/* TASK BUILDER */

template<typename TTask, typename Callable, typename... Args>
struct TaskBuilder : TTask::TaskProperties::template Builder<TaskBuilder<TTask, Callable, Args...>>
{
using Impl = typename std::invoke_result_t<BindArgs<Callable, Args...>, Callable, Args...>;
using Result = typename std::invoke_result_t<Callable, Args...>;
using Impl = typename std::invoke_result_t<
BindArgs<Callable, decltype(detail::forward_arg(std::forward<Args>(std::declval<Args>())))...>,
Callable,
decltype(detail::forward_arg(std::forward<Args>(std::declval<Args>())))...>;
using Result = typename std::
invoke_result_t<Callable, decltype(detail::forward_arg(std::forward<Args>(std::declval<Args>())))...>;

std::shared_ptr<TaskSpace> space;
FunTask<Impl, TTask>* task;
Expand All @@ -66,7 +92,9 @@ namespace redGrapes
this->init_id();

// set impl
task->impl.emplace(BindArgs<Callable, Args...>{}(std::move(f), std::forward<Args>(args)...));
task->impl.emplace(BindArgs<Callable, decltype(detail::forward_arg(std::forward<Args>(args)))...>{}(
std::move(f),
detail::forward_arg(std::forward<Args>(args))...));
}

TaskBuilder(TaskBuilder& other)
Expand Down

0 comments on commit c5d32d6

Please sign in to comment.