#include #include #include #include #include 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), Do>(); auto aThen = makeOperand(); auto aDone = makeOperand(); auto in = link(int)>( aDo & link)>( 4 * link)>(aThen) ) & link, R<0>)>(aDone) ); auto a = link(count * link(P<0>)>(in)); auto f = implement(a); f(7); auto fIn = implement(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(); struct Transform0 { int operator()(int x) { return x+1; } }; auto transform0 = makeOperand(); struct Transform1 { int operator()(int x) { return x-2; } }; auto transform1 = makeOperand(); struct Produce { int operator()(int x, int y) { return x*y; } }; auto produce = makeOperand(); struct Select { int mod; int operator()(int a, int b) { if(a%mod == b%mod) return a b%mod)? a : b; } }; auto select = makeOperand(); auto innerTask = link(int)>( link)>(generate) & link)>(transform0) & link)>(transform1) & link, R<1>)>(produce) ); auto task = link(10 * link(P<0>)>(innerTask)) ->* select; auto f = implement(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 lg{m}; std::cerr << std::this_thread::get_id() << ' ' << id << std::endl; } }; //auto info = makeOperand(); struct Generate { int v; int operator()(std::mt19937& g) { return v+g(); } }; auto generate = makeOperand(); struct Transform0 { int operator()(int x) { use(1000); return x+1; } }; auto transform0 = makeOperand(); struct Transform1 { int operator()(int x) { use(1000); return x-2; } }; auto transform1 = makeOperand(); struct Produce { int operator()(int x, int y) { return x*y; } }; auto produce = makeOperand(); struct Select { int mod; int operator()(int a, int b) { if(a%mod == b%mod) return a b%mod)? a : b; } }; auto select = makeOperand(); auto innerSeq = link(int)>( link(generate) & link)>(transform0) & link)>(transform1) & link, R<1>)>(produce) ); auto innerTask0 = link(16 * link(P<0>)>(innerSeq)) ->* select; auto innerTask = &link(30 * link(innerTask0)) ->* select; auto task = link(2 * link)>(innerTask)); auto f = implement(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(); }