//number of atoms, types and backbone atoms void send_struct(int *nback, int iproc, int nprocs, int *nat, int *ntypes, MPI_Status astatus) { int buffer_size, position,i; char buffer[buffer_max]; buffer_size = 3*sizeof(int); if(buffer_size>buffer_max) { fprintf(stderr,"Buffer too small\n"); MPI_Finalize(); exit(1); } if(iproc==0) { position = 0; MPI_Pack(nat,1,MPI_INT,buffer,buffer_size,&position,MPI_COMM_WORLD); MPI_Pack(ntypes,1,MPI_INT,buffer,buffer_size,&position,MPI_COMM_WORLD); MPI_Pack(nback,1,MPI_INT,buffer,buffer_size,&position,MPI_COMM_WORLD); for(i=1;i<nprocs;i++) MPI_Send(buffer,position,MPI_PACKED,i, 200+i, MPI_COMM_WORLD); } if(iproc!=0) { MPI_Recv(buffer,buffer_size,MPI_PACKED,0, 200+iproc, MPI_COMM_WORLD, &astatus); position=0; MPI_Unpack(buffer,buffer_size,&position,nat,1,MPI_INT,MPI_COMM_WORLD); MPI_Unpack(buffer,buffer_size,&position,ntypes,1,MPI_INT,MPI_COMM_WORLD); MPI_Unpack(buffer,buffer_size,&position,nback,1,MPI_INT,MPI_COMM_WORLD); } return; }
void pack_solution(void* buff, int buff_size, int* pos, solution_vector vec, float score, MPI_Comm comm, void* problem_data) { color_assignment* cassign = (color_assignment*) vec; MPI_Pack(&score, 1, MPI_FLOAT, buff, buff_size, pos, comm); MPI_Pack(&cassign->curr_length, 1, MPI_INT, buff, buff_size, pos, comm); MPI_Pack(cassign->vertex_colors, cassign->max_length, MPI_INT, buff, buff_size, pos, comm); }
// Pack DCell into buffer: [x, y, size, coorX, coorY] void DCellPack(DCell cell, void *buffer, int bufSize, int *pos, MPI_Comm comm ) { MPI_Pack(&cell->x,1,MPI_DOUBLE,buffer,bufSize,pos,comm); MPI_Pack(&cell->y,1,MPI_DOUBLE,buffer,bufSize,pos,comm); MPI_Pack(&cell->size,1,MPI_INT,buffer,bufSize,pos,comm); MPI_Pack(cell->coorX,cell->size,MPI_DOUBLE,buffer,bufSize,pos,comm); MPI_Pack(cell->coorY,cell->size,MPI_DOUBLE,buffer,bufSize,pos,comm); //TODO: Record the size of 'pos' to figure out the typical size of the pack buffer }
/* non-blocking send */ int COM_Send (void *pattern) { COMPATTERN *cp = pattern; int *rankmap = cp->rankmap, *send_position = cp->send_position, (*send_sizes) [3] = cp->send_sizes, *send_rank = cp->send_rank, *recv_rank = cp->recv_rank, (*recv_sizes) [3] = cp->recv_sizes, send_count = cp->send_count, recv_count = cp->recv_count, tag = cp->tag, nsend = cp->nsend, i, j; char **send_data = cp->send_data, **recv_data = cp->recv_data; MPI_Request *send_req = cp->send_req, *recv_req = cp->recv_req; MPI_Comm comm = cp->comm; COMDATA *cd; for (i = 0; i < send_count; i ++) send_position [i] = 0; /* pack ints */ for (i = 0, cd = cp->send; i < nsend; i ++, cd ++) { if (cd->ints) { j = rankmap [cd->rank]; MPI_Pack (cd->i, cd->ints, MPI_INT, send_data [j], send_sizes [j][2], &send_position [j], comm); } } /* pack doubles */ for (i = 0, cd = cp->send; i < nsend; i ++, cd ++) { if (cd->doubles) { j = rankmap [cd->rank]; MPI_Pack (cd->d, cd->doubles, MPI_DOUBLE, send_data [j], send_sizes [j][2], &send_position [j], comm); } } /* send data */ for (i = 0; i < send_count; i ++) { MPI_Isend (send_data [i], send_sizes [i][2], MPI_PACKED, send_rank [i], tag, comm, &send_req [i]); } /* receive data */ for (i = 0; i < recv_count; i ++) { MPI_Irecv (recv_data [i], recv_sizes [i][2], MPI_PACKED, recv_rank [i], tag, comm, &recv_req [i]); } return cp->send_size; }
/* ------------------------------------------------------------------------- */ void RequestPrimalSol(char best_sol, char *stop, int probab, int *var_x, PrimalType *PrimalSol, MPI_Comm communicator) { void *values = NULL; int method = REQUEST_SOLUTION, sizeOfBuffer = 0, position = 0; char *buffer; sizeOfBuffer = ((sizeof(int)) + (sizeof(char)) + (sizeof(int))); buffer = malloc(sizeOfBuffer); MPI_Pack(&method, 1, MPI_INT, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(&best_sol, 1, MPI_CHAR, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(&probab, 1, MPI_INT, buffer, sizeOfBuffer, &position, communicator); MPI_Send(buffer, position, MPI_PACKED, 0, 1, communicator); free(buffer); MPI_Status status; int positionRecv = 0; int sizeOfMessage = 0; MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, communicator, &status); MPI_Get_count(&status, MPI_PACKED, &sizeOfMessage); buffer = malloc(sizeOfMessage); MPI_Recv(buffer, sizeOfMessage, MPI_PACKED, MPI_ANY_SOURCE, MPI_ANY_TAG, communicator, &status); MPI_Unpack(buffer, sizeOfMessage, &positionRecv, stop, 1, MPI_CHAR, communicator); if (!*stop) { MPI_Unpack(buffer, sizeOfMessage, &positionRecv, PrimalSol->agent, TotalAgents, MPI_CHAR, communicator); MPI_Unpack(buffer, sizeOfMessage, &positionRecv, var_x, nb_col, MPI_INT, communicator); PrimalSol->var_x = var_x; MPI_Unpack(buffer, sizeOfMessage, &positionRecv, &PrimalSol->proc_time, 1, MPI_INT, communicator); MPI_Unpack(buffer, sizeOfMessage, &positionRecv, &PrimalSol->value, 1, MPI_DOUBLE, communicator); MPI_Unpack(buffer, sizeOfMessage, &positionRecv, &PrimalSol->agent_ID, 1, MPI_UNSIGNED, communicator); } free(buffer); //printf("\n\n === Fim o empacotamento em agentrot RequestPrimal \n\n"); }
/* mpi_worker() * The main control for an MPI worker process. */ static void mpi_worker(ESL_GETOPTS *go, struct cfg_s *cfg) { int xstatus = eslOK; int status; P7_HMM *hmm = NULL; char *wbuf = NULL; double *xv = NULL; /* result: array of N scores */ int *av = NULL; /* optional result: array of N alignment lengths */ int wn = 0; char errbuf[eslERRBUFSIZE]; int pos; /* Worker initializes */ if ((status = minimum_mpi_working_buffer(go, cfg->N, &wn)) != eslOK) xstatus = status; ESL_ALLOC(wbuf, wn * sizeof(char)); ESL_ALLOC(xv, cfg->N * sizeof(double) + 2); if (esl_opt_GetBoolean(go, "-a")) ESL_ALLOC(av, cfg->N * sizeof(int)); /* Main worker loop */ while (p7_hmm_mpi_Recv(0, 0, MPI_COMM_WORLD, &wbuf, &wn, &(cfg->abc), &hmm) == eslOK) { if (esl_opt_GetBoolean(go, "--recal")) { if (( status = recalibrate_model(go, cfg, errbuf, hmm)) != eslOK) goto CLEANERROR; } if ((status = process_workunit(go, cfg, errbuf, hmm, xv, av)) != eslOK) goto CLEANERROR; pos = 0; MPI_Pack(&status, 1, MPI_INT, wbuf, wn, &pos, MPI_COMM_WORLD); MPI_Pack(xv, cfg->N, MPI_DOUBLE, wbuf, wn, &pos, MPI_COMM_WORLD); if (esl_opt_GetBoolean(go, "-a")) MPI_Pack(av, cfg->N, MPI_INT, wbuf, wn, &pos, MPI_COMM_WORLD); MPI_Send(wbuf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD); p7_hmm_Destroy(hmm); } free(wbuf); free(xv); if (av != NULL) free(av); return; CLEANERROR: pos = 0; MPI_Pack(&status, 1, MPI_INT, wbuf, wn, &pos, MPI_COMM_WORLD); MPI_Pack(errbuf, eslERRBUFSIZE, MPI_CHAR, wbuf, wn, &pos, MPI_COMM_WORLD); MPI_Send(wbuf, pos, MPI_PACKED, 0, 0, MPI_COMM_WORLD); if (wbuf != NULL) free(wbuf); if (hmm != NULL) p7_hmm_Destroy(hmm); if (xv != NULL) free(xv); if (av != NULL) free(av); return; ERROR: p7_Fail("Allocation error in mpi_worker"); }
/* ------------------------------------------------------------------------- O unico parametro a ser passado para esse agente e' o numero do processa- dor em que esta executando o 'ns'. ------------------------------------------------------------------------- */ main(int argc,char *argv[]) { MPI_Init(&argc, &argv); int method = 0, flagConnectMD = 1, flagConnectMP = 1, position = 0; char portMD[MPI_MAX_PORT_NAME], portMP[MPI_MAX_PORT_NAME], bufferMD[100], bufferMP[100]; MPI_Comm commServerMD; MPI_Comm commServerMP; flagConnectMD = MPI_Lookup_name("ServerMD", MPI_INFO_NULL, portMD); flagConnectMP = MPI_Lookup_name("ServerMP", MPI_INFO_NULL, portMP); method = DUMP_MEMORY; int i = 0; int message = 1224; if (!flagConnectMD) { MPI_Comm_connect(portMD, MPI_INFO_NULL, 0, MPI_COMM_SELF, &commServerMD); MPI_Pack(&method, 1, MPI_INT, bufferMD, 100, &position, commServerMD); MPI_Send(bufferMD, position, MPI_PACKED, 0, 1, commServerMD); MPI_Send(&message, 1, MPI_INT, 0, 1, commServerMD); MPI_Comm_disconnect(&commServerMD); } else printf("\n\n* Memoria de solucoes duais nao iniciada *\n\n"); position = 0; if (!flagConnectMP) { MPI_Comm_connect(portMP, MPI_INFO_NULL, 0, MPI_COMM_SELF, &commServerMP); MPI_Pack(&method, 1, MPI_INT, bufferMP, 100, &position, commServerMP); MPI_Send(bufferMP, position, MPI_PACKED, 0, 1, commServerMP); MPI_Send(&message, 1, MPI_INT, 0, 1, commServerMP); MPI_Comm_disconnect(&commServerMP); } else printf("\n\n* Memoria de solucoes primais nao iniciada *\n\n"); if ((flagConnectMD) && (flagConnectMP)){ printf("\n\n* Nenhum dos servidores de memoria do ATeam esta ativo *\n");fflush(stdout); }else printf("\n* A escrita do conteudo das memorias ativas foi realizada *\n"); MPI_Finalize(); }
int main(int argc, char** argv) { int rank, numprocs; int intA; double doubleB; unsigned long ulongC; int packsize, position; char packbuf[100]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); do { if(rank == 0) { fprintf(stdout, "Please input an int, a double, and a ull:\n"); scanf_s("%d%lf%lu", &intA, &doubleB, &ulongC); packsize = 0; MPI_Pack(&intA, 1, MPI_INT, packbuf, 100, &packsize, MPI_COMM_WORLD); printf("packsize = %d\t", packsize); printf("size(int) = %d\n", sizeof(int)); MPI_Pack(&doubleB, 1, MPI_DOUBLE, packbuf, 100, &packsize, MPI_COMM_WORLD); printf("packsize = %d\t", packsize); printf("size(double) = %d\n", sizeof(double)); MPI_Pack(&ulongC, 1, MPI_UNSIGNED_LONG, packbuf, 100, &packsize, MPI_COMM_WORLD); printf("packsize = %d\t", packsize); printf("size(unsigned long) = %d\n", sizeof(unsigned long)); } MPI_Bcast(&packsize, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(packbuf, packsize, MPI_PACKED, 0, MPI_COMM_WORLD); if(rank != 0) { position = 0; MPI_Unpack(packbuf, packsize, &position, &intA, 1, MPI_INT, MPI_COMM_WORLD); printf("postion= %d\t", position); MPI_Unpack(packbuf, packsize, &position, &doubleB, 1, MPI_DOUBLE, MPI_COMM_WORLD); printf("postion= %d\t", position); MPI_Unpack(packbuf, packsize, &position, &ulongC, 1, MPI_UNSIGNED_LONG, MPI_COMM_WORLD); printf("postion= %d\n", position); printf("rank %d got int %d, double %lf, and ull %lu\n", rank, intA, doubleB, ulongC); } MPI_Barrier(MPI_COMM_WORLD); } while (intA >= 0); printf("myid = %d, %d, %lf\n", rank, intA, doubleB); MPI_Finalize(); return 0; }
void sendFinishedJobNotification (finishedJobNotification *fjn) { char *buff = malloc(sizeof(finishedJobNotification)); int pos = 0; MPI_Pack(&(fjn->category), 1, MPI_INT, buff, sizeof(finishedJobNotification), &pos, MPI_COMM_WORLD); MPI_Pack(&(fjn->ranAtPU), 1, MPI_INT, buff, sizeof(finishedJobNotification), &pos, MPI_COMM_WORLD); MPI_Pack(&(fjn->succeeded), 1, MPI_BYTE, buff, sizeof(finishedJobNotification), &pos, MPI_COMM_WORLD); MPI_Pack(&(fjn->overheads), 1, MPI_DOUBLE, buff, sizeof(finishedJobNotification), &pos, MPI_COMM_WORLD); MPI_Pack(&(fjn->executionTime), 1, MPI_DOUBLE, buff, sizeof(finishedJobNotification), &pos, MPI_COMM_WORLD); sendMsg(buff, pos, MPI_PACKED, defaultSchedID, COMM_TAG_PUSTATUS, MPI_STATUS_IGNORE); free(buff); }
/* communicate integers and doubles accodring * to the pattern computed by COMALL_Pattern */ int COMALL_Repeat (void *pattern) { COMALLPATTERN *pp = pattern; COMDATA *cd; int i; for (i = 0; i < pp->ncpu; i ++) pp->send_position [i] = pp->recv_position [i] = 0; /* pack ints */ for (i = 0, cd = pp->send; i < pp->nsend; i ++, cd ++) { if (cd->ints) { MPI_Pack (cd->i, cd->ints, MPI_INT, &pp->send_data [pp->send_disps [cd->rank]], pp->send_counts [cd->rank], &pp->send_position [cd->rank], pp->comm); } } /* pack doubles */ for (i = 0, cd = pp->send; i < pp->nsend; i ++, cd ++) { if (cd->doubles) { MPI_Pack (cd->d, cd->doubles, MPI_DOUBLE, &pp->send_data [pp->send_disps [cd->rank]], pp->send_counts [cd->rank], &pp->send_position [cd->rank], pp->comm); } } #if DEBUG for (i = 0; i < pp->ncpu; i ++) { ASSERT_DEBUG (pp->send_position [i] <= pp->send_counts [i], "Incorrect packing"); } #endif /* all to all send and receive */ MPI_Alltoallv (pp->send_data, pp->send_counts, pp->send_disps, MPI_PACKED, pp->recv_data, pp->recv_counts, pp->recv_disps, MPI_PACKED, pp->comm); if (pp->recv_size) { /* unpack data */ for (i = 0; i < pp->ncpu; i ++) { MPI_Unpack (&pp->recv_data [pp->recv_disps [i]], pp->recv_counts [i], &pp->recv_position [i], pp->recv [i].i, pp->recv [i].ints, MPI_INT, pp->comm); MPI_Unpack (&pp->recv_data [pp->recv_disps [i]], pp->recv_counts [i], &pp->recv_position [i], pp->recv [i].d, pp->recv [i].doubles, MPI_DOUBLE, pp->comm); } } return pp->send_size; }
void serializeSolution(char* buffer, int& position, vector<Move>& _solution) { int data = _solution.size(); MPI_Pack(&data, 1, MPI_INT, buffer, BUFFER_SIZE, &position, MPI_COMM_WORLD); for (vector<Move>::const_iterator it = _solution.begin(); it < _solution.end(); ++it) { it->serialize(buffer, position); } }
/* Function: p7_hmm_mpi_Send() * Synopsis: Send an HMM as an MPI work unit. * * Purpose: Sends an HMM <hmm> as a work unit to MPI process * <dest> (where <dest> ranges from 0..<nproc-1>), tagged * with MPI tag <tag>, for MPI communicator <comm>, as * the sole workunit or result. * * Work units are prefixed by a status code indicating the * number of HMMs sent. If <hmm> is <NULL>, this code is 0, * and <_Recv()> interprets such a unit as an EOD * (end-of-data) signal, a signal to cleanly shut down * worker processes. * * In order to minimize alloc/free cycles in this routine, * caller passes a pointer to a working buffer <*buf> of * size <*nalloc> characters. If necessary (i.e. if <hmm> is * too big to fit), <*buf> will be reallocated and <*nalloc> * increased to the new size. As a special case, if <*buf> * is <NULL> and <*nalloc> is 0, the buffer will be * allocated appropriately, but the caller is still * responsible for free'ing it. * * Returns: <eslOK> on success; <*buf> may have been reallocated and * <*nalloc> may have been increased. * * Throws: <eslESYS> if an MPI call fails; <eslEMEM> if a malloc/realloc * fails. In either case, <*buf> and <*nalloc> remain valid and useful * memory (though the contents of <*buf> are undefined). * * Note: Compare to p7_hmmfile_WriteBinary(). The two operations (sending * an HMM via MPI, or saving it as a binary file to disk) are * similar. */ int p7_hmm_mpi_Send(const P7_HMM *hmm, int dest, int tag, MPI_Comm comm, char **buf, int *nalloc) { int n = 0; int code; int sz, pos; int status; /* Figure out size. We always send at least a status code (0=EOD=nothing sent) */ if ( MPI_Pack_size(1, MPI_INT, comm, &sz) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi pack size failed"); n += sz; if ((status = p7_hmm_mpi_PackSize(hmm, comm, &sz)) != eslOK) return status; n += sz; /* Make sure the buffer is allocated appropriately */ if (*buf == NULL || n > *nalloc) { ESL_REALLOC(*buf, sizeof(char) * n); *nalloc = n; } /* Pack the status code and HMM into the buffer */ /* The status code is the # of HMMs being sent as one MPI message; here 1 or 0 */ pos = 0; code = (hmm ? 1 : 0); if (MPI_Pack(&code, 1, MPI_INT, *buf, n, &pos, comm) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi pack failed"); if (hmm && (status = p7_hmm_mpi_Pack(hmm, *buf, n, &pos, comm)) != eslOK) return status; /* Send the packed HMM to the destination. */ if (MPI_Send(*buf, n, MPI_PACKED, dest, tag, comm) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi send failed"); return eslOK; ERROR: return status; }
/* Extract the source array into the dest array using the DARRAY datatype. "count" integers are returned in destArray */ int PackUnpack( MPI_Datatype darraytype, const int srcArray[], int destArray[], int count ) { int packsize, position; int *packArray; MPI_Type_commit( &darraytype ); MPI_Pack_size( 1, darraytype, MPI_COMM_SELF, &packsize ); packArray = (int *)malloc( packsize ); if (!packArray) { fprintf( stderr, "Unable to allocate pack array of size %d\n", packsize ); MPI_Abort( MPI_COMM_WORLD, 1 ); exit(1); } position = 0; MPI_Pack( (int*)srcArray, 1, darraytype, packArray, packsize, &position, MPI_COMM_SELF ); packsize = position; position = 0; MPI_Unpack( packArray, packsize, &position, destArray, count, MPI_INT, MPI_COMM_SELF ); free( packArray ); return 0; }
int main(int argc, char** argv) { char* mpi_inbuf; char* mpi_outbuf; char* farc_inbuf; char* farc_outbuf; MPI_Init(&argc, &argv); test_start("pack(1, hvector[[int], count=2, blklen=2, stride=-5*sizeof(int)])"); init_buffers(20*sizeof(int), &mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); farc::DDT_Init(); farc::Datatype* t1 = new farc::PrimitiveDatatype(farc::PrimitiveDatatype::INT); farc::Datatype* t2 = new farc::HVectorDatatype(2, 2, -5*sizeof(int), t1); farc::DDT_Commit(t2); farc::DDT_Pack(farc_inbuf+10*sizeof(int), farc_outbuf, t2, 1); MPI_Datatype newtype; MPI_Type_hvector(2, 2, -5*sizeof(int), MPI_INT, &newtype); MPI_Type_commit(&newtype); int position = 0; MPI_Pack(mpi_inbuf+10*sizeof(int), 1, newtype, mpi_outbuf, 20*sizeof(int), &position, MPI_COMM_WORLD); int res = compare_ddt_info(newtype, t2); res = compare_buffers(20*sizeof(int), &mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); // inspect_buffers(20*sizeof(int), &mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); free_buffers(&mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); test_result(res); MPI_Finalize(); return 0; }
FC_FUNC( mpi_pack , MPI_PACK ) ( void *inbuf, int *incount, int *datatype, void *outbuf, int *outsize, int *position, int *comm, int *ierror) { *ierror=MPI_Pack(inbuf, *incount,* datatype, outbuf, *outsize, position, *comm); }
int main(int argc, char** argv) { char* mpi_inbuf; char* mpi_outbuf; char* farc_inbuf; char* farc_outbuf; MPI_Init(&argc, &argv); test_start("pack(2, [MPI_INT])"); init_buffers(20*sizeof(int), &mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); farc::DDT_Init(); farc::Datatype* t1 = new farc::PrimitiveDatatype(farc::PrimitiveDatatype::INT); int res = compare_ddt_info(MPI_INT, t1); farc::DDT_Commit(t1); farc::DDT_Pack(farc_inbuf, farc_outbuf, t1, 2); int position = 0; MPI_Pack(mpi_inbuf, 2, MPI_INT, mpi_outbuf, 20*sizeof(int), &position, MPI_COMM_WORLD); res += compare_buffers(20*sizeof(int), &mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); free_buffers(&mpi_inbuf, &farc_inbuf, &mpi_outbuf, &farc_outbuf); test_result(res); MPI_Finalize(); return 0; }
void message::pack(byte *buf, const size_t buf_size, int *pos, MPI_Comm comm) const { MPI_Pack((void*)&id, 1, MPI_UNSIGNED, buf, buf_size, pos, comm); data->pack(buf, buf_size, pos, comm); }
/* Send eof = true to destination rank dst. * Signals to terminate receive requests */ void SendEOF(DWorld world, int dst) { int tag = 0; int eof = 1; // EOF IS TRUE int pos = 0; MPI_Pack(&eof,1,MPI_INT,world->sPack,world->bufSize,&pos,world->comm); MPI_Send(world->sPack,pos,MPI_PACKED,dst,tag,world->comm); }
/* Send DCell located at array index idx to * destination rank dst. Append eof = false * to pack buffer first. */ void SendDCell(DWorld world, int idx, int dst) { int tag = 0; int eof = 0; // EOF IS FALSE int pos = 0; MPI_Pack(&eof,1,MPI_INT,world->sPack,world->bufSize,&pos,world->comm); DCellPack(world->localcells[idx],world->sPack,world->bufSize,&pos, world->comm); MPI_Ssend(world->sPack,pos,MPI_PACKED,dst,tag,world->comm); }
//polymer status struct s_polymer *send_pol(int iproc, int nprocs, int nback, MPI_Datatype Backtype, MPI_Datatype Sidetype, MPI_Datatype Rottype, struct s_polymer *startp, MPI_Status astatus, int npol, int shell, int nosidechains) { int i, j, k, l,position,buffer_size; char buffer[buffer_max]; (startp+npol)->nback = nback; buffer_size=(sizeof(struct s_back)*nback); // fprintf(stderr,"send_pol: BUFFER SIZE IS %d\n",buffer_size); if(buffer_size>buffer_max) { fprintf(stderr,"Buffer too small\n"); MPI_Finalize(); exit(1); } if(iproc==0) { position=0; for(j=0;j<nback;j++) MPI_Pack(((startp+npol)->back)+j,1,Backtype,buffer,buffer_size,&position,MPI_COMM_WORLD); for(i=1; i<nprocs; i++) MPI_Send(buffer,position,MPI_PACKED,i, 300+100*i, MPI_COMM_WORLD); } if(iproc!=0) { MPI_Recv(buffer,buffer_size,MPI_PACKED,0, 300+100*iproc, MPI_COMM_WORLD, &astatus); position=0; for(j=0;j<nback;j++) MPI_Unpack(buffer,buffer_size,&position,((startp+npol)->back)+j,1,Backtype,MPI_COMM_WORLD); } if(!(nosidechains)) { if(iproc==0) { for(i=1; i<nprocs; i++) for(j=0; j<nback;j++) for(k=0; k<((startp->back)+j)->nside;k++) MPI_Send(((((startp+npol)->back)+j)->side)+k, 1, Sidetype, i, 2000+100*i+10*j+k, MPI_COMM_WORLD); } if(iproc!=0) for(j=0; j<nback;j++) for(k=0; k<((startp->back)+j)->nside;k++) MPI_Recv(((((startp+npol)->back)+j)->side)+k, 1, Sidetype, 0, 2000+100*iproc+10*j+k, MPI_COMM_WORLD, &astatus); if(iproc==0) for(i=1; i<nprocs; i++) for(j=0; j<nback;j++) for(k=0; k<((startp->back)+j)->nside;k++) for(l=0; l<((startp->back)+j)->nrot; l++) MPI_Send(((((((startp+npol)->back)+j)->side)+k)->rot)+l, 1, Rottype, i, 10000+1000*i+100*j+10*k+l, MPI_COMM_WORLD); if(iproc!=0) for(j=0; j<nback;j++) for(k=0; k<((startp->back)+j)->nside;k++) for(l=0; l<((startp->back)+j)->nrot; l++) MPI_Recv(((((((startp+npol)->back)+j)->side)+k)->rot)+l, 1, Rottype, 0, 10000+1000*iproc+100*j+10*k+l , MPI_COMM_WORLD, &astatus); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(iproc!=0) { for(i=0; i<nback; i++) ((startp+npol)->vback)[(((startp+npol)->back)+i)->ia ] = &(((((startp+npol)->back)+i)->pos)); for(i=0; i<nback; i++) for(j=0; j<(((startp+npol)->back)+i)->nside; j++) ((startp+npol)->vback)[ (((((startp+npol)->back)+i)->side)+j)->ia ] = &((((((startp+npol)->back)+i)->side)+j)->pos); } return startp; }
void slave(const struct fracInfo info) { MPI_Status status; int msgsize; struct fracData *data = malloc(sizeof(*data)); data->pixels = (unsigned char*)malloc(get_max_work_size(&info)*sizeof(unsigned char)); // Allocate buffers int membersize, emptysize, fullsize; int position; char *buffer; //Contains no pixel data MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &membersize); emptysize = membersize; MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &membersize); emptysize += membersize; MPI_Pack_size(get_max_work_size(&info), MPI_UNSIGNED_CHAR, MPI_COMM_WORLD, &membersize); fullsize = emptysize+membersize; buffer = malloc(fullsize); while(1) { // Recieve and unpack work MPI_Recv(buffer, emptysize, MPI_PACKED, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); // Check tag for work/die if(status.MPI_TAG == DIETAG) { return; } // Unpack work info position = 0; MPI_Get_count(&status, MPI_PACKED, &msgsize); MPI_Unpack(buffer, msgsize, &position, &data->startRow,1,MPI_INT,MPI_COMM_WORLD); MPI_Unpack(buffer, msgsize, &position, &data->nRows,1,MPI_INT,MPI_COMM_WORLD); // calcPixels calcPixels(&info, data); // Pack and send data back position = 0; MPI_Pack(&data->startRow,1,MPI_INT,buffer,fullsize,&position,MPI_COMM_WORLD); MPI_Pack(&data->nRows,1,MPI_INT,buffer,fullsize,&position,MPI_COMM_WORLD); MPI_Pack(data->pixels, data->nRows*info.nCols, MPI_UNSIGNED_CHAR,buffer,fullsize,&position,MPI_COMM_WORLD); MPI_Send(buffer, position, MPI_PACKED, 0, WORKTAG, MPI_COMM_WORLD); } }
int main(int argc, char *argv[]) { int errs = 0; int position, pack_size, i; int dis[2], blklens[2]; MPI_Datatype type; int send_buffer[60]; int recv_buffer[60]; int pack_buffer[1000]; MTest_Init(&argc, &argv); /* Initialize data in the buffers */ for (i = 0; i < 60; i++) { send_buffer[i] = i; recv_buffer[i] = -1; pack_buffer[i] = -2; } /* Create an indexed type with an empty first block */ dis[0] = 0; dis[1] = 20; blklens[0] = 0; blklens[1] = 40; MPI_Type_indexed(2, blklens, dis, MPI_INT, &type); MPI_Type_commit(&type); position = 0; MPI_Pack(send_buffer, 1, type, pack_buffer, sizeof(pack_buffer), &position, MPI_COMM_WORLD); pack_size = position; position = 0; MPI_Unpack(pack_buffer, pack_size, &position, recv_buffer, 1, type, MPI_COMM_WORLD); /* Check that the last 40 entries of the recv_buffer have the corresponding * elements from the send buffer */ for (i = 0; i < 20; i++) { if (recv_buffer[i] != -1) { errs++; fprintf(stderr, "recv_buffer[%d] = %d, should = -1\n", i, recv_buffer[i]); } } for (i = 20; i < 60; i++) { if (recv_buffer[i] != i) { errs++; fprintf(stderr, "recv_buffer[%d] = %d, should = %d\n", i, recv_buffer[i], i); } } MPI_Type_free(&type); MTest_Finalize(errs); MPI_Finalize(); return 0; }
void resultPack(Result* source, Buffer* destination) { if (source && destination) { int i; destination->size = 0; MPI_Pack(&(source->moveCount), 1, MPI_INT, destination->buffer, destination->capacity, &(destination->size), MPI_COMM_WORLD); for(i = 0; i < source->moveCount; i++) { positionPack(&(source->position[i]), destination); } } }
void StopAteam(MPI_Comm commMD, MPI_Comm commMP) { int method = 0, position = 0; char bufferMD[10], bufferMP[10]; method = STOP_ATEAM; if (commMD){ MPI_Pack(&method, 1, MPI_INT, bufferMD, 10, &position, commMD); MPI_Send(bufferMD, position, MPI_PACKED, 0, 1, commMD); } position = 0; if (commMP){ MPI_Pack(&method, 1, MPI_INT, bufferMP, 10, &position, commMP); MPI_Send(bufferMP, position, MPI_PACKED, 0, 1, commMP); } }
void message_set::pack(byte *buf, const size_t buf_size, MPI_Comm comm) const { int pos(0); unsigned short int size_msg((unsigned short int)size()); MPI_Pack(&size_msg, 1, MPI_UNSIGNED_SHORT, buf, buf_size, &pos, comm); for(list_messages::const_iterator it(lst.begin()); it != lst.end(); ++it) (*it)->pack(buf, buf_size, &pos, comm); }
/** * @brief Wrapper around MPI_Pack * * We check the error code to detect MPI errors and use the default communicator * MPI_WORLD. We also keep track of the calling function to print out relevant * error messages before aborting. This is done by wrapping the function call * using the MyMPI_Pack macro. * * @param inbuf Buffer with elements to pack * @param incount Number of elements to be packed * @param datatype MPI datatype of the elements * @param outbuf Buffer to pack in * @param outsize Total size of the pack buffer * @param position Current position in the pack buffer (is updated) * @param file Filename of the source code file that calls the function * @param function Function name of the calling function * @param line Line number in the source code file where the call happens */ inline void MyMPI_Pack_real(void* inbuf, int incount, MPI_Datatype datatype, void* outbuf, int outsize, int* position, std::string file, std::string function, int line) { int status = MPI_Pack(inbuf, incount, datatype, outbuf, outsize, position, MPI_COMM_WORLD); if(status != MPI_SUCCESS) { std::cerr << "Error during MPI_Pack!" << std::endl; std::cerr << MPIGlobal::rank << ": " << file << "::" << function << ":" << line << std::endl; MPI_Abort(MPI_COMM_WORLD, -1); } }
int main(int argc,char** argv) { int myrank; int position; int n; float a, b; char buffer[100]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (myrank == 0){ n = 4; a = 1.; b = 2.; printf("Rank=%d, a= %f, b= %f, and n=%d\n", myrank, a, b, n); position = 0; // packing MPI_Pack(&a, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD); MPI_Pack(&b, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD); MPI_Pack(&n, 1, MPI_INT, buffer, 100, &position, MPI_COMM_WORLD); // communication MPI_Bcast(buffer, 100, MPI_PACKED, 0, MPI_COMM_WORLD); } else { // communication MPI_Bcast(buffer, 100, MPI_PACKED, 0, MPI_COMM_WORLD); position = 0; // unpacking MPI_Unpack(buffer, 100, &position, &a, 1, MPI_FLOAT, MPI_COMM_WORLD); MPI_Unpack(buffer, 100, &position, &b, 1, MPI_FLOAT, MPI_COMM_WORLD); MPI_Unpack(buffer, 100, &position, &n, 1, MPI_INT, MPI_COMM_WORLD); printf("Rank=%d, a= %f, b=%f, and n=%d\n", myrank, a, b, n); } MPI_Finalize(); }
int MPIU_read_external32_conversion_fn(void *userbuf, MPI_Datatype datatype, int count, void *filebuf) { int position_i = 0; MPI_Aint position = 0; MPI_Aint bytes = 0; int mpi_errno = MPI_SUCCESS; int is_contig = 0; ADIOI_Datatype_iscontig(datatype, &is_contig); mpi_errno = MPI_Pack_external_size("external32", count, datatype, &bytes); if (mpi_errno != MPI_SUCCESS) goto fn_exit; if (is_contig) { mpi_errno = MPI_Unpack_external("external32", filebuf, bytes, &position, userbuf, count, datatype); if (mpi_errno != MPI_SUCCESS) goto fn_exit; } else { void *tmp_buf = NULL; tmp_buf = ADIOI_Malloc(bytes); if (!tmp_buf) { mpi_errno = MPI_ERR_NO_MEM; goto fn_exit; } mpi_errno = MPI_Pack(filebuf, count, datatype, tmp_buf, bytes, &position_i, MPI_COMM_WORLD); if (mpi_errno != MPI_SUCCESS) { ADIOI_Free(tmp_buf); goto fn_exit; } mpi_errno = MPI_Unpack_external("external32", tmp_buf, bytes, &position, userbuf, count, datatype); if (mpi_errno != MPI_SUCCESS) { ADIOI_Free(tmp_buf); goto fn_exit; } ADIOI_Free(tmp_buf); } fn_exit: return mpi_errno; }
int CommunicationPacket :: packHeader(MPI_Comm comm) { int _arry [ 2 ]; int _res, _pos = 0; _arry [ 0 ] = this->number; _arry [ 1 ] = this->EOF_Flag; _res = MPI_Pack(_arry, 2, MPI_INT, this->buff, size, & _pos, comm); return ( _res == MPI_SUCCESS ); }
VT_MPI_INT VTUnify_MPI_Pack( void * inbuf, VT_MPI_INT incount, VTUnify_MPI_Datatype utype, void * outbuf, VT_MPI_INT outsize, VT_MPI_INT * position, VTUnify_MPI_Comm ucomm ) { VT_MPI_INT error; MPI_Datatype type = get_mpi_type( utype ); MPI_Comm comm = get_mpi_comm( ucomm ); error = MPI_Pack( inbuf, incount, type, outbuf, outsize, position, comm ); return (error == MPI_SUCCESS) ? 1 : 0; }