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

Change how resource access and passing resource to task works #80

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions examples/1_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
*/

#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/fieldresource.hpp>
#include <redGrapes/resource/ioresource.hpp>

#include <iostream>

int main(int, char*[])
{
auto rg = redGrapes::init(1);

auto a = rg.createFieldResource<std::vector<int>>();
auto b = rg.createIOResource<int>();
auto c = rg.createIOResource<int>();
auto a = redGrapes::FieldResource<std::vector<int>>();
auto b = redGrapes::IOResource<int>();
auto c = redGrapes::IOResource<int>();

redGrapes::ResourceUser user1(
{a.read(), // complete resource
a.write().area({0}, {10}), // write only indices 0 to 10
a.write({0}, {10}), // write only indices 0 to 10
b.write()},
0,
0);
Expand Down
4 changes: 2 additions & 2 deletions examples/3_functors_with_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ int main(void)
spdlog::set_level(spdlog::level::trace);
auto rg = redGrapes::init();

auto a = rg.createIOResource<int>();
auto b = rg.createIOResource<int>();
auto a = redGrapes::IOResource<int>();
auto b = redGrapes::IOResource<int>();

for(int i = 0; i < 1; ++i)
{
Expand Down
39 changes: 22 additions & 17 deletions examples/5_access_demotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/access/io.hpp>
#include <redGrapes/resource/ioresource.hpp>
#include <redGrapes/resource/resource.hpp>

#include <chrono>
#include <iostream>
Expand All @@ -18,23 +20,26 @@ int main(int, char*[])
{
spdlog::set_level(spdlog::level::trace);
auto rg = rg::init();
auto a = rg.createIOResource<int>();

rg.emplace_task(
[&](auto a)
{
std::cout << "f1 writes A" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));

std::cout << "f1 now only reads A" << std::endl;
rg.update_properties(decltype(rg)::RGTask::TaskProperties::Patch::Builder()
.remove_resources({a.write()})
.add_resources({a.read()}));
std::this_thread::sleep_for(std::chrono::seconds(1));

std::cout << "f1 done" << std::endl;
},
a.write());
auto a = redGrapes::IOResource<int>();

// Access demotion is not implemented

// rg.emplace_task(
// [&](auto a)
// {
// std::cout << "f1 writes A" << std::endl;
// std::this_thread::sleep_for(std::chrono::seconds(1));

// std::cout << "f1 now only reads A" << std::endl;
// rg.update_properties(decltype(rg)::RGTask::TaskProperties::Patch::Builder()
// .remove_resources({a})
// .add_resources({rg::newAccess(a,
// rg::access::IOAccess(rg::access::IOAccess::read))}));
// std::this_thread::sleep_for(std::chrono::seconds(1));

// std::cout << "f1 done" << std::endl;
// },
// a.write());

rg.emplace_task(
[]([[maybe_unused]] auto a)
Expand Down
4 changes: 2 additions & 2 deletions examples/6_resource_scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace rg = redGrapes;
int main()
{
auto rg = rg::init(1);
auto a = rg.createIOResource<int>(); // scope-level=0
auto a = redGrapes::IOResource<int>(); // scope-level=0

rg.emplace_task(
[&]([[maybe_unused]] auto a)
{
std::cout << "scope = " << rg.scope_depth() << std::endl;
auto b = rg.createIOResource<int>(); // scope-level=1
auto b = redGrapes::IOResource<int>(); // scope-level=1

rg.emplace_task(
[&](auto b)
Expand Down
1 change: 1 addition & 0 deletions examples/7_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF

#include <redGrapes/redGrapes.hpp>
#include <redGrapes/resource/ioresource.hpp>

#include <chrono>
#include <iostream>
Expand Down
16 changes: 12 additions & 4 deletions examples/8_child_destruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,35 @@ int main()
spdlog::set_level(spdlog::level::off);

auto rg = redGrapes::init(1);
auto a = rg.createIOResource<int>(1);
auto a = redGrapes::IOResource<int>(1);

rg.emplace_task(
[&rg]([[maybe_unused]] auto a)
{
std::cout << "scope = " << rg.scope_depth() << " a = " << *a << std::endl;

auto a2 = redGrapes::IOResource(a);
rg.emplace_task(
[&rg](auto a)
{
*a = 2;
std::cout << "scope = " << rg.scope_depth() << " a = " << *a << std::endl;
},
a.write());
a2.write());
rg.emplace_task(
[&rg](auto a)
{
*a = 3;
std::cout << "scope = " << rg.scope_depth() << " a = " << *a << std::endl;
auto a3 = redGrapes::IOResource(a);
rg.emplace_task(
[&rg](auto a)
{
std::cout << "scope = " << rg.scope_depth() << " a = " << *a << std::endl;
*a = 4;
},
a3.write());
},
a.write());
a2.write());

*a = 4;
std::cout << "scope = " << rg.scope_depth() << " a = " << *a << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions examples/cholesky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void print_matrix(std::vector<redGrapes::IOResource<double*>> A, int nblks, int
{
for(int jb = 0; jb < blocksize; ++jb)
{
std::cout << (*A[ja * nblks + ia])[jb * blocksize + ib] << "; ";
std::cout << (*A[ja * nblks + ia].getObject())[jb * blocksize + ib] << "; ";
}
}
std::cout << std::endl;
Expand Down Expand Up @@ -85,7 +85,8 @@ int main(int argc, char* argv[])
for(size_t ib = 0; ib < blksz; ++ib)
for(size_t ja = 0; ja < nblks; ++ja)
for(size_t jb = 0; jb < blksz; ++jb)
(*A[ja * nblks + ia])[jb * blksz + ib] = Alin[(ia * blksz + ib) + (ja * blksz + jb) * N];
(*A[ja * nblks + ia].getObject())[jb * blksz + ib]
= Alin[(ia * blksz + ib) + (ja * blksz + jb) * N];

print_matrix(A, nblks, blksz);

Expand Down
4 changes: 2 additions & 2 deletions examples/game_of_life.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ int main(int, char*[])
dst[{x + xi, y + yi}]
= next_state((Cell const(*)[size.x + 2]) & (src[{x + xi, y + yi}]));
},
buffers[next].write().area({x, y}, {x + chunk_size.x, y + chunk_size.y}),
buffers[current].read().area({x - 1, y - 1}, {x + chunk_size.x + 2, y + chunk_size.y + 2}));
buffers[next].write({x, y}, {x + chunk_size.x, y + chunk_size.y}),
buffers[current].read({x - 1, y - 1}, {x + chunk_size.x + 2, y + chunk_size.y + 2}));

current = next;
}
Expand Down
10 changes: 5 additions & 5 deletions examples/mpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main()

mpi_request_pool->get_status(request);
},
field[current].at({3}).read(),
field[current].read({3}),
mpi_config.read())
.enable_stack_switching();

Expand All @@ -158,18 +158,18 @@ int main()
int recv_data_count;
MPI_Get_count(&status, MPI_CHAR, &recv_data_count);
},
field[current].at({0}).write(),
field[current].write({0}),
mpi_config.read())
.enable_stack_switching();

