LINUX.ORG.RU

История изменений

Исправление thunar, (текущая версия) :

блин, походу таки я где-то внутри программы напортачил с доступом к общим ресурсам, отсюда и тормоза. сейчас сделал в чистом виде тестовую болванку — и всё норм:

calculating in pthreads (8 threads): 1000: 15s.
calculating in OpenMP (8 threads): 1000: 14s.
calculating in serial: 1000: 111s.
#include <iostream>
#include <iomanip>  
#include <thread>
#include <omp.h>
#include <mutex>
#include <cmath>

using namespace std;
std::mutex _mutex;

float** xss;
const int points =100000;
const int loops = 1000;

int func(int n){
	auto& xs = xss[n];
	for(int i=0; i<points; i++){
		auto& x = xs[i];
		xs[i] = sin(x) * cos(x) - x + sqrt(abs(x));
	}
  return 0;
}

int main ()
{
	const int threads_n = std::thread::hardware_concurrency();
	xss = new float* [threads_n];
	for(int n = 0; n<threads_n; n++){
		xss[n] = new float [points];
		for(int i = 0; i<points; i++){
			xss[n][i] = rand();
		}
	}
	std::thread threads[threads_n];

	while(1){
		{ //pthreads code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in pthreads ("
					<<threads_n<<" threads): "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					threads[n] = std::thread(func, n);
				}
				for (int n = 0; n < threads_n; n++){
					threads[n].join();
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //openmp code
			omp_set_dynamic(0);
			omp_set_num_threads(threads_n);
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in OpenMP: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				#pragma omp parallel for schedule(static)
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //serial code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in serial: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}
	}

}

Исправление thunar, :

блин, походу таки я где-то внутри программы напортачил с доступом к общим ресурсам, отсюда и тормоза. сейчас сделал в чистом виде тестовую болванку — и всё норм:

calculating in pthreads (8 threads): 1000: 15s.
calculating in OpenMP: 1000: 14s.
calculating in serial: 1000: 111s.
#include <iostream>
#include <iomanip>  
#include <thread>
#include <omp.h>
#include <mutex>
#include <cmath>

using namespace std;
std::mutex _mutex;

float** xss;
const int points =100000;
const int loops = 1000;

int func(int n){
	auto& xs = xss[n];
	for(int i=0; i<points; i++){
		auto& x = xs[i];
		xs[i] = sin(x) * cos(x) - x + sqrt(abs(x));
	}
  return 0;
}

int main ()
{
	const int threads_n = std::thread::hardware_concurrency();
	xss = new float* [threads_n];
	for(int n = 0; n<threads_n; n++){
		xss[n] = new float [points];
		for(int i = 0; i<points; i++){
			xss[n][i] = rand();
		}
	}
	std::thread threads[threads_n];

	while(1){
		{ //pthreads code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in pthreads ("
					<<threads_n<<" threads): "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					threads[n] = std::thread(func, n);
				}
				for (int n = 0; n < threads_n; n++){
					threads[n].join();
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //openmp code
			omp_set_dynamic(0);
			omp_set_num_threads(threads_n);
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in OpenMP: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				#pragma omp parallel for schedule(static)
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //serial code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in serial: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}
	}

}

Исходная версия thunar, :

блин, походу таки я где-то внутри напортачил с доступом к общим ресурсам, отсюда и тормоза. сейчас сделал в чистом виде тестовую болванку — и всё норм:

calculating in pthreads (8 threads): 1000: 15s.
calculating in OpenMP: 1000: 14s.
calculating in serial: 1000: 111s.
#include <iostream>
#include <iomanip>  
#include <thread>
#include <omp.h>
#include <mutex>
#include <cmath>

using namespace std;
std::mutex _mutex;

float** xss;
const int points =100000;
const int loops = 1000;

int func(int n){
	auto& xs = xss[n];
	for(int i=0; i<points; i++){
		auto& x = xs[i];
		xs[i] = sin(x) * cos(x) - x + sqrt(abs(x));
	}
  return 0;
}

int main ()
{
	const int threads_n = std::thread::hardware_concurrency();
	xss = new float* [threads_n];
	for(int n = 0; n<threads_n; n++){
		xss[n] = new float [points];
		for(int i = 0; i<points; i++){
			xss[n][i] = rand();
		}
	}
	std::thread threads[threads_n];

	while(1){
		{ //pthreads code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in pthreads ("
					<<threads_n<<" threads): "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					threads[n] = std::thread(func, n);
				}
				for (int n = 0; n < threads_n; n++){
					threads[n].join();
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //openmp code
			omp_set_dynamic(0);
			omp_set_num_threads(threads_n);
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in OpenMP: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				#pragma omp parallel for schedule(static)
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}

		{ //serial code
			const time_t t_start = std::time(NULL);
			for(int t=0; t<loops+1; t++){
				std::cout<<"\rcalculating in serial: "<<setfill('0')<<setw(4)<<t;
				std::cout.flush();
				for (int n = 0; n < threads_n; n++){
					func(n);
				}
			}
			const time_t t_end = std::time(NULL);
			std::cout << ": " << t_end-t_start << "s." << std::endl;
		}
	}

}