Problem running hopenhayn1992

Dear Robert,

Thank you for the update. I downloaded the latest version of both files but I keep running into errors. I must mention that i don’t have a dedicated GPU in my computer. What i have is an Intel(R) UHD Graphics 620. This is the new error message I am seeing now:

Hopenhayn1992

vfoptions =

struct with fields:

          endogenousexit: 1
        keeppolicyonexit: 1
          ReturnToExitFn: @(a,s)0
ReturnToExitFnParamNames: {}

Error using arrayfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 401 in dimension 1. Input #3 has size 1

Error in CreateReturnFnMatrix_Case1_Disc_Par2 (line 118)
Fmatrix=arrayfun(ReturnFn, d_gridvals(:,1), aprime1vals, a1vals, z1vals, ParamCell{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in ValueFnIter_Case1_EndogExit (line 51)
ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, n_z, d_grid, a_grid, z_grid, ReturnFnParamsVec);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in ValueFnIter_Case1 (line 296)
[V, Policy,ExitPolicy]=ValueFnIter_Case1_EndogExit(V0, n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in Hopenhayn1992 (line 92)
[V,Policy,ExitPolicy]=ValueFnIter_Case1(n_d,n_a,n_z,d_grid,a_grid,z_grid, pi_z, ReturnFn, Params, DiscountFactorParamNames, , vfoptions);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 Like

This function works only on gpu. There should be an if condition in the value function iteration file for entry/exit models that picks CreateReturnFnMatrix_Case1_Disc instead of CreateReturnFnMatrix_Case1_Disc_Par2 if a compatible gpu is not found.

I’ve fixed all three of the firm entry/exit examples so they run without a gpu (note they are 10x or 100x faster with a gpu than without, and any more advanced toolkit examples require a gpu).

I never run models without a gpu nowadays, so I tend to not notice if I break the cpu codes. Thanks for pointing it out!

CPU is really just kept around for the most simple examples, mainly so that it can be used in teaching.

[In FHorz I separated out the cpu codes to be somewhat separate so I don’t break them again, I should do the same with InfHorz, but have not done so yet]

1 Like

I would change the return function of Hopenhayn1992 from

function F=Hopenhayn1992_ReturnFn(n_val,aprime_val, a_val, s_val, p, alpha, cf)
% Note that neither aprime_val nor a_val is actually used for anything in
% this model. But VFI toolkit is not set up to handle that they do not exist.

F=-Inf;

% This example is taken from Chris Edmonds lecture notes.
pi=p*s_val*(n_val^alpha)-n_val-cf;

F=pi;

end

to

function F=Hopenhayn1992_ReturnFn(n_val,aprime_val, a_val, s_val, p, alpha, cf)
% Note that neither aprime_val nor a_val is actually used for anything in
% this model. But VFI toolkit is not set up to handle that they do not exist.

% This example is taken from Chris Edmonds lecture notes.
F = p*s_val*(n_val^alpha)-n_val-cf;

end

I have more substantive issue regarding this code. I ran Hopenhayn1992.m on a laptop without GPU and it runs without errors, relatively fast (8 seconds entire script, with GE and plots). Note that my laptop w/out nvidia gpu is a pretty standard university-provided laptop, nothing fancy.

If I run it instead on my laptop with nvidia GPU, with vfoptions.parallel=1, the code is much slower (i.e. 100 times slower). I have encountered this problem in the past: on a computer with GPU, some arrays are created on the gpu (e.g. z_grid, pi_z, etc). Then at some point the option parallel<2 is used and the toolkit chooses the functions that are coded for cpu, but using arrays on the gpu. This turns out to be very slow.

What to do: probably nothing in the short-medium run, since users who have nvidia gpu will never choose vfoptions.parallel<2 and those who don’t have a supported GPU will never have any array on the GPU accidentally used. So all good. I just report this since if someone tests the cpu codes on a computer with gpu might incorrectly underestimated the performance of the cpu codes :slight_smile:

To really fix this issue, one could add a condition like

if parallel<2
   gather bunch of arrays (if already on cpu, gather will do nothing)
end

I see what you are saying. If you want to tell me which lines exactly to change to get the cpu performance better when there is a gpu present I am happy to do so. But as you note it is not a situation anyone is likely to actually run into in practice as if you have a gpu you are going to use it.

Really what I need to do is go and do the same for CPU in InfHorz as I did in FHorz. Just detect it early and send you off to a dedicated CPU command. That way I can also avoid periodically breaking it :smiley:

1 Like

I want to leave the F=-Inf; at start of return fn, even though it is redundant here, because in almost every other model you need it (otherwise the gpu complains when you set F in if-else statements). So even though you don’t need it here it is a good habit for users to see and pick up. Is one of those things where it is not quite as clean and fast, but is clearer communication for the user about how to use the toolkit.

1 Like