mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Hardware > GPU Computing

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

3×1,601 Posts
Default Help from coders

I have this snippet of code:

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

Code:
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?

Thanks

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

3810 Posts
Default

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
Code:
__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
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

19·499 Posts
Default

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

and

__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 offline   Reply With Quote
Old 2014-01-25, 21:33   #4
TheJudger
 
TheJudger's Avatar
 
"Oliver"
Mar 2005
Germany

11×101 Posts
Default

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.

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

2×3×19×31 Posts
Default

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_
Banned
 
ET_'s Avatar
 
"Luigi"
Aug 2002
Team Italia

3·1,601 Posts
Default

@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!

Luigi

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
Reply

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 06:27.

Sun Mar 7 06:27:52 UTC 2021 up 94 days, 2:39, 0 users, load averages: 2.33, 2.55, 2.21

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.