Files
sakurac/tests/compiler/workers.sakura
T
2026-10-28 22:13:49 -05:00

138 lines
4.7 KiB
Plaintext

#! @file workers.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of sharing the compiler's work out among threads: each
#! index done once, each after those it waits for, each thread with scratch
#! memory of its own, and functions optimized on threads made into what one
#! thread makes them.
import std::containers;
import std::testing;
import sakura::analyzer;
module sakura::compiler;
@test def work_shared_out_is_done_once_for_each_index(t: &mut tester): void {
val count: u64 = 2000;
for threads in 1..6u {
var done := vector::<atomic<u32>>::new();
for _ in 0..count {
done.append(atomic::<u32>::new(0));
}
share_out(count, threads as u32, def(index: u64): void { _ = done[index].fetch_add(1); });
var once := true;
for i in 0..count {
once = once && done[i].load() == 1;
}
t.expects(once);
}
}
@test def work_shared_out_in_order_waits_for_what_it_waits_for(t: &mut tester): void {
# Each index but the first waits for the one halfway to it, as in a heap,
# and is stamped as it is done, after the one it waits for.
val count: u64 = 1023;
var waiting := groups::<u32>::new(count);
for child in 1..count {
waiting.count((child - 1) / 2);
}
waiting.seal();
for child in 1..count {
waiting.put((child - 1) / 2, child as u32);
}
for threads in 1..6u {
var stamps := vector::<atomic<u64>>::new();
for _ in 0..count {
stamps.append(atomic::<u64>::new(0));
}
var clock := atomic::<u64>::new(0);
share_out_ordered(count, threads as u32, &waiting, def(index: u64): void {
stamps[index].store(clock.fetch_add(1) + 1);
});
var ordered := true;
for i in 1..count {
ordered = ordered && stamps[i].load() != 0 && stamps[(i - 1) / 2].load() < stamps[i].load();
}
t.expects(ordered && stamps[0].load() != 0);
}
}
@test def each_thread_takes_scratch_memory_of_its_own(t: &mut tester): void {
# Each index fills scratch memory it marked, then reads it back; another
# thread marking, taking, or rewinding the same memory meanwhile would change
# what it reads.
var wrong := atomic::<u32>::new(0);
share_out(256, 5, def(index: u64): void {
val point := mark_scratch();
var filled := vector::<u64>::new(scratch());
for k in 0..4000u {
filled.append(index * 4000 + k);
}
var also := vector::<u64>::new(scratch());
also.resize(512);
for k in 0..4000u {
if filled[k] != index * 4000 + k {
_ = wrong.fetch_add(1);
}
}
also.close();
filled.close();
release_scratch(point);
});
t.expects(wrong.load() == 0);
}
@test def functions_optimized_on_threads_are_made_what_one_thread_makes_them(t: &mut tester): void {
# Functions calling each other, in a cycle and through several levels, which
# inlining copies into one another, so one optimized before or after what it
# inlines would come out otherwise.
val text := """
def leaf(x: i64): i64 { return x * 2 + 1; }
def twice(x: i64): i64 { return leaf(x) + leaf(x + 1); }
def even(n: i64): bool { return n == 0 ? true : odd(n - 1); }
def odd(n: i64): bool { return n == 0 ? false : even(n - 1); }
def sum(n: i64): i64 {
var total: i64 = 0;
for i in 0..n { total += twice(i); }
return total;
}
def pick(n: i64): i64 { return even(n) ? sum(n) : twice(n); }
def main(): void { if pick(10) == 0 { return; } }
""";
val names := "leaf twice even odd sum pick main";
for level in 1..3u {
val one := optimized_on(1, level as u32, names, text);
t.expects(one.length > 200 && !one.view().starts_with("the program"));
for threads in 2..6u {
val many := optimized_on(threads as u32, level as u32, names, text);
t.expects(one.view() == many.view());
}
}
# The same, where some are `@inline`, and so copied at `-O0` too.
val marked := """
struct inline {}
@inline def leaf(x: i64): i64 { return x * 2 + 1; }
@inline def twice(x: i64): i64 { return leaf(x) + leaf(x + 1); }
@inline def even(n: i64): bool { return n == 0 ? true : odd(n - 1); }
def odd(n: i64): bool { return n == 0 ? false : even(n - 1); }
def sum(n: i64): i64 {
var total: i64 = 0;
for i in 0..n { total += twice(i); }
return total;
}
def pick(n: i64): i64 { return even(n) ? sum(n) : twice(n); }
def main(): void { if pick(10) == 0 { return; } }
""";
for level in 0..3u {
val one := optimized_on(1, level as u32, names, marked);
t.expects(one.length > 200 && !one.view().starts_with("the program"));
for threads in 2..6u {
val many := optimized_on(threads as u32, level as u32, names, marked);
t.expects(one.view() == many.view());
}
}
}