title: What does nvprof output: "No kernels were profiled" mean, and how to fix it
body: <p>I have recently installed Cuda on my arch-Linux machine through the system's package manager, and I have been trying to test whether or not it is working by running a simple vector addition program.</p>
  <p>I simply copy-paste the code from <a href="https://devblogs.nvidia.com/even-easier-introduction-cuda/" rel="nofollow noreferrer">this tutorial</a> (Both the one using one and more kernels) into a file titled <code>cuda_test.cu</code> and run</p>
  <pre><code>&gt; nvcc cuda_test.cu -o cuda_test
  </code></pre>
  <p>In either case, the program can run, and I get no errors (both as in the program doesn't crash and the output is that there were no errors). But when I try to run the Cuda profiler on the program:</p>
  <pre><code>&gt; sudo nvprof ./cuda_test
  </code></pre>
  <p>I get result:</p>
  <pre><code>==3201== NVPROF is profiling process 3201, command: ./cuda_test
  Max error: 0
  ==3201== Profiling application: ./cuda_test
  ==3201== Profiling result:
  No kernels were profiled.
  No API activities were profiled.
  ==3201== Warning: Some profiling data are not recorded. Make sure cudaProfilerStop() or cuProfilerStop() is called before application exit to flush profile data.
  </code></pre>
  <p>The latter warning is not my main problem or the topic of my question, my problem is the message saying that No Kernels were profiled and no API activities were profiled.</p>
  <p>Does this mean that the program was run entirely on my CPU? or is it an error in nvprof?</p>
  <p>I have found a discussion about the same error <a href="https://devtalk.nvidia.com/default/topic/988289/cuda-programming-and-performance/nvprof-error-on-samples/" rel="nofollow noreferrer">here</a>, but there the answer was that the wrong version of Cuda was installed, and in my case, the version installed is the latest version installed through the systems package manager (<a href="https://www.archlinux.org/packages/community/x86_64/cuda/" rel="nofollow noreferrer">Version 10.1.243-1</a>)</p>
  <p>Is there any way I can get either nvprof to display the expected output?</p>
  <h1>Edit</h1>
  <p>Trying to adhere to the warning at the end does not solve the problem:</p>
  <p>Adding call to <code>cudaProfilerStop()</code> (or <code>cuProfilerStop()</code>), and also adding   <code>cudaDeviceReset();</code> at end as suggested and linking the appropriate library (<code>cuda_profiler_api.h</code> or <code>cudaProfiler.h</code>) and compiling with</p>
  <pre><code>&gt; nvcc cuda_test.cu -o cuda_test -lcuda
  </code></pre>
  <p>Yields a program which can still run, but which, when uppon which nvprof is run, returns:</p>
  <pre><code>==12558== NVPROF is profiling process 12558, command: ./cuda_test
  Max error: 0
  ==12558== Profiling application: ./cuda_test
  ==12558== Profiling result:
  No kernels were profiled.
  No API activities were profiled.
  ==12558== Warning: Some profiling data are not recorded. Make sure cudaProfilerStop() or cuProfilerStop() is called before application exit to flush profile data.
  ======== Error: Application received signal 139
  </code></pre>
  <p>This has not solved the original problem, and has in fact created a new error; the same happens when <code>cudaProfilerStop()</code> is used on its own or alongside <code>cuProfilerStop()</code> and <code>cudaDeviceReset();</code></p>
  <h1>The code</h1>
  <p>The code is, as mentioned copied from a tutorial to test if Cuda is working, though I also have included calls to <code>cudaProfilerStop()</code> and <code>cudaDeviceReset()</code>; for clarity, it is here included:</p>
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;math.h&gt;
  
  #include &lt;cuda_profiler_api.h&gt;
  
  // Kernel function to add the elements of two arrays
  
  __global__
  void add(int n, float *x, float *y)
  {
    int index = threadIdx.x;
    int stride = blockDim.x;
    for (int i = index; i &lt; n; i += stride)
        y[i] = x[i] + y[i];
  }
  
  
  int main(void)
  
  {
  
    int N = 1&lt;&lt;20;
  
    float *x, *y;
  
  
    cudaProfilerStart();
  
  
    // Allocate Unified Memory – accessible from CPU or GPU
  
    cudaMallocManaged(&amp;x, N*sizeof(float));
  
    cudaMallocManaged(&amp;y, N*sizeof(float));
  
  
  
    // initialize x and y arrays on the host
  
    for (int i = 0; i &lt; N; i++) {
  
      x[i] = 1.0f;
  
      y[i] = 2.0f;
  
    }
  
  
  
    // Run kernel on 1M elements on the GPU
  
      add&lt;&lt;&lt;1, 1&gt;&gt;&gt;(N, x, y);
  
  
  
    // Wait for GPU to finish before accessing on host
  
    cudaDeviceSynchronize();
  
  
  
    // Check for errors (all values should be 3.0f)
  
    float maxError = 0.0f;
  
    for (int i = 0; i &lt; N; i++)
  
      maxError = fmax(maxError, fabs(y[i]-3.0f));
  
    std::cout &lt;&lt; &quot;Max error: &quot; &lt;&lt; maxError &lt;&lt; std::endl;
  
  
  
    // Free memory
  
    cudaFree(x);
  
    cudaFree(y);
    
    cudaDeviceReset();
    cudaProfilerStop();
  
    
  
    return 0;
  
  }
  </code></pre>