annotate cpack/dml/examples/system_metrics.swinb @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 <div class="notebook">
Daniel@0 2
Daniel@0 3 <div class="nb-cell markdown">
Daniel@0 4 ## Analysis of collection level computation performance
Daniel@0 5
Daniel@0 6 This following block of code defines some predicates for browsing the database of collection level computations and tagging them by what kind of computation it was, what the implementation language was, the date and time of the computation, the size of the collection and the duration of the computation.
Daniel@0 7
Daniel@0 8 Scroll down the query boxes below and press the green arrow to run each query.
Daniel@0 9 </div>
Daniel@0 10
Daniel@0 11 <div class="nb-cell program">
Daniel@0 12 :- use_module(library(computations)).
Daniel@0 13 :- use_module(library(mlserver)).
Daniel@0 14 :- use_module(library(real)).
Daniel@0 15 :- use_module(library(dml_c3)).
Daniel@0 16
Daniel@0 17 :- use_rendering(rdf,[resource_format(nslabel)]).
Daniel@0 18 :- use_rendering(matlab,[format(svg),size(15,10)]).
Daniel@0 19 :- use_rendering(c3).
Daniel@0 20
Daniel@0 21 % Enumerates collection level analysis events, providing
Daniel@0 22 % Func=Label/Lang, collection size, and computation duration.
Daniel@0 23 cla_op(Lab,Size,Dur) :- cla_op(label&gt;&gt;relabel-size-dur,Lab-Size-Dur).
Daniel@0 24
Daniel@0 25 % main CLA browser predicate. Uses Filter to determine which computations
Daniel@0 26 % are returned and what information is provided.
Daniel@0 27 cla_op(Filter,Out) :-
Daniel@0 28 browse(perspectives:cla_memo(Op,CID,_),comp(_,Time,Dur)-ok),
Daniel@0 29 dataset_size(CID,Size),
Daniel@0 30 x(Filter,op(Op,Time,Size,Dur),Out).
Daniel@0 31
Daniel@0 32 % field extractor predicate (a v. small arrow interpreter!)
Daniel@0 33 x(id,X,X).
Daniel@0 34 x(F&gt;&gt;G,X,Y) :- x(F,X,Z), x(G,Z,Y).
Daniel@0 35 x(fst(F),X1-Y,X2-Y) :- x(F,X1,X2).
Daniel@0 36 x(snd(F),X-Y1,X-Y2) :- x(F,Y1,Y2).
Daniel@0 37 x(F1-F2,X,Y1-Y2) :- x(F1,X,Y1), x(F2,X,Y2).
Daniel@0 38 x(H,X,Y) :- defx(H,F), x(F,X,Y).
Daniel@0 39 x(add(N),X,Y) :- Y is X+N.
Daniel@0 40 x(arg(N),X,Y) :- arg(N,X,Y).
Daniel@0 41 x(log,X,Y) :- Y is log10(X).
Daniel@0 42 x(jitter(L,U),X,Y) :- random(D), Y is L+(U-L)*D+X.
Daniel@0 43 x(divide,X-Y,Z) :- Z is X/Y.
Daniel@0 44 x(diff,X-Y,Z) :- Z is Y-X.
Daniel@0 45 x(quant(Q),X,Y) :- Y is Q*floor(X/Q).
Daniel@0 46 x(event,T1-_,T1-start).
Daniel@0 47 x(event,T1-DT,T2-stop) :- T2 is T1+DT.
Daniel@0 48
Daniel@0 49 % gets month number and name from timestamp
Daniel@0 50 x(month_num_name,Time,Num-Name) :-
Daniel@0 51 format_time(atom(Name),'%B',Time),
Daniel@0 52 stamp_date_time(Time,Date,local),
Daniel@0 53 date_time_value(month,Date,Num).
Daniel@0 54
Daniel@0 55 % Transforms collection level operation spec to Label/Lang,
Daniel@0 56 % where Lang is in { ml, pl, r, py }.
Daniel@0 57 x(relabel,Op,Label/Lang) :- cla_label_lang(Op,Label,Lang).
Daniel@0 58 x(lang,_/Lang,Lang).
Daniel@0 59
Daniel@0 60 defx(op, arg(1)).
Daniel@0 61 defx(time, arg(2)).
Daniel@0 62 defx(size, arg(3)).
Daniel@0 63 defx(dur, arg(4)).
Daniel@0 64 defx(month, time&gt;&gt;month_num_name).
Daniel@0 65
Daniel@0 66 normalise_hist(Name-Pairs,Name-Pairs2) :-
Daniel@0 67 unzip(Pairs,Vals,Counts),
Daniel@0 68 stoch(Counts,Probs),
Daniel@0 69 unzip(Pairs2,Vals,Probs).
Daniel@0 70
Daniel@0 71 stoch(Xs,Ys) :- sumlist(Xs,Total), maplist(divby(Total),Xs,Ys).
Daniel@0 72 divby(Z,X,Y) :- Y is X/Z.
Daniel@0 73
Daniel@0 74 concurrency([T0-start|StartStopEvents],ConcurrencyEvents) :-
Daniel@0 75 foldl(conc,StartStopEvents,ConcurrencyEvents,T0-1,_).
Daniel@0 76
Daniel@0 77 conc(T2-Event,(T1-T2)-N1,T1-N1,T2-N2) :-
Daniel@0 78 ( Event=start -&gt; succ(N1,N2)
Daniel@0 79 ; Event=stop -&gt; succ(N2,N1)
Daniel@0 80 ).
Daniel@0 81
Daniel@0 82 % ------- tools for building C3 charts -----------
Daniel@0 83
Daniel@0 84 %% add_points(+Data:pair(term,list(pair(number,number))))// is det.
Daniel@0 85 % Adds a set of points to a scatter plot
Daniel@0 86 add_points(Name-Pairs) --&gt;
Daniel@0 87 {unzip(Pairs,Xs,Ys)},
Daniel@0 88 add_points(Name,Xs,Ys).
Daniel@0 89
Daniel@0 90 %% add_points(+Name:term,+X:list(number),+Y:list(number),+C1:c3,-C2:c3) is det.
Daniel@0 91 % adds a named set of points to a C3 scatter plot.
Daniel@0 92 add_points(Name1,Xs,Ys,Ch1,Ch2) :-
Daniel@0 93 term_to_atom(Name1,Name),
Daniel@0 94 atom_concat(Name,'_x',NameX),
Daniel@0 95 Ch2=Ch1.put(data/columns,[[NameX|Xs],[Name|Ys]|Ch1.data.columns])
Daniel@0 96 .put(data/xs/Name,NameX).
Daniel@0 97 </div>
Daniel@0 98
Daniel@0 99 <div class="nb-cell markdown">
Daniel@0 100 This query shows the relationship between collection size and computation time for all collection level analyses, grouped by month of computation. Note that the axis scales
Daniel@0 101 are _logarithmic_.
Daniel@0 102 </div>
Daniel@0 103
Daniel@0 104 <div class="nb-cell query">
Daniel@0 105 setof(Size-Dur,
Daniel@0 106 cla_op(month-size&gt;&gt;log&gt;&gt;jitter(0,0.05)-dur&gt;&gt;log,(_-M)-Size-Dur),
Daniel@0 107 _Points),
Daniel@0 108 call_dcg(( c3:scat('log size','log dur'),
Daniel@0 109 add_points(all-_Points),
Daniel@0 110 c3:legend(false),
Daniel@0 111 c3:zoom(true)
Daniel@0 112 ),
Daniel@0 113 c3{},Ch0).
Daniel@0 114 </div>
Daniel@0 115
Daniel@0 116 <div class="nb-cell markdown">
Daniel@0 117 Histogram of logarithm of computation time per item, quantised to 0.1 bins (a ratio of about 1.26).
Daniel@0 118 </div>
Daniel@0 119
Daniel@0 120 <div class="nb-cell query">
Daniel@0 121 Q=0.1, % size of quantisation bin (in log domain)
Daniel@0 122 histof(T, cla_op((dur-size)&gt;&gt;divide&gt;&gt;log&gt;&gt;quant(Q),T), _Hist),
Daniel@0 123 call_dcg(( c3:bar('log dur','count'),
Daniel@0 124 add_points(all-_Hist),
Daniel@0 125 c3:put(bar/width/ratio,Q)
Daniel@0 126 ), c3{},Chart).
Daniel@0 127 </div>
Daniel@0 128
Daniel@0 129 <div class="nb-cell markdown">
Daniel@0 130 Histogram of logarithm of computation time per item, quantised to bins of width 0.25, grouped computation label and language.
Daniel@0 131 </div>
Daniel@0 132
Daniel@0 133 <div class="nb-cell query">
Daniel@0 134 findall(L-Hist,
Daniel@0 135 histof(T,
Daniel@0 136 cla_op(op&gt;&gt;relabel-(dur-size)&gt;&gt;divide&gt;&gt;log&gt;&gt;quant(0.25),L-T),
Daniel@0 137 Hist),
Daniel@0 138 _Hists),
Daniel@0 139 maplist(normalise_hist,_Hists,_Dists),
Daniel@0 140 call_dcg(( c3:bar('log dur','count'),
Daniel@0 141 foldl(add_points,_Dists),
Daniel@0 142 c3:put(bar/width/ratio,0.5)
Daniel@0 143 ), c3{}, Ch0).
Daniel@0 144 </div>
Daniel@0 145
Daniel@0 146 <div class="nb-cell markdown">
Daniel@0 147 Histogram of logarithm of computation time per item, quantised to bins of width 0.25, grouped language.
Daniel@0 148 </div>
Daniel@0 149
Daniel@0 150 <div class="nb-cell query">
Daniel@0 151 findall(L-Hist,
Daniel@0 152 histof(T,
Daniel@0 153 cla_op(op&gt;&gt;relabel&gt;&gt;lang - (dur-size)&gt;&gt;divide&gt;&gt;log&gt;&gt;quant(0.25),L-T),
Daniel@0 154 Hist),
Daniel@0 155 Hists),
Daniel@0 156 maplist(normalise_hist,Hists,_Dists),
Daniel@0 157 call_dcg(( c3:bar('log dur','fraction'),
Daniel@0 158 foldl(add_points,_Dists),
Daniel@0 159 c3:put(bar/width/ratio,0.45)
Daniel@0 160 ), c3{}, Ch0).
Daniel@0 161 </div>
Daniel@0 162
Daniel@0 163 <div class="nb-cell markdown">
Daniel@0 164 This next query shows how collection size vs computation duration varies with the kind of analysis being done and the implementation language. Note that computations in Prolog have the lowest overheads, and that computations in Matlab seem to have the most variable range of durations for a given collection size.
Daniel@0 165 </div>
Daniel@0 166
Daniel@0 167 <div class="nb-cell query">
Daniel@0 168 findall(Op-_Ps,
Daniel@0 169 setof(Size-Dur,
Daniel@0 170 cla_op(op&gt;&gt;relabel-(size&gt;&gt;log)-dur&gt;&gt;log,Op-Size-Dur),
Daniel@0 171 _Ps),
Daniel@0 172 _Rs),
Daniel@0 173 foldl(add_points,_Rs,c3{}.scat('log size','log dur'),Ch1).
Daniel@0 174 </div>
Daniel@0 175
Daniel@0 176 <div class="nb-cell markdown">
Daniel@0 177 This query is like the previous one, but grouped by language only.
Daniel@0 178 </div>
Daniel@0 179
Daniel@0 180 <div class="nb-cell query">
Daniel@0 181 findall(Op-_Ps,
Daniel@0 182 setof(Size-Dur,
Daniel@0 183 cla_op(op&gt;&gt;relabel&gt;&gt;lang-(size&gt;&gt;log)-dur&gt;&gt;log,Op-Size-Dur),
Daniel@0 184 _Ps),
Daniel@0 185 _Rs),
Daniel@0 186 foldl(add_points,_Rs,c3{}.scat('log size','log dur'),Ch1).
Daniel@0 187 </div>
Daniel@0 188
Daniel@0 189 <div class="nb-cell markdown">
Daniel@0 190 This query breaks down the performance of analysis method by month. There does not seem to be any significant pattern in this other than the overall volume of computation done in each month.
Daniel@0 191 </div>
Daniel@0 192
Daniel@0 193 <div class="nb-cell query">
Daniel@0 194 setof(Month-_Ps,
Daniel@0 195 setof(Size-Dur,
Daniel@0 196 cla_op( month - op&gt;&gt;relabel - size&gt;&gt;log - dur&gt;&gt;log,
Daniel@0 197 Month - Label - Size - Dur),
Daniel@0 198 _Ps),
Daniel@0 199 _Rs),
Daniel@0 200 call(foldl(add_points,_Rs), c3{}.scat(log_size,log_dur), Ch).
Daniel@0 201 </div>
Daniel@0 202
Daniel@0 203 <div class="nb-cell markdown">
Daniel@0 204 This query analyses the degree of concurrency of collection level computations. It works by getting a complete set of point events describing the beginning and ending times of computations. Then the predicate concurrency/2 (defined in the initial code block) folds over these events and produces a list of time interval events of the form (StartTime-EndTime)-Concurrency, where Concurrency is the number of concurrent computations occuring over that interval. Then, the time intervals are mapped to durations and a histogram of concurrency weighted by duration is produced.
Daniel@0 205 </div>
Daniel@0 206
Daniel@0 207 <div class="nb-cell query">
Daniel@0 208 setof(Ev,cla_op((time-dur)&gt;&gt;event,Ev),_Evs),
Daniel@0 209 concurrency(_Evs,_CEvs),
Daniel@0 210 maplist(x(fst(diff)),_CEvs,_CEvs2),
Daniel@0 211 weighted_histof(Dur,Conc,member(Dur-Conc,_CEvs2),Hist),
Daniel@0 212 select(0-_,Hist,_Hist1),
Daniel@0 213 unzip(_Hist1,_Values,_Durs),
Daniel@0 214 c3_bar(concurrency-_Values,duration-_Durs,Chart).
Daniel@0 215 </div>
Daniel@0 216
Daniel@0 217 </div>