Sorry in advance for the noobie questions but the documentation is a bit sparse on usage examples and this is my first ever attempt at multithreading a program. If you could humor me with this and maybe a couple follow up questions just to push me in the right direction that would be amazing.
For this program
data([[[a,10],[b,11],[c,12],[d,13],[e,14]],
[[f,15],[g,16],[h,16],[i,17],[j,18]],
[[k,19],[l,20],[m,21],[n,22],[o,23]],
[[p,24],[q,25],[r,26],[s,27],[t,28]],
[[u,29],[v,30],[w,31],[x,32],[y,33]]]).
task_([],[]) :- sleep(3).
task_([[_,Num]|Tuples],[Num|Nums]) :-
task_(Tuples,Nums).
task([],[]).
task([Row|Rows],[Result|Results]) :-
task_(Row,Result),
task(Rows,Results).
task_init(Result) :-
data(Data),
task(Data,Result).
I get the expected time profile
?- time(task_init(Result)).
% 42 inferences, 0.000 CPU in 15.016 seconds (0% CPU, 217617 Lips)
Result = [[10, 11, 12, 13, 14], [15, 16, 16, 17, 18], [19, 20, 21, 22, 23], [24, 25, 26, 27, 28], [29, 30, 31, 32, 33]].
because 5 rows x 3 seconds of sleep each = 15 seconds.
How would I multithread this so that I have a thread running in parallel for each row — that is, for each call recursive call of task_/2
— so we get about 3 seconds total sleep? Correct? Because we should get five parallel threads, each sleeping for 3 seconds simultaneously? Is that possible?
I thought maybe
task([],[]).
task([Row|Rows],[Result|Results]) :-
thread_create(task_(Row,Result),_),
task(Rows,Results).
but then I get
?- time(task_init(Result)).
% 16 inferences, 0.000 CPU in 0.000 seconds (30% CPU, 124031 Lips)
Result = ['$VAR'('_'), '$VAR'('_'), '$VAR'('_'), '$VAR'('_'), '$VAR'('_')].