Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 - First, I want to know the validity of this information: If the number of threads increases, the speedup increases. 2 - If this

1- First, I want to know the validity of this information: If the number of threads increases, the speedup increases.
2- If this information is correct, my code should work correctly when I increase the number of threads in the terminal. I was trying to increase the number of n, but it did not work.
(what is the problem and how can I correct it? Can you provide me with the code after modification?)
#include
#include
#include
#include
#include
using namespace std;
void Parallel_sort(vector& X, double& parallel_execution_time)
{
unsigned int i, j, count, N = X.size();
vector tmp(N);
double start_time = omp_get_wtime();
#pragma omp parallel for private(i, j, count) shared(X, tmp)
for (i =0; i < N; i++)
{
count =0;
for (j =0; j < N; j++)
if (X[j]< X[i]||(X[j]== X[i] && j < i))
count++;
tmp[count]= X[i];
}
double end_time = omp_get_wtime();
parallel_execution_time = end_time - start_time;
copy(tmp.begin(), tmp.end(), X.begin());
}
void Serial_sort(vector& X, double& serial_execution_time)
{
double start_time = omp_get_wtime();
sort(X.begin(), X.end());
double end_time = omp_get_wtime();
serial_execution_time = end_time - start_time;
}
int main(int argc, char* argv[])
{
//if (argc !=2){
//cout << "Usage: "<< argv[0]<<""<< endl;
//return 1;
//}
int num_threads = atoi(argv[1]);
omp_set_num_threads(num_threads);
vector num ={5,2,8,1,8};
double parallel_execution_time =0.0;
Parallel_sort(num, parallel_execution_time);
double serial_execution_time =0.0;
Serial_sort(num, serial_execution_time);
cout << "Parallel Execution Time: "<< parallel_execution_time <<" seconds" << endl;
cout << "Serial Execution Time: "<< serial_execution_time <<" seconds" << endl;
double speedup = serial_execution_time / parallel_execution_time;
cout << "Speedup: "<< speedup << endl;
double efficiency = speedup / num_threads;
cout << "Efficiency: "<< efficiency << endl;
for (const auto& n : num){
cout << n <<"";
}
cout << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions