Go Back > Great Internet Mersenne Prime Search > Hardware > GPU Computing

Thread Tools
Old 2014-01-25, 20:48   #1
ET_'s Avatar
Aug 2002
Team Italia

3×1,601 Posts
Default Help from coders

I have this snippet of code:

__global__ void stuff(float a)
    a = 9;
called from this function:

void caller(float &a)
    cout >> a;  //actually a has value 3

    stuff<<<blocks, threads>>>(a);

    cout >> a;  // a has still value 9
that doesn't work.

As I am not familiar with C++ parameter passing, I ask: how would the code look like to permit to the __global__ stuff to modify the "a" value in the caller scope?
I tried all thepossible exchanges with "*" and "&" operators, but I can't find a way.

Would you help me?


ET_ is offline   Reply With Quote
Old 2014-01-25, 21:12   #2
Qubit's Avatar
Jan 2014

2×19 Posts

Proper disclosure: I am absolutely unfamiliar with CUDA, but anyway this seems like a C++ question.

It seems as if it should print 3 instead of 9, but the desired output is unclear from your wording.

Anyway, if instead you write
__global__ void stuff(float& a)
it should print 9.

The '&' is the reference operator in C++. Writing "float &a"/"float& a"/"float & a" indicates that you're passing the parameter a by reference.
Without the reference operator, you'd be passing the parameter a by value, which will essentially create a copy of a which would be destroyed upon returning from the function.
Qubit is offline   Reply With Quote
Old 2014-01-25, 21:14   #3
If I May
chalsall's Avatar
"Chris Halsall"
Sep 2002

3×29×109 Posts

Originally Posted by ET_ View Post
Would you help me?
I'll try... Give:
stuff<<<blocks, threads>>>(&a);


__global__ void stuff(float *a)
    *a = 9;
...a try. While your function "stuff" may be defined as "global", your variable "a" was passed in as a scaler to the function.

At least under "C", "&" says "give me the address of this object", while "*" says "give me the object this address points to".

Of course, I may very well be completely wrong.

Last fiddled with by chalsall on 2014-01-25 at 21:26
chalsall is online now   Reply With Quote
Old 2014-01-25, 21:33   #4
TheJudger's Avatar
Mar 2005

21278 Posts

Hello Luigi,

unless my mind is screwed up:
CPU vs. GPU context!
Executing a GPU kernel from CPU context is asynchronous (CPU continues before GPU has finished the kernel), thus never can return values to the caller.
I guess you'll need explicit
  • synchronisation (let the CPU wait for the GPU to finish)
  • data transfers CPU<->GPU
when using the CUDA API.

TheJudger is offline   Reply With Quote
Old 2014-01-26, 02:13   #5
Tribal Bullet
jasonp's Avatar
Oct 2004

2·3·19·31 Posts

Communication from CPU to GPU and back has to go through memory; just passing the address of something to a GPU will not work, because CPU and GPU have separate address spaces. If you want a GPU to write something and then have the CPU see it, then you have to do a memory transfer into the CPU.

Except when using the latest CUDA toolkit, where you can force a range of memory to be identical between CPU and GPU, so that either one automatically can see the latest version of something in that memory range, no matter who wrote it. I believe this only works on linux though.
jasonp is offline   Reply With Quote
Old 2014-01-26, 13:58   #6
ET_'s Avatar
Aug 2002
Team Italia

480310 Posts

@Chalsall: That was my reasoning also, but the __global__ functions on device don't allow reference arguments. That's the reason of my question

@Oliver: It was not a matter of synchronization of threads, your hint didn't work either.

@Jason: Correct. I cudaMemcopied the value to the device, and after some struggling with pointers it worked!

Thanks to you all!


P.S. And yes, I made typo on my forst message . Before che change, the result in main SHOULD be 9, but it was still 3 instead. Sorry for the confusion.

Last fiddled with by ET_ on 2014-01-26 at 14:02
ET_ is offline   Reply With Quote

Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling hotshot coders: NFS code challenge R.D. Silverman Programming 63 2006-10-09 05:48
IA-32 Assembly Coders, anyone? xenon Programming 6 2005-06-02 13:26

All times are UTC. The time now is 22:49.

Mon Mar 8 22:49:04 UTC 2021 up 95 days, 19 hrs, 0 users, load averages: 1.78, 1.88, 1.94

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.