Ebeworld’s Weblog

Trying to create

Combinable containers

Preventing from excessive use of locks, uses combinable containers, such as following

        vector<int> values(10);
	int i = 0;
	generate(values.begin(), values.end(), [&]{return ++i;} );

	// 1. Calculate the sum of the elements of the vector in parallel.
	combinable<int> sums;
	parallel_for_each(values.begin(),values.end(), [&](int n)
	{
		sums.local() += n;}
	);

	// 2. Combine/Reduce into final result
	int result = sums.combine([](int left, int right) { return left + right; });

Advertisement

March 23, 2011 - Posted by | Algorithms, CPP, Uncategorized

1 Comment »


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.