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 <stdio.h>
  15. #include <stdlib.h>
  16. #include <mpi.h>
  17. int main(int argc, char *argv[]) {
  18. MPI_Init(&argc, &argv);
  19. int rank, size;
  20. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  21. MPI_Comm_size(MPI_COMM_WORLD, &size);
  22. if (size < 2) {
  23. fprintf(stderr, "This program requires at least 2 processes.\n");
  24. MPI_Abort(MPI_COMM_WORLD, 1);
  25. }
  26. const int n = 1000;
  27. int numbers[n];
  28. int localMin = INT_MAX;
  29. int localMax = INT_MIN;
  30. int globalMin, globalMax;
  31. if (rank == 0) {
  32. for (int i = 0; i < n; i++) {
  33. numbers[i] = rand() % 100;
  34. }}
  35. MPI_Bcast(numbers, n, MPI_INT, 0, MPI_COMM_WORLD);
  36. for (int i = rank; i < n; i += size) {
  37. if (numbers[i] < localMin) {
  38. localMin = numbers[i];
  39. }
  40. if (numbers[i] > localMax) {
  41. localMax = numbers[i];
  42. }
  43. }
  44. MPI_Reduce(&localMin, &globalMin, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  45. MPI_Reduce(&localMax, &globalMax, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  46. if (rank == 0) {
  47. printf("Global Min: %d\n", globalMin);
  48. printf("Global Max: %d\n", globalMax);
  49. }
  50. MPI_Finalize();
  51. return 0;
  52. }
Success #stdin #stdout #stderr 0.27s 40544KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(Rmpi) : there is no package called ‘Rmpi’
Execution halted