% The devices the tests loop over, and the sizes a test runs on each.
%
% The suite is full unless the Prolog flag array_test is fast, its first value the
% environment variable ARRAY_TEST: a fast suite leaves out the devices of slow_device/1,
% the Intel CPU runtime, whose builds take half the time of a full one, wherever another
% device of the same type, CPU or GPU, stands for them.

:- module(test_devices, [test_device/2, test_size/5]).
:- use_module(library(array/opencl), [opencl_device/2, cl_device_info/3]).
:- use_module(library(lists)).

:- (  getenv('ARRAY_TEST', Mode) -> true ;  Mode = full ),
   must_be(oneof([full, fast]), Mode),
   create_prolog_flag(array_test, Mode, [type(atom), keep(true)]).

slow_device(intel).

%! test_device(?Name, ?Device) is nondet.
%
%  opencl_device/2 over the devices of this suite: all of them, or without the slow ones.
test_device(Name, Device) :-
   current_prolog_flag(array_test, Mode),
   opencl_device(Name, Device),
   kept(Mode, Name, Device).

kept(fast, Name, Device), slow_device(Name) =>
   cl_device_info(Device, type, Type),
   \+ ( opencl_device(Other, D), \+ slow_device(Other), cl_device_info(D, type, Type) ) .
kept(_, _, _) => true.

%! test_size(+Name, +Types, +All, +Few, -Size) is nondet.
%
%  The sizes a test of concrete sizes runs on the device Name: every one of All on the homes
%  of the test, the first device of each of Types, cpu or gpu or a list of them, where its
%  code path is the device's own, or the first device where there is none; the
%  representatives Few on the others.
test_size(Name, Types, All, Few, Size) :-
   types(Types, Ts),
   maplist(home, Ts, Homes),
   sizes(Name, Homes, All, Few, Sizes),
   member(Size, Sizes).

types(Types, Ts), is_list(Types) => Ts = Types.
types(Type, Ts) => Ts = [Type].

home(Type, Home), test_device(Name, Device), cl_device_info(Device, type, Type) => Home = Name.
home(_, Home) => once(test_device(Home, _)).

sizes(Name, Homes, All, _, Sizes), memberchk(Name, Homes) => Sizes = All.
sizes(_, _, _, Few, Sizes) => Sizes = Few.
