#! @file workers.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains how the compiler shares work out among threads: a crew of them, #! with the thread asking, takes the indexes of what is to be done, each the #! next one none has taken, until none is left. What each does is its own, as #! the code of one function, and what they all read stays as it is while they #! work, so they never wait on one another but to take an index. Each thread of #! the crew has scratch memory of its own, as `scratch.sakura` says. #! #! Where one thread is asked for, none starts, and the thread asking does every #! index, in order: the compiler then works as it would with no threads at all. import std::containers; import std::thread; module sakura::compiler; #! Does something for each index below a count, on as many threads as asked, #! the one calling among them, each taking the next index none has taken. #! @param count How many indexes there are. #! @param threads How many threads work, the one calling among them; where #! more are asked for than there are indexes, one is started #! for each index but the first, and where some cannot be #! started, those that were do it all. #! @param each What is done for an index, which may be done on any of the #! threads, at the same time as for others. pub def share_out(count: u64, threads: u32, each: def(u64): void): void { val helpers := crew_for(count, threads); if helpers == 0 { for i in 0..count { each(i); } return; } var next := atomic::::new(0); run_crew(helpers, def(): void { take_each(&mut next, count, each); }); } #! Does something for each index below a count, as `share_out` does, but for #! each only once it is done for those the index waits for, each of which is #! below it, so one thread doing them all does them in order. #! @param count How many indexes there are. #! @param threads How many threads work, the one calling among them. #! @param waiting The indexes waiting for each index, grouped by it, sealed, #! each once in a group. #! @param each What is done for an index, which may be done on any of the #! threads, at the same time as for others it does not wait #! for. pub def share_out_ordered(count: u64, threads: u32, waiting: &groups, each: def(u64): void): void { val helpers := crew_for(count, threads); if helpers == 0 { for i in 0..count { each(i); } return; } # Each index is handed out once nothing it waits for is left undone, and the # thread doing the last closes the channel, which ends every thread taking # from it. var waits := vector::::new(); waits.resize(count); for i in 0..count { for &after in waiting.of(i) { waits[after] = waits[after] + 1; } } var left := vector::>::new(); var ready := thread::channel::::new(0); var done := atomic::::new(0); for i in 0..count { left.append(atomic::::new(waits[i])); if waits[i] == 0 { _ = ready.send(i); } } val work := def(): void { while ready.receive() is some(index) { each(index); for &after in waiting.of(index) { if left[after].fetch_sub(1) == 1 { _ = ready.send(after as u64); } } if done.fetch_add(1) + 1 == count { ready.close(); } } }; run_crew(helpers, work); } #! Runs work on a crew of threads started beside the one calling, which runs it #! too, each with scratch memory of its own, and waits for every one to end. #! @param helpers How many threads to start; where some cannot be started, #! those that were and the one calling do the work. #! @param work What each thread runs, which shares out what it does with #! the others. pub def run_crew(helpers: u32, work: def(): void): void { gather_crew(helpers, helpers); var started := vector::>::new(); for k in 0..helpers { var made := thread::spawn_with::(def(place: u32): void { join_crew(place); work(); }, k) else (_) { break; }; started.append(move(&mut made)); } work(); for &mut handle in started { handle.join(); } dismiss_crew(); } #! How many threads to start beside the one calling, for a count of indexes and #! the threads asked for. #! @param count How many indexes there are. #! @param threads How many threads are asked for, the one calling among them. #! @returns How many, 0 where the one calling does it all. pub def crew_for(count: u64, threads: u32): u32 { return 0 if threads <= 1 || count <= 1; return (count - 1) as u32 if count - 1 < (threads - 1) as u64; return threads - 1; } #! Takes the next index none has taken, and does what is done for it, until #! none is left. #! @param next The next index none has taken. #! @param count How many indexes there are. #! @param each What is done for each. def take_each(next: &mut atomic, count: u64, each: def(u64): void): void { while true { val index := next.fetch_add(1); return if index >= count; each(index); } }