fork download
  1. #include <omp.h> // openmp library for parallel processing
  2. #include <stdlib.h> // Standard library for memory allocation and random number generation.
  3.  
  4. #include <array>
  5. #include <chrono> // for working with time, clocks and timers
  6. #include <functional> // predefined functions for working with functions in a more flexible and generic way
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10.  
  11. using std::chrono::duration_cast; // converting between different durations (eg. milisecs to secs)
  12. using std::chrono::high_resolution_clock; // used for high resolution timing measurements
  13. using std::chrono::milliseconds; // representing a period of time in milliseconds.
  14. using namespace std;
  15.  
  16. void p_mergesort(int *a, int i, int j); // sequential merge-sort function declaration
  17. void s_mergesort(int *a, int i, int j); // parallel merge-sort function declaration
  18. void merge(int *a, int i1, int j1, int i2, int j2);
  19.  
  20. void p_mergesort(int *a, int i, int j) {
  21. int mid;
  22. if (i < j) {
  23. if ((j - i) > 1000) {
  24. mid = (i + j) / 2;
  25.  
  26. #pragma omp task firstprivate(a, i, mid)
  27. p_mergesort(a, i, mid);
  28. #pragma omp task firstprivate(a, mid, j)
  29. p_mergesort(a, mid + 1, j);
  30.  
  31. #pragma omp taskwait
  32. merge(a, i, mid, mid + 1, j);
  33. } else {
  34. s_mergesort(a, i, j);
  35. }
  36. }
  37. }
  38.  
  39. // parallel merge-sort
  40. void parallel_mergesort(int *a, int i, int j) {
  41. #pragma omp parallel
  42. {
  43. #pragma omp single
  44. p_mergesort(a, i, j);
  45. }
  46. }
  47.  
  48. // sequential merge-sort
  49. void s_mergesort(int *a, int i, int j) {
  50. int mid;
  51. if (i < j) {
  52. mid = (i + j) / 2;
  53. s_mergesort(a, i, mid);
  54. s_mergesort(a, mid + 1, j);
  55. merge(a, i, mid, mid + 1, j);
  56. }
  57. }
  58.  
  59. // common to both, sequential and parallel merge sort
  60. void merge(int *a, int i1, int j1, int i2, int j2) {
  61. int temp[2000000];
  62. int i, j, k;
  63. i = i1;
  64. j = i2;
  65. k = 0;
  66. while (i <= j1 && j <= j2) {
  67. if (a[i] < a[j]) {
  68. temp[k++] = a[i++];
  69. } else {
  70. temp[k++] = a[j++];
  71. }
  72. }
  73. while (i <= j1) {
  74. temp[k++] = a[i++];
  75. }
  76. while (j <= j2) {
  77. temp[k++] = a[j++];
  78. }
  79. for (i = i1, j = 0; i <= j2; i++, j++) {
  80. a[i] = temp[j];
  81. }
  82. }
  83.  
  84. // measures the execution time of a given function using traverse_fn
  85. std::string bench_traverse(std::function<void()> traverse_fn) {
  86. auto start = high_resolution_clock::now();
  87. traverse_fn();
  88. auto stop = high_resolution_clock::now();
  89.  
  90. // Subtract stop and start timepoints and cast it to required unit.
  91. // Predefined units are nanoseconds, microseconds, milliseconds, seconds,
  92. // minutes, hours. Use duration_cast() function.
  93. auto duration = duration_cast<milliseconds>(stop - start);
  94.  
  95. // To get the value of duration use the count() member function on the
  96. // duration object
  97. return std::to_string(duration.count());
  98. }
  99.  
  100. // argc: argument count(represents the number of command-line arguments passed to the program)
  101. // **argv: representing the command-line arguments themselves.
  102. int main() {
  103. // checks if the number of command-line arguments is less than 3.
  104. // If so, it prints a message asking the user to specify the array length and maximum random value
  105. // and then exits the program with a return code of 1
  106.  
  107. int *a, n, rand_max;
  108.  
  109. n = 1000;
  110. rand_max = 1000;
  111. a = new int[n]; // dynamic array of size n
  112.  
  113. // fills the array a with random integer values between 0 and rand_max.
  114. for (int i = 0; i < n; i++) {
  115. a[i] = rand() % rand_max;
  116. }
  117.  
  118. // Another array b is dynamically allocated and initialized with the same contents as array a.
  119. int *b = new int[n];
  120. copy(a, a + n, b);
  121.  
  122. // Displaying message
  123. cout << "Generated random array of length " << n << " with elements between 0 to " << rand_max
  124. << "\n\n";
  125.  
  126. // Performing sequential merge-sort
  127. std::cout << "Sequential Merge sort: " << bench_traverse([&] { s_mergesort(a, 0, n - 1); })
  128. << "ms\n";
  129. // printing sorted array
  130. cout << "Sorted array is =>\n";
  131. for (int i = 0; i < n; i++) {
  132. cout << a[i] << ", ";
  133. }
  134. cout << "\n\n";
  135.  
  136. // sets the number of threads to 16 for OpenMP parallelization
  137. // omp_set_num_threads(16);
  138.  
  139. // performs parallel merge-sort
  140. std::cout << "Parallel (16) Merge sort: "
  141. << bench_traverse([&] { parallel_mergesort(b, 0, n - 1); }) << "ms\n";
  142. // printing sorted array
  143. cout << "Sorted array is =>\n";
  144. for (int i = 0; i < n; i++) {
  145. cout << b[i] << ", ";
  146. }
  147. return 0;
  148. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Generated random array of length 1000 with elements between 0 to 1000

Sequential Merge sort: 0ms
Sorted array is =>
0, 0, 2, 2, 4, 6, 8, 9, 10, 11, 11, 12, 16, 17, 17, 18, 19, 21, 21, 21, 21, 22, 23, 25, 27, 27, 28, 29, 30, 30, 30, 31, 32, 33, 34, 36, 36, 36, 39, 40, 41, 42, 42, 42, 43, 43, 46, 47, 49, 49, 50, 51, 52, 53, 54, 55, 57, 57, 57, 58, 59, 59, 59, 60, 62, 63, 67, 67, 69, 71, 71, 72, 73, 74, 74, 80, 81, 81, 81, 81, 81, 82, 82, 84, 84, 84, 84, 86, 87, 87, 88, 90, 90, 90, 91, 93, 94, 95, 97, 97, 97, 98, 99, 100, 100, 105, 107, 109, 109, 114, 115, 116, 117, 117, 120, 121, 122, 122, 123, 124, 124, 125, 126, 126, 127, 127, 127, 127, 128, 130, 131, 131, 133, 134, 134, 135, 136, 137, 139, 139, 142, 143, 144, 147, 149, 149, 150, 151, 152, 153, 154, 154, 155, 157, 157, 157, 159, 159, 163, 167, 168, 170, 171, 171, 172, 172, 172, 173, 176, 177, 178, 179, 179, 179, 181, 181, 181, 183, 184, 188, 189, 189, 189, 190, 190, 190, 193, 195, 195, 196, 197, 197, 198, 199, 199, 202, 202, 204, 205, 205, 205, 206, 207, 209, 210, 211, 211, 211, 214, 215, 217, 217, 218, 218, 219, 222, 223, 224, 224, 225, 226, 226, 227, 228, 228, 228, 229, 231, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 237, 237, 238, 244, 245, 248, 249, 250, 253, 253, 254, 255, 255, 258, 259, 260, 261, 262, 262, 266, 266, 269, 269, 269, 270, 270, 270, 272, 273, 274, 275, 276, 278, 278, 280, 281, 281, 282, 283, 283, 284, 285, 285, 286, 289, 289, 290, 291, 291, 292, 292, 292, 294, 296, 297, 297, 298, 298, 299, 299, 301, 301, 303, 303, 304, 305, 305, 306, 308, 308, 309, 309, 310, 311, 312, 312, 313, 314, 314, 315, 317, 320, 321, 321, 324, 324, 324, 325, 325, 326, 327, 328, 333, 334, 335, 335, 335, 336, 336, 336, 337, 338, 338, 338, 339, 340, 340, 340, 340, 342, 342, 343, 343, 346, 346, 347, 348, 348, 350, 350, 350, 353, 355, 355, 355, 358, 358, 360, 360, 362, 363, 363, 364, 365, 365, 367, 367, 367, 368, 368, 368, 368, 368, 369, 370, 372, 373, 375, 376, 376, 377, 378, 378, 379, 379, 379, 379, 379, 382, 382, 383, 385, 386, 386, 388, 390, 390, 393, 393, 395, 396, 398, 398, 399, 403, 403, 404, 404, 404, 407, 412, 412, 412, 413, 414, 415, 415, 416, 417, 417, 418, 421, 421, 421, 422, 422, 425, 426, 426, 427, 428, 428, 428, 429, 429, 431, 432, 433, 433, 434, 434, 435, 436, 437, 437, 438, 440, 441, 443, 443, 444, 444, 445, 445, 451, 452, 456, 457, 458, 459, 460, 460, 462, 464, 465, 466, 467, 468, 470, 471, 474, 476, 478, 479, 481, 483, 483, 483, 485, 486, 486, 487, 488, 490, 490, 491, 491, 492, 492, 493, 494, 497, 497, 498, 499, 499, 500, 500, 500, 503, 503, 504, 504, 505, 505, 506, 506, 506, 507, 512, 516, 516, 518, 518, 521, 522, 522, 524, 524, 525, 525, 526, 528, 528, 528, 528, 528, 529, 529, 529, 530, 532, 532, 535, 536, 537, 537, 538, 538, 539, 539, 540, 541, 542, 542, 543, 545, 550, 551, 551, 552, 552, 555, 555, 556, 559, 560, 560, 563, 563, 566, 566, 567, 567, 567, 567, 568, 569, 569, 570, 570, 571, 573, 573, 574, 574, 577, 578, 579, 580, 581, 582, 582, 583, 584, 586, 586, 587, 587, 589, 590, 590, 590, 593, 593, 593, 595, 596, 599, 600, 601, 603, 604, 605, 606, 606, 607, 610, 610, 611, 613, 613, 614, 614, 618, 618, 618, 619, 620, 621, 621, 622, 622, 624, 624, 625, 626, 626, 627, 629, 630, 631, 633, 637, 637, 639, 639, 640, 640, 641, 641, 643, 644, 647, 647, 648, 649, 651, 651, 652, 653, 657, 657, 658, 659, 660, 661, 661, 667, 668, 669, 672, 672, 675, 676, 677, 677, 681, 681, 682, 682, 682, 683, 683, 684, 685, 685, 685, 686, 688, 689, 690, 690, 691, 692, 697, 698, 699, 699, 701, 703, 705, 708, 708, 708, 709, 710, 710, 711, 713, 713, 713, 714, 715, 715, 717, 720, 720, 721, 722, 722, 723, 723, 725, 726, 728, 729, 729, 729, 729, 730, 732, 732, 732, 735, 736, 736, 739, 740, 743, 743, 743, 744, 746, 746, 747, 748, 750, 753, 754, 754, 754, 756, 756, 757, 759, 761, 762, 763, 763, 763, 764, 764, 768, 769, 770, 770, 771, 772, 773, 774, 775, 776, 776, 776, 776, 776, 777, 777, 782, 783, 783, 784, 784, 786, 787, 788, 788, 789, 791, 793, 793, 794, 794, 795, 795, 796, 796, 797, 797, 801, 802, 804, 805, 805, 805, 805, 806, 808, 808, 810, 811, 812, 813, 813, 814, 814, 815, 818, 818, 818, 819, 819, 820, 821, 822, 825, 826, 827, 828, 829, 829, 830, 836, 839, 839, 840, 841, 842, 846, 846, 847, 848, 849, 850, 850, 850, 851, 853, 856, 856, 856, 857, 857, 857, 857, 858, 858, 859, 860, 860, 862, 862, 862, 865, 865, 868, 868, 871, 872, 873, 873, 873, 875, 878, 881, 882, 884, 886, 886, 887, 888, 888, 888, 890, 890, 892, 892, 892, 894, 895, 898, 898, 898, 899, 899, 900, 902, 902, 904, 904, 904, 904, 907, 908, 910, 911, 914, 915, 916, 917, 917, 918, 919, 919, 920, 921, 921, 924, 924, 925, 925, 926, 926, 927, 928, 928, 929, 930, 930, 931, 932, 932, 933, 933, 934, 936, 936, 939, 940, 943, 944, 945, 946, 947, 949, 949, 950, 950, 951, 952, 954, 954, 954, 955, 955, 955, 956, 958, 959, 959, 961, 962, 963, 964, 965, 967, 969, 970, 970, 971, 972, 973, 975, 976, 977, 977, 980, 981, 981, 982, 982, 984, 984, 987, 987, 988, 989, 990, 990, 991, 993, 993, 994, 994, 996, 996, 996, 996, 996, 997, 999, 

Parallel (16) Merge sort: 0ms
Sorted array is =>
0, 0, 2, 2, 4, 6, 8, 9, 10, 11, 11, 12, 16, 17, 17, 18, 19, 21, 21, 21, 21, 22, 23, 25, 27, 27, 28, 29, 30, 30, 30, 31, 32, 33, 34, 36, 36, 36, 39, 40, 41, 42, 42, 42, 43, 43, 46, 47, 49, 49, 50, 51, 52, 53, 54, 55, 57, 57, 57, 58, 59, 59, 59, 60, 62, 63, 67, 67, 69, 71, 71, 72, 73, 74, 74, 80, 81, 81, 81, 81, 81, 82, 82, 84, 84, 84, 84, 86, 87, 87, 88, 90, 90, 90, 91, 93, 94, 95, 97, 97, 97, 98, 99, 100, 100, 105, 107, 109, 109, 114, 115, 116, 117, 117, 120, 121, 122, 122, 123, 124, 124, 125, 126, 126, 127, 127, 127, 127, 128, 130, 131, 131, 133, 134, 134, 135, 136, 137, 139, 139, 142, 143, 144, 147, 149, 149, 150, 151, 152, 153, 154, 154, 155, 157, 157, 157, 159, 159, 163, 167, 168, 170, 171, 171, 172, 172, 172, 173, 176, 177, 178, 179, 179, 179, 181, 181, 181, 183, 184, 188, 189, 189, 189, 190, 190, 190, 193, 195, 195, 196, 197, 197, 198, 199, 199, 202, 202, 204, 205, 205, 205, 206, 207, 209, 210, 211, 211, 211, 214, 215, 217, 217, 218, 218, 219, 222, 223, 224, 224, 225, 226, 226, 227, 228, 228, 228, 229, 231, 231, 231, 232, 232, 233, 234, 234, 235, 235, 236, 237, 237, 238, 244, 245, 248, 249, 250, 253, 253, 254, 255, 255, 258, 259, 260, 261, 262, 262, 266, 266, 269, 269, 269, 270, 270, 270, 272, 273, 274, 275, 276, 278, 278, 280, 281, 281, 282, 283, 283, 284, 285, 285, 286, 289, 289, 290, 291, 291, 292, 292, 292, 294, 296, 297, 297, 298, 298, 299, 299, 301, 301, 303, 303, 304, 305, 305, 306, 308, 308, 309, 309, 310, 311, 312, 312, 313, 314, 314, 315, 317, 320, 321, 321, 324, 324, 324, 325, 325, 326, 327, 328, 333, 334, 335, 335, 335, 336, 336, 336, 337, 338, 338, 338, 339, 340, 340, 340, 340, 342, 342, 343, 343, 346, 346, 347, 348, 348, 350, 350, 350, 353, 355, 355, 355, 358, 358, 360, 360, 362, 363, 363, 364, 365, 365, 367, 367, 367, 368, 368, 368, 368, 368, 369, 370, 372, 373, 375, 376, 376, 377, 378, 378, 379, 379, 379, 379, 379, 382, 382, 383, 385, 386, 386, 388, 390, 390, 393, 393, 395, 396, 398, 398, 399, 403, 403, 404, 404, 404, 407, 412, 412, 412, 413, 414, 415, 415, 416, 417, 417, 418, 421, 421, 421, 422, 422, 425, 426, 426, 427, 428, 428, 428, 429, 429, 431, 432, 433, 433, 434, 434, 435, 436, 437, 437, 438, 440, 441, 443, 443, 444, 444, 445, 445, 451, 452, 456, 457, 458, 459, 460, 460, 462, 464, 465, 466, 467, 468, 470, 471, 474, 476, 478, 479, 481, 483, 483, 483, 485, 486, 486, 487, 488, 490, 490, 491, 491, 492, 492, 493, 494, 497, 497, 498, 499, 499, 500, 500, 500, 503, 503, 504, 504, 505, 505, 506, 506, 506, 507, 512, 516, 516, 518, 518, 521, 522, 522, 524, 524, 525, 525, 526, 528, 528, 528, 528, 528, 529, 529, 529, 530, 532, 532, 535, 536, 537, 537, 538, 538, 539, 539, 540, 541, 542, 542, 543, 545, 550, 551, 551, 552, 552, 555, 555, 556, 559, 560, 560, 563, 563, 566, 566, 567, 567, 567, 567, 568, 569, 569, 570, 570, 571, 573, 573, 574, 574, 577, 578, 579, 580, 581, 582, 582, 583, 584, 586, 586, 587, 587, 589, 590, 590, 590, 593, 593, 593, 595, 596, 599, 600, 601, 603, 604, 605, 606, 606, 607, 610, 610, 611, 613, 613, 614, 614, 618, 618, 618, 619, 620, 621, 621, 622, 622, 624, 624, 625, 626, 626, 627, 629, 630, 631, 633, 637, 637, 639, 639, 640, 640, 641, 641, 643, 644, 647, 647, 648, 649, 651, 651, 652, 653, 657, 657, 658, 659, 660, 661, 661, 667, 668, 669, 672, 672, 675, 676, 677, 677, 681, 681, 682, 682, 682, 683, 683, 684, 685, 685, 685, 686, 688, 689, 690, 690, 691, 692, 697, 698, 699, 699, 701, 703, 705, 708, 708, 708, 709, 710, 710, 711, 713, 713, 713, 714, 715, 715, 717, 720, 720, 721, 722, 722, 723, 723, 725, 726, 728, 729, 729, 729, 729, 730, 732, 732, 732, 735, 736, 736, 739, 740, 743, 743, 743, 744, 746, 746, 747, 748, 750, 753, 754, 754, 754, 756, 756, 757, 759, 761, 762, 763, 763, 763, 764, 764, 768, 769, 770, 770, 771, 772, 773, 774, 775, 776, 776, 776, 776, 776, 777, 777, 782, 783, 783, 784, 784, 786, 787, 788, 788, 789, 791, 793, 793, 794, 794, 795, 795, 796, 796, 797, 797, 801, 802, 804, 805, 805, 805, 805, 806, 808, 808, 810, 811, 812, 813, 813, 814, 814, 815, 818, 818, 818, 819, 819, 820, 821, 822, 825, 826, 827, 828, 829, 829, 830, 836, 839, 839, 840, 841, 842, 846, 846, 847, 848, 849, 850, 850, 850, 851, 853, 856, 856, 856, 857, 857, 857, 857, 858, 858, 859, 860, 860, 862, 862, 862, 865, 865, 868, 868, 871, 872, 873, 873, 873, 875, 878, 881, 882, 884, 886, 886, 887, 888, 888, 888, 890, 890, 892, 892, 892, 894, 895, 898, 898, 898, 899, 899, 900, 902, 902, 904, 904, 904, 904, 907, 908, 910, 911, 914, 915, 916, 917, 917, 918, 919, 919, 920, 921, 921, 924, 924, 925, 925, 926, 926, 927, 928, 928, 929, 930, 930, 931, 932, 932, 933, 933, 934, 936, 936, 939, 940, 943, 944, 945, 946, 947, 949, 949, 950, 950, 951, 952, 954, 954, 954, 955, 955, 955, 956, 958, 959, 959, 961, 962, 963, 964, 965, 967, 969, 970, 970, 971, 972, 973, 975, 976, 977, 977, 980, 981, 981, 982, 982, 984, 984, 987, 987, 988, 989, 990, 990, 991, 993, 993, 994, 994, 996, 996, 996, 996, 996, 997, 999,