fork download
  1. #include <mpi.h>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7. using namespace std
  8.  
  9. double cpu_time(void);
  10. int prime_number(int n, int id, int numtasks);
  11.  
  12. int main(int argc, char** argv) {
  13. int rank, size, n_factor = 2, n_lo = 1, n_hi = 100;
  14. double start_time, end_time;
  15. int n, primes, primes_part;
  16.  
  17. MPI_Init(&argc, &argv);
  18. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19. MPI_Comm_size(MPI_COMM_WORLD, &size);
  20.  
  21. if (rank == 0) {
  22. cout << "Pomiar ilosci liczb pierwszych w powiekszajacych sie " << n_factor << "-krotnie podprzedzialach przedzialu [" << n_lo << " , " << n_hi << " ]" << endl;
  23. cout << "\n";
  24. cout << " N_max Ilosc liczb pierwszych Czas " << endl;
  25. cout << "\n";
  26. }
  27.  
  28. n = n_lo;
  29.  
  30. while (n <= n_hi) {
  31. start_time = MPI_Wtime();
  32.  
  33. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  34. primes_part = prime_number(n, rank, size);
  35.  
  36. MPI_Reduce(&primes_part, &primes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  37.  
  38. end_time = MPI_Wtime();
  39.  
  40. if (rank == 0) {
  41. cout << " " << setw(3) << n
  42. << " " << setw(18) << primes
  43. << " " << setw(18) << end_time - start_time << endl;
  44. }
  45.  
  46. n = n * n_factor;
  47. }
  48.  
  49. MPI_Finalize();
  50. return 0;
  51. }
  52.  
  53. double cpu_time() {
  54. return (double)clock() / (double)CLOCKS_PER_SEC;
  55. }
  56.  
  57. int prime_number(int n, int id, int numtasks) {
  58. int prime;
  59. int total = 0;
  60.  
  61. for (int i = id + 2; i <= n; i += numtasks) {
  62. prime = 1;
  63. for (int j = 2; j < i; j++) {
  64. if ((i % j) == 0) {
  65. prime = 0;
  66. break;
  67. }
  68. }
  69. total = total + prime;
  70. }
  71. return total;
  72. }
  73.  
Success #stdin #stdout #stderr 0.32s 40752KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "using namespace"
Execution halted