-4

I have installed CUDA under LINUX and most of time this simple code below throws errors..

// Kernel that executes on the CUDA device
__global__ void square_array(float *a, int N)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx<N) a[idx] = a[idx] * a[idx];
}

// main routine that executes on the host
int main(void)
{
    cudaSetDevice(0);
    cudaDeviceSynchronize();
    cudaThreadSynchronize();
    cudaError_t cudaError;



    float *a_h, *a_d;  // Pointer to host & device arrays
    const int N = 10;  // Number of elements in arrays
    size_t size = N * sizeof(float);
    a_h = (float *)malloc(size);        // Allocate array on host
    cudaError=cudaMalloc((void **) &a_d, size);   // Allocate array on device
    if(cudaError!=cudaSuccess)
    {
    printf("asdasd1");
    }
    //// Initialize host array and copy it to CUDA device
    for (int i=0; i<N; i++) a_h[i] = (float)i;
    cudaError=cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
    if(cudaError!=cudaSuccess)
    {
    printf("asdasd2");
    }
    //// Do calculation on device:
    int block_size = 4;
    int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
    square_array <<< n_blocks, block_size >>> (a_d, N);
    //// Retrieve result from device and store it in host array
    cudaMemcpy(a_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
    //// Print results
    for (int i=0; i<N; i++) printf("%d %f\n", i, a_h[i]);
    //// Cleanup
    free(a_h); cudaFree(a_d);

I compile it with : nvcc pi.cu -arch=sm_11 -o asd.out I have the same problem on Windows so what can be the problem?

user3490530
  • 53
  • 1
  • 7
  • 5
    What errors? And please indent your code for a better readability. – bitcell Dec 15 '14 at 11:15
  • Errors at : cudaMemcpy and cudaMalloc. – user3490530 Dec 15 '14 at 11:17
  • 1
    Use `printf("%s\n", cudaGetErrorString(cudaError))` for some descriptive error message. – Grzegorz Szpetkowski Dec 15 '14 at 12:18
  • Before starting to write CUDA code and ask on SO why it throws errors, please read this: http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api – Michael Haidl Dec 15 '14 at 14:32
  • Yes because in [this post](http://stackoverflow.com/questions/27320527/cuda-compilation-of-examples) i listed deviceQuery. The output from 'printf("%s\n", cudaGetErrorString(cudaError));' is like this: **no CUDA-capable device is detected** WHY?!? lspci | grep -i nvidia : 01:00.0 VGA compatible controller: NVIDIA Corporation G86 [GeForce 8500 GT] (rev a1) – user3490530 Dec 15 '14 at 14:58
  • Maybe your driver is too old or is not loaded correctly. – Michael Haidl Dec 15 '14 at 15:56
  • 1
    run `nvidia-smi`. If you get an error, re-run `nvidia-smi` as root. If that works, then go back and run your tests. If they begin working, note [this section](http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/index.html#runfile-verifications) in the linux getting started guide. – Robert Crovella Dec 15 '14 at 16:59
  • 1
    In your previous post, you showed a deviceQuery listing that indicated CUDA 6.5. Your nvidia-smi output that you have posted as an "answer" shows driver version 331.113. CUDA 6.5 is *not compatible* with driver version 331.113. If you installed CUDA 6.5 properly you would not have this issue. I suggest you use the [runfile installer method](http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-linux/index.html#runfile) to re-install CUDA 6.5, and be sure that the driver associated with the CUDA 6.5 installer gets installed as well (340.29 or newer). – Robert Crovella Dec 15 '14 at 21:00
  • So can I use CUDA 6.5 on me computer or should I download other version?.. – user3490530 Dec 15 '14 at 21:59
  • I would download [the CUDA 6.5 runfile installer](https://developer.nvidia.com/cuda-downloads) and reload that. When you are done, make sure that the driver version is 340.29 or newer, with `nvidia-smi`. You must have had this done correctly at some point to get your previous deviceQuery output, but then something happened to your config and the older driver 331.113 got loaded, somehow. – Robert Crovella Dec 15 '14 at 22:23
  • deviceQuery was from my Windows installations, in which I have the same problem, but now I'm working with Linux version of Cuda;) – user3490530 Dec 15 '14 at 22:45
  • Why don't you provide an answer explaining what you did to fix it. You can answer your own question. Later on (24 hours) you can come back and accept the answer. – Robert Crovella Dec 15 '14 at 23:42

1 Answers1

1

After all those hints from ALL OF YOU I have reinstalled NVIDIA Drivers and CUDA;) and I think it works;))

Mon Dec 15 23:43:23 2014       
+------------------------------------------------------+                       
| NVIDIA-SMI 340.29     Driver Version: 340.29         |                       
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce 8500 GT     Off  | 0000:01:00.0     N/A |                  N/A |
| N/A   66C    P0    N/A /  N/A |     53MiB /   511MiB |     N/A      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Compute processes:                                               GPU Memory |
|  GPU       PID  Process name                                     Usage      |
|=============================================================================|
|    0            Not Supported                                               |
+-----------------------------------------------------------------------------+

And code don't throw any errors;) THX ALL:))

user3490530
  • 53
  • 1
  • 7