iyi

Standard library, 0.22.0

std/parallel

std/parallel: data parallelism as a library - SPEC.md III.4.14.

import std/parallel to use it. Source: src/std/parallel.iyi at v0.22.0.

  import std/parallel
  squares = (0...1000).map_parallel { |i| i * i }      # order kept
  total = words.sum_parallel { |w| w.size }            # an Array, copied once
  failed = jobs.each_parallel { |job| job.run }        # nil, or the first error

each_parallel, map_parallel, sum_parallel and reduce_parallel on a List(T) (std/list), an Array(T) and an integer Range: the elements are cut into chunks, and as many parallel tasks as the pool has workers (g.spawn(unit: :new), III.4.13) take the chunks one at a time from a shared count until none is left, so a worker that finishes early takes more. The caller waits in the tasks' group, so a caller on a worker - inside a parallel task, or inside another map_parallel - gives its worker to the chunks while it waits.

What the compiler asks, at the call: the block may capture only Share values, as spawn(unit: :new)'s block may, and what the method hands the block - the elements - must be Share too, since chunks on several workers read them at once (@[Share(elements: ...)]). The sequence the chunks read must not change under them either: a List and a range cannot, and an Array is copied into a List first, the price List already names for an array nobody else may write while tasks read it.

The answers:

  • map_parallel answers what map answers: one value per element, in the elements' order, an error member included in its place.
  • each_parallel answers nil, or the error member the block answered for the lowest index that answered one, as a loop that stops at its first error would: no element after it is started, every element before it is run.
  • sum_parallel adds each chunk's sum in chunk order; reduce_parallel folds each chunk from identity with the block and folds the chunks' answers in order, so the block must be associative and identity its identity. A Float64 sum is the same for the same chunks, which the worker count decides, and may differ from sum's in its last bits.
  • A panic in one chunk stops the chunks not yet started, and the caller panics with it at the join, "a task panicked: ...", as a group's unread panic does.
  • A cancelled caller - its group failed or was cancelled - stops the chunks not yet started, and is stopped itself at the end of the call, as a cancelled task joining another is: no caller ever sees part of an answer.

Run on the caller, chunk after chunk, with no task started: when the pool would have one worker (IYI_WORKERS=1), when there is one chunk, and in a build under -Dgc_none or -Dgc_boehm, which has no threads. The answers are the same.

Paralleltrait

The four verbs, on whatever can say how many elements it has and give the sequence the chunks read (ParallelRun.source).

std/paralleliyi doc src/std/parallel.iyi
pub trait Parallel
  type Elem
  # The block on every element, in chunks the pool's tasks take one at a
  # time; nil, or the error the block answered for the lowest index that
  # answered one. A chunk is *grain* elements; 0 cuts `PIECES` chunks a
  # worker. The same for the three below.
  def each_parallel(grain : Int32 = 0, &block : (Elem -> R)) : R | ::Nil forall R
  # `map`'s answer, computed in chunks on the pool, in the elements' order.
  def map_parallel(grain : Int32 = 0, &block : (Elem -> U)) : Array(U) forall U
  # The elements folded with the block from *identity*, a chunk at a time.
  def reduce_parallel(identity : Elem, grain : Int32 = 0, &block : (Elem, Elem -> Elem)) : Elem
  # The sum of what the block answers for each element, and its type's
  # zero when there are none.
  def sum_parallel(grain : Int32 = 0, &block : (Elem -> U)) : U forall U
end

ParallelRunclass

:nodoc: - the engine the verbs share.

std/paralleliyi doc src/std/parallel.iyi
pub class ParallelRun
  # The chunk *at*'s bounds.
  def self.bounds(at : Int64, chunk : Int64, size : Int64) : ::Tuple(Int64, Int64)
  def self.each(source : S, grain : Int32, work : SharedProc(T, R)) : R | ::Nil forall S, T, R
  def self.each_here(source : S, size : Int64, chunk : Int64, work : SharedProc(T, R)) : R | ::Nil forall S, T, R
  # Each task answers the lowest index it saw fail, with its error, or
  # *size* and nothing. `failed_at` is the lowest any task has seen: no
  # chunk starting past it is begun, no element past it is run, and
  # every element before it still is.
  def self.each_tasks(source : S, size : Int64, chunk : Int64, chunks : Int64, tasks : Int32, work : SharedProc(T, R)) : Array(IyiTask(::Tuple(Int64, Array(R)))) forall S, T, R
  def self.fetch(list : List(T), index : Int64) : T forall T
  def self.fetch(range : Range(Int32, Int32), index : Int64) : Int32
  def self.fetch(range : Range(Int64, Int64), index : Int64) : Int64
  def self.map(source : S, grain : Int32, work : SharedProc(T, U)) : Array(U) forall S, T, U
  def self.map_here(source : S, size : Int64, chunk : Int64, work : SharedProc(T, U)) : Array(U) forall S, T, U
  # The tasks, each taking chunks until none is left, and each answering
  # the chunks it ran by number; joined before this answers.
  def self.map_tasks(source : S, size : Int64, chunk : Int64, chunks : Int64, tasks : Int32, work : SharedProc(T, U)) : Array(IyiTask(Array(::Tuple(Int64, Array(U))))) forall S, T, U
  # How the elements are cut: {elements a chunk, chunks, tasks to take
  # them}, and 0 tasks when the caller runs them itself - one worker, one
  # chunk, or a build with no threads.
  def self.plan(size : Int64, grain : Int32) : ::Tuple(Int64, Int64, Int32)
  def self.reduce(source : S, identity : T, grain : Int32, work : SharedProc(::Tuple(T, T), T)) : T forall S, T
  def self.reduce_here(source : S, identity : T, size : Int64, chunk : Int64, work : SharedProc(::Tuple(T, T), T)) : T forall S, T
  def self.reduce_tasks(source : S, identity : T, size : Int64, chunk : Int64, chunks : Int64, tasks : Int32, work : SharedProc(::Tuple(T, T), T)) : Array(IyiTask(Array(::Tuple(Int64, T)))) forall S, T
  def self.size(list : List(T)) : Int64 forall T
  def self.size(range : Range(B, E)) : Int64 forall B, E
  # The sequence the chunks read, which nothing may write while they do.
  def self.source(list : List(T)) : List(T) forall T
  def self.source(array : Array(T)) : List(T) forall T
  def self.source(range : Range(Int32, Int32)) : Range(Int32, Int32)
  def self.source(range : Range(Int64, Int64)) : Range(Int64, Int64)
  # The caller, cancelled while the chunks ran, does not see what they
  # left: it stops here, as a cancelled task joining another does.
  def self.stop_if_cancelled : Nil
  def self.sum(source : S, grain : Int32, work : SharedProc(T, U)) : U forall S, T, U
  def self.sum_here(source : S, size : Int64, chunk : Int64, work : SharedProc(T, U)) : U forall S, T, U
  def self.sum_tasks(source : S, size : Int64, chunk : Int64, chunks : Int64, tasks : Int32, work : SharedProc(T, U)) : Array(IyiTask(Array(::Tuple(Int64, U)))) forall S, T, U
end

Parallel for Std::List::List(T) forall Timpl

std/paralleliyi doc src/std/parallel.iyi
impl Parallel for Std::List::List(T) forall T
end

Parallel for Array(T) forall Timpl

std/paralleliyi doc src/std/parallel.iyi
impl Parallel for Array(T) forall T
end

Parallel for Range(B, E) forall B, Eimpl

std/paralleliyi doc src/std/parallel.iyi
impl Parallel for Range(B, E) forall B, E
end