Last active
November 15, 2022 07:41
-
-
Save AshyIsMe/a35611566bf7793a9585cc50dce01e09 to your computer and use it in GitHub Desktop.
Parallel Each in j
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NB. parallel each: peach | |
NB. https://code.jsoftware.com/wiki/NYCJUG/2022-11-08#Work_with_Multi-threading | |
echo 'Must be at least running J904' | |
echo JLIB | |
nproc =: {{ {. 8 T. '' }} | |
createThreads=: 3 : '{{ 0 T. '''' }} ^:({. 8 T. '''')]''''' | |
peach =: 1 : 'u t. '''' y' | |
NB. Or even simpler: | |
peach =: t. '' | |
0 : 0 | |
Usage: | |
First create some background worker threads, 1 per core: | |
createThreads '' | |
Run 5 shell commands in parallel. | |
NB. a is a list of strings to execute. | |
a=. '; date +%s',"1~ 'sleep ' ,"1 ":"0 i.5 | |
a | |
sleep 0; date +%s | |
sleep 1; date +%s | |
sleep 2; date +%s | |
sleep 3; date +%s | |
sleep 4; date +%s | |
NB. Execute (2!:0) on each string of a in parallel. | |
r =. 2!:0 peach"1 a | |
$r | |
5 | |
r | |
┌───────────┬───────────┬───────────┬───────────┬───────────┐ | |
│1667991735 │1667991740 │1667991742 │1667991745 │1667991739 │ | |
└───────────┴───────────┴───────────┴───────────┴───────────┘ | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment