1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| tee submit.sh << EOF #!/bin/bash
srun hostname EOF sbatch submit.sh
tee hello_mpi.c << EOF /* "Hello World" MPI Test Program */
int main(int argc, char **argv) { char buf[256]; int my_rank, num_procs;
/* Initialize the infrastructure necessary for communication */ MPI_Init(&argc, &argv);
/* Identify this process */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out how many total processes are active */ MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
/* Until this point, all programs have been doing exactly the same. Here, we check the rank to distinguish the roles of the programs */ if (my_rank == 0) { int other_rank; printf("We have %i processes.\n", num_procs);
/* Send messages to all other processes */ for (other_rank = 1; other_rank < num_procs; other_rank++) { sprintf(buf, "Hello %i!", other_rank); MPI_Send(buf, sizeof(buf), MPI_CHAR, other_rank, 0, MPI_COMM_WORLD); }
/* Receive messages from all other process */ for (other_rank = 1; other_rank < num_procs; other_rank++) { MPI_Recv(buf, sizeof(buf), MPI_CHAR, other_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("%s\n", buf); }
} else {
/* Receive message from process MPI_Recv(buf, sizeof(buf), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); assert(memcmp(buf, "Hello ", 6) == 0),
/* Send message to process sprintf(buf, "Process %i reporting for duty.", my_rank); MPI_Send(buf, sizeof(buf), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
/* Tear down the communication infrastructure */ MPI_Finalize(); return 0; } EOF mpicc hello_mpi.c -o hello.mpi
tee submit_mpi.sh << EOF #!/bin/bash
srun ~/F/hello.mpi EOF sbatch submit_mpi.sh
|