Results 1 to 2 of 2

Thread: Newbie question about lock step

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    1

    Newbie question about lock step

    Hello, I'm beginning programming with openCL. I'm using it on my CPU because at this time I don't have a compatible GPU Card.

    I think that my question should be easy to solve.

    Suppose I have an array of 4 integers [0, 2, 0, 0] and a kernel function like this:

    Code :
    __kernel void 
    tst(__global int *s,
    	__global int *answer)
    {
    	int gid = get_global_id(0);
    	if(s[gid] == 0 && s[gid - 1] > 0) {
    		s[gid] = s[gid - 1];
    	}
    	answer[gid] = s[gid];
    }

    What I'm expecting is that the 4 elements will be evaluated in parallel and the result will be [0, 2, 2, 0]. Instead I get [0, 2, 2, 2], as if elements were processed sequentially.

    What I'm doing wrong in your opinion?

    Many thanks for your help

  2. #2
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    Re: Newbie question about lock step

    The CPU will, of course, process elements serially since it's not a parallel processor. (If you have multiple cores some of them will be processed in parallel, but you'll never know which ones.)

    It looks like your program is relying on some ordering of execution and memory accesses, that is, synchronization between work-items. (I.e., thread 2 depends on whether thread 1 executed before or after it.) Such synchronization is only permitted in OpenCL between members of a single work-group (so you can't do it across all work-items) and you must use explicit barrier calls to enforce it.

    Basically your code has a data race whereby the result will be different depending on which work-items are executed in which order, and the only control over this you can get in OpenCL is explicit synchronization between work-items in a single work-group. If you re-wrote your code under any threading model you'd see similarly unpredictable results.

Similar Threads

  1. newbie question about OpenGL
    By xargon in forum OpenCL
    Replies: 2
    Last Post: 05-15-2012, 02:49 AM
  2. Newbie question
    By lkeene in forum OpenCL
    Replies: 2
    Last Post: 07-21-2009, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •