Commit a06701ba authored by Xavier Thompson's avatar Xavier Thompson

Adapt and move tests/ into examples/

parent ef3a110f
Makefile.g++-11
\ No newline at end of file
#include <typon/typon.hpp> #include <typon/typon.hpp>
#include <cstdio> #include <typon/logger.hpp>
using namespace typon; using namespace typon;
...@@ -10,31 +10,29 @@ int fibo(int n) { ...@@ -10,31 +10,29 @@ int fibo(int n) {
return fibo(n - 1) + fibo(n - 2); return fibo(n - 1) + fibo(n - 2);
} }
Task<void> hello(int i) { Task<void> hello(int id) {
int n = fibo(10); int n = fibo(20);
(void) i; LOG("hello(%d), fibo(20) = %d", id, n);
(void) n;
printf("hello from %d on %u, fibo(40) = %d\n", i, Scheduler::thread_id, n);
co_return; co_return;
} }
Join<void> parallel() { Join<void> parallel() {
for (int i = 0; i < 30; i++) { for (int id = 0; id < 30; id++) {
co_await fork(hello(i)); co_await fork(hello(id));
printf("parallel resumed on %u\n", Scheduler::thread_id); LOG("resume after fork(hello(%d))", id);
} }
printf("parallel syncing on %u\n", Scheduler::thread_id); LOG("sync()");
co_await Sync(); co_await Sync();
printf("parallel resumed on %u\n", Scheduler::thread_id); LOG("resume after sync()");
for (int i = 30; i < 60; i++) { for (int id = 30; id < 60; id++) {
co_await fork(hello(i)); co_await fork(hello(id));
printf("parallel resumed on %u\n", Scheduler::thread_id); LOG("resume after fork(hello(%d))", id);
} }
} }
Root root() { Root root() {
co_await parallel(); co_await parallel();
printf("root resumed on %u\n", Scheduler::thread_id); LOG("resume root after parallel()");
} }
int main() { int main() {
......
#include <typon/typon.hpp>
#include <typon/logger.hpp>
#include <cstdio>
using namespace typon;
int fibo(int n) {
if (n < 2) {
return n;
}
return fibo(n - 1) + fibo(n - 2);
}
Task<void> hello(int id) {
int result = fibo(20);
LOG(" || hello(%2d), fibo(20) = %d", id, result);
co_return;
}
Task<void> fail() {
throw std::exception();
co_return;
}
Join<void> fail_join() {
co_await fork(fail());
}
Join<void> join_sync(int max) {
for (int id = 0; id < max; id++) {
co_await fork(hello(id));
}
try {
co_await fork(fail_join());
LOG(" ... fork(fail_join()) did not throw (stolen continuation)");
} catch(std::exception &) {
LOG(" ... fork(fail_join()) did throw, rethrowing");
throw;
}
LOG(" ... sync()");
try
{
co_await Sync();
LOG(" ... ERROR sync() did not throw");
} catch(std::exception &) {
LOG(" ... OK sync() did throw, rethrowing");
throw;
}
}
Join<void> join_no_sync(int max) {
for (int id = 0; id < max; id++) {
co_await fork(hello(id));
}
try {
co_await fork(fail_join());
LOG(" ... fork(fail_join()) did not throw (stolen continuation)");
} catch(std::exception &) {
LOG(" ... fork(fail_join()) did throw, rethrowing");
throw;
}
LOG(" ... join_no_sync(%d) is exiting normally", max);
}
Root root_sync(int max) {
try {
LOG(">>> join_sync(%d)", max);
co_await join_sync(max);
LOG("ERROR join_sync(%d) did not throw\n", max);
} catch(std::exception &) {
LOG("OK join_sync(%d) did throw\n", max);
}
}
Root root_no_sync(int max) {
try {
LOG(">>> join_no_sync(%d)", max);
co_await join_no_sync(max);
LOG("ERROR join_no_sync(%d) did not throw\n", max);
} catch(std::exception &) {
LOG("OK join_no_sync(%d) did throw\n", max);
}
}
int main() {
root_sync(0).call();
root_sync(20).call();
root_no_sync(0).call();
root_no_sync(20).call();
puts("done");
return 0;
}
...@@ -4,9 +4,18 @@ ...@@ -4,9 +4,18 @@
using namespace typon; using namespace typon;
Task<int> fibo(int n) {
if (n < 2) {
co_return n;
}
Future a = co_await future(fibo(n - 1));
Future b = co_await future(fibo(n - 2));
co_return co_await a.get() + co_await b.get();
}
Root root() { Root root() {
printf("root resumed on %u\n", Scheduler::thread_id); int result = co_await fibo(40);
co_return; printf("%d\n", result);
} }
int main() { int main() {
......
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>
using u32 = std::uint_fast32_t;
u32 N = u32(1) << 26;
int main() {
std::vector<u32> v;
v.reserve(N);
for (u32 i = 0; i < N; i++) {
v.push_back(i);
}
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
printf("v[0] = %lu\n", v[0]);
puts("done");
}
#include <typon/typon.hpp>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>
using namespace typon;
long N = 1 << 26;
long C = N >> 8;
template <typename RandomIt>
Task<void> shuffle(RandomIt first, RandomIt last)
{
std::shuffle(first, last, std::mt19937{std::random_device{}()});
co_return;
}
template <typename RandomIt>
Join<void> parallel_shuffle(RandomIt first, RandomIt last)
{
auto it = first;
for (; it < last; it += C) {
co_await fork(shuffle(it, it + C));
}
if (it < last)
{
co_await fork(shuffle(it, last));
}
}
Root root() {
std::vector<long> v;
v.reserve(N);
for (long i = 0; i < N; i++) {
v.push_back(i);
}
co_await parallel_shuffle(v.begin(), v.end());
printf("v[0] = %lu\n", v[0]);
}
int main() {
root().call();
puts("done");
}
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>
using u64 = std::uint_fast64_t;
u64 N = u64(1) << 26;
int R = 100;
u64 sum(u64 * array, u64 size)
{
u64 s = 0;
for (u64 i = 0; i < size; i++) {
s += array[i];
}
return s;
}
int main() {
std::vector<u64> v;
v.reserve(N);
for (u64 i = 0; i < N; i++) {
v.push_back(i);
}
u64 result = 0;
for (int i = 0; i < R; i++) {
result += sum(v.data(), v.size());
}
printf("sum = %lu\n", result);
puts("done");
}
#include <typon/typon.hpp>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <random>
#include <vector>
using namespace typon;
using u64 = std::uint_fast64_t;
u64 N = u64(1) << 26;
int C = 256;
int R = 100;
Task<u64> sum(u64 * array, u64 size)
{
u64 s = 0;
for (u64 i = 0; i < size; i++) {
s += array[i];
}
co_return s;
}
Join<u64> parallel_sum(u64 * array, u64 size)
{
std::vector<Forked<u64>> v;
v.reserve(C - 1);
u64 start = 0;
u64 chunk = size / C;
for (int i = 0; i < C - 1; i++) {
v.push_back(co_await fork(sum(&(array[start]), chunk)));
start = start + chunk;
}
u64 s = co_await sum(&(array[start]), size - start);
co_await Sync();
for (auto & f : v) {
s += f.get();
}
co_return s;
}
Root root() {
std::vector<u64> v;
v.reserve(N);
for (u64 i = 0; i < N; i++) {
v.push_back(i);
}
u64 result = 0;
for (int i = 0; i < R; i++) {
result += co_await parallel_sum(v.data(), v.size());
}
printf("sum = %lu\n", result);
}
int main() {
root().call();
puts("done");
}
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
int fibo(int n) {
if (n < 2) {
return n;
}
return fibo(n - 1) + fibo(n - 2);
}
Task<int> fib() {
co_return fibo(30);
}
Task<void> fail() {
throw std::exception();
co_return;
}
Join<void> fail_join() {
co_await fork(fail());
}
Join<void> join(int max) {
std::vector<Forked<int>> v;
for (int i = 0; i < max; i++) {
v.push_back(co_await fork(fib()));
}
co_await fork(fail_join());
puts("info: fork did not throw");
co_await Sync();
for (auto & f : v) {
printf("result = %d\n", f.get());
}
puts("info: join exited normally");
}
Root root1() {
try {
co_await join(0);
puts("error: join did not raise an exception");
} catch(std::exception &) {
puts("ok: join raised an exception");
}
}
Root root2() {
try {
co_await join(20);
puts("error: join did not raise an exception");
} catch(std::exception &) {
puts("ok: join raised an exception");
}
}
int main() {
puts("join(0)");
root1().call();
puts("join(20)");
root2().call();
puts("done");
return 0;
}
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
Task<int> fibo(int n) {
// printf("fibo(%d)\n", n);
if (n < 2) {
co_return n;
}
int a = co_await fibo(n - 1);
int b = co_await fibo(n - 2);
co_return a + b;
}
Join<int> parallel_fibo(int n) {
// printf("parallel_fibo(%d)\n", n);
if (n < 2) {
co_return n;
}
if (n < 10)
{
int a = co_await fibo(n - 1);
int b = co_await fibo(n - 2);
// printf("// parallel_fibo(%d)\n", n);
co_return a + b;
}
auto a = co_await fork(parallel_fibo(n - 1));
auto b = co_await fork(parallel_fibo(n - 2));
co_await Sync();
co_return a.get() + b.get();
}
Root root() {
int result = co_await parallel_fibo(40);
printf("result = %d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
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