# Nvidia GPU: code performance is not as expected

**URL:** <https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413>\
**Category:** Code\
**Created:** [May 26, 2021, 7:45am UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413 "2021-05-26T07:45:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/comway/32/1483_2.png) [@comway](https://discourse.cmake.org/u/comway)\
**Post date:** [May 26, 2021, 7:45am UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413/1 "2021-05-26T07:45:20Z")

</div>

Hello,  
This code will output two values, representing the address of a static variable. The two output values should be equal, but tested on the Nvidia, they are not equal. It is now expected that the problem lies in the cu cpp mixed compilation, but no relevant official samples have been found. Please also help locate the problem.  
Thank you  
The code is follows.

main1.cu:

```auto
#include "timer.h"

int main() {
  Timer tm;
  tm.f1();
  tm.f2();
  return 0;
}

```

main2.cpp:

```auto
#include "timer.h"

int f() {
  Timer tm;
  tm.f1();
// tm.f2();
  return 0;
}

```

time.h:

```auto
#pragma once
#include <iostream>

static int TimerT;

class Timer {
 public:
  void f1() {
    std::cout << (long)(&TimerT) << std::endl;
  }

  void f2() {
    std::cout << (long)(&TimerT) << std::endl;
  }
};

```

CMakeLists.txt:

```cmake
cmake_minimum_required(VERSION 3.5)
project(main)

find_package(CUDA)

#set(CUDA_NVCC_FLAGS -O3)

cuda_add_executable(main main1.cu main2.cpp)

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [May 26, 2021, 11:06am UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413/2 "2021-05-26T11:06:30Z")

</div>

@robert.maynard

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [May 26, 2021, 7:00pm UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413/3 "2021-05-26T19:00:32Z")

</div>

A couple of points:

1. When testing the linked code locally I see the expected and correct behavior of the static variable address being the same
2. If a problem did exist it would be located with how the `nvcc` compiler interacts with the host compiler. I recommend asking for help in [https://forums.developer.nvidia.com/](https://forums.developer.nvidia.com/)

---

<div class="post-metadata">

**Author:** ![comway](https://discourse.cmake.org/user_avatar/discourse.cmake.org/comway/32/1483_2.png) [@comway](https://discourse.cmake.org/u/comway)\
**Post date:** [May 27, 2021, 1:37am UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413/4 "2021-05-27T01:37:34Z")

</div>

Thank you very much, Are you using cmakelist to compile? I use nvcc to compile the value is the same, but use cmakelists to compile, there will be different values

---

<div class="post-metadata">

**Author:** ![tgq](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@tgq](https://discourse.cmake.org/u/tgq)\
**Post date:** [May 27, 2021, 11:19am UTC](https://discourse.cmake.org/t/nvidia-gpu-code-performance-is-not-as-expected/3413/5 "2021-05-27T11:19:53Z")

</div>

Uhm, thats exactly what you asked for. By making the `int` in the header static, you told every translation unit thay includes the header to have unique copy of the variable, that’s how static storage spexifier works.
