Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hello world doesn't actually say hello world #3

Open
xdavidliu opened this issue Aug 30, 2019 · 8 comments
Open

hello world doesn't actually say hello world #3

xdavidliu opened this issue Aug 30, 2019 · 8 comments

Comments

@xdavidliu
Copy link

Why does the hello world example do nothing? This fact is mentioned in the tutorial, but no further explanation is given. This is extremely confusing; what is the point of even writing that code if it doesn't work?

@puttsk
Copy link
Owner

puttsk commented Aug 30, 2019

Thanks for the comment.

The purpose of the hello world part was to quickly introduce the term "kernel" and how to compile CUDA program to the reader without introducing too much information. Right now, that is the smallest code I could think of. Please suggest if you have some ideas for the example.

BTW, the code is actually work. After additional investigation, I found that the program exit before the GPU could send the printf message back. To avoid such problem, you could use the following code.

#include <stdio.h>
#include <unistd.h>

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int main() {
    cuda_hello<<<1,1>>>(in);
    sleep(10);
    return 0;
}

@LucaPaterlini
Copy link

hello, I am getting hello.cu(9): error: identifier "in" is undefined

hello.cu(9): error: too many arguments in function call

2 errors detected in the compilation of "/tmp/tmpxft_00001b05_00000000-8_hello.cpp1.ii".

are you sure in is a good parameter to pass?
have you tested the code you shared?

@puttsk
Copy link
Owner

puttsk commented Nov 4, 2019

Sorry, there is a typo in the code. There should not be in in the kernel launch.

The correct code is

#include <stdio.h>
#include <unistd.h>

__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int main() {
    cuda_hello<<<1,1>>>();
    sleep(10);
    return 0;
}

@127
Copy link

127 commented Mar 25, 2022

by the way fails to output anyway

@puttsk
Copy link
Owner

puttsk commented Mar 25, 2022

At this point, the code won't show any output. I mentioned that in Putting things in actions Section. The purpose of the hello world code is for comparing it with C counterpart, which should be common for anyone who starts learning C code.

Please suggest if you have a better example. I could update the code if it makes more sense.

@SpacelySpaceSprockets
Copy link

SpacelySpaceSprockets commented Apr 3, 2022

I think technically the correct way to get the print function to work is to insert a call to cudaDeviceSynchronize() after the function call.

int main(){
    cuda_hello<<<1,1>>>();
    cudaDeviceSynchronize();
    return 0;
}

It's worth updating in the tutorial since it appears there are a lot of people posting this problem/question on various message boards.

@ryao
Copy link

ryao commented May 1, 2022

@SpacelySpaceSprockets Nvidia has its own slightly more complex Hello World that does exactly that:

#include <stdio.h>

__global__ void helloCUDA(float f)
{
    printf("Hello thread %d, f=%f\n", threadIdx.x, f);
}

int main()
{
    helloCUDA<<<1, 5>>>(1.2345f);
    cudaDeviceSynchronize();
    return 0;
}

https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#examples

As @puttsk pointed out, this is needed because the GPU and CPU run asynchronously. The CPU kills the program before the CUDA runtime has executed the printf() from the GPU.

@ColoMAX ColoMAX mentioned this issue Jun 24, 2023
@wzm2256
Copy link

wzm2256 commented Oct 17, 2023

Also, #include <stdio.h> is needed in the first line. Please add this line or make it clear that the code is not a complete example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants