fork download
  1.  
  2. #include <mpi.h>
  3. #include <iostream>
  4.  
  5. int main(int argc, char** argv) {
  6. MPI_Init(&argc, &argv);
  7.  
  8. // Your MPI code goes here
  9.  
  10. MPI_Finalize();
  11. return 0;
  12. }
  13.  
  14. int numProcesses, rank;
  15. MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
  16. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  17.  
  18. std::cout << "Total processes: " << numProcesses << ", My rank: " << rank << std::endl;
  19.  
  20. int data = 100;
  21. if (rank == 0) {
  22. MPI_Send(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  23. } else if (rank == 1) {
  24. MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  25. std::cout << "Received data: " << data << std::endl;
  26. }
  27.  
  28. int sendData = 100;
  29. int recvData;
  30. MPI_Bcast(&sendData, 1, MPI_INT, 0, MPI_COMM_WORLD); // Broadcast data from rank 0 to all other ranks
  31. MPI_Reduce(&sendData, &recvData, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); // Reduce data from all ranks to rank 0
  32.  
  33. MPI_Request request;
  34. MPI_Isend(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &request);
  35. // Perform other computation
  36. MPI_Wait(&request, MPI_STATUS_IGNORE); // Wait for the send operation to complete
  37.  
Success #stdin #stdout #stderr 0.26s 40852KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "int main"
Execution halted