fork download
  1. library(Rmpi)
  2. mpi.spawn.Rslaves(needlog = FALSE)
  3.  
  4. mpi.bcast.cmd( id <- mpi.comm.rank() )
  5. mpi.bcast.cmd( np <- mpi.comm.size() )
  6. mpi.bcast.cmd( host <- mpi.get.processor.name() )
  7. result <- mpi.remote.exec(paste("I am", id, "of", np, "running on", host))
  8.  
  9. print(unlist(result))
  10.  
  11. mpi.close.Rslaves(dellog = FALSE)
  12. mpi.exit()
  13.  
  14. #include "mpi.h"
  15. #include <stdio.h>
  16. #include <math.h>
  17.  
  18. /* module load gcc */
  19. /* module load mpich/ge/gcc/64/3.1 */
  20. /* Compile me with 'mpicc -o cpi-parallel cpi-parallel.c' */
  21.  
  22.  
  23. double f( double );
  24. double f( double a )
  25. {
  26. return (4.0 / (1.0 + a*a));
  27. }
  28.  
  29. int main( int argc, char *argv[])
  30. {
  31. int done = 0, n, myid, numprocs, i;
  32. double PI25DT = 3.141592653589793238462643;
  33. double mypi, pi, h, sum, x;
  34. double startwtime = 0.0, endwtime;
  35. int namelen;
  36. char processor_name[MPI_MAX_PROCESSOR_NAME];
  37.  
  38. MPI_Init(&argc,&argv);
  39. MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  40. MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  41. MPI_Get_processor_name(processor_name,&namelen);
  42.  
  43. fprintf(stderr,"Process %d on %s\n",
  44. myid, processor_name);
  45.  
  46. n = 0;
  47. while (!done)
  48. {
  49. if (myid == 0)
  50. {
  51. /*
  52.   printf("Enter the number of intervals: (0 quits) ");
  53.   scanf("%d",&n);
  54. */
  55. if (n==0) n=1024*numprocs; else n=0;
  56.  
  57. startwtime = MPI_Wtime();
  58. }
  59. MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  60. if (n == 0)
  61. done = 1;
  62. else
  63. {
  64. h = 1.0 / (double) n;
  65. sum = 0.0;
  66. for (i = myid + 1; i <= n; i += numprocs)
  67. {
  68. x = h * ((double)i - 0.5);
  69. sum += f(x);
  70. }
  71. mypi = h * sum;
  72.  
  73. MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  74.  
  75. if (myid == 0)
  76. {
  77. printf("pi is approximately %.16f, Error is %.16f\n",
  78. pi, fabs(pi - PI25DT));
  79. endwtime = MPI_Wtime();
  80. printf("wall clock time = %f\n",
  81. endwtime-startwtime);
  82. }
  83. }
  84. }
  85. MPI_Finalize();
  86.  
  87. return 0;
  88. }
Success #stdin #stdout #stderr 0.27s 40040KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(Rmpi) : there is no package called ‘Rmpi’
Execution halted