|
Set addressing and references |
Top Previous Next |
|
GAMS employs a sparse matrix data storage scheme. A parameter like X(A,B,C) is stored in the order
a1 b1 c1 a1 b1 c2 ... a1 b1 cm a1 b2 c1 ... a1 b2 cm ... a1 bn cm a2 b1 c1 ... ak bn cm
Note the entries are stored internally in systematic order with the last entry varied the fastest then the second then the first. GAMS withdraws entries from memory fastest if they are referenced in the order most consistent with the storage order. As a consequence the calculation specification
Y(a,b,c)=X (a,b,c); is faster than Y(a,b,c)=X(b,c,a).
Referencing in a manner inconsistent with the storage order slows things down. One should arrange set reference order so that across calculations, equation definitions etc, the sets are always referenced in the same order. We then arrive at speed tip number one. To increase speed modelers should endeavor to arrange definitions, calculations, sums, and equation references to sets in a consistent order. Consider the example (gamsslow.gms versus gamsfast.gms)
parameter x(e,d,c,b,a); X(e,d,c,b,a)=10; parameter z(a,b,c,d,e); z(a,b,c,d,e)=x(e,d,c,b,a); parameter y; Y=sum ((a,b,c,d,e),z(a,b,c,d,e)*x(e,d,c,b,a)); variables obj Positive variables var(e,b,a); equations objeq R( b,c,d) q(a,b,c);
objeq.. obj=e=sum((a,b,c,d,e),z(a,b,c,d,e)*x(e,d,c,b,a)*var(e,b,a)); r(b,c,d).. sum((a,e),Var(e,b,a))=l=sum((a,e),x(e,d,c,b,a)*z(a,b,c,d,e)); q(a,b,c).. sum((d,e),var(e,b,a)/x(e,d,c,b,a)*z(a,b,c,d,e))=l=20; model slow /all/; *option lp=bdmlp; slow.workspace=10; solve slow maximizing obj using lp; parameter sumofvar; sumofvar=sum((a,b,c,d,e),z(a,b,c,d,e)*x(e,d,c,b,a)*var.l(e,b,a));
which yields the Profile lines where the coloring corresponds to source file lines.
---- 13 ASSIGNMENT x 0.090 0.090 SECS 5.7 Mb 172800 ---- 15 ASSIGNMENT z 0.520 0.610 SECS 9.9 Mb 172800 ---- 17 ASSIGNMENT y 0.561 1.171 SECS 9.9 Mb ---- 29 ASSIGNMENT slow 0.000 1.171 SECS 9.9 Mb ---- 30 SOLVE INIT slow 0.000 1.171 SECS 9.9 Mb ---- 24 EQUATION objeq 1.142 2.313 SECS 10.5 Mb 1 ---- 25 EQUATION R 0.861 3.174 SECS 17.8 Mb 1200 ---- 26 EQUATION q 1.142 4.316 SECS 18.3 Mb 1440 ---- 30 SOLVE FINI slow 0.120 4.436 SECS 18.3 Mb ---- 1 EXEC-INIT 0.000 0.000 SECS 9.2 Mb solve appeared here ---- 30 SOLVE READ slow 0.010 0.010 SECS 9.7 Mb ---- 32 ASSIGNMENT sumofvar 0.701 0.711 SECS 10.5 Mb
A rearrangement of set indexes to a consistent order in gamsfast.gms yields the profile information
---- 13 ASSIGNMENT x 0.050 0.050 SECS 3.7 Mb 100000 ---- 15 ASSIGNMENT z 0.090 0.140 SECS 6.3 Mb 100000 ---- 17 ASSIGNMENT y 0.110 0.250 SECS 6.3 Mb ---- 29 ASSIGNMENT slow 0.000 0.250 SECS 6.3 Mb ---- 30 SOLVE INIT slow 0.000 0.250 SECS 6.3 Mb ---- 24 EQUATION objeq 0.410 0.660 SECS 6.3 Mb 1 ---- 25 EQUATION R 0.401 1.061 SECS 11.0 Mb 1000 ---- 26 EQUATION q 0.411 1.472 SECS 11.0 Mb 1000 ---- 30 SOLVE FINI slow 0.070 1.542 SECS 11.0 Mb ---- 30 GAMS FINI 0.030 1.572 SECS 11.0 Mb ---- 30 SOLVE READ slow 0.010 0.010 SECS 6.1 Mb ---- 32 ASSIGNMENT sumofvar 0.150 0.160 SECS 6.8 Mb
Where note the redefinition of the line numbered 15
z(a,b,c,d,e)=x(e,d,c,b,a); into z(a,b,c,d,e)=x(a,b,c,d,e);
drops execution time from 0.52 to 0.09 seconds. Substantial percentage reductions were achieved in all the time consuming cases by the reordering and improved consistency of set addressing. |