Skip to contents

This brick encapsulates the activity of n workers running parallel sub-trajectories.

Usage

do_parallel(.trj, ..., .env, wait = TRUE, mon_all = FALSE)

Arguments

.trj

the trajectory object.

...

sub-trajectories or list of sub-trajectories to parallelise.

.env

the simulation environment.

wait

if TRUE, the arrival waits until all parallel sub-trajectories are finished; if FALSE, the arrival continues as soon as the first parallel task ends.

mon_all

if TRUE, get_mon_arrivals will show one line per clone.

Value

Returns the following chain of activities: clone

> synchronize (> wait

> untrap if wait=FALSE) (see examples below).

Examples

env <- simmer()
signal <- function() get_name(env)

task.1 <- trajectory("task 1") %>%
  timeout(function() rexp(1))
task.2 <- trajectory("task 2") %>%
  timeout(function() rexp(1))

## These are equivalent:
trajectory() %>%
  do_parallel(
    task.1,
    task.2,
    .env = env, wait = TRUE
  )
#> trajectory: anonymous, 10 activities
#> { Activity: Clone        | n: 3 }
#>   Fork 1, continue,  trajectory: original, 4 activities
#>   { Activity: Trap         | signals: function() }
#>   { Activity: Wait         |  }
#>   { Activity: Wait         |  }
#>   { Activity: UnTrap       | signals: function() }
#>   Fork 2, continue,  trajectory: task 1, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#>   Fork 3, continue,  trajectory: task 2, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#> { Activity: Synchronize  | wait: 1 }

trajectory() %>%
  clone(
    n = 3,
    trajectory("original") %>%
      trap(signal) %>%
      wait() %>%
      wait() %>%
      untrap(signal),
    task.1[] %>%
      send(signal),
    task.2[] %>%
      send(signal)) %>%
  synchronize(wait = TRUE)
#> trajectory: anonymous, 10 activities
#> { Activity: Clone        | n: 3 }
#>   Fork 1, continue,  trajectory: original, 4 activities
#>   { Activity: Trap         | signals: function() }
#>   { Activity: Wait         |  }
#>   { Activity: Wait         |  }
#>   { Activity: UnTrap       | signals: function() }
#>   Fork 2, continue,  trajectory: task 1, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#>   Fork 3, continue,  trajectory: task 2, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#> { Activity: Synchronize  | wait: 1 }

## These are equivalent:
trajectory() %>%
  do_parallel(
    task.1,
    task.2,
    .env = env, wait = FALSE
  )
#> trajectory: anonymous, 9 activities
#> { Activity: Clone        | n: 3 }
#>   Fork 1, continue,  trajectory: original, 1 activities
#>   { Activity: Trap         | signals: function() }
#>   Fork 2, continue,  trajectory: task 1, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#>   Fork 3, continue,  trajectory: task 2, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#> { Activity: Synchronize  | wait: 0 }
#> { Activity: Wait         |  }
#> { Activity: UnTrap       | signals: function() }

trajectory() %>%
  clone(
    n = 3,
    trajectory("original") %>%
      trap(signal),
    task.1[] %>%
      send(signal),
    task.2[] %>%
      send(signal)) %>%
  synchronize(wait = FALSE) %>%
  wait() %>%
  untrap(signal)
#> trajectory: anonymous, 9 activities
#> { Activity: Clone        | n: 3 }
#>   Fork 1, continue,  trajectory: original, 1 activities
#>   { Activity: Trap         | signals: function() }
#>   Fork 2, continue,  trajectory: task 1, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#>   Fork 3, continue,  trajectory: task 2, 2 activities
#>   { Activity: Timeout      | delay: function() }
#>   { Activity: Send         | signals: function(), delay: 0 }
#> { Activity: Synchronize  | wait: 0 }
#> { Activity: Wait         |  }
#> { Activity: UnTrap       | signals: function() }