/* ******************************************************************************** * * BROADCAST IN AMDC USING DIRECT COMMUNICATION * -------------------------------------------- * * 1) Each node sends a broadcast message, containing its own id, to every other * node, including itself. * * 2) When a broadcast is received, the sender's id from the message and the * receiver's id are sent to node 0 in a print message. * * 3) When a print message is received, it will print the ids contained in the * message, then put the message into the table at that location. * * 4) When the number of print messages at that location is equal to * (num_of_nodes * num_of_nodes), then all expected prints have been received * and the program will terminate. * ******************************************************************************** */ #include #include "amdc96.h" /* -------------------------------------------------------------------------------- Definitions -------------------------------------------------------------------------------- */ #define BROADCAST 1L #define PRINT 2L typedef struct { int sender_id; } Broadcast; typedef struct { int sender_id; int receiver_id; } Print; /* -------------------------------------------------------------------------------- Function print_op -------------------------------------------------------------------------------- */ print_op(Message msg, Location loc) { Print *print = msg; printf("From %ld to %ld\n", print->sender_id, print->receiver_id); amdc_putLoc(loc, msg); } /* -------------------------------------------------------------------------------- Function broadcast_op -------------------------------------------------------------------------------- */ broadcast_op(Message msg, Location loc) { LocName loc_name_0 = amdc_0DLocName(AMDC_AbsoluteSymbol(0, AMDC_SYMB_X0)); Broadcast *broadcast = msg; Print *print; print = amdc_newMsg((Script)print_op, PRINT, sizeof(Print)); print->sender_id = broadcast->sender_id; amdc_freeMsg(broadcast); print->receiver_id = amdc_getMyId(); amdc_sendTo(print, loc_name_0); } /* -------------------------------------------------------------------------------- Function Main -------------------------------------------------------------------------------- */ Main(int argc, char *argv[]) { Location my_loc; LocName loc_name; const int num_of_nodes = amdc_getNumNodes(); int num_of_nodes_squared; int count; int my_id; Broadcast *broadcast; my_id = amdc_getMyId(); loc_name = amdc_0DLocName(AMDC_AbsoluteSymbol(0, AMDC_SYMB_X0)); loc_name.X[0] = my_id; /* Send broadcast to every node */ for(count = 0; count < num_of_nodes; count++) { broadcast = amdc_newMsg((Script)broadcast_op, BROADCAST, sizeof(Broadcast)); broadcast->sender_id = my_id; loc_name.X[0]++; amdc_sendTo(broadcast, loc_name); } if(my_id == 0) { my_loc = amdc_getMyLoc(); num_of_nodes_squared = num_of_nodes * num_of_nodes; printf("Wait for %ld messages\n", num_of_nodes_squared); /* Execute pollBlock until all print messages received */ while(amdc_numLoc(my_loc, PRINT) < num_of_nodes_squared) amdc_pollBlock(); printf("Received all messages!\n"); /* Free all print messages at my_loc */ while(amdc_presentLoc(my_loc, PRINT)) amdc_freeMsg(amdc_getLoc(my_loc, PRINT)); printf("Removed messages\n"); } else { /* Execute pollBlock forever */ while(1) amdc_pollBlock(); } }