fork download
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. // Function to check if a number is prime
  7. int is_prime(int num) {
  8. if (num <= 1) return 0; // 0 and 1 are not prime
  9. if (num <= 3) return 1; // 2 and 3 are prime
  10. if (num % 2 == 0 || num % 3 == 0) return 0; // eliminate even numbers and multiples of 3
  11. for (int i = 5; i <= sqrt(num); i += 6) {
  12. if (num % i == 0 || num % (i + 2) == 0) return 0;
  13. }
  14. return 1;
  15. }
  16.  
  17. int main(int argc, char** argv) {
  18. int rank, size;
  19. int lower, upper;
  20.  
  21. // Initialize MPI
  22. MPI_Init(&argc, &argv);
  23. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  24. MPI_Comm_size(MPI_COMM_WORLD, &size);
  25.  
  26. // Read the range of numbers from the user (only by the master process)
  27. if (rank == 0) {
  28. printf("Enter the lower bound of the range: ");
  29. scanf("%d", &lower);
  30. printf("Enter the upper bound of the range: ");
  31. scanf("%d", &upper);
  32. }
  33.  
  34. // Broadcast the range to all processes
  35. MPI_Bcast(&lower, 1, MPI_INT, 0, MPI_COMM_WORLD);
  36. MPI_Bcast(&upper, 1, MPI_INT, 0, MPI_COMM_WORLD);
  37.  
  38. // Calculate the range of numbers for each process
  39. int range = upper - lower + 1;
  40. int local_lower = lower + (rank * range / size);
  41. int local_upper = lower + ((rank + 1) * range / size) - 1;
  42.  
  43. // Find primes in the assigned range
  44. int* local_primes = malloc((local_upper - local_lower + 1) * sizeof(int));
  45. int local_count = 0;
  46.  
  47. for (int i = local_lower; i <= local_upper; i++) {
  48. if (is_prime(i)) {
  49. local_primes[local_count++] = i;
  50. }
  51. }
  52.  
  53. // Gather prime counts from all processes
  54. int* recv_counts = NULL;
  55. if (rank == 0) {
  56. recv_counts = malloc(size * sizeof(int));
  57. }
  58. MPI_Gather(&local_count, 1, MPI_INT, recv_counts, 1, MPI_INT, 0, MPI_COMM_WORLD);
  59.  
  60. // Gather all primes from all processes
  61. int* displacements = NULL;
  62. int total_primes = 0;
  63. if (rank == 0) {
  64. displacements = malloc(size * sizeof(int));
  65. displacements[0] = 0;
  66. for (int i = 1; i < size; i++) {
  67. displacements[i] = displacements[i - 1] + recv_counts[i - 1];
  68. }
  69. total_primes = displacements[size - 1] + recv_counts[size - 1];
  70. }
  71.  
  72. int* global_primes = NULL;
  73. if (rank == 0) {
  74. global_primes = malloc(total_primes * sizeof(int));
  75. }
  76. MPI_Gatherv(local_primes, local_count, MPI_INT, global_primes, recv_counts, displacements, MPI_INT, 0, MPI_COMM_WORLD);
  77.  
  78. // Print the result
  79. if (rank == 0) {
  80. printf("Prime numbers in the range [%d, %d]:\n", lower, upper);
  81. for (int i = 0; i < total_primes; i++) {
  82. printf("%d ", global_primes[i]);
  83. }
  84. printf("\n");
  85.  
  86. // Free dynamically allocated memory
  87. free(global_primes);
  88. free(recv_counts);
  89. free(displacements);
  90. }
  91.  
  92. // Finalize MPI and free local memory
  93. free(local_primes);
  94. MPI_Finalize();
  95.  
  96. return 0;
  97. }
  98.  
Success #stdin #stdout #stderr 0.29s 40104KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted