fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <omp.h>
  5. #include <ctime>
  6. #include <chrono>
  7. using namespace std;
  8.  
  9. // Function to generate a random array of given size
  10. vector<int> generateRandomArray(int size) {
  11. vector<int> arr(size);
  12. for (int i = 0; i < size; ++i) {
  13. arr[i] = rand() % 100; // Generate random numbers between 0 and 99
  14. }
  15. return arr;
  16. }
  17. // Function to find the minimum value in an array using parallel reduction
  18. int parallelMin(const std::vector<int>& arr) {
  19. int min_val = arr[0];
  20. #pragma omp parallel for reduction(min:min_val)
  21.  
  22. for (int i = 1; i < arr.size(); ++i) {
  23. if (arr[i] < min_val) {
  24. min_val = arr[i];
  25. }
  26. }
  27. return min_val;
  28. }
  29. int sequentialMin(const std::vector<int>& arr) {
  30. int min_val = arr[0];
  31.  
  32. for (int i = 1; i < arr.size(); ++i) {
  33. if (arr[i] < min_val) {
  34. min_val = arr[i];
  35. }
  36. }
  37. return min_val;
  38. }
  39.  
  40. // Function to find the maximum value in an array using parallel reduction
  41. int parallelMax(const std::vector<int>& arr) {
  42. int max_val = arr[0];
  43. #pragma omp parallel for reduction(max:max_val)
  44. for (int i = 1; i < arr.size(); ++i) {
  45. if (arr[i] > max_val) {
  46. max_val = arr[i];
  47. }
  48. }
  49. return max_val;
  50. }
  51. int sequentialMax(const std::vector<int>& arr) {
  52. int max_val = arr[0];
  53.  
  54. for (int i = 1; i < arr.size(); ++i) {
  55. if (arr[i] > max_val) {
  56. max_val = arr[i];
  57. }
  58. }
  59. return max_val;
  60. }
  61. // Function to compute the sum of elements in an array using parallel reduction
  62. int parallelSum(const std::vector<int>& arr) {
  63. int sum = 0;
  64. #pragma omp parallel for reduction(+:sum)
  65. for (int i = 0; i < arr.size(); ++i) {
  66. sum += arr[i];
  67. }
  68. return sum;
  69. }
  70. int sequentialSum(const std::vector<int>& arr) {
  71. int sum = 0;
  72.  
  73. for (int i = 0; i < arr.size(); ++i) {
  74. sum += arr[i];
  75. }
  76. return sum;
  77. }
  78.  
  79. // Function to compute the average of elements in an array using parallel reduction
  80. double parallelAverage(const std::vector<int>& arr) {
  81. int sum = parallelSum(arr);
  82. return static_cast<double>(sum) / arr.size();
  83. }
  84. double sequentialAverage(const std::vector<int>& arr) {
  85. int sum = sequentialSum(arr);
  86. return static_cast<double>(sum) / arr.size();
  87. }
  88.  
  89.  
  90. int main() {
  91. int size;
  92. cout << "Enter the number of elements in the array: ";
  93. cin >> size;
  94.  
  95. // Generate a random array
  96. vector<int> arr = generateRandomArray(size);
  97. cout<<"Input array is : ";
  98. for(int i=0;i<arr.size();i++)
  99. {
  100. cout<<arr[i]<<" ";
  101. }
  102. cout<<"\n";
  103. // Finding minimum and maximum using parallel reduction
  104. auto start_time =chrono::high_resolution_clock::now();
  105. int min_val = parallelMin(arr);
  106. auto end_time = chrono::high_resolution_clock::now();
  107. chrono::duration<double> duration = end_time - start_time;
  108. cout << "Time taken by parallel min value function: " << duration.count() << " seconds" << endl;
  109.  
  110.  
  111. start_time = chrono::high_resolution_clock::now();
  112. int max_val = parallelMax(arr);
  113. end_time = chrono::high_resolution_clock::now();
  114. duration = end_time - start_time;
  115. cout << "Time taken by parallel max value function: " << duration.count() << " seconds" << endl;
  116.  
  117.  
  118.  
  119. start_time = chrono::high_resolution_clock::now();
  120. int sum = parallelSum(arr);
  121. end_time = chrono::high_resolution_clock::now();
  122. duration = end_time - start_time;
  123. cout << "Time taken by parallelsun function: " << duration.count() << " seconds" << endl;
  124.  
  125.  
  126. start_time = chrono::high_resolution_clock::now();
  127. double average = parallelAverage(arr);
  128. end_time = chrono::high_resolution_clock::now();
  129. duration = end_time - start_time;
  130. cout << "Time taken by parallel average functions: " << duration.count() << " seconds" << endl;
  131.  
  132.  
  133. cout << "parallel Minimum value: " << min_val << endl;
  134. cout << "parallel Maximum value: " << max_val << endl;
  135. cout << "parallel Sum: " << sum << endl;
  136. cout << "parallel Average: " << average << endl;
  137.  
  138. start_time = chrono::high_resolution_clock::now();
  139. int sequential_min_val = sequentialMin(arr);
  140. end_time = chrono::high_resolution_clock::now();
  141. duration = end_time - start_time;
  142. cout << "Time taken by sequential functions: " << duration.count() << " seconds" << endl;
  143.  
  144.  
  145. start_time = chrono::high_resolution_clock::now();
  146. int sequential_max_val = sequentialMax(arr);
  147. end_time = chrono::high_resolution_clock::now();
  148. duration = end_time - start_time;
  149. cout << "Time taken by sequential functions: " << duration.count() << " seconds" << endl;
  150.  
  151.  
  152. start_time = chrono::high_resolution_clock::now();
  153. int sequential_sum = sequentialSum(arr);
  154. end_time = chrono::high_resolution_clock::now();
  155. duration = end_time - start_time;
  156. cout << "Time taken by sequential functions: " << duration.count() << " seconds" << endl;
  157.  
  158.  
  159. start_time = chrono::high_resolution_clock::now();
  160. double sequential_average = sequentialAverage(arr);
  161. end_time = chrono::high_resolution_clock::now();
  162. duration = end_time - start_time;
  163. cout << "Time taken by sequential functions: " << duration.count() << " seconds" << endl;
  164.  
  165.  
  166. cout << "sequential Minimum value: " << sequential_min_val << endl;
  167. cout << "sequential Maximum value: " << sequential_max_val << endl;
  168. cout << "sequential Sum: " << sequential_sum << endl;
  169. cout << "sequential Average: " << sequential_average << endl;
  170.  
  171. return 0;
  172. }
  173. // g++ -fopenmp Sorting.cpp -o Sorting
Success #stdin #stdout 0.01s 5280KB
stdin
10
stdout
Enter the number of elements in the array: Input array is : 83 86 77 15 93 35 86 92 49 21 
Time taken by parallel min value function: 2.06e-07 seconds
Time taken by parallel max value function: 1.22e-07 seconds
Time taken by parallelsun function: 9.9e-08 seconds
Time taken by parallel average functions: 1.29e-07 seconds
parallel Minimum value: 15
parallel Maximum value: 93
parallel Sum: 637
parallel Average: 63.7
Time taken by sequential functions: 1.2e-07 seconds
Time taken by sequential functions: 1.14e-07 seconds
Time taken by sequential functions: 8.7e-08 seconds
Time taken by sequential functions: 1.08e-07 seconds
sequential Minimum value: 15
sequential Maximum value: 93
sequential Sum: 637
sequential Average: 63.7