pmap
.. pmap(f, lsts...; err_retry=true, err_stop=false, pids=workers())
Transform collections ``lsts`` by applying ``f`` to each element in parallel.
(Note that ``f`` must be made available to all worker processes; see :ref:`Code Availability and Loading Packages <man-parallel-computing-code-availability>` for details.)
If ``nprocs() > 1``, the calling process will be dedicated to assigning tasks.
All other available processes will be used as parallel workers, or on the processes specified by ``pids``.
If ``err_retry`` is ``true``, it retries a failed application of ``f`` on a different worker.
If ``err_stop`` is ``true``, it takes precedence over the value of ``err_retry`` and ``pmap`` stops execution on the first error.
Examples
The pmap
function in Julia allows you to apply a given function f
to each element of one or more collections lsts
in parallel. Here are some examples and explanations of its usage:
-
Parallel map on a single collection:
julia> pmap(x -> x^2, [1, 2, 3, 4, 5]) 5-element Array{Int64,1}: 1 4 9 16 25
In this example, the function
x -> x^2
is applied to each element of the collection[1, 2, 3, 4, 5]
in parallel, resulting in a new collection with the squared values. -
Parallel map on multiple collections:
julia> pmap(+, [1, 2, 3], [4, 5, 6]) 3-element Array{Int64,1}: 5 7 9
Here, the
+
function is applied to the corresponding elements of the two collections[1, 2, 3]
and[4, 5, 6]
in parallel. The result is a new collection with the element-wise sum. - Using custom functions:
julia> function myfunc(x) return x^2 + 2x + 1 end julia> pmap(myfunc, [1, 2, 3, 4, 5]) 5-element Array{Int64,1}: 4 9 16 25 36
In this example, a custom function
myfunc
is defined and applied to each element of the collection[1, 2, 3, 4, 5]
in parallel.
Additional options:
err_retry=true
: If a worker process fails while applying the functionf
, the failed application is retried on a different worker.err_stop=false
: By default,pmap
continues execution on all elements even if an error occurs. Settingerr_stop=true
stops execution on the first error and returns the results obtained so far.
Please note that the function f
must be available to all worker processes. Refer to Julia's documentation on "Code Availability and Loading Packages" for more details.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.