fork download
  1. import mpi.*;
  2.  
  3. public class MPITest {
  4. public static void main(String[] args) throws MPIException {
  5. MPI.Init(args);
  6. int rank = MPI.COMM_WORLD.Rank();
  7. int size = MPI.COMM_WORLD.Size();
  8. int n = 0;
  9.  
  10. if(rank == 0) {
  11. Scanner scanner = new Scanner(System.in);
  12. System.out.print("Enter the value of n: ");
  13. n = scanner.nextInt();
  14. }
  15.  
  16. MPI.COMM_WORLD.Bcast(new int[] { n }, 0, 1, MPI.INT, 0);
  17.  
  18. if(rank == 0) {
  19. System.out.println("Even numbers from 1 to " + n + ": ");
  20. for(int i = 2; i <= n; i += 2) {
  21. System.out.print(i + " ");
  22. }
  23. System.out.println();
  24. } else if(rank == 1) {
  25. int a = 0, b = 1, c;
  26. System.out.println("Fibonacci series from 1 to " + n + ": ");
  27. System.out.print(a + " " + b + " ");
  28. for(int i = 2; i < n; i++) {
  29. c = a + b;
  30. System.out.print(c + " ");
  31. a = b;
  32. b = c;
  33. }
  34. System.out.println();
  35. } else if(rank == 2) {
  36. int sum = 0;
  37. for(int i = 1; i <= n; i++) {
  38. sum += i;
  39. }
  40. System.out.println("Sum of numbers from 1 to " + n + ": " + sum);
  41. } else if(rank == 3) {
  42. System.out.println("Prime numbers between 1 to " + n + ": ");
  43. for(int i = 2; i <= n; i++) {
  44. boolean isPrime = true;
  45. for(int j = 2; j < i; j++) {
  46. if(i % j == 0) {
  47. isPrime = false;
  48. break;
  49. }
  50. }
  51. if(isPrime) {
  52. System.out.print(i + " ");
  53. }
  54. }
  55. System.out.println();
  56. }
  57.  
  58. MPI.Finalize();
  59. }
  60. }
  61.  
  62.  
  63.  
Success #stdin #stdout #stderr 0.24s 39060KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "import mpi."
Execution halted