Results 1 to 2 of 2
  1. #1
    Grand Central Dispatch Programming

    Member Since
    Aug 10, 2010
    Posts
    1
    Thumbs up Grand Central Dispatch Programming
    Hi Guys,

    I have a question on using Grand Central Dispatch.

    I am using GCD to parallelize a for loop. However, I am summing up the result from each iteration of the for loop. The summing up portion is the critical section. I can succesfully parallelize the for loop using dispatch_apply. But, I am unable to figure out how to correctly implement the critical section. Below is the baseline code and GCD based code. Can you help?

    Base code:

    for (i=0;i<= num_steps; i++)
    {
    x = (i+0.5)*step;
    sum = sum + 4.0/(1.0+x*x);
    }
    pi = step * sum;
    printf("The value of pi is %f\n", pi);

    Code written Using GCD:

    dispatch_queue_t theQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply (num_steps, theQueue, ^ (size_t i)
    {
    double x, pi, sum = 0.0;
    step = 1.0/(double) num_steps;
    x = (i+0.5)*step;
    dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT,0),
    ^{
    double sum;
    sum = sum + 4.0/(1.0+x*x);

    });
    pi = step * sum;
    printf("The value of pi is %f\n", pi);
    });

    Thanks a bunch.

    Regards,
    Hema

  2. #2
    Grand Central Dispatch Programming
    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    You had several problems with the code you posted, but I'm only going to talk about why you had trouble with sum.

    First, the sum variable has to be placed outside the dispatch and declared with the __block keyword. This allows altering that variable from within the block. Also, given its new placement, you can now use it to get pi after the threading is complete.

    Second, using the sum variable for an assignment to another variable before all threads have completed will likely return an invalid number because it hasn't had the final result assigned to it yet. To avoid that, look at the group functions I used.

    Third, notice the new sumQueue. I used that because some other info I found suggested that it was good practice in this case, to not use the default queue. I quick check showed the default queue also works. So I'm not sure what is best.


    Code:
    void doFixed()
    {
        NSLog(@"doFixed");
    	
    	NSInteger num_steps = 314;
    	
    	dispatch_queue_t theQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    	dispatch_queue_t sumQueue = dispatch_queue_create("com.example.sum", NULL);
    	dispatch_group_t asyncGroup = dispatch_group_create();
    	
    	__block double sum = 0.0;
    	double step = 1.0/(double) num_steps;
    	double pi;
    			
    	dispatch_apply (num_steps, theQueue, ^ (size_t i) 
    	{
    		double x;
    		x = (i+0.5)*step;
    		dispatch_group_async( asyncGroup, sumQueue,
    					   ^{
    					   sum = sum + 4.0/(1.0+x*x);	
    					   });
    
    	});
    	dispatch_group_wait(asyncGroup, DISPATCH_TIME_FOREVER);
    	dispatch_release(sumQueue);
    
    	pi = step * sum;
    	printf("The value of pi is %f\n", pi);
    }
    By the way, when you post code, use the CODE wrapping tags, like I did. It makes it easier for people to read.
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Central email with three users - Possible?
    By Red Eye in forum Switcher Hangout
    Replies: 1
    Last Post: 01-05-2012, 08:54 AM
  2. Apple store at Grand Central Station, NYC
    By proscene in forum iPhone Hardware and Accessories
    Replies: 0
    Last Post: 12-18-2011, 06:35 PM
  3. Maxtor Central Axis
    By billkriv in forum OS X - Operating System
    Replies: 0
    Last Post: 02-28-2010, 08:31 PM
  4. Frozen Grand Central
    By Brandon in forum Schweb's Lounge
    Replies: 9
    Last Post: 02-11-2008, 02:40 AM
  5. Newbie from central IL
    By josh3111 in forum Schweb's Lounge
    Replies: 11
    Last Post: 04-11-2007, 11:44 PM

Posting Permissions

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