-2

I have just installed CUDA 5.5 on my notebook and trying out using NVCC to compile a basic hello world program from this link http://computer-graphics.se/hello-world-for-cuda.html

The code I'm trying out is this:

// This is the REAL "hello world" for CUDA!
// It takes the string "Hello ", prints it, then passes it to CUDA with an array
// of offsets. Then the offsets are added in parallel to produce the string "World!"
// By Ingemar Ragnemalm 2010

#include <stdio.h>

const int N = 16; 
const int blocksize = 16; 

__global__ 
void hello(char *a, int *b) 
{
    a[threadIdx.x] += b[threadIdx.x];
}

int main()
{
    char a[N] = "Hello \0\0\0\0\0\0";
    int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    char *ad;
    int *bd;
    const int csize = N*sizeof(char);
    const int isize = N*sizeof(int);

    printf("%s", a);

    cudaMalloc( (void**)&ad, csize ); 
    cudaMalloc( (void**)&bd, isize ); 
    cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ); 
    cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ); 

    dim3 dimBlock( blocksize, 1 );
    dim3 dimGrid( 1, 1 );
    hello<<<dimGrid, dimBlock>>>(ad, bd);
    cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ); 
    cudaFree( ad );
    cudaFree( bd );

    printf("%s\n", a);
    return EXIT_SUCCESS;
}

It is supposed to print out "Hello world!", but after I compiled using "nvcc hello.cu -o a.out", my output is "Hello Hello", can someone tell me what is going on?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
mib1413456
  • 337
  • 1
  • 3
  • 16
  • 5
    This code doesn't use [proper cuda error checking](http://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda-runtime-api) and so it is not a very good choice for a beginner who may have an improperly set up system. I would suggest either adding the error checking, or else run your code with `cuda-memcheck` and you will likely get an idea of what is wrong with your setup. If you installed CUDA, you should also have followed the verification steps in the getting started guide installed with your CUDA install in the `/usr/local/cuda/doc` directory. – Robert Crovella Dec 05 '15 at 15:50
  • 3
    Code that ignores return values is broken and worthless. It's impossible to tell what state your program is in. – Kerrek SB Dec 05 '15 at 15:51
  • 3
    For what it is worth, the above program builds and runs successfully for me, so the most likely cause of the behavior observed is a faulty CUDA installation, causing CUDA to fail at initialization. Since API calls are not checked, this goes unnoticed. I would suggest to follow the steps outlined by Robert Crovella. – njuffa Dec 05 '15 at 17:27
  • Oh I just realised it was because I did not install the NVIDIA drivers properly. I have reinstalled everything and it is working well now. – mib1413456 Dec 11 '15 at 06:37

1 Answers1

1

This was caused by a broken CUDA driver installation. A corrected installation allowed what was otherwise correct code to run without error.

[This community wiki entry was assembled from comments to get this question off the unanswered queue]

talonmies
  • 70,661
  • 34
  • 192
  • 269