Error running basic real business cycle model

Hi friends,

I’m starting my journey with the VFI Toolkit. I tried the BasicRealBusinessCycleModel.m example and got the following error:

>> BasicRealBusinessCycleModel
No vfoptions given, using defaults
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 4).
Analyzing and transferring files to the workers ...done.
Error using BasicRealBusinessCycleModel>@(d_val,aprime_val,a_val,s_val,alpha,delta,theta,tau)BasicRealBusinessCycleModel_ReturnFn(d_val,aprime_val,a_val,s_val,alpha,delta,theta,tau)
An UndefinedFunction error was thrown on the workers for
'BasicRealBusinessCycleModel_ReturnFn'.  This might be because the file containing
'BasicRealBusinessCycleModel_ReturnFn' is not accessible on the workers. Use
addAttachedFiles(pool, files) to specify the required files to be attached.  For more
information, see the documentation for 'parallel.Pool/addAttachedFiles'.

Error in CreateReturnFnMatrix_Case1_Disc (line 91)
        parfor i4=1:N_z

Error in ValueFnIter_Case1_Refine (line 18)
        ReturnMatrix=CreateReturnFnMatrix_Case1_Disc(ReturnFn, n_d, n_a, n_z, d_grid, a_grid, z_grid, vfoptions.parallel, ReturnFnParamsVec);

Error in ValueFnIter_Case1 (line 629)
    [VKron,Policy]=ValueFnIter_Case1_Refine(V0,n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z,ReturnFn,ReturnFnParamsVec,DiscountFactorParamsVec,vfoptions);

Error in BasicRealBusinessCycleModel (line 56)
[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,[]);

Caused by:
    Undefined function 'BasicRealBusinessCycleModel_ReturnFn' for input arguments of type
    'double'.

Any ideas on what’s happening?

Cheers!

1 Like

There should be two files, BasicRealBusinessCycleModel.m and BasicRealBusinessCycleModel_ReturnFn.m. When you run the first file it has to call the second, this error is telling you it cannot find the second.

You need to make sure you have downloaded both, and that Matlab can find the second file (either because it is in your active folder, or you add it to the “path”)

Oh, and welcome :smiley:

I will just add I can see from error msg that you are running without a GPU, having a GPU will make all the runtimes vastly faster.

Many thanks for the quick reply! Yes, you are correct, I ran it without the BasicRealBusinessCycleModel_ReturnFn.m file. My bad!

However, after downloading the file and running it again, I got another error:

>> BasicRealBusinessCycleModel
No vfoptions given, using defaults
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 4).
Error using ValueFnIter_Case1_NoD_Par1_raw/parfor%supply_1
Index in position 2 exceeds array bounds. Index must not exceed 21.

Error in ValueFnIter_Case1_NoD_Par1_raw (line 21)
    parfor z_c=1:N_z

Error in ValueFnIter_Case1_Refine (line 87)
    [VKron,Policy_a]=ValueFnIter_Case1_NoD_Par1_raw(V0, N_a, N_z, pi_z, DiscountFactorParamsVec, ReturnMatrix, vfoptions.howards, vfoptions.maxhowards, vfoptions.tolerance);

Error in ValueFnIter_Case1 (line 629)
    [VKron,Policy]=ValueFnIter_Case1_Refine(V0,n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z,ReturnFn,ReturnFnParamsVec,DiscountFactorParamsVec,vfoptions);

Error in BasicRealBusinessCycleModel (line 55)
[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,[]);

Any ideas?

1 Like

Thanks! How should I add the GPU?

1 Like

You can only add GPU if your computer has one (and has to be NVIDIA, as theirs is only one that Matlab can use)

If you have a NVIDIA gpu. You just need to install CUDA so matlab can see it.

You can test if gpu is working in matlab by running Matlab command X=ones(3,3,'gpuArray'). (It will only work if matlab can see a gpu, this command it creating a matrix on the gpu)

Thanks! I ran the command and indeed Matlab wasn’t able to find a supported GPU device. I’m running it in my work’s computer, so I’m skeptical about being able to install it.

Is it possible to parallelize instead across CPUs?

When VFI Toolkit cannot find a GPU the default will be to parallelize across CPUs (but this is nowhere near as fast)

The error was because ‘refine’ is not implemented correctly for cpu. I am away on holidays next week, so I have just pushed a quick fix/hack, and will do an actual fix when I return.

[What refine is, is not important here. But it was a recent change to make this the default in infinite horizon models as it is faster (my quick hack is just to switch back to the old default when there is no gpu). All my testing was with a gpu so I did not notice that it was broken on cpu. Thanks for reporting this.]

1 Like

To give some idea, this BasicRealBusinessCycleModel.m takes fractions of a second with a GPU, is going to be minutes (or maybe much more) on CPUs.

1 Like

Many thanks for the quick fix. I’ll download the toolkit again and retry.

Noted. I’ll check with IT the viability of having it on my device.

It worked now! It took 219.17 seconds to finish, not bad.

1 Like

Went and fixed this properly. Likely faster now too.

1 Like

Thank you! I downloaded the updated toolkit and ran the model again. Got the following error:

>> BasicRealBusinessCycleModel
No vfoptions given, using defaults
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to the parallel pool (number of workers: 4).
Error using zeros
Unable to find a supported GPU device. For more information on GPU support, see GPU
Support by Release.

Error in ValueFnIter_Case1_NoD_Par1_raw (line 13)
Ftemp=zeros(N_a,N_z,'gpuArray');

Error in ValueFnIter_Case1_Refine (line 87)
    [VKron,Policy_a]=ValueFnIter_Case1_NoD_Par1_raw(V0, N_a, N_z, pi_z, DiscountFactorParamsVec, ReturnMatrix, vfoptions.howards, vfoptions.maxhowards, vfoptions.tolerance);

Error in ValueFnIter_Case1 (line 628)
    [VKron,Policy]=ValueFnIter_Case1_Refine(V0,n_d,n_a,n_z,d_grid,a_grid,z_grid,pi_z,ReturnFn,ReturnFnParamsVec,DiscountFactorParamsVec,vfoptions);

Error in BasicRealBusinessCycleModel (line 55)
[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,[]);
1 Like

You should specify vfoptions.parallel=1 because otherwise the default is parallel=2 which requires a GPU.

Try replacing the line

[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,);

with the following

vfoption.parallel=1;
[V,Policy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames,,vfoptions);

2 Likes

Sorry, should be fixed now. (Was a typo)

Unnecessary detail: You do not need to set vfoptions.parallel, as VFI Toolkit will simply use the default value. When you don’t set vfoptions.parallel the default is that Matlab looks for a GPU and if it finds one then it sets vfoptions.parallel=2 and if it does not then it sets vfoptions.parallel=1. Specifically, the line of code is vfoptions.parallel=1+(gpuDeviceCount>0)

1 Like

Thank you! It’s indeed faster: it took 145.27 seconds now (219.17 seconds previously).

1 Like