Commit 28034ea5 authored by Xavier Thompson's avatar Xavier Thompson

Add test programs

parent 0ea612aa
CXX=g++-11
CXXFLAGS=-g -O3 -fwhole-program -flto -Wall -Wextra -Werror -std=c++20 -fcoroutines -I..
LDLIBS=-ljemalloc -pthread
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <cstdio>
using namespace typon::rt;
int fibo(int n) {
if (n < 2) {
return n;
}
return fibo(n - 1) + fibo(n - 2);
}
Fork<int> fib() {
co_return fibo(30);
}
Fork<void> fail() {
throw std::exception();
co_return;
}
Join<void> join(int max) {
std::vector<Future<Fork<int>::promise_type>> v;
for (int i = 0; i < max; i++) {
v.push_back(co_await fib());
}
co_await fail();
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 <task.hpp>
#include <cstdio>
using namespace typon::rt;
Task<int> fibo(int 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;
}
int main() {
int result = fibo(40).call();
printf("%d\n", result);
}
#include <task.hpp>
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <cstdio>
using namespace typon::rt;
int fibo(int n) {
if (n < 2)
return n;
return fibo(n - 1) + fibo(n - 2);
}
template <typename T>
Fork<T> fork(Task<T> task)
{
co_return co_await std::move(task);
}
Task<void> hello(int i) {
int n = fibo(10);
(void) i;
(void) n;
printf("hello from %d on %u, fibo(40) = %d\n", i, Scheduler::thread_id, n);
co_return;
}
Join<void> parallel() {
for (int i = 0; i < 30; i++) {
co_await fork(hello(i));
printf("parallel resumed on %u\n", Scheduler::thread_id);
}
printf("parallel syncing on %u\n", Scheduler::thread_id);
co_await Sync();
printf("parallel resumed on %u\n", Scheduler::thread_id);
for (int i = 30; i < 60; i++) {
co_await fork(hello(i));
printf("parallel resumed on %u\n", Scheduler::thread_id);
}
}
Root root() {
co_await parallel();
printf("root resumed on %u\n", Scheduler::thread_id);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <cstdio>
using namespace typon::rt;
int fibo(int n) {
if (n < 2)
return n;
return fibo(n - 1) + fibo(n - 2);
}
Fork<void> hello(int i) {
int n = fibo(10);
(void) i;
(void) n;
printf("hello from %d on %u, fibo(40) = %d\n", i, Scheduler::thread_id, n);
// printf("hello from %d on %lu, fibo(40) = %d\n", i, riften::detail::static_id, n);
co_return;
}
Join<void> parallel() {
for (int i = 0; i < 30; i++) {
co_await hello(i);
printf("parallel resumed on %u\n", Scheduler::thread_id);
// printf("parallel resumed on %lu\n", riften::detail::static_id);
}
printf("parallel syncing on %u\n", Scheduler::thread_id);
// printf("parallel syncing on %lu\n", riften::detail::static_id);
co_await Sync();
printf("parallel resumed on %u\n", Scheduler::thread_id);
// printf("parallel resumed on %lu\n", riften::detail::static_id);
for (int i = 30; i < 60; i++) {
co_await hello(i);
printf("parallel resumed on %u\n", Scheduler::thread_id);
// printf("parallel resumed on %lu\n", riften::detail::static_id);
}
}
Root root() {
co_await parallel();
printf("root resumed on %u\n", Scheduler::thread_id);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <root.hpp>
#include <join.hpp>
#include <fork.hpp>
#include <cstdio>
using namespace typon::rt;
Join<int> fibo(int n);
Fork<int> fork_fibo(int n);
Join<int> fibo(int n) {
if (n < 2) {
co_return n;
}
Future a = co_await fork_fibo(n - 1);
Future b = co_await fork_fibo(n - 2);
co_await Sync();
co_return a.get() + b.get();
}
Fork<int> fork_fibo(int n) {
co_return co_await fibo(n);
}
Root root() {
int result = co_await fibo(40);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
int fibo(int n) {
if (n < 2) {
return n;
}
int a = fibo(n - 1);
int b = fibo(n - 2);
return a + b;
}
Join<int> parallel_fibo(int n);
Fork<int> fork_fibo(int n);
Join<int> parallel_fibo(int n) {
if (n < 2) {
co_return n;
}
if (n < 25)
{
int a = fibo(n - 1);
int b = fibo(n - 2);
co_return a + b;
}
Future a = co_await fork_fibo(n - 1);
Future b = co_await fork_fibo(n - 2);
co_await Sync();
co_return a.get() + b.get();
}
Fork<int> fork_fibo(int n) {
co_return co_await parallel_fibo(n);
}
Root root() {
int result = co_await parallel_fibo(40);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
Task<int> add(int a, int b) {
co_return a + b;
}
Task<int> loop(int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result = result + co_await add(i, i+1);
}
co_return result;
}
int main() {
int result = loop(100000000).call();
printf("%d\n", result);
}
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
Task<int> fibo(int 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;
}
int main() {
int result = fibo(40).call();
printf("%d\n", result);
}
CXX = clang++-14
CXXFLAGS = -g -O3 -flto -Werror -Wall -Wextra -std=c++20 -stdlib=libc++ -I..
LDLIBS = -pthread
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
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);
Fork<int> fork_fibo(int n);
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;
}
Future a = co_await fork_fibo(n - 1);
Future b = co_await fork_fibo(n - 2);
co_await Sync();
co_return a.get() + b.get();
}
Fork<int> fork_fibo(int n) {
co_return co_await parallel_fibo(n);
}
Root root() {
int result = co_await parallel_fibo(40);
printf("result = %d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <fork.hpp>
#include <join.hpp>
#include <root.hpp>
#include <cstdio>
using namespace typon::rt;
Root root() {
printf("root resumed on %u\n", Scheduler::thread_id);
co_return;
}
int main() {
root().call();
puts("done");
return 0;
}
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
Task<int> add(int a, int b) {
printf("add(%d, %d)\n", a, b);
co_return a + b;
}
Task<int> call_add_but_ignore_result(int a, int b) {
co_await add(a, b).discard();
co_return 1;
}
int main() {
call_add_but_ignore_result(2, 3).call_discard();
}
#include <task.hpp>
#include <cstdio>
using namespace typon::rt;
Task<int> add(int a, int b) {
co_return a + b;
}
Task<int> add_indirection(int a, int b) {
co_return co_await add(a, b);
}
int main() {
int result = add_indirection(2, 3).call();
return result;
}
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