void Tiger::generatePath() {
	world->set(position);
	world->set(destination);

	generateDistances();

	if (pathFound) { traceBack(); }
}
Example #2
0
 void traceBack(
                unordered_map<string, unordered_set<string>> &trace,
                vector<string> &temp,
                string end,
                vector<vector<string>> &result) {
     temp.emplace_back(end);
     if (trace.count(end) != 1) {
         vector<string> path = temp;
         reverse(path.begin(), path.end());
         result.emplace_back(move(path));
     }
     else {
         for (auto& word: trace[end]) {
             traceBack(trace, temp, word, result);
         }
     }
     temp.pop_back();
 }
Example #3
0
    /**
     * @param start, a string
     * @param end, a string
     * @param dict, a set of string
     * @return a list of lists of string
     */
    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        unordered_map<string, unordered_set<string>> trace;

        // BFS, each round is a level
        unordered_set<string> levels[2];
        int cur_level = 0;

        levels[cur_level].emplace(start);
        while (dict.size() > 0 && levels[cur_level % 2].size() > 0) {
            // end is in current level, stop
            if (trace.count(end) == 1) {
                ++cur_level;
                break;
            }

            // clear words from the dictionary to avoid it' apear in future level again
            for (auto& word : levels[cur_level % 2]) {
                dict.erase(word);
            }

            levels[(cur_level + 1) % 2].clear();
            for (auto& from : levels[cur_level % 2]) {
                string new_word = from;
                for (int i = 0; i < new_word.length(); ++i) {
                    char orig_c = from[i];
                    for (char c = 'a'; c <= 'z'; ++c) {
                        new_word[i] = c;
                        if (dict.count(new_word) == 1) {
                            trace[new_word].emplace(from);
                            levels[(cur_level + 1) % 2].emplace(new_word);
                        }
                    }
                    new_word[i] = orig_c;
                }
            }

            ++cur_level;
        }

        vector<vector<string>> result;
        vector<string> temp;
        traceBack(trace, temp, end, result);
        return result;
    }
Example #4
0
void doPartitionTraceBack (ProcessData * pData, WavesData * wData, TracebackData * tbData) {
    char msg[MID_MESSAGE_SIZE];
    /*Perform Partition trace back*/
    tbData->pathParts ++;
    if (tbData->pathParts == 1) {
        tbData->aSeqLen = mmalloc (((MOATypeInd) sizeof *(tbData->aSeqLen)));
        if (tbData->aSeqLen == NULL) {
            printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
            fflush (stdout);
            return;
        }
        tbData->algnseq = mmalloc (((MOATypeInd) sizeof *(tbData->algnseq)));
        if (tbData->aSeqLen == NULL) {
            printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
            fflush (stdout);
            return;
        }
    }
    else {
        tbData->aSeqLen = realloc (tbData->aSeqLen, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *(tbData->aSeqLen)));
        if (tbData->aSeqLen == NULL) {
            printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
            fflush (stdout);
            return;
        }
        tbData->algnseq = realloc (tbData->algnseq, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *(tbData->algnseq)));
        if (tbData->algnseq == NULL) {
            printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
            fflush (stdout);
            return;
        }
    }
    tbData->algnseq[tbData->pathParts-1] = NULL;
    tbData->algnseq[tbData->pathParts-1] = mmalloc (((MOATypeInd) tbData->seqNum) * ((MOATypeInd) sizeof *(tbData->algnseq[tbData->pathParts-1])));    
    if (tbData->algnseq[tbData->pathParts-1] == NULL) {
        printf ("Failed to reallocate memory for %ld partial alignments sequences. Exiting.\n", tbData->pathParts);
        fflush (stdout);
        return;
    }
    tbData->aSeqLen[tbData->pathParts-1] = traceBack(pData, wData, tbData);
    sprintf(msg, "DMTB returned Local path of length %ld\n", tbData->aSeqLen[tbData->pathParts-1]);  
    mprintf(3, msg, 1);    
}
Example #5
0
bool CDStarFECDecoder::decode(const bool* in, bool* out, unsigned int inLen, unsigned int& outLen)
{
	wxASSERT(in != NULL);
	wxASSERT(out != NULL);

	for (unsigned int i = 0U; i < 4U; i++)
		m_pathMetric[i] = 0;

	unsigned int n = 0U;
	for (unsigned int i = 0U; i < 660U; i += 2U, n++) {
		int data[2];

		if (in[i + 0U])
			data[1] = 1;
		else
			data[1] = 0;

		if (in[i + 1U])
			data[0] = 1;
		else
			data[0] = 0;

		viterbiDecode(n, data);
	}

	traceBack(out, outLen);

	// Swap endian-ness
	for (unsigned int i = 0U; i < 330U; i += 8U) {
		bool temp;
		temp = out[i + 0U]; out[i + 0U] = out[i + 7U]; out[i + 7U] = temp;
		temp = out[i + 1U]; out[i + 1U] = out[i + 6U]; out[i + 6U] = temp;
		temp = out[i + 2U]; out[i + 2U] = out[i + 5U]; out[i + 5U] = temp;
		temp = out[i + 3U]; out[i + 3U] = out[i + 4U]; out[i + 4U] = temp;
	}

	return true;
}
Example #6
0
void Viterbi5::decodeFromSymbols(
        unsigned char *dataBits,      //!< Decoded output data bits
        const unsigned char *symbols, //!< Input symbols
        unsigned int nbSymbols,       //!< Number of imput symbols
        unsigned int startstate)      //!< Encoder starting state

