This topic has not yet been rated - Rate this topic

# How to: Use Parallel Containers to Increase Efficiency

Visual Studio 2013

This topic shows how to use parallel containers to efficiently store and access data in parallel.

The example code computes the set of prime and Carmichael numbers in parallel. Then, for each Carmichael number, the code computes the prime factors of that number.

The following example shows the is_prime function, which determines whether an input value is a prime number, and the is_carmichael function, which determines whether the input value is a Carmichael number.

```// Determines whether the input value is prime.
bool is_prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i < n; ++i)
{
if ((n % i) == 0)
return false;
}
return true;
}

// Determines whether the input value is a Carmichael number.
bool is_carmichael(const int n)
{
if (n < 2)
return false;

int k = n;
for (int i = 2; i <= k / i; ++i)
{
if (k % i == 0)
{
if ((k / i) % i == 0)
return false;
if ((n - 1) % (i - 1) != 0)
return false;
k /= i;
i = 1;
}
}
return k != n && (n - 1) % (k - 1) == 0;
}
```

The following example uses the is_prime and is_carmichael functions to compute the sets of prime and Carmichael numbers. The example uses the concurrency::parallel_invoke and concurrency::parallel_for algorithms to compute each set in parallel. For more information about parallel algorithms, see Parallel Algorithms.

This example uses a concurrency::concurrent_queue object to hold the set of Carmichael numbers because it will later use that object as a work queue. It uses a concurrency::concurrent_vector object to hold the set of prime numbers because it will later iterate through this set to find prime factors.

```// The maximum number to test.
const int max = 10000000;

// Holds the Carmichael numbers that are in the range [0, max).
concurrent_queue<int> carmichaels;

// Holds the prime numbers that are in the range  [0, sqrt(max)).
concurrent_vector<int> primes;

// Generate the set of Carmichael numbers and the set of prime numbers
// in parallel.
parallel_invoke(
[&] {
parallel_for(0, max, [&](int i) {
if (is_carmichael(i)) {
carmichaels.push(i);
}
});
},
[&] {
parallel_for(0, int(sqrt(static_cast<double>(max))), [&](int i) {
if (is_prime(i)) {
primes.push_back(i);
}
});
});
```

The following example shows the prime_factors_of function, which uses trial division to find all prime factors of the given value.

This function uses the concurrency::parallel_for_each algorithm to iterate through the collection of prime numbers. The concurrent_vector object enables the parallel loop to concurrently add prime factors to the result.

```// Finds all prime factors of the given value.
concurrent_vector<int> prime_factors_of(int n,
const concurrent_vector<int>& primes)
{
// Holds the prime factors of n.
concurrent_vector<int> prime_factors;

// Use trial division to find the prime factors of n.
// Every prime number that divides evenly into n is a prime factor of n.
const int max = sqrt(static_cast<double>(n));
parallel_for_each(begin(primes), end(primes), [&](int prime)
{
if (prime <= max)
{
if ((n % prime) == 0)
prime_factors.push_back(prime);
}
});

return prime_factors;
}
```

This example processes each element in the queue of Carmichael numbers by calling the prime_factors_of function to compute its prime factors. It uses a task group to perform this work in parallel. For more information about task groups, see Task Parallelism (Concurrency Runtime).

This example prints the prime factors for each Carmichael number if that number has more than four prime factors.

```// Use a task group to compute the prime factors of each
// Carmichael number in parallel.

int carmichael;
while (carmichaels.try_pop(carmichael))
{
{
// Compute the prime factors.
auto prime_factors = prime_factors_of(carmichael, primes);

// For brevity, print the prime factors for the current number only
// if there are more than 4.
if (prime_factors.size() > 4)
{
// Sort and then print the prime factors.
sort(begin(prime_factors), end(prime_factors));

wstringstream ss;
ss << L"Prime factors of " << carmichael << L" are:";

for_each (begin(prime_factors), end(prime_factors),
[&](int prime_factor) { ss << L' ' << prime_factor; });
ss << L'.' << endl;

wcout << ss.str();
}
});
}

// Wait for the task group to finish.
```

The following code shows the complete example, which uses parallel containers to compute the prime factors of the Carmichael numbers.

```// carmichael-primes.cpp
// compile with: /EHsc
#include <ppl.h>
#include <concurrent_queue.h>
#include <concurrent_vector.h>
#include <iostream>
#include <sstream>

using namespace concurrency;
using namespace std;

// Determines whether the input value is prime.
bool is_prime(int n)
{
if (n < 2)
return false;
for (int i = 2; i < n; ++i)
{
if ((n % i) == 0)
return false;
}
return true;
}

// Determines whether the input value is a Carmichael number.
bool is_carmichael(const int n)
{
if (n < 2)
return false;

int k = n;
for (int i = 2; i <= k / i; ++i)
{
if (k % i == 0)
{
if ((k / i) % i == 0)
return false;
if ((n - 1) % (i - 1) != 0)
return false;
k /= i;
i = 1;
}
}
return k != n && (n - 1) % (k - 1) == 0;
}

// Finds all prime factors of the given value.
concurrent_vector<int> prime_factors_of(int n,
const concurrent_vector<int>& primes)
{
// Holds the prime factors of n.
concurrent_vector<int> prime_factors;

// Use trial division to find the prime factors of n.
// Every prime number that divides evenly into n is a prime factor of n.
const int max = sqrt(static_cast<double>(n));
parallel_for_each(begin(primes), end(primes), [&](int prime)
{
if (prime <= max)
{
if ((n % prime) == 0)
prime_factors.push_back(prime);
}
});

return prime_factors;
}

int wmain()
{
// The maximum number to test.
const int max = 10000000;

// Holds the Carmichael numbers that are in the range [0, max).
concurrent_queue<int> carmichaels;

// Holds the prime numbers that are in the range  [0, sqrt(max)).
concurrent_vector<int> primes;

// Generate the set of Carmichael numbers and the set of prime numbers
// in parallel.
parallel_invoke(
[&] {
parallel_for(0, max, [&](int i) {
if (is_carmichael(i)) {
carmichaels.push(i);
}
});
},
[&] {
parallel_for(0, int(sqrt(static_cast<double>(max))), [&](int i) {
if (is_prime(i)) {
primes.push_back(i);
}
});
});

// Use a task group to compute the prime factors of each
// Carmichael number in parallel.

int carmichael;
while (carmichaels.try_pop(carmichael))
{
{
// Compute the prime factors.
auto prime_factors = prime_factors_of(carmichael, primes);

// For brevity, print the prime factors for the current number only
// if there are more than 4.
if (prime_factors.size() > 4)
{
// Sort and then print the prime factors.
sort(begin(prime_factors), end(prime_factors));

wstringstream ss;
ss << L"Prime factors of " << carmichael << L" are:";

for_each (begin(prime_factors), end(prime_factors),
[&](int prime_factor) { ss << L' ' << prime_factor; });
ss << L'.' << endl;

wcout << ss.str();
}
});
}

// Wait for the task group to finish.
}
```

This example produces the following sample output.

```Prime factors of 9890881 are: 7 11 13 41 241.
Prime factors of 825265 are: 5 7 17 19 73.
Prime factors of 1050985 are: 5 13 19 23 37.```

Copy the example code and paste it in a Visual Studio project, or paste it in a file that is named carmichael-primes.cpp and then run the following command in a Visual Studio Command Prompt window.

cl.exe /EHsc carmichael-primes.cpp