Login Form



Welcome, Guest
Please Login or Register.    Lost Password?
Forum is locked
How to use local variable as an argument in kernel Forum is locked
Go to bottomPage: 1
TOPIC: How to use local variable as an argument in kernel
***
#430
How to use local variable as an argument in kernel 11 Months, 2 Weeks ago Karma: 0
I just want to use local variable as an kernel argument. I have written the kernel like:
__kernel myfunc(__local uchar *data){ ... }

and in the host, I did this:
byte [] test1 = new byte[100];
CLCalc.Program.Variable cltest1;
cltest1 = new CLCalc.Program.Variable(test1);

myfuncKernel.Execute(new CLCalc.Program.Variable[]{cltest1},1)

This will finally give me an exception, saying "InvalidArgValue".

If I change the argument in the kernel to be "__global", it will work.

So I am wondering if I set the argument of a kernel as "__local", what should I do in the host code?
Jing
Fresh Boarder
Posts: 12
graphgraph
User Offline Click here to see the profile of this user
The topic has been locked.
 
#431
Re:How to use local variable as an argument in kernel 11 Months, 2 Weeks ago Karma: 1

I just want to use local variable as an kernel argument. I have written the kernel like:
__kernel myfunc(__local uchar *data){ ... }

(...)

If I change the argument in the kernel to be "__global", it will work.

So I am wondering if I set the argument of a kernel as "__local", what should I do in the host code?


Hello;

Short answer: you can't.

This is because of the OpenCL memory model: the host code can ONLY communicate with the GLOBAL memory of the device. The __local memory is internal to the GPU. It is located inside what we call "workgroups" in OpenCL and that means only workitems inside the same workgroup can access __local variables to that group.

If you are going to reuse data and you want better performance, you need to copy the __global into __local INSIDE the kernel.




As further reading, I suggest:

Topic about memory model:
www.cmsoft.com.br/index.php?option=com_c...id=67&Itemid=114

Matrix multiplication case study. This shows how to go about copying __global to __local.
www.cmsoft.com.br/index.php?option=com_c...id=94&Itemid=145

Regards
Douglas
Admin
Posts: 154
graph
User Offline Click here to see the profile of this user
Gender: Male Location: Congonhas - Brasil Birthday: 09/25
The topic has been locked.
 
#432
Re:How to use local variable as an argument in kernel 11 Months, 2 Weeks ago Karma: 0
Thanks. This makes sense to me. But one thing I don't understand is that since kernel is meant to be called from host and the host can not access local memory of GPU directly, why local variable is allowed to be kernel argument? Thanks for your explanation.
Jing
Fresh Boarder
Posts: 12
graphgraph
User Offline Click here to see the profile of this user
The topic has been locked.
 
#433
Re:How to use local variable as an argument in kernel 11 Months, 2 Weeks ago Karma: 1
Well, I agree that it doesn't seem to make much of a sense at first, but kernel functions are just functions that can have a pointer from the Host code. In other words, a __kernel can be called from inside the OpenCL code just like any other function.

Consider this: you create a OpenCL framework which requires the user to implement functions F and G. If either F or G are missing, the overall code won't compile, agree?

You can compile only the code supplied by the user (which your framework will call) to see if their implementation has mistakes. By using __kernel F and __kernel G, you can check whether F and G are implemented or not. Then AFTER that you may go on to the (slower) compilation of the entire framework with F and G embedded.

I know this is far-fetched in many ways, but my point is that since __kernel can be used just like any function there is a chance, however remote, that this could be useful.

For example, this code compiles without warning (note that there's even a variable local to the function itself):

Code:


__kernel void F(float x, __local float * y)
{
   y[0] = x;
}

__kernel void myKernel1(__global float * z)
{
   float x = 1.0f;
   __local float y[5];
   F(x, y);
}




Does that answer your question?
Douglas
Admin
Posts: 154
graph
User Offline Click here to see the profile of this user
Gender: Male Location: Congonhas - Brasil Birthday: 09/25
The topic has been locked.
 
#434
Re:How to use local variable as an argument in kernel 11 Months, 2 Weeks ago Karma: 0
hmm, I get it. Thanks for your explanation.
Jing
Fresh Boarder
Posts: 12
graphgraph
User Offline Click here to see the profile of this user
The topic has been locked.
 
Go to topPage: 1
 
 
Copyright © 2013 CMSoft. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Design by handy online shop & windows 7 forum