Timing of execution with $Call and Execute or Put_Utility

Top  Previous  Next

The timing of program execution can at times be confusing.  Consider the following example (callexecute.gms)

 

set i /i1,i2/

$onmulti

parameter a(i) /i1 22, i2 33/;

$gdxout ss

$unload a

$gdxOUT

execute 'Gdxxrw ss.gdx par=a Rng=sheet1!a1'

$Call Gdxxrw ss.gdx par=a Rng=sheet2!a1

parameter a/i1 44/;

a(i)=a(i)*2;

$GDXout ss

$unload a

$Gdxout

$Call Gdxxrw ss.gdx par=a Rng=sheet3!a1

execute_unload 'ss.gdx' , a

execute 'Gdxxrw ss.gdx par=a Rng=sheet4!a1'

 

which uses Gdxxrw as explained below and generates information into four sheets of a spreadsheet workbook as below

 

_img71

 

_img72

 

_img73

 

_img74

 

Now let me explain the results.  First I should note all $ commands and the item redefinition allowed by $Onmulti (which should not ordinarily be used) are resolved at compile time before execution begins.  So the statements are implicitly reordered with the $Call and $Unload occurring before the Execute, Execute_Unload.and Put_utility.

So the $Unload GDX file creation at the bottom occurs before the Execute Gdxxrw at the top and before that I redefined the element a(i1) that appears in cell A2 of each spreadsheet.  Thus in sheet1 I have the redefined number for a(i1) (44) that is present at compile time when the Unload GDX file creation at the bottom appears. But note these numbers are unaffected by the execution time a(i)=a(i)*2;
The $Unload GDX file creation at the top occurs before the $Call Gdxxrw at the top and before that I redefined the element a(i1) that appears in cell A2 of each spreadsheet.  Thus in sheet2 I have the original number for a(i1) (22) that is present at compile time when the Unload GDX file creation at the top appears.
So the $Unload GDX file creation at the bottom occurs before the $Call Gdxxrw at the bottom.  Thus in sheet3 I have the redefined number for a(i1) (44) that is present at compile time but note these numbers are unaffected by the execution time a(i)=a(i)*2;
The Execute Gdxxrw at the bottom follows everything and uses a GDX file created at execution time by the Execute_Unload and thus sheet4 has the final data.  This is also true of Put_utility
Obviously this can be confusing with statements below the one at hand influencing its results.  It is never a good idea to intermix Execute, Execute_load, Execute_unload with $Call, $Load and $Unload.  I feel all the $ commands should be towards the top and the Executes or Put_utilitys toward the bottom.