I’m using: SWI-Prolog version 8.2.4
on a 32-core CPU running linux OS, fedora 34
I am running a large number of threads, 27 at the time, with the intention to make use of most of the cores.
But what I’m getting is: when the program is running, htop reports only one or two cores being used at any given time. Most of the threads are shown as sleeping. The program eventually finishes, and produces the right results. However, it looks like most of the cores go unused.
My code looks like this:
control:-
file_to_list(closed_repr_f4_short_test,NodeList),
length(NodeList,Len),
iota(Len,Iota), clean(middle_zero),
maplist(node_to_middle_zero,Iota,NodeList),
current_prolog_flag(cpu_count,CPUCores),
Running is min(CPUCores,Len) - 5, Running > 0, !,
thread_create(init(Running,Running),_Control,[alias(control)]).
node_to_middle_zero(ID,Node):-
middle(Node,Middle),
recordz(middle_zero,[ID,Middle]).
init(Remain,0):-
loop(Remain).
init(Remain,ToCreate):-
ToCreate > 0,
recorded(middle_zero,[ID,Middle],Ref),
erase(Ref),
single_launch(ID,Middle),
NewToCreate is ToCreate - 1,
init(Remain,NewToCreate).
loop(_Remain):-
recorded(middle_zero,[ID,Middle],Ref),
erase(Ref),
get_message_and_record,
single_launch(ID,Middle),
fail.
loop(Remain):-
finish(Remain).
finish(0):-
thread_exit('control thread is done.').
finish(Remain):-
Remain > 0,
get_message_and_record,
NewRemain is Remain-1,
finish(NewRemain).
get_message_and_record:- !,
thread_get_message([OldMiddle,OldMiddleCount]),
recordz(middle_count,[OldMiddle,OldMiddleCount]).
single_launch(ID,Middle):- !,
current_prolog_flag(cpu_count,CPUCores),
Core is ID mod CPUCores,
atomic_list_concat([m,t,ID],'_',TID),
thread_create(mch_thread(Middle,ID),_MCH_Thread,[alias(TID),affinity([Core])]).
mch_thread(Middle,ID):-
thread_self(Self),
middle_count_hybrid(Middle,MiddleCount),
thread_send_message(control,[ID,[Middle,MiddleCount]]),
thread_detach(Self),
thread_exit([Self,Middle,MiddleCount]).