A worker index implementation for octave.
Matlab can identify the current worker id with get(getCurrentTask(), 'ID')
.
While Octave does not even natively support parallel computing, the parallel
package offers some parallel capabilities, but does not support labindex
.
So, I hacked my own solution (that currently only works on Linux).
tl;dr: give me the code
My oct_labindex
package consists of the following pieces:
- An
init
function that shuts down all workers and re-starts them. Additionally, the file/<tmp-dir>/<pid>/child_pids
is created. - When restarting a worker, the parallel package always runs the
~/.octaverc
file. Theinit_worker
function must be called from~/.octaverc
. It checks if the command line of its parent proces starts withoctave
. If so, it assumes it is a parallel worker started by the parallel package and adds its own pid to the/<tmp-dir>/<pid>/child_pids
file. - The
get_labindex
function reads the/<tmp-dir>/<parent-id>/child_pids
file and searches for its pid. The row index (starting from one) of the matching pid is returned as the labindex.
To use it, add the oct_labindex.init_worker()
call to your ~/.octaverc
startup file, load the parallel
package, call oct_labindex.init()
and run a parallel function:
% For proper usage, obviously add the functions to your path.
% Call `oct_labindex.init_worker()` from your `~/.octaverc` startup file
cd src
pkg('load', 'parallel');
n_workers = 2;
oct_labindex.init(n_workers);
pararrayfun(n_workers, @() disp(oct_labindex.get_labindex()), 1:10)
which returns the labindex for each evaluation:
1
2
1
2
1
2
1
2
1
2