0

I have just installed the nvidia CUDA toolkit on my fresh Ubuntu 20.04 installation. Nvcc compiles CUDA programs, and they run without crashing. However, none of the results are correct.

Here is the output of the test script (deviceQuery) that Nvidia provides:

./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "NVIDIA GeForce GTX 770"
  CUDA Driver Version / Runtime Version          11.4 / 11.4
  CUDA Capability Major/Minor version number:    3.0
  Total amount of global memory:                 1997 MBytes (2093875200 bytes)
  (008) Multiprocessors, (192) CUDA Cores/MP:    1536 CUDA Cores
  GPU Max Clock rate:                            1110 MHz (1.11 GHz)
  Memory Clock rate:                             3505 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 524288 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total shared memory per multiprocessor:        49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Managed Memory:                Yes
  Device supports Compute Preemption:            No
  Supports Cooperative Kernel Launch:            No
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 2 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.4, CUDA Runtime Version = 11.4, NumDevs = 1 
Result = PASS

And here is the very simple vector addition program I am trying to run:

#include <cuda_runtime.h>

#include <iostream>
#include <cuda.h>
using namespace std;

int *a, *b;  // host data
int *c;  // results

__global__ void vecAdd(int *A,int *B,int *C)
{
   int i = blockIdx.x * blockDim.x + threadIdx.x;
   C[i] = A[i] + B[i];
}

int main(int argc,char **argv)
{
   printf("Begin \n");
   int n=1000000;
   int nBytes = n*sizeof(int);
   int block_size, block_no;
   a = (int *)malloc(nBytes);
   b = (int *)malloc(nBytes);
   c = (int *)malloc(nBytes);
   int *a_d,*b_d,*c_d;
   block_size=1000;
   block_no = n/block_size;
   for(int i=0;i<n;i++) {
      a[i]=i;
      b[i]=i;
   }

   printf("Allocating device memory on host..\n");
   cudaMalloc((void **)&a_d,n*sizeof(int));
   cudaMalloc((void **)&b_d,n*sizeof(int));

   printf("Copying to device..\n");
   cudaMemcpy(a_d,a,n*sizeof(int),cudaMemcpyHostToDevice);
   cudaMemcpy(b_d,b,n*sizeof(int),cudaMemcpyHostToDevice);

   printf("Doing GPU Vector add\n");
   vecAdd<<<block_no,block_size>>>(a_d,b_d,c_d);

   cudaMemcpy(c,c_d,n*sizeof(int),cudaMemcpyDeviceToHost);
   for(int i = 0; i < 10; i++) {
        std::cout << a[i] << " + " << b[i] << " = " << c[i] << std::endl;
   }
   cudaFree(a_d);
   cudaFree(b_d);
   cudaFree(c_d);
   free(a);
   free(b);
   free(c);
   return 0;
}

And last but not least, here is it's faulty output:


Begin
Allocating device memory on host..
Copying to device..
Doing GPU Vector add
0 + 0 = 0
1 + 1 = 0
2 + 2 = 0
3 + 3 = 0
4 + 4 = 0
5 + 5 = 0
6 + 6 = 0
7 + 7 = 0
8 + 8 = 0
9 + 9 = 0

Any help is greatly appreciated.

hjfeldy
  • 9
  • 1
  • 6
    You have multiple problems. compute capability 3.0 devices are not supported by any version of CUDA 11.x. Switch to CUDA 10.x. Also pay attention to the answer given, your code is indeed broken. Also, use [proper CUDA error checking](https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api), any time you are having trouble with a CUDA code. – Robert Crovella Jul 13 '21 at 01:01
  • This is very helpful, thank you. And I see now that the code is broken (that is, if you're referring to the missing cudaMalloc for the d_c variable). Like I said in the response to that answer, problems persist. Hopefully installing the correct version of CUDA will fix this. – hjfeldy Jul 13 '21 at 01:07

1 Answers1

3

You never allocate storage for c on the device. Try adding

cudaMalloc((void **)&c_d,n*sizeof(int));

before you call the CUDA kernel.

alter_igel
  • 6,899
  • 3
  • 21
  • 40
  • I actually just deleted that exact same line of code before uploading my question because I was convinced it was incorrect. Same exact problem persists – hjfeldy Jul 13 '21 at 01:00
  • When I add this line, on an otherwise known good/working CUDA setup, I get the expected results. – Robert Crovella Jul 13 '21 at 01:09
  • Robert, that is really good to know, thank you. And as for what you said about users only being able to help with the information given, I totally agree. That's my bad. Seriously, thank you so much for this comment, for your answer, and for taking the time to run my code. – hjfeldy Jul 13 '21 at 01:23
  • 1
    I strongly recommend adding error handling to your code if you want to avoid debugging completely in the dark. When I run the code on a machine without a GPU driver, I get your reported results of all zeros. With added error reporting code, I instead see `GPUassert: CUDA driver version is insufficient for CUDA runtime version main.cu 45` which is far more helpful – alter_igel Jul 13 '21 at 01:26