diff --git a/.gitmodules b/.gitmodules index e5b4810..0f0b71d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,15 @@ [submodule "benchmarks/vendor/bots"] path = benchmarks/vendor/bots url = https://github.com/bsc-pm/bots +[submodule "benchmarks/vendor/staccato"] + path = benchmarks/vendor/staccato + url = https://github.com/rkuchumov/staccato +[submodule "benchmarks/vendor/uts-benchmark-code"] + path = benchmarks/vendor/uts-benchmark-code + url = https://git.code.sf.net/p/uts-benchmark/code +[submodule "benchmarks/vendor/fibril"] + path = benchmarks/vendor/fibril + url = https://github.com/chaoran/fibril +[submodule "benchmarks/vendor/tasking-2.0"] + path = benchmarks/vendor/tasking-2.0 + url = https://github.com/aprell/tasking-2.0 diff --git a/benchmarks/fibonacci/cilk_fib.cpp b/benchmarks/fibonacci/cilk_fib.cpp new file mode 100644 index 0000000..6aa7f7e --- /dev/null +++ b/benchmarks/fibonacci/cilk_fib.cpp @@ -0,0 +1,73 @@ +#include +#include +#include + +#include +#include + +using namespace std; +using namespace std::chrono; + +void fib(int n, unsigned long *sum) +{ + if (n < 2) { + *sum = 1; + return; + } + + unsigned long x; + cilk_spawn fib(n - 1, &x); + + unsigned long y; + fib(n - 2, &y); + + cilk_sync; + + *sum = x + y; +} + +void test(int n, unsigned long *sum) +{ + cilk_spawn fib(n, sum); + cilk_sync; +} + +int main(int argc, char *argv[]) +{ + size_t n = 40; + unsigned long answer; + const char *nthreads = nullptr; + + if (argc >= 2) + nthreads = argv[1]; + if (argc >= 3) + n = atoi(argv[2]); + if (nthreads == nullptr) + nthreads = to_string(thread::hardware_concurrency()).c_str(); + + __cilkrts_end_cilk(); + + auto start = system_clock::now(); + + if (__cilkrts_set_param("nworkers", nthreads) != 0) { + cerr << "Failed to set worker count\n"; + exit(EXIT_FAILURE); + } + + __cilkrts_init(); + + test(n, &answer); + + __cilkrts_end_cilk(); + + auto stop = system_clock::now(); + + cout << "Scheduler: cilk\n"; + cout << "Benchmark: fib\n"; + cout << "Threads: " << nthreads << "\n"; + cout << "Time(us): " << duration_cast(stop - start).count() << "\n"; + cout << "Input: " << n << "\n"; + cout << "Output: " << answer << "\n"; + + return 0; +} diff --git a/benchmarks/fibonacci/staccato_fib.cpp b/benchmarks/fibonacci/staccato_fib.cpp new file mode 100644 index 0000000..b2c9e7f --- /dev/null +++ b/benchmarks/fibonacci/staccato_fib.cpp @@ -0,0 +1,73 @@ +#include +#include +#include + +#include +#include + +using namespace std; +using namespace chrono; +using namespace staccato; + +class FibTask: public task +{ +public: + FibTask (int n_, unsigned long *sum_): n(n_), sum(sum_) + { } + + void execute() { + if (n < 2) { + *sum = 1; + return; + } + + unsigned long x; + spawn(new(child()) FibTask(n - 1, &x)); + + unsigned long y; + spawn(new(child()) FibTask(n - 2, &y)); + + wait(); + + *sum = x + y; + + return; + } + +private: + int n; + unsigned long *sum; +}; + +int main(int argc, char *argv[]) +{ + size_t n = 40; + unsigned long answer; + size_t nthreads = 0; + + if (argc >= 2) + nthreads = atoi(argv[1]); + if (argc >= 3) + n = atoi(argv[2]); + if (nthreads == 0) + nthreads = thread::hardware_concurrency(); + + auto start = system_clock::now(); + + { + scheduler sh(2, nthreads); + sh.spawn(new(sh.root()) FibTask(n, &answer)); + sh.wait(); + } + + auto stop = system_clock::now(); + + cout << "Scheduler: staccato\n"; + cout << "Benchmark: fib\n"; + cout << "Threads: " << nthreads << "\n"; + cout << "Time(us): " << duration_cast(stop - start).count() << "\n"; + cout << "Input: " << n << "\n"; + cout << "Output: " << answer << "\n"; + + return 0; +} diff --git a/benchmarks/nqueens/weave_nqueens.nim b/benchmarks/nqueens/weave_nqueens.nim index 17bcdc8..0528e4c 100644 --- a/benchmarks/nqueens/weave_nqueens.nim +++ b/benchmarks/nqueens/weave_nqueens.nim @@ -126,7 +126,7 @@ proc nqueens_par(n, j: int32, a: CharArray): int32 = localCounts[i] = spawn nqueens_par(n, j+1, b) for i in 0 ..< n: - if localCounts[i].isAllocated: + if localCounts[i].isSpawned(): result += sync(localCounts[i]) const solutions = [ diff --git a/benchmarks/vendor/fibril b/benchmarks/vendor/fibril new file mode 160000 index 0000000..9991aac --- /dev/null +++ b/benchmarks/vendor/fibril @@ -0,0 +1 @@ +Subproject commit 9991aac319908136edebcee8607743e23d1b6894 diff --git a/benchmarks/vendor/staccato b/benchmarks/vendor/staccato new file mode 160000 index 0000000..4e22cb7 --- /dev/null +++ b/benchmarks/vendor/staccato @@ -0,0 +1 @@ +Subproject commit 4e22cb7e1f78cbba3a11c8eef6093b31544ae09f diff --git a/benchmarks/vendor/tasking-2.0 b/benchmarks/vendor/tasking-2.0 new file mode 160000 index 0000000..303ce3e --- /dev/null +++ b/benchmarks/vendor/tasking-2.0 @@ -0,0 +1 @@ +Subproject commit 303ce3e09a7c118241b9d5482219e3e3b848212e diff --git a/benchmarks/vendor/uts-benchmark-code b/benchmarks/vendor/uts-benchmark-code new file mode 160000 index 0000000..722e896 --- /dev/null +++ b/benchmarks/vendor/uts-benchmark-code @@ -0,0 +1 @@ +Subproject commit 722e896caa09fcab621312b1aca528c1985cf3c6 diff --git a/weave/datatypes/flowvars.nim b/weave/datatypes/flowvars.nim index 64aee2c..b2946ca 100644 --- a/weave/datatypes/flowvars.nim +++ b/weave/datatypes/flowvars.nim @@ -41,10 +41,10 @@ type else: chan: ptr ChannelSpscSingleObject[T] -func isAllocated*(fv: Flowvar): bool {.inline.}= - ## Returns true if a future is allocated +func isSpawned*(fv: Flowvar): bool {.inline.}= + ## Returns true if a future is spawned ## This may be useful for recursive algorithms that - ## may or may not return a future. + ## may or may not spawn a future depending on a condition. ## This is similar to Option or Maybe types when defined(WV_LazyFlowvar): return not fv.lfv.isNil