fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <mpi.h>
  5.  
  6. // Function to swap two elements
  7. void swap(int *a, int *b) {
  8. int temp = *a;
  9. *a = *b;
  10. *b = temp;
  11. }
  12.  
  13. // Function to partition the array
  14. int partition(int arr[], int low, int high) {
  15. int pivot = arr[high];
  16. int i = low - 1;
  17. for (int j = low; j < high; j++) {
  18. if (arr[j] < pivot) {
  19. i++;
  20. swap(&arr[i], &arr[j]);
  21. }
  22. }
  23. swap(&arr[i + 1], &arr[high]);
  24. return i + 1;
  25. }
  26.  
  27. // Function to implement quicksort
  28. void quicksort(int arr[], int low, int high) {
  29. if (low < high) {
  30. int pivot = partition(arr, low, high);
  31. quicksort(arr, low, pivot - 1);
  32. quicksort(arr, pivot + 1, high);
  33. }
  34. }
  35.  
  36. // Function to merge two sorted arrays
  37. void merge(int arr1[], int arr2[], int result[], int size1, int size2) {
  38. int i = 0, j = 0, k = 0;
  39. while (i < size1 && j < size2) {
  40. if (arr1[i] < arr2[j]) {
  41. result[k] = arr1[i];
  42. i++;
  43. } else {
  44. result[k] = arr2[j];
  45. j++;
  46. }
  47. k++;
  48. }
  49. while (i < size1) {
  50. result[k] = arr1[i];
  51. i++;
  52. k++;
  53. }
  54. while (j < size2) {
  55. result[k] = arr2[j];
  56. j++;
  57. k++;
  58. }
  59. }
  60.  
  61. int main(int argc, char** argv) {
  62. int num_elements, num_processors, processor_id;
  63. double start_time, end_time;
  64.  
  65. // Initialize MPI
  66. MPI_Init(&argc, &argv);
  67. MPI_Comm_size(MPI_COMM_WORLD, &num_processors);
  68. MPI_Comm_rank(MPI_COMM_WORLD, &processor_id);
  69.  
  70. // Get the number of elements from the user
  71. if (processor_id == 0) {
  72. printf("Enter the number of elements (max 1000): ");
  73. scanf("%d", &num_elements);
  74. if (num_elements < 1 || num_elements > 1000) {
  75. printf("Invalid input. Please enter a value between 1 and 1000.\n");
  76. MPI_Abort(MPI_COMM_WORLD, 1);
  77. }
  78. }
  79.  
  80. // Broadcast the number of elements to all processors
  81. MPI_Bcast(&num_elements, 1, MPI_INT, 0, MPI_COMM_WORLD);
  82.  
  83. // Generate random data
  84. int* data = (int*)malloc(num_elements * sizeof(int));
  85. if (processor_id == 0) {
  86. srand(time(NULL));
  87. for (int i = 0; i < num_elements; i++) {
  88. data[i] = rand() % 1000;
  89. }
  90. }
  91.  
  92. // Distribute the data among processors
  93. int chunk_size = num_elements / num_processors;
  94. int* local_data = (int*)malloc(chunk_size * sizeof(int));
  95. MPI_Scatter(data, chunk_size, MPI_INT, local_data, chunk_size, MPI_INT, 0, MPI_COMM_WORLD);
  96.  
  97. // Sort the local data
  98. start_time = MPI_Wtime();
  99. quicksort(local_data, 0, chunk_size - 1);
  100.  
  101. // Gather the sorted data
  102. int* sorted_data = (int*)malloc(num_elements * sizeof(int));
  103. MPI_Gather(local_data, chunk_size, MPI_INT, sorted_data, chunk_size, MPI_INT, 0, MPI_COMM_WORLD);
  104.  
  105. // Merge the sorted data
  106. if (processor_id == 0) {
  107. int* temp = (int*)malloc(num_elements * sizeof(int));
  108. int offset = 0;
  109. for (int i = 0; i < num_processors; i++) {
  110. merge(sorted_data + offset, sorted_data + offset + chunk_size, temp + offset, chunk_size, chunk_size);
  111. offset += chunk_size;
  112. }
  113. end_time = MPI_Wtime();
  114. printf("Actual data: ");
  115. for (int i = 0; i < num_elements; i++) {
  116. printf("%d ", data[i]);
  117. }
  118. printf("\n");
  119. printf("Sorted data: ");
  120. for (int i = 0; i < num_elements; i++) {
  121. printf("%d ", temp[i]);
  122. }
  123. printf("\n");
  124. printf("Execution time: %f seconds\n", end_time - start_time);
  125. free(temp);
  126. }
  127.  
  128. // Clean up
  129. free(data);
  130. free(local_data);
  131. free(sorted_data);
  132. MPI_Finalize();
  133. return 0;
  134. }
Success #stdin #stdout #stderr 0.31s 40792KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted