/* blocking receive */ void COM_Recv (void *pattern) { COMPATTERN *cp = pattern; int (*recv_sizes) [3] = cp->recv_sizes, recv_count = cp->recv_count, send_count = cp->send_count, i, j; char **recv_data = cp->recv_data; MPI_Request *send_req = cp->send_req, *recv_req = cp->recv_req; MPI_Status *send_sta = cp->send_sta, *recv_sta = cp->recv_sta; MPI_Comm comm = cp->comm; COMDATA *cd; /* wait until until send is done */ MPI_Waitall (send_count, send_req, send_sta); /* wait until until receive is done */ MPI_Waitall (recv_count, recv_req, recv_sta); /* unpack data */ for (i = j = 0, cd = cp->recv; i < recv_count; i ++, cd ++, j = 0) { MPI_Unpack (recv_data [i], recv_sizes [i][2], &j, cd->i, cd->ints, MPI_INT, comm); MPI_Unpack (recv_data [i], recv_sizes [i][2], &j, cd->d, cd->doubles, MPI_DOUBLE, comm); } }
//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 WorkerMaster::getBestSolutionFromProcessors() { cout << myRank << " waiting for solution=========================================================" << endl; cout << "My solution is: " << result->IsExistResult() << ", distance:" << result->GetHammingDistance() << ", ham weight:" << result->GetHammingWeight() << endl; for (int i = 1; i < processors; i++) { char buffer[sizeOfHelpArray]; int position = 0; bool array[sizeOfHelpArray]; bool existSolution; MPI_Recv(buffer, workBufferLenght, MPI_PACKED, i, TAG_SOLUTION, MPI_COMM_WORLD, &status); MPI_Unpack(buffer, workBufferLenght, &position, &existSolution, 1, MPI_C_BOOL, MPI_COMM_WORLD); MPI_Unpack(buffer, workBufferLenght, &position, &array, sizeOfHelpArray, MPI_C_BOOL, MPI_COMM_WORLD); cout << "0: recieved from " << i << " solution: "; if (existSolution) { cout << "exist "; BinaryVector::printArray(array, sizeOfHelpArray); } else { cout << "doesn´t exist "; } cout << endl; if (existSolution) { solveBestSolution(array); } } }
solution_vector unpack_solution(void* buff, int buff_size, MPI_Comm comm, int* pos, float* score, void* problem_data) { color_assignment* cassign = (color_assignment*) get_root_partial_solution(problem_data); MPI_Unpack(buff, buff_size, pos, score, 1, MPI_FLOAT, comm); MPI_Unpack(buff, buff_size, pos, &cassign->curr_length, 1, MPI_INT, comm); MPI_Unpack(buff, buff_size, pos, cassign->vertex_colors, cassign->max_length, MPI_INT, comm); return (solution_vector) cassign; }
//Unpack [x,y,size,coorX,coorY] into DCell void DCellUnpack(DCell cell, void *buffer, int bufSize, int *pos, MPI_Comm comm ) { MPI_Unpack(buffer,bufSize,pos,&cell->x,1,MPI_DOUBLE,comm); MPI_Unpack(buffer,bufSize,pos,&cell->y,1,MPI_DOUBLE,comm); MPI_Unpack(buffer,bufSize,pos,&cell->size,1,MPI_INT,comm); cell->coorX = (double *) malloc(cell->size*sizeof(double)); cell->coorY = (double *) malloc(cell->size*sizeof(double)); MPI_Unpack(buffer,bufSize,pos,cell->coorX,cell->size,MPI_DOUBLE,comm); MPI_Unpack(buffer,bufSize,pos,cell->coorY,cell->size,MPI_DOUBLE,comm); }
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; }
/* 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; }
template < class T > void AbstractCommunicator::unpackArray (T * p, int n) { if(n > 0) MPI_Unpack (mRecvBuffer.getBuf(), mRecvBuffer.getSize(), &mRecvBufferPosition, (void *) p, n, getMPIType < T > (), *(MPI_Comm*)mAuxData); }
/* Function: p7_oprofile_MPIRecv() * Synopsis: Receives an OPROFILE as a work unit from an MPI sender. * Incept: MSF, Wed Oct 21, 2009 [Janelia] * * Purpose: Receive a work unit that consists of a single OPROFILE * sent by MPI <source> (<0..nproc-1>, or * <MPI_ANY_SOURCE>) tagged as <tag> for MPI communicator <comm>. * * Work units are prefixed by a status code. If the unit's * code is <eslOK> and no errors are encountered, this * routine will return <eslOK> and a non-<NULL> <*ret_om>. * If the unit's code is <eslEOD> (a shutdown signal), * this routine returns <eslEOD> and <*ret_om> is <NULL>. * * Caller provides a working buffer <*buf> of size * <*nalloc> characters. These are passed by reference, so * that <*buf> can be reallocated and <*nalloc> increased * if necessary. 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. * * Caller may or may not already know what alphabet the OPROFILE * is expected to be in. A reference to the current * alphabet is passed in <abc>. If the alphabet is unknown, * pass <*abc = NULL>, and when the OPROFILE is received, an * appropriate new alphabet object is allocated and passed * back to the caller via <*abc>. If the alphabet is * already known, <*ret_abc> is that alphabet, and the new * OPROFILE's alphabet type is verified to agree with it. This * mechanism allows an application to let the first OPROFILE * determine the alphabet type for the application, while * still keeping the alphabet under the application's scope * of control. * * Returns: <eslOK> on success. <*ret_om> contains the received OPROFILE; * it is allocated here, and the caller is responsible for * free'ing it. <*buf> may have been reallocated to a * larger size, and <*nalloc> may have been increased. If * <*abc> was passed as <NULL>, it now points to an * <ESL_ALPHABET> object that was allocated here; caller is * responsible for free'ing this. * * Returns <eslEOD> if an end-of-data signal was received. * In this case, <*buf>, <*nalloc>, and <*abc> are left unchanged, * and <*ret_om> is <NULL>. * * Returns <eslEINCOMPAT> if the OPROFILE is in a different alphabet * than <*abc> said to expect. In this case, <*abc> is unchanged, * <*buf> and <*nalloc> may have been changed, and <*ret_om> is * <NULL>. * * Throws: <eslEMEM> on allocation error, in which case <*ret_om> is * <NULL>. */ int p7_oprofile_MPIRecv(int source, int tag, MPI_Comm comm, char **buf, int *nalloc, ESL_ALPHABET **abc, P7_OPROFILE **ret_om) { int status; int code; P7_OPROFILE *om = NULL; int n; int pos; MPI_Status mpistatus; /* Probe first, because we need to know if our buffer is big enough. */ MPI_Probe(source, tag, comm, &mpistatus); MPI_Get_count(&mpistatus, MPI_PACKED, &n); /* Make sure the buffer is allocated appropriately */ if (*buf == NULL || n > *nalloc) { void *tmp; ESL_RALLOC(*buf, tmp, sizeof(char) * n); *nalloc = n; } /* Receive the packed work unit */ MPI_Recv(*buf, n, MPI_PACKED, source, tag, comm, &mpistatus); /* Unpack it, looking at the status code prefix for EOD/EOK */ pos = 0; if (MPI_Unpack(*buf, n, &pos, &code, 1, MPI_INT, comm) != 0) ESL_XEXCEPTION(eslESYS, "mpi unpack failed"); if (code == eslEOD) { *ret_om = NULL; return eslEOD; } return p7_oprofile_MPIUnpack(*buf, *nalloc, &pos, comm, abc, ret_om); ERROR: if (om != NULL) p7_oprofile_Destroy(om); return status; }
/* Function: p7_hmm_mpi_Recv() * Synopsis: Receives an HMM as a work unit from an MPI sender. * * Purpose: Receive a work unit that consists of a single HMM * sent by MPI <source> (<0..nproc-1>, or * <MPI_ANY_SOURCE>) tagged as <tag> for MPI communicator <comm>. * * Work units are prefixed by a status code that gives the * number of HMMs to follow; here, 0 or 1 (but in the future, * we could easily extend to sending several HMMs in one * packed buffer). If we receive a 1 code and we successfully * unpack an HMM, this routine will return <eslOK> and a non-<NULL> <*ret_hmm>. * If we receive a 0 code (a shutdown signal), * this routine returns <eslEOD> and <*ret_hmm> is <NULL>. * * Caller provides a working buffer <*buf> of size * <*nalloc> characters. These are passed by reference, so * that <*buf> can be reallocated and <*nalloc> increased * if necessary. 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. * * Caller may or may not already know what alphabet the HMM * is expected to be in. A reference to the current * alphabet is passed in <byp_abc>. If the alphabet is unknown, * pass <*byp_abc = NULL>, and when the HMM is received, an * appropriate new alphabet object is allocated and passed * back to the caller via <*abc>. If the alphabet is * already known, <*byp_abc> is that alphabet, and the new * HMM's alphabet type is verified to agree with it. This * mechanism allows an application to let the first HMM * determine the alphabet type for the application, while * still keeping the alphabet under the application's scope * of control. * * Args: source - index of MPI sender, 0..nproc-1 (0=master), or MPI_ANY_SOURCE * tag - MPI message tag; MPI_ANY_TAG, or a specific message tag (0..32767 will work on any MPI) * comm - MPI communicator; MPI_COMM_WORLD, or a specific MPI communicator * buf - working buffer (for receiving packed message); * if <*buf> == NULL, a <*buf> is allocated and returned; * if <*buf> != NULL, it is used (and may be reallocated) * nalloc - allocation size of <*buf> in bytes; pass 0 if <*buf==NULL>. * byp_abc - BYPASS: <*byp_abc> == ESL_ALPHABET *> if known; * <*byp_abc> == NULL> if alphabet unknown. * ret_hmm - RETURN: newly allocated/received profile * * Returns: <eslOK> on success. <*ret_hmm> contains the received HMM; * it is allocated here, and the caller is responsible for * free'ing it. <*buf> may have been reallocated to a * larger size, and <*nalloc> may have been increased. If * <*abc> was passed as <NULL>, it now points to an * <ESL_ALPHABET> object that was allocated here; caller is * responsible for free'ing this. * * Returns <eslEOD> if an end-of-data signal was received. * In this case, <*buf>, <*nalloc>, and <*abc> are left unchanged, * and <*ret_hmm> is <NULL>. * * Returns <eslEINCOMPAT> if the HMM is in a different alphabet * than <*abc> said to expect. In this case, <*abc> is unchanged, * <*buf> and <*nalloc> may have been changed, and <*ret_hmm> is * <NULL>. * * Throws: <eslEMEM> on allocation error, and <eslESYS> on MPI communication * errors; in either case <*ret_hmm> is <NULL>. */ int p7_hmm_mpi_Recv(int source, int tag, MPI_Comm comm, char **buf, int *nalloc, ESL_ALPHABET **byp_abc, P7_HMM **ret_hmm) { int pos = 0; int code; int n; MPI_Status mpistatus; int status; /* Probe first, because we need to know if our buffer is big enough. */ if ( MPI_Probe(source, tag, comm, &mpistatus) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi probe failed"); if ( MPI_Get_count(&mpistatus, MPI_PACKED, &n) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi get count failed"); /* Make sure the buffer is allocated appropriately */ if (*buf == NULL || n > *nalloc) { ESL_REALLOC(*buf, sizeof(char) * n); *nalloc = n; } /* Receive the entire packed work unit */ if (MPI_Recv(*buf, n, MPI_PACKED, source, tag, comm, &mpistatus) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi recv failed"); /* Unpack the status code prefix */ if (MPI_Unpack(*buf, n, &pos, &code, 1, MPI_INT, comm) != MPI_SUCCESS) ESL_EXCEPTION(eslESYS, "mpi unpack failed"); if (code == 0) { status = eslEOD; *ret_hmm = NULL; } else if (code == 1) status = p7_hmm_mpi_Unpack(*buf, *nalloc, &pos, comm, byp_abc, ret_hmm); else ESL_EXCEPTION(eslESYS, "bad mpi buffer transmission code"); return status; ERROR: /* from ESL_REALLOC only */ *ret_hmm = NULL; 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; }
/* ------------------------------------------------------------------------- */ void SendPrimalSolutiontoServer(char *stop, char *sleep_ag, PrimalType *PrimalSol, MPI_Comm communicator) { int method = AGENT_SEND_SOLUTION, sizeOfBuffer = 0, position = 0; char *buffer; sizeOfBuffer = ( (sizeof(int)) + ((sizeof(char)) * TotalAgents) + ((sizeof(int)) * nb_col) + (sizeof(int)) + (sizeof(double)) + sizeof(unsigned int) ); buffer = malloc(sizeOfBuffer); MPI_Pack(&method, 1, MPI_INT, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(PrimalSol->agent, TotalAgents, MPI_CHAR, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(PrimalSol->var_x, nb_col, MPI_INT, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(&PrimalSol->proc_time, 1, MPI_INT, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(&PrimalSol->value, 1, MPI_DOUBLE, buffer, sizeOfBuffer, &position, communicator); MPI_Pack(&PrimalSol->agent_ID, 1, MPI_UNSIGNED, buffer, sizeOfBuffer, &position, communicator); // O agente envia a solucao gerada ou modificada por um dos agentes primais //para o servidor de memoria de solucoes primais. MPI_Send(buffer, position, MPI_PACKED, 0, 1, communicator); char messageConfirm[2 * sizeof(char)]; MPI_Status status; int positionConfirm = 0; MPI_Recv(messageConfirm, 2, MPI_PACKED, MPI_ANY_SOURCE, MPI_ANY_TAG, communicator, &status); MPI_Unpack(messageConfirm, 2 * sizeof(char), &positionConfirm, stop, 1, MPI_CHAR, communicator); MPI_Unpack(messageConfirm, 2 * sizeof(char), &positionConfirm, sleep_ag, 1, MPI_CHAR, communicator); free(buffer); //printf("\n\n === Fim agentrot SendPrimal \n\n"); }
FC_FUNC( mpi_unpack , MPI_UNPACK ) ( void *inbuf, int *insize, int *position, void *outbuf, int *outcount, int *datatype, int *comm, int *ierror ) { *ierror=MPI_Unpack( inbuf, *insize, position, outbuf, *outcount, *datatype, *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 deserializeSolution(char* buffer, int& position, vector<Move>& _solution) { int size; MPI_Unpack(buffer, BUFFER_SIZE, &position, &size, 1, MPI_INT, MPI_COMM_WORLD); for (int i = 0; i < size; ++i) { Move move; move.deserialize(buffer, position); _solution.push_back(move); } }
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; }
message* message::unpack(byte *buf, const size_t buf_size, int *pos, MPI_Comm comm) { node::node_id id; MPI_Unpack(buf, buf_size, pos, &id, 1, MPI_UNSIGNED, comm); simple_tuple *stpl(simple_tuple::unpack(buf, buf_size, pos, comm)); return new message(id, stpl); }
void DWorldSave(DWorld world, int time) { MPI_Status status; int i; int tag = 0; int root = 0; // Root proc that will write to disk int rank; int size; MPI_Comm_rank(world->comm, &rank); MPI_Comm_size(world->comm, &size); //TODO: see if using proper MPI tags can eliminate this barrier MPI_Barrier(world->comm); if( rank == root ) { // Open file cells.time = [cell_1, cell_2, ...] FILE *fp; char filename[128]; //TODO: make directory location adjustable sprintf(filename,"data/cells.%d",time); fp = fopen(filename,"wb"); // Root proc just saves cells to file locally int pos; // Position in packed buffer for (i = 0; i < world->localSize; ++i) { if( world->localcells[i] != NULL ) { pos = 0; DCellPack(world->localcells[i], world->rPack,world->bufSize,&pos,world->comm); DCellSave(world->rPack, fp ); } } // Gather remote cells and write to file int countEOF = 0; // number of EOFs received int isEOF; // if received msg is an EOF when unpacked while( countEOF < size - 1 ) { MPI_Recv(world->rPack,world->bufSize,MPI_PACKED,MPI_ANY_SOURCE,tag,world->comm,&status); pos = 0; MPI_Unpack(world->rPack,world->bufSize,&pos,&isEOF,1,MPI_INT,world->comm); if( isEOF == 1 ) { countEOF++; } else { DCellSave( world->rPack + pos, fp ); } } // Closes the file pointer fclose( fp ); } else { // In a remote process, send all DCells to root. for (i = 0; i < world->localSize; ++i) { if( world->localcells[i] != NULL ) { SendDCell(world, i, root); } } // Signal root that no more DCells will come from this rank. SendEOF(world, root); } }
int CommunicationPacket :: unpackHeader(MPI_Comm comm) { int _arry [ 2 ]; int _res, _pos = 0; _res = MPI_Unpack(this->buff, this->size, & _pos, _arry, 2, MPI_INT, comm); this->number = _arry [ 0 ]; this->EOF_Flag = _arry [ 1 ]; return ( _res == MPI_SUCCESS ); }
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_write_external32_conversion_fn (const 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_Pack_external("external32", userbuf, count, datatype, filebuf, bytes, &position); 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_external("external32", userbuf, count, datatype, tmp_buf, bytes, &position); if (mpi_errno != MPI_SUCCESS) { ADIOI_Free(tmp_buf); goto fn_exit; } mpi_errno = MPI_Unpack(tmp_buf, bytes, &position_i, filebuf, count, datatype, MPI_COMM_WORLD); if (mpi_errno != MPI_SUCCESS) { ADIOI_Free(tmp_buf); goto fn_exit; } ADIOI_Free(tmp_buf); } fn_exit: return mpi_errno; }
/** * @brief Wrapper around MPI_Unpack * * We check the error code to detect MPI errors and use the default communicator * MPI_WORLD. * * @param inbuf Buffer to unpack from * @param insize Total size of the unpack buffer * @param position Current position in the unpack buffer (is updated) * @param outbuf Buffer to unpack elements in * @param outcount Number of elements to unpack * @param datatype MPI datatype of the elements * @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_Unpack_real(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype datatype, std::string file, std::string function, int line) { int status = MPI_Unpack(inbuf, insize, position, outbuf, outcount, datatype, MPI_COMM_WORLD); if(status != MPI_SUCCESS) { std::cerr << "Error during MPI_Unpack!" << std::endl; std::cerr << insize << "\t" << *position << "\t" << std::endl; std::cerr << MPIGlobal::rank << ": " << file << "::" << function << ":" << line << std::endl; MPI_Abort(MPI_COMM_WORLD, -1); } }
VT_MPI_INT VTUnify_MPI_Unpack( void * inbuf, VT_MPI_INT insize, VT_MPI_INT * position, void * outbuf, VT_MPI_INT outcount, VTUnify_MPI_Datatype utype, VTUnify_MPI_Comm ucomm ) { VT_MPI_INT error; MPI_Datatype type = get_mpi_type( utype ); MPI_Comm comm = get_mpi_comm( ucomm ); error = MPI_Unpack( inbuf, insize, position, outbuf, outcount, type, comm ); return (error == MPI_SUCCESS) ? 1 : 0; }
void resultUnpack(Result* destination, Buffer* source) { if (source && destination) { int i, count = 0; source->position = 0; resultDeinit(destination); resultInit(destination); MPI_Unpack(source->buffer, source->capacity, &(source->position), &count, 1, MPI_INT, MPI_COMM_WORLD); for(i = 0; i < count; i++) { Position position; positionUnpack(&position, source); resultAddPosition(destination, position); } } }
/* ------------------------------------------------------------------------- */ void GetResultsPath(char *path, MPI_Comm communicator) { char *path_aux = NULL; int path_len = 0, method = GET_RESULTS_PATH; char buffer[sizeof(int)]; int position = 0; MPI_Status status; MPI_Pack(&method, 1, MPI_INT, buffer, sizeof(int), &position, communicator); MPI_Send(buffer, position, MPI_PACKED, 0, 1, communicator); //printf("\n == first \n"); char * bufferRecv; int positionRecv = 0; int sizeOfMessage = 0; MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, communicator, &status); MPI_Get_count(&status, MPI_PACKED, &sizeOfMessage); bufferRecv = malloc(sizeOfMessage); MPI_Recv(bufferRecv, sizeOfMessage, MPI_PACKED, MPI_ANY_SOURCE, MPI_ANY_TAG, communicator, &status); MPI_Unpack(bufferRecv, sizeOfMessage, &positionRecv, &path_len, 1, MPI_INT, communicator); path_aux = malloc(sizeof(char) * path_len); MPI_Unpack(bufferRecv, sizeOfMessage, &positionRecv, path_aux, path_len, MPI_CHAR, communicator); strncpy(path, path_aux,path_len); free(bufferRecv); free(path_aux); }
/* ------------------------------------------------------------------------- */ 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"); }
message_set* message_set::unpack(byte *buf, const size_t buf_size, MPI_Comm comm) { message_set *ms(new message_set()); unsigned short int size_msg; int pos(0); MPI_Unpack(buf, buf_size, &pos, &size_msg, 1, MPI_UNSIGNED_SHORT, comm); for(size_t i(0); i < size_msg; ++i) { message *msg(message::unpack(buf, buf_size, &pos, comm)); ms->add(msg); } return ms; }
void Communicator::Unpack(void *buf, int bufSize, DataType::Type type, int outCount, /*out*/void *data) { MPI_Datatype mpiType; switch (type) { case DataType::DOUBLE: mpiType = MPI_DOUBLE; break; case DataType::Int: //Intentional default: mpiType = MPI_INT; break; } MPI_Unpack(buf, bufSize, &this->recvPosition, data, outCount, mpiType, MPI_COMM_WORLD); }
/* Generate memory data such that file data is written to according to * position which allows for easier checking. Put the correct data * into tmp_buf and then unpack it based on the access pattern. */ int init_data_buf(char *buf, int64_t buf_sz, int myid, int numprocs, int noncontig_type, int region_count, int region_size, int region_spacing, int base_dtype_sz, int mpiio_count, MPI_Datatype *memtype_p) { int i, j, buf_pos = 0; int64_t global_test_dtype_block = 0, tmp_buf_sz = -1; char *tmp_buf = NULL; tmp_buf_sz = (int64_t) region_count * region_size * base_dtype_sz; if ((tmp_buf = malloc(tmp_buf_sz * sizeof(char))) == NULL) { fprintf(stderr, "init_data_buf: malloc tmp_buf of size %Ld failed\n", tmp_buf_sz * sizeof(char)); return -1; } if (noncontig_type == C_C || noncontig_type == NC_C) global_test_dtype_block = myid * region_count; else global_test_dtype_block = myid; for (i = 0; i < region_count; i++) { for (j = 0; j < region_size * base_dtype_sz; j++) { tmp_buf[(i * region_size * base_dtype_sz) + j] = gen_file_char(global_test_dtype_block + j); } if (noncontig_type == C_C || noncontig_type == NC_C) global_test_dtype_block++; else global_test_dtype_block += numprocs; } buf_pos = 0; MPI_Unpack(tmp_buf, tmp_buf_sz, &buf_pos, buf, mpiio_count, *memtype_p, MPI_COMM_SELF); assert(buf_pos = tmp_buf_sz); free(tmp_buf); return 0; }