[2136] | 1 | #include <mpi.h> |
---|
| 2 | #include <stdio.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | |
---|
| 6 | #define CMD_MAXLEN 4096 |
---|
| 7 | #define CMD_MASTER 0 |
---|
| 8 | #define TAG_AVAIL 1 |
---|
| 9 | #define TAG_CMD 2 |
---|
| 10 | |
---|
| 11 | void read_and_exec(char *filename); |
---|
| 12 | void read_and_send(int slaves, char *filename); |
---|
| 13 | void recv_and_exec(int rank); |
---|
| 14 | void read_next_command(FILE *fl, char *cmd); |
---|
| 15 | void exec_command(char *cmd, int rank); |
---|
| 16 | |
---|
| 17 | int main(int argc,char **argv) |
---|
| 18 | { |
---|
| 19 | FILE *fl; |
---|
| 20 | int rank,size; |
---|
| 21 | |
---|
| 22 | MPI_Init(&argc,&argv); |
---|
| 23 | MPI_Comm_rank(MPI_COMM_WORLD,&rank); |
---|
| 24 | MPI_Comm_size(MPI_COMM_WORLD,&size); |
---|
| 25 | if (rank == CMD_MASTER) |
---|
| 26 | printf("master is %d , nb slaves: %d\n", |
---|
| 27 | CMD_MASTER,size-1); |
---|
| 28 | |
---|
| 29 | if (size == 1) |
---|
| 30 | read_and_exec(argv[1]); |
---|
| 31 | else if(size != 1 && rank == CMD_MASTER) |
---|
| 32 | read_and_send(size-1,argv[1]); |
---|
| 33 | else |
---|
| 34 | recv_and_exec(rank); |
---|
| 35 | MPI_Finalize(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | void read_and_exec(char *filename) |
---|
| 39 | { |
---|
| 40 | FILE *fl; |
---|
| 41 | char cmd[CMD_MAXLEN]; |
---|
| 42 | double t; |
---|
| 43 | |
---|
| 44 | fl = fopen(filename,"r"); |
---|
| 45 | do { |
---|
| 46 | read_next_command(fl,cmd); |
---|
| 47 | if(!strlen(cmd)) |
---|
| 48 | break; |
---|
| 49 | exec_command(cmd,CMD_MASTER); |
---|
| 50 | } while (strlen(cmd)); |
---|
| 51 | fclose(fl); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void read_and_send(int slaves,char *filename) |
---|
| 55 | { |
---|
| 56 | int i, num; |
---|
| 57 | MPI_Status status; |
---|
| 58 | FILE *fl; |
---|
| 59 | char cmd[CMD_MAXLEN]; |
---|
| 60 | |
---|
| 61 | fl = fopen(filename,"r"); |
---|
| 62 | i=0; |
---|
| 63 | do { |
---|
| 64 | read_next_command(fl,cmd); |
---|
| 65 | MPI_Recv(&num,1,MPI_INT,MPI_ANY_SOURCE, |
---|
| 66 | TAG_AVAIL,MPI_COMM_WORLD,&status); |
---|
| 67 | MPI_Send(cmd,CMD_MAXLEN,MPI_CHAR,num, |
---|
| 68 | TAG_CMD,MPI_COMM_WORLD); |
---|
| 69 | if (!strlen(cmd)) /* terminate command sent */ |
---|
| 70 | i++; |
---|
| 71 | } while (i < slaves); |
---|
| 72 | fclose(fl); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void recv_and_exec(int rank) |
---|
| 76 | { |
---|
| 77 | char cmd[CMD_MAXLEN]; |
---|
| 78 | MPI_Status status; |
---|
| 79 | |
---|
| 80 | do { |
---|
| 81 | MPI_Send(&rank,1,MPI_INT,CMD_MASTER, |
---|
| 82 | TAG_AVAIL,MPI_COMM_WORLD); |
---|
| 83 | MPI_Recv(cmd,CMD_MAXLEN,MPI_CHAR,CMD_MASTER, |
---|
| 84 | TAG_CMD,MPI_COMM_WORLD,&status); |
---|
| 85 | if(!strlen(cmd)) /* terminate command */ |
---|
| 86 | break; |
---|
| 87 | exec_command(cmd,rank); |
---|
| 88 | } while (strlen(cmd)); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void read_next_command(FILE *fl,char *cmd) |
---|
| 92 | { |
---|
| 93 | if(!fgets(cmd,CMD_MAXLEN,fl)) cmd[0]='\0'; |
---|
| 94 | if(cmd[strlen(cmd)-1]=='\n') cmd[strlen(cmd)-1]='\0'; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void exec_command(char *cmd, int rank) |
---|
| 98 | { |
---|
| 99 | double t; |
---|
| 100 | int r; |
---|
| 101 | |
---|
| 102 | t = MPI_Wtime(); |
---|
| 103 | r=system(cmd); |
---|
| 104 | t = MPI_Wtime() - t; |
---|
| 105 | fprintf(stderr,"#executed by process\t%d in %gs\twith status\t%d : %s\n", |
---|
| 106 | rank,t,r,cmd); |
---|
| 107 | } |
---|