Я пытаюсь заставить Hanoi Towers работать с OpenMP, но я чувствую, что это не работает так, что задачи разделены в каждом потоке, чтобы ускорить процесс, я впервые работаю с потоками .. Это то, что я делал
# include <iostream>
# include <chrono>
using namespace std;
int cont_times(0);
void hanoi_parallel(const int cant_disk, const char & first, const char & aux, const char & finish)
{
//have this counter just to know how many times execute this
cont_times++;
if (cant_disk == 1){
cout << "Move disk " << cant_disk << " from " << first << " to " << finish << endl;
return;
}
# pragma omp parallel
{
# pragma omp single
{
hanoi(cant_disk - 1, first, finish, aux);
}
cout << "Move disk " << cant_disk << " from " << first << " to " << finish << endl;
# pragma omp single
{
hanoi(cant_disk-1, aux, first, finish);
}
}
}
int main()
{
int cant_disc(11);
auto init_time2 = chrono::steady_clock::now();
hanoi_parallel(cant_disc, 'A', 'B', 'C');
auto finish_time2 = chrono::steady_clock::now();
cout << cont_times << endl;
cout << "Duration time parallel: " << chrono::duration_cast<chrono::nanoseconds>(finish_time2 - init_time2 ).count() << endl;
return 0;
}
На самом деле я пытался использовать: # pragma omp critical
вместо # pragma omp single
, но это делает переменную "count_times" значением ((2 ^ 11) -1) * cant_thread. Я пытался изменить, где я вызываю OMP, но у меня та же проблема с примером "count_times":
void hanoi(const int cant_disk, const char & first, const char & aux, const char & finish)
{
cont_times++;
if (cant_disk == 1){
cout << "Move disk " << cant_disk << " from " << first << " to " << finish << endl;
return;
}
hanoi(cant_disk - 1, first, finish, aux);
cout << "Move disk " << cant_disk << " from " << first << " to " << finish << endl;
hanoi(cant_disk-1, aux, first, finish);
int main()
{
int cant_disc(11);
auto init_time2 = chrono::steady_clock::now();
# pragma omp parallel
{
hanoi_parallel(cant_disc, 'A', 'B', 'C');
}
auto finish_time2 = chrono::steady_clock::now();
cout << cont_times << endl;
cout << "Duration time parallel: " << chrono::duration_cast<chrono::nanoseconds>(finish_time2 - init_time2 ).count() << endl;
return 0;
}
Любые предложения о том, что я могу сделать