X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d27239d695b18203a5b53cae488067deb9446e26..5a5c4a0d5915bb7a2b308e061bbf5f5c90e20575:/examples/gras/pmm/pmm.c diff --git a/examples/gras/pmm/pmm.c b/examples/gras/pmm/pmm.c index 379cbb5705..15412760cd 100755 --- a/examples/gras/pmm/pmm.c +++ b/examples/gras/pmm/pmm.c @@ -7,8 +7,9 @@ #include "gras.h" #define MATRIX_SIZE 3 +#define SLAVE_COUNT 9 -XBT_LOG_NEW_DEFAULT_CATEGORY(pmm,"Messages specific to this example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(pmm,"Parallel Matrix Multiplication"); GRAS_DEFINE_TYPE(s_matrix,struct s_matrix { int rows; @@ -25,7 +26,7 @@ GRAS_DEFINE_TYPE(s_result,struct s_result { }); typedef struct s_result result_t; -/* struct to send initial data to sensor */ +/* struct to send initial data to slave */ GRAS_DEFINE_TYPE(s_init_data,struct s_init_data { int myrow; int mycol; @@ -41,21 +42,23 @@ static void register_messages(void) { result_type=gras_datadesc_by_symbol(s_result); init_data_type=gras_datadesc_by_symbol(s_init_data); - gras_msgtype_declare("result", result_type); - gras_msgtype_declare("init_data", init_data_type); - - gras_msgtype_declare("ask_result", gras_datadesc_by_name("int")); - gras_msgtype_declare("step", gras_datadesc_by_name("int")); - gras_msgtype_declare("step_ack", gras_datadesc_by_name("int")); - gras_msgtype_declare("data", gras_datadesc_by_name("int")); + gras_msgtype_declare("result", result_type); // recieve a final result from slave + gras_msgtype_declare("init_data", init_data_type); // send from master to slave to initialize data bA,bB + + gras_msgtype_declare("ask_result", gras_datadesc_by_name("int")); // send from master to slave to ask a final result + gras_msgtype_declare("step", gras_datadesc_by_name("int"));// send from master to slave to indicate the begining of step + gras_msgtype_declare("step_ack", gras_datadesc_by_name("int"));//send from slave to master to indicate the end of the current step + gras_msgtype_declare("dataA", gras_datadesc_by_name("double"));// send data between slave + gras_msgtype_declare("dataB", gras_datadesc_by_name("double"));// send data between slave } /* Function prototypes */ -int maestro (int argc,char *argv[]); -int sensor (int argc,char *argv[]); +int slave (int argc,char *argv[]); +int master (int argc,char *argv[]); + /* ********************************************************************** - * Maestro code + * master code * **********************************************************************/ /* Global private data */ @@ -63,33 +66,16 @@ typedef struct { int nbr_col,nbr_row; int remaining_step; int remaining_ack; -} maestro_data_t; - - -static int maestro_cb_data_handler(gras_msg_cb_ctx_t ctx, void *payload) { - - xbt_ex_t e; - /* 1. Get the payload into the msg variable */ - int msg=*(int*)payload_data; +} master_data_t; - gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx); - - /*code of callback */ - - /* 8. Make sure we don't leak sockets */ - gras_socket_close(expeditor); - - /* 9. Tell GRAS that we consummed this message */ - return 1; -} /* end_of_maestro_cb_data_handler */ /*** Function initilaze matrixs ***/ static void initmatrix(matrix_t *X){ - int i; - for(i=0 ; irows)*(X->cols); i++) + X->data[i]=1.0;//*rand()/(RAND_MAX+1.0); } /* end_of_initmatrixs */ /*** Function Scatter Sequentiel ***/ @@ -107,27 +93,6 @@ static void scatter_parl(){ /*** Function: multiplication ***/ static void multiplication(){ - - int step,i; - - for (step=1; step <= MATRIX_SIZE; step++){ - for (i=0; i< nbr_sensor; i++){ - TRY { - gras_msg_send(proc[(i/3)+1][(i%3)+1], gras_msgtype_by_name("step"), &step); /* initialize Mycol, MyRow, mydataA,mydataB*/ - - myrow,mycol,mydataA,mydataB - } CATCH(e) { - gras_socket_close(proc[(i/3)+1][(i%3)+1]); - RETHROW0("Unable to send the msg : %s"); - } - } - /* wait for computing and sensor messages exchange */ - TRY { - gras_msg_wait(600,gras_msgtype_by_name("init_data"),&from,&mydata); - } CATCH(e) { - RETHROW0("I Can't get a init Data message from Maestro : %s"); - } - } }/* end_of_multiplication */ @@ -135,7 +100,6 @@ static void multiplication(){ static void gather(){ - }/* end_of_gather */ /*** Function: Display Matrix ***/ @@ -166,17 +130,23 @@ int i,j,t=0; }/* end_of_display */ -int maestro (int argc,char *argv[]) { +int master (int argc,char *argv[]) { xbt_ex_t e; -int i,ask_result,step; -result_t result; + +int i,port,ask_result,step; + matrix_t A,B,C; +result_t result; - gras_socket_t socket[MATRIX_SIZE*MATRIX_SIZE]; /* sockets for brodcast to other sensor */ - +gras_socket_t from; - /* Initialize Matrixs */ + /* Init the GRAS's infrastructure */ + gras_init(&argc, argv); + + gras_socket_t socket[MATRIX_SIZE*MATRIX_SIZE]; /* sockets for brodcast to other slave */ + + /* Initialize Matrixs */ A.rows=A.cols=MATRIX_SIZE; B.rows=B.cols=MATRIX_SIZE; @@ -189,62 +159,71 @@ matrix_t A,B,C; initmatrix(&A); initmatrix(&B); - /* Init the GRAS's infrastructure */ - gras_init(&argc, argv); /* Get arguments and create sockets */ port=atoi(argv[1]); - //scatter();multiplication();gather(); + //scatter(); //scatter_parl(); + //multiplication(); + //gather(); + //display(A); /****************************** Init Data Send *********************************/ - int j=0; + int step_ack,j=0; init_data_t mydata; + gras_os_sleep(60); // MODIFIER LES TEMPS D'ATTENTE 60 c trop normalement for( i=2;i< argc;i+=3){ - TRY { - socket[j]=gras_socket_client(argv[i],port); - + socket[j]=gras_socket_client(argv[i],port); } CATCH(e) { RETHROW0("Unable to connect to the server: %s"); } INFO2("Connected to %s:%d.",argv[i],port); - mydata.myrow=argv[i+1]; // My rank of row - mydata.mycol=argv[i+2]; // My rank of column + mydata.myrow=atoi(argv[i+1]); // My row + mydata.mycol=atoi(argv[i+2]); // My column + mydata.a=A.data[(mydata.myrow-1)*MATRIX_SIZE+(mydata.mycol-1)]; mydata.b=B.data[(mydata.myrow-1)*MATRIX_SIZE+(mydata.mycol-1)];; gras_msg_send(socket[j],gras_msgtype_by_name("init_data"),&mydata); + INFO3("Send Init Data to %s : data A= %.3g & data B= %.3g",gras_socket_peer_name(socket[j]),mydata.a,mydata.b); j++; } // end init Data Send /******************************* multiplication ********************************/ - + INFO0("begin Multiplication"); + for (step=1; step <= MATRIX_SIZE; step++){ - for (i=0; i< nbr_sensor; i++){ + gras_os_sleep(50); + for (i=0; i< SLAVE_COUNT; i++){ TRY { gras_msg_send(socket[i], gras_msgtype_by_name("step"), &step); /* initialize Mycol, MyRow, mydataA,mydataB*/ - - myrow,mycol,mydataA,mydataB - } CATCH(e) { + } CATCH(e) { gras_socket_close(socket[i]); RETHROW0("Unable to send the msg : %s"); } } + INFO1("send to slave to begin a %d th step",step); + /* wait for computing and slave messages exchange */ + i=0; - /* wait for computing and sensor messages exchange */ - for (i=0; i< nbr_sensor; i++){ + while ( i< SLAVE_COUNT){ TRY { - gras_msg_wait(600,gras_msgtype_by_name(""),&from,&mydata); + gras_msg_wait(1300,gras_msgtype_by_name("step_ack"),&from,&step_ack); } CATCH(e) { - RETHROW0("I Can't get a init Data message from Maestro : %s"); + RETHROW0("I Can't get a Ack step message from slave : %s"); } + i++; + INFO3("Receive Ack step ack from %s (got %d of %d)", + gras_socket_peer_name(from), + i, SLAVE_COUNT); } } /********************************* gather ***************************************/ - - int ask_result=0; + + ask_result=0; for( i=1;i< argc;i++){ gras_msg_send(socket[i],gras_msgtype_by_name("ask_result"),&ask_result); + INFO1("Send (Ask Result) message to %s",gras_socket_peer_name(socket[i])); } /* wait for results */ for( i=1;i< argc;i++){ @@ -252,28 +231,34 @@ matrix_t A,B,C; C.data[(result.i-1)*MATRIX_SIZE+(result.j-1)]=result.value; } /* end of gather */ + INFO0 ("The Result of Multiplication is :"); display(C); return 0; -} /* end_of_maestro */ +} /* end_of_master */ /* ********************************************************************** - * Sensor code + * slave code * **********************************************************************/ -int sensor(int argc,char *argv[]) { +int slave(int argc,char *argv[]) { xbt_ex_t e; - static int bC=0; - static int myrow,mycol; - static double mydataA,mydataB; - int bA,bB; - int step,l,result=0; + int step,port,l,result_ack=0; + double bA,bB; + + int myrow,mycol; + double mydataA,mydataB; + double bC=0; + +// static end_step; - gras_socket_t from; /* to recive from server for steps */ + result_t result; + + gras_socket_t from,sock; /* to recive from server for steps */ - gras_socket_t socket_row[2],socket_column[2]; /* sockets for brodcast to other sensor */ + gras_socket_t socket_row[MATRIX_SIZE-1],socket_column[MATRIX_SIZE-1]; /* sockets for brodcast to other slave */ /* Init the GRAS's infrastructure */ @@ -282,108 +267,118 @@ int sensor(int argc,char *argv[]) { /* Get arguments and create sockets */ port=atoi(argv[1]); + + /* Create my master socket */ + sock = gras_socket_server(port); + INFO2("Launch %s (port=%d)",argv[0],port); + gras_os_sleep(1); //wait to start all slaves + int i; for (i=1;i Myrow(%d)",step,myrow); + TRY { + gras_msg_wait(600,gras_msgtype_by_name("dataB"), + &from,&bB); + } CATCH(e) { + RETHROW0("I Can't get a data message from row : %s"); + } + INFO2("Recive data B (%.3g) from my neighbor: %s",bB,gras_socket_peer_name(from)); } - /* a column brodcast */ + /* a column brodcast */ if(mycol==step){ - for (l=1;l < MATRIX_SIZE ;l++){ - gras_msg_send(socket_column[l],gras_msgtype_by_name("data"), &mydataA); - bA=mydataA; - } - } - else - { - TRY { - gras_msg_wait(600,gras_msgtype_by_name("data"), - &from,&bA); - } CATCH(e) { - RETHROW0("I Can't get a data message from column : %s"); + for (l=1;l < MATRIX_SIZE ;l++){ + gras_msg_send(socket_row[l-1],gras_msgtype_by_name("dataA"), &mydataA); + bA=mydataA; + INFO1("send my data A (%.3g) to my (horizontal) neighbors",bA); } } + + if(mycol != step){ + TRY { + gras_msg_wait(1200,gras_msgtype_by_name("dataA"), + &from,&bA); + } CATCH(e) { + RETHROW0("I Can't get a data message from column : %s"); + } + INFO2("Recive data A (%.3g) from my neighbor : %s ",bA,gras_socket_peer_name(from)); + } bC+=bA*bB; - } - /* send a ack msg to Maestro */ + INFO1(">>>>>>>> My BC = %.3g",bC); + + /* send a ack msg to master */ gras_msg_send(from,gras_msgtype_by_name("step_ack"),&step); - INFO1("Send ack to maestro for to end %d th step",step); - + INFO1("Send ack to master for to end %d th step",step); + } if(step==MATRIX_SIZE-1) break; - + } while (step < MATRIX_SIZE); - /* wait Message from maestro to send the result */ - /*after finished the bC computing */ + /* wait Message from master to send the result */ + + result.value=bC; + result.i=myrow; + result.j=mycol; + TRY { - gras_msg_wait(600,gras_msgtype_by_name("result"), - &from,&result); + gras_msg_wait(600,gras_msgtype_by_name("ask_result"), + &from,&result_ack); } CATCH(e) { RETHROW0("I Can't get a data message from row : %s"); } - /* 5. send Result to the Maestro */ + /* send Result to master */ TRY { - gras_msg_send(from, gras_msgtype_by_name("result"),&bC); + gras_msg_send(from, gras_msgtype_by_name("result"),&result); } CATCH(e) { - gras_socket_close(from); + // gras_socket_close(from); RETHROW0("Failed to send PING to server: %s"); } - INFO3(">>>>>>>> Result: %d sent to %s:%d <<<<<<<<", + INFO3(">>>>>>>> Result: %.3f sent to %s:%d <<<<<<<<", bC, gras_socket_peer_name(from),gras_socket_peer_port(from)); /* Free the allocated resources, and shut GRAS down */ @@ -391,4 +386,4 @@ int sensor(int argc,char *argv[]) { gras_exit(); INFO0("Done."); return 0; -} /* end_of_sensor */ \ No newline at end of file +} /* end_of_slave */