{
    if (nbSymbols > m_nbSymbolsMax)
    {
        if (m_traceback) {
            delete[] m_traceback;
        }

        if (m_pathMetrics) {
            delete[] m_pathMetrics;
        }

        m_traceback = new unsigned char[16 * nbSymbols];
        m_pathMetrics = new uint32_t[16];
        m_nbSymbolsMax = nbSymbols;
    }

    // initial path metrics state
    memset(m_pathMetrics, Viterbi::m_maxMetric, 16 * sizeof(uint32_t));
    m_pathMetrics[startstate] = 0;

    for (unsigned int is = 0; is < nbSymbols; is++)
    {
//        std::cerr << "Viterbi3::decodeFromSymbols: S[" << is << "]=" << (int) symbols[is] << std::endl;

        // compute metrics
        doMetrics(
                is,
                m_branchCodes,
                symbols[is],
                &m_traceback[0*nbSymbols],
                &m_traceback[1*nbSymbols],
                &m_traceback[2*nbSymbols],
                &m_traceback[3*nbSymbols],
                &m_traceback[4*nbSymbols],
                &m_traceback[5*nbSymbols],
                &m_traceback[6*nbSymbols],
                &m_traceback[7*nbSymbols],
                &m_traceback[8*nbSymbols],
                &m_traceback[9*nbSymbols],
                &m_traceback[10*nbSymbols],
                &m_traceback[11*nbSymbols],
                &m_traceback[12*nbSymbols],
                &m_traceback[13*nbSymbols],
                &m_traceback[14*nbSymbols],
                &m_traceback[15*nbSymbols],
                m_pathMetrics
        );
    } // symbols

    // trace back

    uint32_t minPathMetric = m_pathMetrics[0];
    unsigned int minPathIndex = 0;

    for (int i = 1; i < 16; i++)
    {
        if (m_pathMetrics[i] < minPathMetric)
        {
            minPathMetric = m_pathMetrics[i];
            minPathIndex = i;
        }
    }

//    std::cerr << "Viterbi3::decodeFromSymbols: last path node: " << minPathIndex << std::endl;

    traceBack(
            nbSymbols,
            minPathIndex,
            dataBits,
            &m_traceback[0*nbSymbols],
            &m_traceback[1*nbSymbols],
            &m_traceback[2*nbSymbols],
            &m_traceback[3*nbSymbols],
            &m_traceback[4*nbSymbols],
            &m_traceback[5*nbSymbols],
            &m_traceback[6*nbSymbols],
            &m_traceback[7*nbSymbols],
            &m_traceback[8*nbSymbols],
            &m_traceback[9*nbSymbols],
            &m_traceback[10*nbSymbols],
            &m_traceback[11*nbSymbols],
            &m_traceback[12*nbSymbols],
            &m_traceback[13*nbSymbols],
            &m_traceback[14*nbSymbols],
            &m_traceback[15*nbSymbols]
    );
}
Example #7
0
File: 1717.c Project: saki45/OJ
int main(){
	char buf[75][75], res[150][150];
	int prev[75][75], l[75][75], nMax, lMax, r, c, i, j, tb, rres[150], cres[150], out[150];

	while(scanf("%d%d", &c, &r) && (r+c)){
		getchar();

		memset(rres, 0, sizeof(rres));
		memset(cres, 0, sizeof(cres));

		memset(prev, 0, sizeof(prev));
		memset(l, 0, sizeof(l));
		lMax = 0;

		for(i=0; i<r; i++)
			fgets(buf[i], 75, stdin);

		if(isNumber(buf[0][0])){
			prev[0][0] = -1;
			l[0][0] = 1;
			lMax = 1;
		}

		for(i=1; i<r; i++){
			if(isNumber(buf[i][0])){
				if(l[i-1][0]>0){
					prev[i][0] = 1;
					l[i][0] = l[i-1][0]+1;
				}
				else{
					prev[i][0] = -1;
					l[i][0] = 1;
				}

				if(lMax < l[i][0]){
					lMax = l[i][0];
				}
			}
		}

		for(j=1; j<c; j++){
			if(isNumber(buf[0][j])){
				if(l[0][j-1]>0){
					prev[0][j] = 2;
					l[0][j] = l[0][j-1]+1;
				}
				else{
					prev[0][j] = -1;
					l[0][j] = 1;
				}

				if(lMax < l[0][j]){
					lMax = l[0][j];
				}
			}
		}

		for(i=1; i<r; i++){
			for(j=1; j<c; j++){
				if(isNumber(buf[i][j])){
					if(l[i-1][j] > l[i][j-1]){
						prev[i][j] = 1;
						l[i][j] = l[i-1][j]+1;
					}
					else if(l[i-1][j] < l[i][j-1]){
						prev[i][j] = 2;
						l[i][j] = l[i][j-1]+1;
					}
					else{
						if(l[i][j-1]){
							l[i][j] = l[i-1][j]+1;
							tb = traceBack(buf, prev, l, i, j);
							prev[i][j] = tb;
						}
						else{
							prev[i][j] = -1;
							l[i][j] = 1;
						}
					}
					if(l[i][j] > lMax){
						lMax = l[i][j];
					}
				}
			}
		}

		int rtmp, ctmp, ll;
		nMax = 0;
		for(i=0; i<r; i++){
			for(j=0; j<c; j++){
				if(l[i][j] == lMax){
					rres[nMax]= i;
					cres[nMax] = j;
					rtmp = i;
					ctmp = j;
					ll = lMax;
					while(ll--){
						res[nMax][ll] = buf[rtmp][ctmp];
						if(prev[rtmp][ctmp] == 1)
							rtmp--;
						else
							ctmp--;
					}
					nMax++;
				}
			}
		}

		memset(out, 0, sizeof(out));
		if(nMax == 1)
			i = nMax-1;
		else{
			tb = nMax;
			char curMax;
			for(j=0; j<lMax; j++){
				curMax = 0;
				for(i=0; i<nMax; i++){
					if(out[i] == 0)
						if(curMax < res[i][j])
							curMax = res[i][j];
				}

				for(i=0; i<nMax; i++){
					if(out[i] == 0 && res[i][j] < curMax){
						out[i] = 1;
						tb--;
					}
				}
				if(tb == 1)
					break;
			}

			i = 0;
			while(out[i]){
				i++;
			}
		}

		j = 0;
		while(res[i][j] == '0' && j < lMax)
			j++;
		if(j == lMax)
			j--;
		while(j<lMax){
			printf("%c", res[i][j]);
			j++;
		}
		printf("\n");

	}
	return 0;
}
Example #8
0
File: moaSq.c Project: mhelal/mmDST
int main (int argc, char ** argv) {
  long seqNum, i, k  = 0;
 
  char * * sequences = NULL;
  long * seqLen = NULL;
  char * * * algnseq = NULL;
  long * aSeqLen = NULL;
  int alignmentsNo=0;
  MOA_rec * msaAlgn = NULL;
 
  int stype = 0;
  long currentScore, currentCell = 0;
  long prevScore, prevCell = 0;
  Mode = Sequential;
  processArguments(argc, argv, &seqNum, &sequences, &seqLen, &stype);
  prevNow = NULL;
  prevNow = (struct tm *) mmalloc (sizeof(struct tm));
  currNow = getTime();
  prevNow->tm_hour = currNow->tm_hour;
  prevNow->tm_isdst = currNow->tm_isdst;
  prevNow->tm_mday = currNow->tm_mday;
  prevNow->tm_min = currNow->tm_min;
  prevNow->tm_mon = currNow->tm_mon;
  prevNow->tm_sec = currNow->tm_sec;
  prevNow->tm_wday = currNow->tm_wday;
  prevNow->tm_yday = currNow->tm_yday;
  prevNow->tm_year = currNow->tm_year;
    

  
  /* A. Crteate MOA Alignment Tensor */
  /*msaAlgn = (MOA_rec *) mmalloc(sizeof(MOA_rec));*/
  createMOAStruct (&msaAlgn);
  
  createMOA(seqLen /* shape*/, seqNum /* dimension*/, msaAlgn /* MOA structure*/,0,0);
  if (pdebug == 1)
    mprintf(outputfilename, "MOA dimn %d, elm ub %d\n", 2, msaAlgn->dimn, msaAlgn->elements_ub); 
  /* B. Fill the Tensor */
 
  if (AlignmentType == Global) 
    initTensor (msaAlgn, stype);
  fillTensor (sequences, msaAlgn, stype);
  if (pdebug == 1) 
    printMOA(msaAlgn);

  /* C. trace back */
  aSeqLen = (long *)   mmalloc (sizeof(long));
  aSeqLen[0] = 0;
  prevCell = -1;
  algnseq = (char * * *) mmalloc(sizeof(char * *));
  algnseq[0] = (char * *) mmalloc (seqNum * sizeof(char *));    
  if (AlignmentType == Global) { /* if Global Alignment */
    /*PrintPrevChains(msaAlgn);
    // Get Max Cell on Last Border as Current Cell */
    alignmentsNo = 0;
    currentScore = getMaxOnLastBorder (msaAlgn, &currentCell);
    traceBack (seqNum, sequences, seqLen, msaAlgn, stype, &algnseq, &aSeqLen, &alignmentsNo, &currentCell, &currentScore, 0);
  }
  else { /* if Local Alignment */
    alignmentsNo = -1;
    for (k = 0;k<maxAlignmentsNumber;k++) {
      if (k == 0)
	currentScore = MOA_max(msaAlgn, 0, 0, &currentCell);
      else {
	currentScore = MOA_max(msaAlgn, 1, currentScore, &currentCell);
      }
      if ((prevCell != currentCell) && (currentScore > 0)){
	alignmentsNo ++;
	algnseq = (char * * *) realloc (algnseq, (alignmentsNo + 1) * sizeof(char * *));
     if (algnseq == NULL) {
		mprintf(outputfilename, "Could not reallocate memory for Aligned Sequence Set %d!\n", 1, alignmentsNo + 1);
		return -1;
      }
	algnseq[alignmentsNo] = (char * *) mmalloc (seqNum * sizeof(char *));
	aSeqLen = (long *)  realloc (aSeqLen, (alignmentsNo + 1) * sizeof(long));
     if (aSeqLen == NULL) {
		mprintf(outputfilename, "Could not reallocate memory for Aligned Sequence Length %d!\n", 1, alignmentsNo + 1);
		return -1;
      }
	traceBack_loc (seqNum, sequences, seqLen, msaAlgn, stype, &algnseq, &aSeqLen, &alignmentsNo, &currentCell, &currentScore, 0);
	}
	prevCell = currentCell;
	prevScore = currentScore;
    }
  }

  /* D. Print the resulting Alignemnts */
  PrintASeq (seqNum, sequences, seqLen, &algnseq, aSeqLen, alignmentsNo+1) ;

  /* Free all Memory Allocations & Exit. */
  deleteMOA (msaAlgn);
  if (sequences != NULL) {
    for (i=0;i<seqNum;i++) {
      if (sequences[i] != NULL) 
        free(sequences[i]);
	}
    free(sequences);
  }
  if (algnseq != NULL) {
    for (k=0;k<=alignmentsNo;k++) {
	  if (algnseq[k] != NULL) {
        for (i=0;i<seqNum;i++) {
          if (algnseq[k][i] != NULL)
	        free(algnseq[k][i]);
        }
        free(algnseq[k]);
	  }
    }
    free(algnseq);
  }
  if (seqLen != NULL)
    free(seqLen);
  if (aSeqLen != NULL)
    free(aSeqLen);
  if (prevNow != NULL)
    free(prevNow);

  return 0;

}
Example #9
0
File: mtb.c Project: mhelal/mmDST
void * tbMaster (ProcessData * pData, WavesData * wData) {
    int MPI_return, foundproc, done = 0; /*currProc*/
    MOATypeDimn i, k;
    long long receviedLongLong;
    
    char msg[MID_MESSAGE_SIZE];
    MPI_Request request;
    MPI_Status status;
    TracebackData * tbData;
    tbData = NULL;
    if (initTBData(&tbData, pData->seqNum, pData->seqLen) != 0) {
        printf ("Failed to allocate memory for trace back structure. Exiting\n");
        return NULL;
    }
    if (AlignmentType == Local) 
        getmaxCellScore (pData, tbData);    
    else {
        //tbData->maxCellScore = getLocalMaxCellScore(pData, wData, tbData, 0);
        pData->waveNo = wData->wavesTotal - 1;
        pData->partNo = wData->partsInWave[pData->waveNo]-1;
        tbData->maxCellScore = 0;
        if (getProcID(wData, pData->waveNo, pData->partNo) != myProcid) 
            getPrevPartition (wData, &pData->waveNo, &pData->partNo);
        if (pData->partNo != -1) {
            if (restorePartitionCheckPoint(pData, wData, pData->waveNo, pData->partNo) != 0) {
                printf ("Error Retrieving partition file. Exitiing.\n");
                return NULL;
            }
            for (k=0;k<pData->seqNum;k++)
                tbData->maxCellIndex[k] = pData->msaAlgn->indexes[pData->msaAlgn->elements_ub-1][k];
        }
        else {
            printf ("Error Retrieving part No. Exitiing.\n");
            return NULL;
        }
        
        tbData->currProc = 0;
    }
    
    //currProc = tbData->currProc;
    while (done == 0) {
        printf ("currProc = %d done = %d maxCellIndex { %lld", tbData->currProc, done, tbData->maxCellIndex[0]);
        for (k=1;k<tbData->seqNum;k++)
            printf (", %lld", tbData->maxCellIndex[k]);
        printf ("} in proc %d\n", tbData->currProc);
        /*send to processor containing the maxCellScore to trace back*/
        if (tbData->currProc == 0) {
                /*Perform Partition trace back*/
                tbData->pathParts ++;
                if (tbData->pathParts == 1) {
                    tbData->aSeqLen = mmalloc (((MOATypeInd) sizeof *(tbData->aSeqLen)));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                    tbData->algnseq = mmalloc (((MOATypeInd) sizeof *(tbData->algnseq)));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                }
                else {
                    tbData->aSeqLen = realloc (tbData->aSeqLen, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *(tbData->aSeqLen)));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                    tbData->algnseq = realloc (tbData->algnseq, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *(tbData->algnseq)));
                    if (tbData->algnseq == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                }
                tbData->algnseq[tbData->pathParts-1] = NULL;
                tbData->algnseq[tbData->pathParts-1] = mmalloc (((MOATypeInd) tbData->seqNum) * ((MOATypeInd) sizeof *(tbData->algnseq[tbData->pathParts-1])));    
                if (tbData->algnseq[tbData->pathParts-1] == NULL) {
                    printf ("Failed to reallocate memory for %ld partial alignments sequences. Exiting.\n", tbData->pathParts);
                    fflush (stdout);
                    return NULL;
                }
                tbData->aSeqLen[tbData->pathParts-1] = traceBack(pData, wData, tbData);
                sprintf(msg, "DMTB returned Local path of length %ld\n", tbData->aSeqLen[tbData->pathParts-1]);  
                mprintf(3, msg, 1);
        }
        else {
                /*1. Send Tracing flag (done = 0) to the processor where the maximum score was found*/
                MPI_return = MPI_Send (&done, 1, MPI_INT, tbData->currProc, 2, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
                sprintf(msg, "DMTB sent flag %d to proc %d first\n", done, tbData->currProc);  
                mprintf(3, msg, 1);
#endif
                /*2. send starting global index*/
                MPI_return = MPI_Send (tbData->maxCellIndex, tbData->seqNum, MPI_LONG_LONG, tbData->currProc, 3, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
                sprintf(msg, "DMTB sent maxCellIndex to proc %d\n", tbData->currProc);  
                mprintf(3, msg, 1);
#endif


                tbData->pathParts ++;
                if (tbData->pathParts == 1) {
                    tbData->aSeqLen = mmalloc (((MOATypeInd) sizeof *tbData->aSeqLen));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                    tbData->algnseq = mmalloc (((MOATypeInd) sizeof *tbData->algnseq));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                }
                else {
                    tbData->aSeqLen = realloc (tbData->aSeqLen, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *tbData->aSeqLen));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments Lengths. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                    tbData->algnseq = realloc (tbData->algnseq, ((MOATypeInd) tbData->pathParts) * ((MOATypeInd) sizeof *tbData->algnseq));
                    if (tbData->aSeqLen == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments. Exiting.\n", tbData->pathParts);
                        fflush (stdout);
                        return NULL;
                    }
                }
                tbData->algnseq[tbData->pathParts-1] = NULL;
                tbData->algnseq[tbData->pathParts-1] = mmalloc (((MOATypeInd) tbData->seqNum) * ((MOATypeInd) sizeof *(tbData->algnseq[tbData->pathParts-1])));  
                if (tbData->algnseq[tbData->pathParts-1] == NULL) {
                    printf ("Failed to reallocate memory for %ld partial alignments sequences. Exiting.\n", tbData->pathParts);
                    fflush (stdout);
                    return NULL;
                }
                tbData->aSeqLen[tbData->pathParts-1] = 2;
                /*3. receive partital alignment length*/
                MPI_return = MPI_Recv (&receviedLongLong, 1, MPI_LONG_LONG, tbData->currProc, 4, MOAMSA_COMM_WORLD, &status);
                tbData->aSeqLen[tbData->pathParts-1] = receviedLongLong;
#ifndef NDEBUG
                printf("Master received length %ld  MPI_return %d \n", tbData->aSeqLen[tbData->pathParts-1], MPI_return);  
                fflush(stdout);
                sprintf(msg, "DMTB received Remote path of length %ld :", tbData->aSeqLen[tbData->pathParts-1]);  
                mprintf(3, msg, 1);
#endif
                /*4. receive the partital alignment itself*/
                for (i=0;i<tbData->seqNum && tbData->aSeqLen[tbData->pathParts-1] > 0;i++) {
                    tbData->algnseq[tbData->pathParts-1][i] = NULL;
                    tbData->algnseq[tbData->pathParts-1][i] = mmalloc (((MOATypeInd) (tbData->aSeqLen[tbData->pathParts-1] +1)) * ((MOATypeInd) sizeof *(tbData->algnseq[tbData->pathParts-1][i] )));    
                    if (tbData->algnseq[tbData->pathParts-1][i] == NULL) {
                        printf ("Failed to reallocate memory for %ld partial alignments sequences %lld residues. Exiting.\n", tbData->pathParts, tbData->aSeqLen[tbData->pathParts-1]);
                        fflush (stdout);
                        return NULL;
                    }
                    MPI_return = MPI_Recv (tbData->algnseq[tbData->pathParts-1][i], tbData->aSeqLen[tbData->pathParts-1], MPI_CHAR, tbData->currProc, 5, MOAMSA_COMM_WORLD, &status);
                    tbData->algnseq[tbData->pathParts-1][i][tbData->aSeqLen[tbData->pathParts-1]] = '\0';
#ifndef NDEBUG
                    printf("Master received aligned seq for seq %lld MPI_return %d = %s\n", i, MPI_return, tbData->algnseq[tbData->pathParts-1][i]);  
                    fflush(stdout);
                    sprintf(msg, " %s ", tbData->algnseq[tbData->pathParts-1][i]);  
                    mprintf(3, msg, 1);
#endif
                }
                /*5. receive the last global index in this partial alignment*/
                MPI_return = MPI_Recv (tbData->maxCellIndex, tbData->seqNum, MPI_LONG_LONG, tbData->currProc, 7, MOAMSA_COMM_WORLD, &status);
#ifndef NDEBUG
                printf ("Master received MPI_return %d maxCellIndex { %lld", MPI_return, tbData->maxCellIndex[0]);
                for (i=1;i<tbData->seqNum;i++)
                    printf (", %lld", tbData->maxCellIndex[i]);
                printf ("}\n");
                fflush(stdout);
#endif
                /*6. receive the next Processor where a next remote score was found from this partial alignment*/
                MPI_return = MPI_Recv (&tbData->currProc, 1, MPI_INT, tbData->currProc, 8, MOAMSA_COMM_WORLD, &status);
                //tbData->currProc = currProc;
#ifndef NDEBUG
                sprintf(msg, "DMTB received currProc %d, maxCellIndex {%lld", tbData->currProc, tbData->maxCellIndex[0]);  
                for (k=1;k<tbData->seqNum;k++)
                    sprintf(msg, "%s, %lld", msg, tbData->maxCellIndex[k]);
                sprintf(msg, "%s}\n", msg);
                mprintf(3, msg, 1);
#endif
            }
            /*test if end of aligned sequence is zero to exit */
            done = 2;
            for (k=0;k<tbData->seqNum;k++)
                if (tbData->maxCellIndex[k] != 0)
                    done = 0;
            /* else, determine the next tracing Processor*/
            if ((done == 0) && (tbData->currProc < 0)) { 
                /*if other slave processes don't have this index, check the master*/
                if (getPartitionDetails (pData, wData, &tbData->partIndex, tbData->maxCellIndex, &pData->waveNo, &pData->partNo, &tbData->currProc) == 0) { 
#ifndef NDEBUG
                    sprintf(msg, "DMTB max cell {%lld", tbData->maxCellIndex[0]);  
                    for (k=1;k<tbData->seqNum;k++)
                        sprintf (msg, "%s, %lld", msg, tbData->maxCellIndex[k]);
                    sprintf (msg, "%s} in proc %d\n", msg, tbData->currProc);
                    mprintf(3, msg, 1);
#endif
                    done = 0;
                }
                else /*other wise, end of tracing*/
                    done = 2;
            }
    } /* End While*/
    /*send to all processes to exit tracing*/
    done = 2;
    for (i=1;i<ClusterSize;i++) {
        MPI_return = MPI_Send (&done, 1, MPI_INT, i, 2, MOAMSA_COMM_WORLD);
        sprintf(msg, "DMTB sent flag %d to proc %ld\n", done, i);  
        mprintf(3, msg, 1);
    }
    assemblePathParts (tbData);
    calcAlignmentSPScore(pData, tbData);
    outputAlignment(pData, tbData, 1);
    editAlignment (pData, tbData);
    calcAlignmentSPScore (pData, tbData);
    outputAlignment(pData, tbData, 1);
    outputFastaAlignment(pData, tbData);    
    outputMSFAlignment(outputfilename, pData, tbData);    
    if (tbData != NULL)
        freetbData(&tbData);
    return NULL;
}
Example #10
0
File: mtb.c Project: mhelal/mmDST
void * tbSlave (ProcessData * pData, WavesData * wData) {
    MOATypeInd localCellIndex, maxlocalCellIndex;
    MOATypeDimn i;
    int MPI_return, isLower, done = 0;
    MPI_Request request;
    MPI_Status status;
    char msg[MID_MESSAGE_SIZE];
    TracebackData * tbData;

    tbData = NULL;
    if (initTBData(&tbData, pData->seqNum, pData->seqLen) != 0) {
        printf ("Failed to allocate memory for trace back structure. Exiting\n");
        return NULL;
    }

    if (AlignmentType == Local) 
        sendmaxCellScore (pData, wData, tbData);
    

    tbData->aSeqLen = mcalloc ((MOATypeInd) 1, (MOATypeInd) sizeof *(tbData->aSeqLen));
    tbData->algnseq = mmalloc ((MOATypeInd) sizeof *(tbData->algnseq));
    tbData->pathParts = 1;
    tbData->algnseq[0] = NULL;
    tbData->algnseq[0] = mmalloc (tbData->seqNum * sizeof *(tbData->algnseq[0]));    
    while (done < 2) {
        /*receive tracing flag, 0: trace back, Otherwise: finish & exit*/
#ifndef NDEBUG
        mprintf (3, "before receive new flag from master", 1);
#endif
        /*1. Receive flag */
        MPI_return = MPI_Recv (&done, 1, MPI_INT, 0, 2, MOAMSA_COMM_WORLD, &status);
#ifndef NDEBUG
        sprintf(msg, "DSTB received flag %d \n", done);  
        mprintf(3, msg, 1);
#endif

        /*Check tracing flag (done = 0)*/
        if (done == 0) {
            /*2. receive starting global index*/
            MPI_return = MPI_Recv (tbData->maxCellIndex, tbData->seqNum, MPI_LONG_LONG, 0, 3, MOAMSA_COMM_WORLD, &status);
#ifndef NDEBUG
            printf ("[%d] Received maxCellIndex { %lld", myProcid, tbData->maxCellIndex[0]);
            for (i=1;i<tbData->seqNum;i++)
                printf (", %lld", tbData->maxCellIndex[i]);
            printf ("}\n");
            fflush(stdout);
            sprintf(msg, "DSTB received maxCellIndex {%lld", tbData->maxCellIndex[0]);  
            for (i=1;i<tbData->seqNum;i++) 
                sprintf (msg, "%s, %lld", msg, tbData->maxCellIndex[i]);
            sprintf (msg, "}\n", msg);
            mprintf(3, msg, 1);
#endif
            /*Perform local Partitions trace back*/
            tbData->aSeqLen[0] = traceBack (pData, wData, tbData);
#ifndef NDEBUG
            sprintf(msg, "DSTB returned path of length %ld \n", tbData->aSeqLen[0]);  
            mprintf(3, msg, 1);
#endif

            /*3. send partital alignment length*/	
            MPI_return = MPI_Send (&tbData->aSeqLen[0], 1, MPI_LONG_LONG, 0, 4, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
            printf("[%d]  sent length %ld  MPI_return %d \n", myProcid, tbData->aSeqLen[0], MPI_return);  
            fflush(stdout);
#endif
            /*4. send the partital alignment itself*/
            for (i=0;i<tbData->seqNum;i++) {
                tbData->algnseq[0][i][tbData->aSeqLen[0]] = '\0';
                MPI_return = MPI_Send (tbData->algnseq[0][i], tbData->aSeqLen[0], MPI_CHAR, 0, 5, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
                sprintf(msg, " sent aligned seq for seq %lld MPI_return %d = %s ", i, MPI_return, tbData->algnseq[0][i]);  
                mprintf(3, msg, 1);
                printf("[%d]  sent aligned seq for seq %lld MPI_return %d = %s\n", myProcid, i, MPI_return, tbData->algnseq[0][i]);  
                fflush(stdout);
#endif
            }
#ifndef NDEBUG
            mprintf(3, "\n", 1);
#endif
            /*5. send the last global index in this partial alignment to Master*/		
            MPI_return = MPI_Send (tbData->maxCellIndex, tbData->seqNum, MPI_LONG_LONG, 0, 7, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
            printf ("[%d] sent MPI_return %d maxCellIndex { %lld", myProcid, MPI_return, tbData->maxCellIndex[0]);
            for (i=1;i<tbData->seqNum;i++)
                printf (", %lld", tbData->maxCellIndex[i]);
            printf ("}\n");
            fflush(stdout);
            sprintf(msg, "after send maxCellIndex {%ld", tbData->maxCellIndex[0]);
            mprintf(3, msg, 1);
            for (i=1;i<tbData->seqNum;i++) {
                sprintf(msg, ", %ld", tbData->maxCellIndex[i]);
                mprintf(3, msg, 1);
            }
            mprintf(3, "}\n", 1);
#endif
            /*6. send the next Processor where a next remote score was found from this partial alignment to Master*/
            MPI_return = MPI_Send (&tbData->currProc, 1, MPI_INT, 0, 8, MOAMSA_COMM_WORLD);
#ifndef NDEBUG
            printf("[%d]  sent new  tbData->currProc = %d MPI_return %d \n", myProcid, tbData->currProc, MPI_return);  
            fflush(stdout);
            sprintf(msg, "after send next proc %d\n", tbData->currProc);
            mprintf(3, msg, 1);
#endif
        }
    }
    if (tbData != NULL)
        freetbData(&tbData);
    return NULL;
}