void CBTF_Waitfor_MRNet_Shutdown() { Packet_t * p; p = (Packet_t *)malloc(sizeof(Packet_t)); Assert(p); if (CBTF_MRNet_netPtr && Network_is_ShutDown(CBTF_MRNet_netPtr)) { fprintf(stderr,"CBTF_Waitfor_MRNet_Shutdown -- already SHUTDOWN %d\n",getpid()); return; } if (CBTF_MRNet_netPtr) { /* get our stream */ Stream_t* stream = Network_get_Stream(CBTF_MRNet_netPtr,stream_id); int tag = 0; /* wait for FE to request the shutdown */ do { //fprintf(stderr,"CBTF_Waitfor_MRNet_Shutdown WAIT FOR FE request of shutdown %d\n",getpid()); if( CBTF_MRNet_netPtr && Network_recv(CBTF_MRNet_netPtr, &tag, p, &stream) != 1 ) { fprintf(stderr, "BE: receive failure\n"); break; } } while ( tag != 101 ); /* send the FE the acknowledgement of shutdown */ //fprintf(stderr,"CBTF_Waitfor_MRNet_Shutdown SENDS FE acknowledgement of shutdown %d\n",getpid()); if ( (Stream_send(stream, /*tag*/ 102, "%d", 102) == -1) || Stream_flush(stream) == -1 ) { fprintf(stderr, "CBTF_Waitfor_MRNet_Shutdown BE: stream::send() failure\n"); } /* Now wait for mrnet to shutdown */ if (CBTF_MRNet_netPtr && !Network_is_ShutDown(CBTF_MRNet_netPtr)) { //fprintf(stderr,"CBTF_Waitfor_MRNet_Shutdown Network_waitfor_ShutDown %d\n",getpid()); Network_waitfor_ShutDown(CBTF_MRNet_netPtr); } } /* delete out network pointer */ if (CBTF_MRNet_netPtr != NULL) { //fprintf(stderr,"CBTF_Waitfor_MRNet_Shutdown delete_Network_t CBTF_MRNet_netPtr %d\n",getpid()); delete_Network_t(CBTF_MRNet_netPtr); } if (p != NULL) free(p); //fprintf(stderr,"EXIT CBTF_Waitfor_MRNet_Shutdown %d\n",getpid()); }
void CBTF_MRNet_LW_sendToFrontend(const int tag, const int size, void *data) { const char* fmt_str = "%auc"; /* * No need to repeatedly check mrnet_connected within a while loop as is * typical with a condition variable because it is only ever set once by * CBTF_MRNet_LW_connect() above. */ pthread_mutex_lock(&mrnet_connected_mutex); if (!mrnet_connected) { pthread_cond_wait(&mrnet_connected_cond, &mrnet_connected_mutex); } pthread_mutex_unlock(&mrnet_connected_mutex); #if defined(ENABLE_CBTF_MRNET_PLAYBACK) if (playback_intercept(tag, (uint32_t)size, data)) { return; /* Squelch the actual sending of this message. */ } #endif #ifndef NDEBUG if (IsMRNetDebugEnabled) { fprintf(stderr,"[%d,%d] CBTF_MRNet_LW_sendToFrontend: sends message with tag %d\n", getpid(),monitor_get_thread_num(),tag); } #endif Stream_t* CBTF_MRNet_stream = Network_get_Stream(CBTF_MRNet_netPtr,stream_id); if ( (Stream_send(CBTF_MRNet_stream, tag, fmt_str, data, size) == -1) || Stream_flush(CBTF_MRNet_stream) == -1 ) { fprintf(stderr, "BE: stream::send() failure\n"); } fflush(stdout); fflush(stderr); }
int main(int argc, char **argv) { Stream_t * stream; Packet_t* pkt = (Packet_t*)malloc(sizeof(Packet_t)); Network_t * net; int tag=0; uint32_t recv_array_len=0; void * recv_array=NULL; int success=1; Rank my_rank; assert(pkt); net = Network_CreateNetworkBE( argc, argv ); my_rank = Network_get_LocalRank(net); do{ if ( Network_recv(net, &tag, pkt, &stream) != 1){ fprintf(stderr, "BE(%d): net::recv() failure\n", my_rank); } recv_array=NULL; switch(tag){ case PROT_CHAR: //fprintf(stdout, "BE(%d): Processing PROT_CHAR_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%ac", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%ac) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%ac", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%ac) failure\n", my_rank); success=0; } break; case PROT_UCHAR: //fprintf(stdout, "BE(%d): Processing PROT_UCHAR_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%auc", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%auc) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%auc", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%auc) failure\n", my_rank); success=0; } break; case PROT_INT: //fprintf(stdout, "BE(%d): Processing PROT_INT_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%ad", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%ad) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%ad", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%ad) failure\n", my_rank); success=0; } break; case PROT_UINT: //fprintf(stdout, "BE(%d): Processing PROT_UINT_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%aud", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%aud) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%aud", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%aud) failure\n", my_rank); success=0; } break; case PROT_SHORT: //fprintf(stdout, "BE(%d): Processing PROT_SHORT_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%ahd", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%ahd) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%ahd", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%ahd) failure\n", my_rank); success=0; } break; case PROT_USHORT: //fprintf(stdout, "BE(%d): Processing PROT_USHORT_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%auhd", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%auhd) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%auhd", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%auhd) failure\n", my_rank); success=0; } break; case PROT_LONG: //fprintf(stdout, "BE(%d): Processing PROT_LONG_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%ald", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%ald) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%ald", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%ald) failure\n", my_rank); success=0; } break; case PROT_ULONG: //fprintf(stdout, "BE(%d): Processing PROT_ULONG_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%auld", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%auld) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%auld", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%auld) failure\n", my_rank); success=0; } break; case PROT_FLOAT: //fprintf(stdout, "BE(%d): Processing PROT_FLOAT_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%af", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%af) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%af", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%af) failure\n", my_rank); success=0; } break; case PROT_DOUBLE: //fprintf(stdout, "BE(%d): Processing PROT_DOUBLE_ARRAY ...\n", my_rank); if( Packet_unpack(pkt, "%alf", &recv_array, &recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::unpack(%%alf) failure\n", my_rank); success=0; } if( Stream_send(stream,tag, "%alf", recv_array, recv_array_len) == -1 ){ fprintf(stderr, "BE(%d): stream::send(%%alf) failure\n", my_rank); success=0; } break; case PROT_EXIT: //fprintf(stdout, "BE(%d): Processing PROT_EXIT ...\n", my_rank); break; default: fprintf(stderr, "BE(%d): Unknown Protocol: %d\n", my_rank, tag); exit(-1); } //fflush(stdout); if( tag != PROT_EXIT ) { if( Stream_flush(stream) == -1 ) { fprintf(stderr, "BE(%d): stream::flush() failure\n", my_rank); return -1; } } } while( tag != PROT_EXIT ); if( pkt != NULL ) free(pkt); // wait for final teardown packet from FE; this will cause // us to exit Network_waitfor_ShutDown(net); if( net != NULL ) delete_Network_t(net); return 0; }
void* BackendThreadMain( void* arg ) { info_t* rinfo = (info_t*)arg; int32_t recv_int=0; int tag; Packet_t* p; Stream_t* stream; Network_t * net = NULL; const char* fmt_str = "%d"; char parHostname[64], parPort[10], parRank[10], myRank[10]; Rank mRank = rinfo->mrnet_rank; sprintf(myRank, "%d", mRank); if( getParentInfo(connfile, rinfo->mpi_rank, parHostname, parPort, parRank) != 0 ) { fprintf(stderr, "Failed to parse connections file\n"); return NULL; } assert( rinfo->argc == 6 ); fprintf( stdout, "BE[%d] on %s connecting to %s:%s[%s]\n", mRank, rinfo->argv[4], parHostname, parPort, parRank ); rinfo->argv[1] = parHostname; rinfo->argv[2] = parPort; rinfo->argv[3] = parRank; rinfo->argv[5] = myRank; net = Network_CreateNetworkBE( rinfo->argc, rinfo->argv ); p = (Packet_t*) calloc( (size_t)1, sizeof(Packet_t) ); do { if( Network_recv(net, &tag, p, &stream) != 1 ) { printf("net->recv() failure\n"); tag = PROT_EXIT; } switch( tag ) { case PROT_INT: if( Packet_unpack( p, fmt_str, &recv_int) == -1 ) { printf("Packet_unpack() failure\n"); return NULL; } fprintf(stdout, "BE[%d]: received int = %d\n", mRank, recv_int); if( (Stream_send(stream, PROT_INT, fmt_str, recv_int) == -1) || (Stream_flush(stream) == -1 ) ) { printf("Stream_send() failure\n"); return NULL; } break; case PROT_EXIT: fprintf(stdout, "BE[%d]: received PROT_EXIT\n", mRank); break; default: fprintf(stdout, "BE[%d]: Unknown Protocol %d\n", mRank, tag); tag = PROT_EXIT; break; } fflush(stdout); fflush(stderr); } while ( tag != PROT_EXIT ); // FE delete of the network will cause us to exit, wait for it Network_waitfor_ShutDown(net); delete_Network_t(net); return NULL; }