alsk/examples/tests.cpp

106 lines
3.7 KiB
C++

#include <alsk/alsk.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace alsk::arg;
using namespace alsk::edsl;
void example0(int count) {
struct Do { int operator()(int x) { std::puts("Do"); return x+1; } };
struct Then { void operator()(int v) { std::printf("Then {%d}\n", v); } };
struct Done { int operator()(int x, int y) { std::puts("Done"); return x*y; } };
auto aDo = makeOperand<int(P<0>), Do>();
auto aThen = makeOperand<Then>();
auto aDone = makeOperand<Done>();
auto in = link<R<2>(int)>(
aDo &
link<void(R<0>)>(
4 * link<void(P<0>)>(aThen)
) &
link<int(P<0>, R<0>)>(aDone)
);
auto a = link<void(int)>(count * link<R<2>(P<0>)>(in));
auto f = implement<alsk::exec::Sequential>(a);
f(7);
auto fIn = implement<alsk::exec::Sequential>(in);
std::printf("result: %d\n", fIn(5));
}
void example1() {
// TODO? not really stateful here
struct Generate { int value; int operator()(int b) { return ++value+b; } }; auto generate = makeOperand<Generate>();
struct Transform0 { int operator()(int x) { return x+1; } }; auto transform0 = makeOperand<Transform0>();
struct Transform1 { int operator()(int x) { return x-2; } }; auto transform1 = makeOperand<Transform1>();
struct Produce { int operator()(int x, int y) { return x*y; } }; auto produce = makeOperand<Produce>();
struct Select {
int mod;
int operator()(int a, int b) { if(a%mod == b%mod) return a<b? a : b; return (a%mod > b%mod)? a : b; }
};
auto select = makeOperand<int(int, int), Select>();
auto innerTask = link<R<3>(int)>(
link<int(P<0>)>(generate) &
link<int(R<0>)>(transform0) &
link<int(R<0>)>(transform1) &
link<int(R<2>, R<1>)>(produce)
);
auto task = link<int(int)>(10 * link<R<3>(P<0>)>(innerTask)) ->* select;
auto f = implement<alsk::exec::StaticPool>(task);
f.skeleton.select.mod = 5;
std::printf("results: {");
for(int i = 4; i < 9; ++i) std::printf("%d, ", f(i));
std::puts("}");
}
std::mutex m;
void use(unsigned int n) { unsigned long long volatile v{}; for(unsigned int i{}; i < n; ++i) for(unsigned int j{}; j < 500; ++j) ++v; }
void example2() {
struct Info { void operator()(std::size_t id) {
std::lock_guard<std::mutex> lg{m};
std::cerr << std::this_thread::get_id() << ' ' << id << std::endl;
} }; //auto info = makeOperand<void(CtxId), Info>();
struct Generate { int v; int operator()(std::mt19937& g) { return v+g(); } }; auto generate = makeOperand<Generate>();
struct Transform0 { int operator()(int x) { use(1000); return x+1; } }; auto transform0 = makeOperand<Transform0>();
struct Transform1 { int operator()(int x) { use(1000); return x-2; } }; auto transform1 = makeOperand<Transform1>();
struct Produce { int operator()(int x, int y) { return x*y; } }; auto produce = makeOperand<Produce>();
struct Select {
int mod;
int operator()(int a, int b) { if(a%mod == b%mod) return a<b? a : b; return (a%mod > b%mod)? a : b; }
};
auto select = makeOperand<int(int, int), Select>();
auto innerSeq = link<R<3>(int)>(
link<int(RNG)>(generate) &
link<int(R<0>)>(transform0) &
link<int(R<0>)>(transform1) &
link<int(R<2>, R<1>)>(produce)
);
auto innerTask0 = link<int(int)>(16 * link<R<3>(P<0>)>(innerSeq)) ->* select;
auto innerTask = &link<int(int)>(30 * link<int(int)>(innerTask0)) ->* select;
auto task = link<void(int)>(2 * link<int(P<0>)>(innerTask));
auto f = implement<alsk::exec::StaticThread>(task);
f.executor.cores = 4;
f.executor.repeatability.upTo(f.executor.cores);
f.skeleton.task.select.mod = 12;
f.skeleton.task.task.select.mod = 17;
for(int i = 4; i < 9; ++i) f(i);
}
int main(int argc, char**) {
example0(argc);
example2();
}