#include #include #include "mpi.h" #define MXLEN 71 #define NMAX 1000 #define DEBUG 0 void get_data(char s[][MXLEN], int *n) { int i; char *nl; fgets( s[0], MXLEN, stdin ); *n = atoi(s[0]); for ( i=0; i<*n; i++ ) { fgets(s[i],MXLEN,stdin); if (( nl=strchr(s[i],'\n'))) *nl='\0'; // remove newline character } } int ordered( char s[][MXLEN], int n ) { int i; for ( i=0; i 0 ) return 0; } return 1; } //****************************************************************** // // Array of strings is divided into blocks. One process checks if // the block assigned to it is in alphabetical order. // Master process checks first the block junctions. // //****************************************************************** char s[NMAX][MXLEN]; int main( int argc, char *argv[] ) { int n, np, me, i, nn, ok, pos, nloc; MPI_Status stat; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&np); if ( np < 2 ) { MPI_Finalize(); printf("np should be at least 2\n"); return -1; } MPI_Comm_rank(MPI_COMM_WORLD,&me); // int ok2[np], is[np], ie[np]; MPI_Request req[np]; // if ( me==0 ) { // ******************** // *** Master task **** // ******************** // Get strings // get_data(s,&n); if ( DEBUG > 1 ) { printf("Strings:\n"); for ( i=0; i 0 ) { ok=0; printf("\n ***Strings s[%d] and s[%d] not in order\n\n", ie[i],is[i+1]); } } // // Send result of the previous check to workers // for ( i=1; i 1 ) { printf("me=%d received strings\n",me); for ( i=0; i No work assigned to me. // if ( DEBUG ) printf("Task %d needs to do nothing\n",me); } } MPI_Finalize(); }