A flexible parallel/sequential loop implementation with progress tracking, proper argument passing, and comprehensive error handling.

parfor(
  what,
  args,
  cl = NULL,
  combine = c,
  errorhandling = c("stop", "remove", "pass"),
  verbose = FALSE,
  show_progress = TRUE,
  export = NULL,
  packages = NULL,
  cl_type = "PSOCK",
  use_do_call = FALSE,
  ...
)

Arguments

what

A function to apply to each element.

args

A vector or list of arguments to iterate over. For functions with multiple arguments, provide a list where each element contains the arguments for one call.

cl

Number of cores to use. If `NULL` (default), runs sequentially. If -1, uses all available cores. Positive integers specify exact core count.

combine

A function to combine results (default: `c`). Must be a function.

errorhandling

How to handle errors ("stop", "remove", or "pass").

verbose

Logical; if `TRUE`, prints debugging information.

show_progress

Logical; if `TRUE`, shows progress bar.

export

Character vector of object names to export to workers (parallel only).

packages

Character vector of package names to load on workers (parallel only).

cl_type

Cluster type ("PSOCK", "FORK", etc). Default is "PSOCK" for cross-platform compatibility.

use_do_call

Logical; if `TRUE`, treats each element of args as a list of arguments to pass via do.call. If `FALSE`, passes each element as a single argument.

...

Additional arguments to pass to `what`.

Value

Results combined according to `combine` function.

Examples

# Basic usage - single arguments
parfor(function(x) x^2, 1:10)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%
#>  [1]   1   4   9  16  25  36  49  64  81 100

# Parallel with 2 cores
parfor(function(x) x^2, 1:10, cl = 2)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%
#>  [1]   1   4   9  16  25  36  49  64  81 100

# Multiple arguments using do.call style
args_list <- list(list(x=1, y=2), list(x=3, y=4), list(x=5, y=6))
parfor(function(x, y) x + y, args_list, use_do_call = TRUE)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |======================================================================| 100%
#> [1]  3  7 11

# With additional fixed arguments
parfor(function(x, power) x^power, 1:5, power = 3)
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |======================================================================| 100%
#> [1]   1   8  27  64 125

# With error handling
parfor(function(x) if(x==3) stop("error") else x^2, 1:5, 
      errorhandling = "remove")
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |======================================================================| 100%
#> [1]  1  4 16 25