New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Grand Central Dispatch Programming


Post Reply New Thread Subscribe

 
Thread Tools
Hema S

 
Member Since: Aug 10, 2010
Posts: 1
Hema S is on a distinguished road

Hema S is offline
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
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« How do you back out MacOSX10.6.sdk | having problems with xcode build »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Mackbook or MBP for programming smilemoveon Apple Notebooks 5 02-13-2008 08:55 AM
Java Programming at School pr0wn3r Web Design and Hosting 5 11-25-2007 10:35 PM
Mac Programming frewegooh Switcher Hangout 20 05-01-2007 02:32 PM
Need advice on programming. HareshKainth OS X - Development and Darwin 3 10-09-2005 02:33 PM
Robotic NQC Programming ApplejustWorks Schweb's Lounge 0 01-31-2005 06:28 AM

All times are GMT -4. The time now is 07:32 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?