# CUDAFortan error: ... from device code to a host function...

**URL:** <https://forums.developer.nvidia.com/t/cudafortan-error-from-device-code-to-a-host-function/133833>\
**Category:** Legacy PGI Compilers (archived)\
**Created:** [November 11, 2013, 2:11am UTC](https://forums.developer.nvidia.com/t/cudafortan-error-from-device-code-to-a-host-function/133833 "2013-11-11T02:11:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danh-Tai\_HOANG](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Danh-Tai\_HOANG](https://forums.developer.nvidia.com/u/Danh-Tai_HOANG)\
**Post date:** [November 11, 2013, 2:11am UTC](https://forums.developer.nvidia.com/t/cudafortan-error-from-device-code-to-a-host-function/133833/1 "2013-11-11T02:11:35Z")

</div>

Dear All, I am learning CUDA Fortran. I want to write a program to calculate the sumations E of n=10 values of random number. Here is my code:

!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
MODULE sum\_random\_number  
IMPLICIT NONE

CONTAINS  
!!!=================================================================  
ATTRIBUTES(GLOBAL) SUBROUTINE average\_thermal(E)  
IMPLICIT NONE

REAL (KIND=8),INTENT(INOUT):: E(:)  
INTEGER (KIND=8) :: iT,i  
REAL (KIND=8) :: rdn  
INTEGER (KIND=8),PARAMETER :: n=10,N\_rdn=200

!REAL (KIND=8),device :: deviceData (N\_rdn)

iT=threadIdx%x

E(iT)=0.  
DO i=1,n  
E(iT)=E(iT)+deviceData(i)  
END DO

END SUBROUTINE average\_thermal

END MODULE sum\_random\_number

!!!=================================================================  
PROGRAM main\_sum  
USE cudafor  
USE sum\_random\_number  
USE curand\_m

IMPLICIT NONE  
INTEGER (KIND=8),PARAMETER :: nT=15  
REAL (KIND=8),DEVICE :: Ed(nT)  
REAL (KIND=8) :: E(nT)  
INTEGER (KIND=8) :: iT

INTEGER (KIND=8),PARAMETER :: N\_rdn=200

REAL (KIND=8),device :: deviceData(N\_rdn)  
INTEGER (KIND=8) :: gen,seed

CALL curandCreateGenerator(gen,CURAND\_RNG\_PSEUDO\_DEFAULT)  
seed=1234  
CALL curandSetPseudoRandomGeneratorSeed(gen,seed)  
CALL curandGenerateUniform (gen,deviceData, N\_rdn)

Ed=E !!! Host to Divice transpers  
CALL average\_thermal\<\<\<1,nT\>\>\>(Ed)  
E=Ed !! Device to Host transpers

OPEN(unit=20,file=‘average\_thermal.dat’)  
DO iT=1,nT  
WRITE(20,_) iT,E(iT)  
WRITE(_,\*) iT,E(iT)  
END DO

CLOSE(20)

! Destroy the generator  
CALL curandDestroyGenerator(gen)

END PROGRAM main\_sum  
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

When I compiled, there was the erros: PGF90-S-0155-Calls from device code to a host function are allowed only in emulation mode - devicedata

Please help me to figure out this problem! Thank you in avance.

---

<div class="post-metadata">

**Author:** ![MatColgrove](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/matcolgrove/32/14047_2.png) [@MatColgrove](https://forums.developer.nvidia.com/u/MatColgrove)\
**Post date:** [November 11, 2013, 4:44pm UTC](https://forums.developer.nvidia.com/t/cudafortan-error-from-device-code-to-a-host-function/133833/2 "2013-11-11T16:44:32Z")

</div>

Hi Danh-Tai HOANG,

Notice the second error message you are getting:

> PGF90-S-0038-Symbol, devicedata, has not been explicitly declared

Since “deviceData” isn’t declared in average\_thermal’s scope, implicit typing is used and “deviceData(I)” defaults to being typed as an external function call. To fix, you need to me deviceData’s declaration into the module’s data section so it’s visible to both the main program and the subroutine.

- Mat

---

<div class="post-metadata">

**Author:** ![Danh-Tai\_HOANG](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Danh-Tai\_HOANG](https://forums.developer.nvidia.com/u/Danh-Tai_HOANG)\
**Post date:** [November 11, 2013, 10:57pm UTC](https://forums.developer.nvidia.com/t/cudafortan-error-from-device-code-to-a-host-function/133833/3 "2013-11-11T22:57:46Z")

</div>

Hi mkcolg,  
Thank you for your help.  
In fact, when “deviceData” is declared in “average\_thermal”, there is not error but the values of E are always 0.

Based on your idea, I added “deviceData” as the second input:  
…  
average\_thermal(E,deviceData)  
REAL (KIND=8),device,INTENT(IN) :: deviceData (N\_rdn)  
…

And in main program: CALL average\_thermal\<\<\<1,nT\>\>\>(Ed,deviceData)

It works now!
