Non-constant expression where constant expression required

I’m attempting to compile some code with the pgfortran compiler with the -Mcuda option. I’m performing an operation that might be getting me into trouble.

I’m importing all the variables I need for my gpu code in a setup host subroutine. In the use statement I’m using the h_univ=>univ syntax to specify the imported variable is in host memory. I’m then redeclaring device variables with original names: integer, device :: univ = h_univ.

For “nearly” all (which makes it even more confusing) I’m getting the following error:
Non-constant expression where constant expression required

Any thoughts on what might be happening here? I’m hitting my error cap before I even get out of my data transfer. Thanks!

Further investigation seems to suggest that imported parameter variables do not throw the error, but others do. If anything this seems counter-intuitive to the error message

Hi R.M. Sivley,

Device data can only be initialized using constants. I’m assuming “h_univ” is a variable.

The simple work around is initialize your device variables in the runtime code. For example:

integer, device :: univ
! more declarations
univ = h_univ   ! perform the host to device copy.

Is “univ” constant on the device? If so, you may want to declare it in the module’s data with the “constant” attribute instead of “device”. Constant memory is read-only on the device, but much faster to access than device memory. Something like:

module cuda_drv
     integer, constant :: univ
! more module data declarations


subroutine cuda_drv_setup (integer h_univ, ... more variables)
   univ = h_univ  
! continues

Hope this helps,

Oh okay, I’m new to fortran in general so that’s a newbie error. No problem declaring and assigning in two separate statements.

I’m working with preexisting code so I haven’t made the determination yet of which are open to editing and which are merely for reference. Per your advice I will definitely declare any parameters as constants on the device.

Thanks again.