Repository navigation
IO.thread_count answers the worker pool's size, and 1 on the JS lanes - #1048
Conversation
A program could not learn how many workers run its parallel work: the native runtime consumes --threads before IO.args sees the command line, and the default (the CPUs the process may use, under its affinity mask and cgroup quota) could only be recomputed by copying cpu_count into a foreign effect (bendlang#971). IO.thread_count() -> IO(U32) is a Base effect with no arguments, in the shape of IO.args and IO.now. Its C side answers pool_size, which corpus_setup sets once, before io_loop runs, to --threads or cpu_count() clamped to 1..CUBE_T (128); the effect is spliced after the runtime, so it reads the global as args.c reads io_argv, and comp.ts does not change. Its JS side answers 1: a JS program runs one thread and ignores --threads. tests/io/thread_count.bend prints whether the count is in 1..128, since the gate passes no flag and the CPU count differs by machine. On the branch it prints True on all four lanes (check, interpreted, JS, C); on main its check fails with "expected : a defined name, observed : IO.thread_count". By hand, on Linux x86-64 with 4 CPUs: the binary prints 4 with no flag, 1, 3 and 128 with --threads 1, 3 and 128, and 128 with --threads 129 and 10000; --threads 0 is refused by the CLI as before; under taskset -c 0,1 it prints 2. A binary with a `!` prints the same after its parallel work. bun x.js and the interpreted run print 1. Regression: the tests/io checkup differs from main only by the new module, and the tests/base, tests/check and tests/import checkups (307 modules) are identical to main's; args, now, random_u32, get_env, hello_print, sleep_order, spawn_sleep, fork_join and confluence_threads pass all four lanes; gates/repo.ts 46/46. base.bend 25375 -> 25403 ttok. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thank you, this is merged. I ran it on macOS (10 CPUs), which you could not: Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself. |
What changes
IO.thread_count() -> IO(U32)is a new Base effect, as #971 proposes:bend2/base.bend: the def, next toIO.now, two imports like everyother Base effect.
bend2/effs/thread_count.c: answerspool_size.corpus_setupsets itonce, before
io_loopruns, to the--threadsvalue orcpu_count(),clamped to
1..CUBE_T(128). Effect sources are spliced after theruntime, so the effect reads the global directly, as
args.creadsio_argv.comp.tsdoes not change.bend2/effs/thread_count.js: answers1. A JS program runs one threadand ignores
--threads(RUNTIME_MAIN'scli).tests/io/thread_count.bend: the new test.The guide is unchanged.
IO.random_u32(#837),TCP.poll(#858) andFile.read_at/File.size(#823) were added without a guide line, and Ifollowed them.
Why
A program cannot find out how many workers run its parallel work. The
runtime consumes
--threadsbeforeIO.argssees the command line. Thedefault is the number of CPUs the process may use: the affinity mask,
under the cgroup quota. Without this effect, a program has to copy
cpu_countinto a foreign effect to get it (#971).Test
tests/io/thread_count.bendprints whether the count is in1..128. Thegate runs each binary with no flag, and the CPU count differs by machine,
so the test does not print the count itself.
On this branch it prints
Trueon all four lanes (check, interpreted,JS, C). On main (95317d9) the check fails:
By hand, with a probe that prints the count
(
n : U32 <- IO.thread_count()thenIO.print(U32.show(n))), built withbun bend2/main.ts count.bend -o count.js -o counton a machine with 4CPUs:
A binary with a
!and a parallel sum before the call (BANGS 1, builtwithout a GPU on this machine) prints 4, 2 and 3 with no flag,
--threads 2and--threads 3 --gpu off.Regression
gates/test.tsruns it): the sameoutput as main except for the new module.
same output as main.
args,now,random_u32,get_env,hello_print,sleep_order,spawn_sleep,fork_joinandconfluence_threadspass on all fourlanes.
gates/repo.ts: 46/46.base.bendgoes from 25375 to 25403 ttok. Thetwo effect files are 102 and 38 ttok.
Not tested here
clang 22. On macOS,
cpu_countissysconf(_SC_NPROCESSORS_ONLN), andthe effect answers whatever
corpus_setupstored.--gpu on,pool_sizeis set the same way. The effectanswers the CPU pool's size, not a GPU lane count. I could not run a
device build here.
gates/test.ts,gates/perf.ts) were not run.Closes #971.