# Problems calling \_\_device\_\_ function in \_\_host\_\_ \_\_device\_\_ function

**URL:** <https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949>\
**Category:** CUDA Programming and Performance\
**Created:** [April 8, 2013, 12:48pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949 "2013-04-08T12:48:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SKrot](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SKrot](https://forums.developer.nvidia.com/u/SKrot)\
**Post date:** [April 8, 2013, 12:48pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949/1 "2013-04-08T12:48:40Z")

</div>

Hi,

I got some problems calling a **host**  **device** function. It seems like, the program always tries to call the compiled **host** code instead of the **device** code.

My code looks like:

```auto
main.cu
...
void foo(int *a, int *b){
  dev_sth(a,b)
}
...
foo(a,b)
...

```

```auto
sth.h
...
void do_sth(int *a, int *b);
void dev_sth(int *a, int *b);

```

```auto
sth.cu
...
__host__  __device__ void do_sth(int *a, int *b){
  a[1]=....
//some code
}
...
__global__ void sth(int *a. int *b){
  do_sth(a,b);
}

...
void dev_sth(int *a, int *b){
 if(!use_cuda){
  do_sth<<<blockspergrid, threadsperblock>>>(a,b);
 }else{
  sth(a,b)
 }
}

```

Thus the code does following:

1. call function foo
2. foo calls dev\_sth
3. dev\_sth checks, wheter to use cpu or gpu code, cpu version runs fine (sth(..)), but if cuda:
4. start \_\_global\_\_ function sth(...), which calls do\_sth(...)

Now it appears, as if the **device** do\_sth(…) call in the **global** function tries to call the **host** do\_sth function.

Am I doing something totally wrong? I would be really thankfull if anyone got some hints or the solution.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [April 8, 2013, 12:55pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949/2 "2013-04-08T12:55:19Z")

</div>

> [@SKrot](#):
>
> Now it appears, as if the **device** do\_sth(…) call in the **global** function tries to call the **host** do\_sth function.

What makes you think so?

---

<div class="post-metadata">

**Author:** ![SKrot](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SKrot](https://forums.developer.nvidia.com/u/SKrot)\
**Post date:** [April 8, 2013, 1:03pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949/3 "2013-04-08T13:03:10Z")

</div>

If I comment out all of the **host** functions everything works fine. Furthermore, if I debug with NSight, weirdly only some of the threads got the following error:

Status: Exception  
Exception: OutOfRangeLoad  
Exception Details: MemorySpace=Global Size=4

---

<div class="post-metadata">

**Author:** ![SKrot](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SKrot](https://forums.developer.nvidia.com/u/SKrot)\
**Post date:** [April 8, 2013, 3:24pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949/4 "2013-04-08T15:24:34Z")

</div>

I got another news. As the compiler says:  
“Cannot tell what pointer points to, assuming global memory” pointing to the line with “do\_sth(a,b)” within the **global** function, I think I’m on the right way.

Now the question: How to tell the compiler it should link to device memory?

---

<div class="post-metadata">

**Author:** ![SKrot](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SKrot](https://forums.developer.nvidia.com/u/SKrot)\
**Post date:** [April 9, 2013, 4:02pm UTC](https://forums.developer.nvidia.com/t/problems-calling-device-function-in-host-device-function/28949/5 "2013-04-09T16:02:17Z")

</div>

Hi,

if anyone got the same issue, I was able to solve the problem. I don’t know why but I had to **forceinline** the device code. Thus the working code now looks like

```auto
sth.cu
    ...
    __host__  __device__  __forceinline__ void do_sth(int *a, int *b){
    a[1]=....
    //some code
    }
    ...
    __global__ void sth(int *a. int *b){
    do_sth(a,b);
    }

    ...
    void dev_sth(int *a, int *b){
    if(!use_cuda){
    do_sth<<<blockspergrid, threadsperblock>>>(a,b);
    }else{
    sth(a,b)
    }
    }

```

Thanks anyway.
