Download cpp source code

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <omp.h>
#include <string>
#include <vector>

void distribute_iterations(int thread_count, std::vector<int>& static_iterations,
	std::vector<int>& dynamic_iterations, std::vector<int>& guided_iterations);
void distribute_iterations(int thread_count, std::vector<int>& static_iterations,
	std::vector<int>& dynamic_iterations, std::vector<int>& guided_iterations, int block_size);
void print_iterations(const char* name, const std::vector<int>& iterations);
void print_header(const size_t iterations);

int main(int argc, char* argv[])
{
	int thread_count = omp_get_max_threads();
	if ( argc >= 2 )
		thread_count = atoi(argv[1]);

	int iteration_count = thread_count;
	if ( argc >= 3 )
		iteration_count = atoi(argv[2]);

	std::vector<int> static_iterations(iteration_count);
	std::vector<int> dynamic_iterations(iteration_count);
	std::vector<int> guided_iterations(iteration_count);

	if ( argc >= 4 )
		distribute_iterations(thread_count, static_iterations, dynamic_iterations, guided_iterations, atoi(argv[3]));
	else
		distribute_iterations(thread_count, static_iterations, dynamic_iterations, guided_iterations);
}

void distribute_iterations(int thread_count, std::vector<int>& static_iterations,
	std::vector<int>& dynamic_iterations, std::vector<int>& guided_iterations)
{
	#pragma omp parallel for num_threads(thread_count) schedule(static) \
		default(none) shared(std::cout, static_iterations)
	for ( size_t iteration = 0; iteration < static_iterations.size(); ++iteration )
		static_iterations[iteration] = omp_get_thread_num();

	#pragma omp parallel for num_threads(thread_count) schedule(dynamic) \
		default(none) shared(std::cout, dynamic_iterations)
	for ( size_t iteration = 0; iteration < dynamic_iterations.size(); ++iteration )
		dynamic_iterations[iteration] = omp_get_thread_num();

	#pragma omp parallel for num_threads(thread_count) schedule(guided) \
		default(none) shared(std::cout, guided_iterations)
	for ( size_t iteration = 0; iteration < guided_iterations.size(); ++iteration )
		guided_iterations[iteration] = omp_get_thread_num();

	print_header(static_iterations.size());
	print_iterations("static    ", static_iterations);
	print_iterations("dynamic   ", dynamic_iterations);
	print_iterations("guided    ", guided_iterations);
}

void distribute_iterations(int thread_count, std::vector<int>& static_iterations,
	std::vector<int>& dynamic_iterations, std::vector<int>& guided_iterations, int block_size)
{
	#pragma omp parallel for num_threads(thread_count) schedule(static, block_size) \
		default(none) shared(std::cout, static_iterations, block_size)
	for ( size_t iteration = 0; iteration < static_iterations.size(); ++iteration )
		static_iterations[iteration] = omp_get_thread_num();

	#pragma omp parallel for num_threads(thread_count) schedule(dynamic, block_size) \
		default(none) shared(std::cout, dynamic_iterations, block_size)
	for ( size_t iteration = 0; iteration < dynamic_iterations.size(); ++iteration )
		dynamic_iterations[iteration] = omp_get_thread_num();

	#pragma omp parallel for num_threads(thread_count) schedule(guided, block_size) \
		default(none) shared(std::cout, guided_iterations, block_size)
	for ( size_t iteration = 0; iteration < guided_iterations.size(); ++iteration )
		guided_iterations[iteration] = omp_get_thread_num();

	print_header(static_iterations.size());
	print_iterations("static,N  ", static_iterations);
	print_iterations("dynamic,N ", dynamic_iterations);
	print_iterations("guided,N  ", guided_iterations);
}

void print_header(const size_t iterations)
{
	const int width = std::to_string(iterations).length();
	std::cout << "          ";
	for ( size_t iteration = 0; iteration < iterations; ++iteration )
		std::cout << std::setw(width) << iteration
			<< (iteration < iterations - 1 ? ' ' : '\n');
}

void print_iterations(const char* name, const std::vector<int>& iterations)
{
	const int width = std::to_string( iterations.size() ).length();
	std::cout << name;
	for ( size_t iteration = 0; iteration < iterations.size(); ++iteration )
		std::cout << std::setw(width) << iterations[iteration]
			<< (iteration < iterations.size() - 1 ? ' ' : '\n');
}