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.  
  18. #define SIZE 3
  19.  
  20. // Function to print matrix
  21. void printMatrix(int matrix[SIZE][SIZE]) {
  22. for (int i = 0; i < SIZE; i++) {
  23. for (int j = 0; j < SIZE; j++) {
  24. printf("%d ", matrix[i][j]);
  25. }
  26. printf("\n");
  27. }
  28. }
  29.  
  30. // Function to multiply matrices
  31. void multiplyMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int c[SIZE][SIZE], int startRow, int endRow) {
  32. for (int i = startRow; i < endRow; i++) {
  33. for (int j = 0; j < SIZE; j++) {
  34. c[i][j] = 0;
  35. for (int k = 0; k < SIZE; k++) {
  36. c[i][j] += a[i][k] * b[k][j];
  37. }
  38. }
  39. }
  40. }
  41.  
  42. int main(int argc, char* argv[]) {
  43. int rank, size;
  44. int a[SIZE][SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  45. int b[SIZE][SIZE] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
  46. int c[SIZE][SIZE];
  47.  
  48. MPI_Init(&argc, &argv);
  49. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  50. MPI_Comm_size(MPI_COMM_WORLD, &size);
  51.  
  52. // Calculate the number of rows each process will handle
  53. int rowsPerProcess = SIZE / size;
  54. int startRow = rank * rowsPerProcess;
  55. int endRow = startRow + rowsPerProcess;
  56.  
  57. // Perform matrix multiplication
  58. multiplyMatrices(a, b, c, startRow, endRow);
  59.  
  60. // Gather the results from all processes
  61. MPI_Gather(&c[startRow][0], rowsPerProcess * SIZE, MPI_INT, c, rowsPerProcess * SIZE, MPI_INT, 0, MPI_COMM_WORLD);
  62.  
  63. if (rank == 0) {
  64. printf("Matrix A:\n");
  65. printMatrix(a);
  66. printf("\nMatrix B:\n");
  67. printMatrix(b);
  68. printf("\nMatrix C (Result):\n");
  69. printMatrix(c);
  70. }
  71.  
  72. MPI_Finalize();
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout #stderr 0.26s 39092KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(Rmpi) : there is no package called ‘Rmpi’
Execution halted