/*
* Compute iteration
*/
for(size_t i = 1; i < field[current]->size(); ++i)
for(size_t i = 1; i < field[current].getObject()->size(); ++i)
rg.emplace_task(
[i](auto dst, auto src) { dst[{i}] = src[{i - 1}]; },
field[next].at({i}).write(),
field[current].at({i - 1}).read());
field[next].write({i}),
field[current].read({i - 1}));

/*
* Write Output
Expand Down
2 changes: 1 addition & 1 deletion redGrapes/SchedulerDescription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace redGrapes
using Key = TTag;
using ValueType = TScheduler;

SchedulerDescription(std::shared_ptr<TScheduler> scheduler, TTag = DefaultTag{}) : scheduler{scheduler}
SchedulerDescription(std::shared_ptr<TScheduler> const& scheduler, TTag = DefaultTag{}) : scheduler{scheduler}
{
}

Expand Down
39 changes: 8 additions & 31 deletions redGrapes/redGrapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "redGrapes/SchedulerDescription.hpp"
#include "redGrapes/TaskFreeCtx.hpp"
#include "redGrapes/globalSpace.hpp"
#include "redGrapes/resource/fieldresource.hpp"
#include "redGrapes/resource/ioresource.hpp"
#include "redGrapes/scheduler/event.hpp"
#include "redGrapes/scheduler/pool_scheduler.hpp"
#include "redGrapes/task/task.hpp"
Expand Down Expand Up @@ -139,7 +137,10 @@ 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(forward_arg(std::declval<Args>()))...>,
Callable,
decltype(forward_arg(std::declval<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 All @@ -154,10 +155,10 @@ namespace redGrapes
new(task) FunTask<Impl, RGTask>(worker_id, scope_depth_impl(), *scheduler_map[TSchedTag{}]);

return TaskBuilder<RGTask, Callable, Args...>(
task,
current_task_space(),
std::forward<Callable>(f),
std::forward<Args>(args)...);
task,
current_task_space(),
std::forward<Callable>(f),
std::forward<Args>(args)...);
}

template<typename Callable, typename... Args>
Expand All @@ -177,30 +178,6 @@ namespace redGrapes
return getScheduler<DefaultTag>();
}

template<typename Container>
auto createFieldResource(Container* c) -> FieldResource<Container>
{
return FieldResource<Container>(c);
}

template<typename Container, typename... Args>
auto createFieldResource(Args&&... args) -> FieldResource<Container>
{
return FieldResource<Container>(std::forward<Args>(args)...);
}

template<typename T>
auto createIOResource(std::shared_ptr<T> o) -> IOResource<T>
{
return IOResource<T>(o);
}

template<typename T, typename... Args>
auto createIOResource(Args&&... args) -> IOResource<T>
{
return IOResource<T>(std::forward<Args>(args)...);
}

template<typename AccessPolicy>
auto createResource() -> Resource<AccessPolicy>
{
Expand Down
4 changes: 2 additions & 2 deletions redGrapes/resource/access/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

#pragma once

#include "redGrapes/resource/access/area.hpp"
#include "redGrapes/resource/access/combine.hpp"
#include "redGrapes/resource/access/io.hpp"
#include "redGrapes/resource/access/range.hpp"

namespace redGrapes
{
namespace access
{

template<size_t dimension_t>
using FieldAccess = CombineAccess<IOAccess, ArrayAccess<AreaAccess, dimension_t, And_t>, And_t>;
using FieldAccess = CombineAccess<IOAccess, ArrayAccess<RangeAccess, dimension_t, And_t>, And_t>;

} // namespace access

Expand Down
4 changes: 3 additions & 1 deletion redGrapes/resource/access/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <fmt/format.h>

#include <cstdint>

namespace redGrapes
{
namespace access
Expand All @@ -22,7 +24,7 @@ namespace redGrapes
*/
struct IOAccess
{
enum Mode
enum Mode : uint8_t
{
write,
read,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ namespace redGrapes
{
namespace access
{

struct AreaAccess : std::array<size_t, 2>
// Must be in increasing order
struct RangeAccess : std::array<size_t, 2>
{
AreaAccess()
RangeAccess()
{
(*this)[0] = std::numeric_limits<size_t>::min();
(*this)[1] = std::numeric_limits<size_t>::max();
}

AreaAccess(std::array<size_t, 2> a) : std::array<size_t, 2>(a)
RangeAccess(std::array<size_t, 2> a) : std::array<size_t, 2>(a)
{
}

Expand All @@ -39,17 +39,17 @@ namespace redGrapes
&& (*this)[1] == std::numeric_limits<size_t>::max();
}

static bool is_serial(AreaAccess const& a, AreaAccess const& b)
static bool is_serial(RangeAccess const& a, RangeAccess const& b)
{
return !((a[1] <= b[0]) || (a[0] >= b[1]));
}

bool is_superset_of(AreaAccess const& a) const
bool is_superset_of(RangeAccess const& a) const
{
return (((*this)[0] <= a[0]) && ((*this)[1] >= a[1]));
}

bool operator==(AreaAccess const& other) const
bool operator==(RangeAccess const& other) const
{
return (*this)[0] == other[0] && (*this)[1] == other[1];
}
Expand All @@ -62,15 +62,15 @@ namespace redGrapes
} // namespace redGrapes

template<>
struct fmt::formatter<redGrapes::access::AreaAccess>
struct fmt::formatter<redGrapes::access::RangeAccess>
{
constexpr auto parse(format_parse_context& ctx)
{
return ctx.begin();
}

template<typename FormatContext>
auto format(redGrapes::access::AreaAccess const& acc, FormatContext& ctx)
auto format(redGrapes::access::RangeAccess const& acc, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "{{ \"area\" : {{ \"begin\" : {}, \"end\" : {} }} }}", acc[0], acc[1]);
}
Expand Down
Loading