Skip to content

IO.fork computations run their pure work on one core; the guide says "in parallel, on every core" #1375

Closed

Description

@nuxyel

What you did

bend io_fork_parallel.bend -o f && time ./f --threads 1 && time ./f --threads 16

What happened

$ time ./f --threads 1
real 1.681s  user 1.678s
$ time ./f --threads 16
real 1.684s  user 1.681s
 
Four IO.fork computations; each reads the clock inside the fork and then runs a
flat loop seeded with it, so the work can only happen inside the forked
computation (not when the action is built, and not in the joiner). They take the
same time with 1 and 16 threads, and user time equals real time:
 
                                                     --threads 1   --threads 16
io_fork_parallel.bend      4 IO.fork, then 4 join       1.68 s        1.68 s
io_fork_sequential.bend    the same 4 jobs in a row     1.68 s        1.68 s
io_fork_parallel_control   same loops, 2 parallel lets  1.69 s        0.85 s
 
Also on main at 0ad47fc.
 
The guide (IO and Concurrency) says: "A Bend program is a set of computations
interleaved by one event loop, as in Node.js: each runs its pure code (in
parallel, on every core) up to its next effect". We read it as forked
computations running their pure parts at the same time on different cores, and
loading GPT-2's 12 layers with one IO.fork each was slower than in sequence
(6.6-7.1 s against 5.1-5.6 s), the same with 1 and 16 threads.
 
If forks are meant to take turns on the event loop, a sentence in the guide
saying so would help; if they are meant to overlap, this shows they do not.
 
Files: https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/io_fork_parallel.bend
       https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/io_fork_sequential.bend
       https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/io_fork_parallel_control.bend
 
Not a duplicate of #831 (fixed in 2.0.13): that one was about parallel lets after a
closure call; here there are no parallel lets in the forked work, the forks are
IO.fork computations, and the parallel-let version (io_fork_parallel_control) scales.

The file

# Four IO.fork computations. Each reads the clock INSIDE the fork and then runs a flat loop seeded with it,
# so the work can only happen in the forked computation (not when the action is built, and not in the
# joiner). On Bend 2.0.35 and on main (0ad47fc) this takes the same time with --threads 1 and --threads 16,
# and the same time as io_fork_sequential.bend, which runs the four jobs one after another.
#
#   bend io_fork_parallel.bend -o f && time ./f --threads 1 && time ./f --threads 16
import Base
 
def work(n: Nat, acc: U32) -> U32:
  match n:
    case 0n:
      acc
    case 1n+p:
      work(p, (acc * 1103515245 + 12345 : U32))
 
# the forked computation needs the value for its own effect, so it cannot leave the work to the joiner
def job(+seed: U32) -> IO(U32):
  do IO<U32>:
    +now : Nat <- IO.now()
    +v : U32 = work(4000000000n, (seed + U32.from_nat(now) : U32))
    IO.print(U32.show(v))
    return v
 
def main() -> IO(Unit):
  do IO<Unit>:
    +t : Nat <- IO.now()
    a : Chan(U32) <- IO.fork(U32, job((U32.from_nat(t) + 1 : U32)))
    b : Chan(U32) <- IO.fork(U32, job((U32.from_nat(t) + 2 : U32)))
    c : Chan(U32) <- IO.fork(U32, job((U32.from_nat(t) + 3 : U32)))
    d : Chan(U32) <- IO.fork(U32, job((U32.from_nat(t) + 4 : U32)))
    x : U32 <- IO.join(U32, a)
    y : U32 <- IO.join(U32, b)
    z : U32 <- IO.join(U32, c)
    w : U32 <- IO.join(U32, d)
    IO.print(U32.show((x + y + z + w : U32)))

bend --version

bend 2.0.35

uname -sm

Linux x86_64

clang --version (the first line)

clang version 22.1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions