void UHMMCalibrate::calibrateParallel(WorkPool_s *wpool, TaskStateInfo& si) { HMMERTaskLocalData *tls = getHMMERTaskLocalData(); struct alphabet_s *al = &tls->al; struct plan7_s *hmm = wpool->hmm; struct dpmatrix_s *mx = CreatePlan7Matrix(1, hmm->M, 25, 0); int len; float sc; char *seq; unsigned char *dsq; while(true) { /* generate a sequence */ { QMutexLocker locker(&wpool->lockInput); wpool->nseq++; if (wpool->nseq > wpool->nsample) { /* we're done; release input lock, break loop */ break; } if (wpool->fixedlen) { len = wpool->fixedlen; } else { do { len = (int) Gaussrandom(wpool->lenmean, wpool->lensd); } while (len < 1); } seq = RandomSequence(al->Alphabet, wpool->randomseq.data(), al->Alphabet_size, len); } /* compute score */ dsq = DigitizeSequence(seq, len); if (P7ViterbiSpaceOK(len, hmm->M, mx)) { sc = P7Viterbi(dsq, len, hmm, mx, NULL); } else { int pStub; sc = P7SmallViterbi(dsq, len, hmm, mx, NULL, pStub); } free(dsq); free(seq); /* save output */ QMutexLocker locker(&wpool->lockOutput); AddToHistogram(wpool->hist, sc); wpool->max_score = qMax(wpool->max_score, sc); si.progress = int(100*wpool->nseq/float(wpool->nsample)); //TODO: update progress for all tasks? if (wpool->progress!=NULL) { *wpool->progress = si.progress; } } FreePlan7Matrix(mx); }
/* Function: main_loop_serial() * Date: SRE, Tue Aug 18 16:18:28 1998 [St. Louis] * * Purpose: Given an HMM and parameters for synthesizing random * sequences; return a histogram of scores. * (Serial version) * * Args: hmm - an HMM to calibrate. * seed - random number seed * nsample - number of seqs to synthesize * lenmean - mean length of random sequence * lensd - std dev of random seq length * fixedlen - if nonzero, override lenmean, always this len * ret_hist - RETURN: the score histogram * ret_max - RETURN: highest score seen in simulation * * Returns: (void) * hist is alloc'ed here, and must be free'd by caller. */ static void main_loop_serial(struct plan7_s *hmm, int seed, int nsample, float lenmean, float lensd, int fixedlen, struct histogram_s **ret_hist, float *ret_max) { struct histogram_s *hist; float randomseq[MAXABET]; float p1; float max; char *seq; char *dsq; float score; int sqlen; int idx; /* Initialize. * We assume we've already set the alphabet (safe, because * HMM input sets the alphabet). */ sre_srandom(seed); P7Logoddsify(hmm, TRUE); P7DefaultNullModel(randomseq, &p1); hist = AllocHistogram(-200, 200, 100); max = -FLT_MAX; for (idx = 0; idx < nsample; idx++) { /* choose length of random sequence */ if (fixedlen) sqlen = fixedlen; else do sqlen = (int) Gaussrandom(lenmean, lensd); while (sqlen < 1); /* generate it */ seq = RandomSequence(Alphabet, randomseq, Alphabet_size, sqlen); dsq = DigitizeSequence(seq, sqlen); if (P7ViterbiSize(sqlen, hmm->M) <= RAMLIMIT) score = P7Viterbi(dsq, sqlen, hmm, NULL); else score = P7SmallViterbi(dsq, sqlen, hmm, NULL); AddToHistogram(hist, score); if (score > max) max = score; free(dsq); free(seq); } *ret_hist = hist; *ret_max = max; return; }
void JMMArrayTable::StreamAllocationSizeHistogram ( ostream& output ) const { JSize histo[ JMemoryManager::kHistogramSlotCount ]; bzero(histo, sizeof(histo)); const JSize count = itsAllocatedTable->GetElementCount(); for (JIndex i=1;i<=count;i++) { AddToHistogram(itsAllocatedTable->GetElement(i), histo); } StreamHistogram(output, histo); }
void JMMHashTable::StreamAllocationSizeHistogram ( ostream& output ) const { JSize histo[ JMemoryManager::kHistogramSlotCount ]; bzero(histo, sizeof(histo)); JConstHashCursor<JMMRecord> cursor(itsAllocatedTable); while ( cursor.NextFull() ) { AddToHistogram(cursor.GetValue(), histo); } StreamHistogram(output, histo); }
boolean should_store_Hscore(Hscore * hs,int score) { hs->total++; if( hs->report_level != -1 && (hs->total % hs->report_level == 0)) { if( hs->len > 0) { info("Done %d comparisons: last stored comparison was %s to %s",hs->total,hs->ds[hs->len-1]->query->name,hs->ds[hs->len-1]->target->name); } else { info("Done %d comparisons: No stored comparisons",hs->total); } } if( hs->his != NULL && hs->score_to_his != NULL ) { AddToHistogram(hs->his,(*hs->score_to_his)(score)); } if( hs->should_store == NULL ) { return TRUE; } return (*hs->should_store)(score,hs->score_level); }
static void main_loop_serial(struct plan7_s *hmm, const char* seq, int seqLen, struct threshold_s *thresh, int do_forward, int do_null2, int do_xnu, struct histogram_s *histogram, struct tophit_s *ghit, struct tophit_s *dhit, int *ret_nseq, TaskStateInfo& ti) { //get HMMERTaskLocalData HMMERTaskLocalData *tld = getHMMERTaskLocalData(); alphabet_s *al = &tld->al; struct dpmatrix_s *mx; // DP matrix, growable struct p7trace_s *tr; // traceback unsigned char *dsq; // digitized target sequence float sc; // score of an HMM search double pvalue; // pvalue of an HMM score double evalue; // evalue of an HMM score // Create a DP matrix; initially only two rows big, but growable; // we overalloc by 25 rows (L dimension) when we grow; not growable // in model dimension, since we know the hmm size mx = CreatePlan7Matrix(1, hmm->M, 25, 0); assert(seqLen > 0); dsq = DigitizeSequence(seq, seqLen); if (do_xnu && al->Alphabet_type == hmmAMINO) { XNU(dsq, seqLen); } // 1. Recover a trace by Viterbi. // In extreme cases, the alignment may be literally impossible; // in which case, the score comes out ridiculously small (but not // necessarily <= -INFTY, because we're not terribly careful // about underflow issues), and tr will be returned as NULL. if (P7ViterbiSpaceOK(seqLen, hmm->M, mx)) { sc = P7Viterbi(dsq, seqLen, hmm, mx, &tr); } else { sc = P7SmallViterbi(dsq, seqLen, hmm, mx, &tr, ti.progress); } // 2. If we're using Forward scores, calculate the // whole sequence score; this overrides anything // PostprocessSignificantHit() is going to do to the per-seq score. if (do_forward) { sc = P7Forward(dsq, seqLen, hmm, NULL); if (do_null2) sc -= TraceScoreCorrection(hmm, tr, dsq); } // 2. Store score/pvalue for global alignment; will sort on score, // which in hmmsearch is monotonic with E-value. // Keep all domains in a significant sequence hit. // We can only make a lower bound estimate of E-value since // we don't know the final value of nseq yet, so the list // of hits we keep in memory is >= the list we actually // output. // pvalue = PValue(hmm, sc); evalue = thresh->Z ? (double) thresh->Z * pvalue : (double) pvalue; if (sc >= thresh->globT && evalue <= thresh->globE) { sc = PostprocessSignificantHit(ghit, dhit, tr, hmm, dsq, seqLen, (char *)"sequence", //todo: sqinfo.name, NULL, NULL, do_forward, sc, do_null2, thresh, FALSE); // FALSE-> not hmmpfam mode, hmmsearch mode } AddToHistogram(histogram, sc); P7FreeTrace(tr); free(dsq); FreePlan7Matrix(mx); return; }
static void main_loop_serial(struct plan7_s *hmm, int seed, int nsample, float lenmean, float lensd, int fixedlen, struct histogram_s **ret_hist, float *ret_max, int& cancelFlag, int& progress) { struct histogram_s *hist; struct dpmatrix_s *mx; float randomseq[MAXABET]; float p1; float max; char *seq; unsigned char *dsq; float score; int sqlen; int idx; // Initialize. // We assume we've already set the alphabet (safe, because // HMM input sets the alphabet). sre_srandom(seed); //get HMMERTaskLocalData HMMERTaskLocalData *tls = getHMMERTaskLocalData(); alphabet_s &al = tls->al; SetAlphabet(hmm->atype); P7Logoddsify(hmm, TRUE); P7DefaultNullModel(randomseq, &p1); hist = AllocHistogram(-200, 200, 100); mx = CreatePlan7Matrix(1, hmm->M, 25, 0); max = -FLT_MAX; progress = 0; int pStub; for (idx = 0; idx < nsample && !cancelFlag; idx++) { // choose length of random sequence if (fixedlen) { sqlen = fixedlen; } else { do sqlen = (int) Gaussrandom(lenmean, lensd); while (sqlen < 1); } // generate it seq = RandomSequence(al.Alphabet, randomseq, al.Alphabet_size, sqlen); dsq = DigitizeSequence(seq, sqlen); if (P7ViterbiSpaceOK(sqlen, hmm->M, mx)) { score = P7Viterbi(dsq, sqlen, hmm, mx, NULL); } else { score = P7SmallViterbi(dsq, sqlen, hmm, mx, NULL, pStub); } AddToHistogram(hist, score); max = qMax(score, max); progress = int(100*idx/float(nsample)); free(dsq); free(seq); } FreePlan7Matrix(mx); *ret_hist = hist; *ret_max = max; }
int main(void) { int master_tid; /* PVM TID of our master */ int slaveidx; /* my slave index (0..nslaves-1) */ struct plan7_s *hmm; /* HMM to calibrate, sent from master */ struct histogram_s *hist; /* score histogram */ int hmmidx; /* index of this HMM */ char *seq; /* synthetic random sequence */ char *dsq; /* digitized seq */ int len; /* length of seq */ float sc; /* score of seq aligned to HMM */ float max; /* maximum score seen in sample */ int seed; /* random number seed */ int nsample; /* number of seqs to sample */ int fixedlen; /* if nonzero, fixed length of seq */ float lenmean; /* Gaussian mean length of seq */ float lensd; /* Gaussian length std. dev. for seq */ int fitok; /* TRUE if EVD fit was OK */ float randomseq[MAXABET]; /* iid frequencies of residues */ float p1; int alphatype; /* alphabet type, hmmAMINO or hmmNUCLEIC */ int idx; int code; /* Register leave_pvm() cleanup function so any exit() call * first calls pvm_exit(). */ if (atexit(leave_pvm) != 0) { pvm_exit(); Die("slave couldn't register leave_pvm()"); } /***************************************************************** * initialization. * Master broadcasts the problem to us: parameters of the * HMM calibration. ******************************************************************/ master_tid = pvm_parent(); /* who's our master? */ pvm_recv(master_tid, HMMPVM_INIT); pvm_upkint(&nsample, 1, 1); pvm_upkint(&fixedlen, 1, 1); pvm_upkfloat(&lenmean, 1, 1); pvm_upkfloat(&lensd, 1, 1); /* tell the master we're OK and ready to go (or not) */ code = HMMPVM_OK; pvm_initsend(PvmDataDefault); pvm_pkint(&code, 1, 1); pvm_send(master_tid, HMMPVM_RESULTS); /***************************************************************** * Main loop. * Receive a random number seed, then an HMM to search against. * If we receive a -1 seed, we shut down. *****************************************************************/ slaveidx = -1; for (;;) { pvm_recv(master_tid, HMMPVM_WORK); pvm_upkint(&seed, 1, 1); if (seed == -1) break; /* shutdown signal */ pvm_upkint(&hmmidx, 1, 1); pvm_upkint(&alphatype,1, 1); SetAlphabet(alphatype); hmm = PVMUnpackHMM(); if (hmm == NULL) Die("oh no, the HMM never arrived"); if (slaveidx == -1) slaveidx = hmmidx; P7DefaultNullModel(randomseq, &p1); sre_srandom(seed); P7Logoddsify(hmm, TRUE); hist = AllocHistogram(-200, 200, 100); max = -FLT_MAX; for (idx = 0; idx < nsample; idx++) { /* choose length of random sequence */ if (fixedlen) len = fixedlen; else do len = (int) Gaussrandom(lenmean, lensd); while (len < 1); /* generate it */ seq = RandomSequence(Alphabet, randomseq, Alphabet_size, len); dsq = DigitizeSequence(seq, len); if (P7ViterbiSize(len, hmm->M) <= RAMLIMIT) sc = P7Viterbi(dsq, len, hmm, NULL); else sc = P7SmallViterbi(dsq, len, hmm, NULL); AddToHistogram(hist, sc); if (sc > max) max = sc; free(seq); free(dsq); } /* Fit an EVD to the observed histogram. * The TRUE left-censors and fits only the right slope of the histogram. * The 9999. is an arbitrary high number that means we won't trim outliers * on the right. */ fitok = ExtremeValueFitHistogram(hist, TRUE, 9999.); /* Return output to master. * Currently we don't send the histogram back, but we could. */ pvm_initsend(PvmDataDefault); pvm_pkint(&slaveidx, 1, 1); pvm_pkint(&hmmidx, 1, 1); PVMPackString(hmm->name); pvm_pkint(&fitok, 1, 1); pvm_pkfloat(&(hist->param[EVD_MU]), 1, 1); pvm_pkfloat(&(hist->param[EVD_LAMBDA]), 1, 1); pvm_pkfloat(&max, 1, 1); pvm_send(master_tid, HMMPVM_RESULTS); /* cleanup */ FreeHistogram(hist); FreePlan7(hmm); } /*********************************************** * Cleanup, return. ***********************************************/ return 0; /* pvm_exit() is called by atexit() registration. */ }
/* Function: main_loop_pvm() * Date: SRE, Wed Aug 19 13:59:54 1998 [St. Louis] * * Purpose: Given an HMM and parameters for synthesizing random * sequences; return a histogram of scores. * (PVM version) * * Args: hmm - an HMM to calibrate. * seed - random number seed * nsample - number of seqs to synthesize * lumpsize- # of seqs per slave exchange; controls granularity * lenmean - mean length of random sequence * lensd - std dev of random seq length * fixedlen- if nonzero, override lenmean, always this len * hist - RETURN: the score histogram * ret_max - RETURN: highest score seen in simulation * extrawatch - RETURN: total CPU time spend in slaves. * ret_nslaves- RETURN: number of PVM slaves run. * * Returns: (void) * hist is alloc'ed here, and must be free'd by caller. */ static void main_loop_pvm(struct plan7_s *hmm, int seed, int nsample, int lumpsize, float lenmean, float lensd, int fixedlen, struct histogram_s **ret_hist, float *ret_max, Stopwatch_t *extrawatch, int *ret_nslaves) { struct histogram_s *hist; int master_tid; int *slave_tid; int nslaves; int nsent; /* # of seqs we've asked for so far */ int ndone; /* # of seqs we've got results for so far */ int packet; /* # of seqs to have a slave do */ float max; int slaveidx; /* id of a slave */ float *sc; /* scores returned by a slave */ Stopwatch_t slavewatch; int i; StopwatchZero(extrawatch); hist = AllocHistogram(-200, 200, 100); max = -FLT_MAX; /* Initialize PVM */ if ((master_tid = pvm_mytid()) < 0) Die("pvmd not responding -- do you have PVM running?"); #if DEBUGLEVEL >= 1 pvm_catchout(stderr); /* catch output for debugging */ #endif PVMSpawnSlaves("hmmcalibrate-pvm", &slave_tid, &nslaves); /* Initialize the slaves */ pvm_initsend(PvmDataDefault); pvm_pkfloat(&lenmean, 1, 1); pvm_pkfloat(&lensd, 1, 1); pvm_pkint( &fixedlen, 1, 1); pvm_pkint( &Alphabet_type, 1, 1); pvm_pkint( &seed, 1, 1); if (! PVMPackHMM(hmm)) Die("Failed to pack the HMM"); pvm_mcast(slave_tid, nslaves, HMMPVM_INIT); SQD_DPRINTF1(("Initialized %d slaves\n", nslaves)); /* Confirm slaves' OK status. */ PVMConfirmSlaves(slave_tid, nslaves); SQD_DPRINTF1(("Slaves confirm that they're ok...\n")); /* Load the slaves */ nsent = ndone = 0; for (slaveidx = 0; slaveidx < nslaves; slaveidx++) { packet = (nsample - nsent > lumpsize ? lumpsize : nsample - nsent); pvm_initsend(PvmDataDefault); pvm_pkint(&packet, 1, 1); pvm_pkint(&slaveidx, 1, 1); pvm_send(slave_tid[slaveidx], HMMPVM_WORK); nsent += packet; } SQD_DPRINTF1(("Loaded %d slaves\n", nslaves)); /* Receive/send loop */ sc = MallocOrDie(sizeof(float) * lumpsize); while (nsent < nsample) { /* integrity check of slaves */ PVMCheckSlaves(slave_tid, nslaves); /* receive results */ SQD_DPRINTF2(("Waiting for results...\n")); pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); pvm_upkint(&packet, 1, 1); pvm_upkfloat(sc, packet, 1); SQD_DPRINTF2(("Got results.\n")); ndone += packet; /* store results */ for (i = 0; i < packet; i++) { AddToHistogram(hist, sc[i]); if (sc[i] > max) max = sc[i]; } /* send new work */ packet = (nsample - nsent > lumpsize ? lumpsize : nsample - nsent); pvm_initsend(PvmDataDefault); pvm_pkint(&packet, 1, 1); pvm_pkint(&slaveidx, 1, 1); pvm_send(slave_tid[slaveidx], HMMPVM_WORK); SQD_DPRINTF2(("Told slave %d to do %d more seqs.\n", slaveidx, packet)); nsent += packet; } /* Wait for the last output to come in. */ while (ndone < nsample) { /* integrity check of slaves */ PVMCheckSlaves(slave_tid, nslaves); /* receive results */ SQD_DPRINTF1(("Waiting for final results...\n")); pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); pvm_upkint(&packet, 1, 1); pvm_upkfloat(sc, packet, 1); SQD_DPRINTF2(("Got some final results.\n")); ndone += packet; /* store results */ for (i = 0; i < packet; i++) { AddToHistogram(hist, sc[i]); if (sc[i] > max) max = sc[i]; } } /* Shut down the slaves: send -1,-1,-1. */ pvm_initsend(PvmDataDefault); packet = -1; pvm_pkint(&packet, 1, 1); pvm_pkint(&packet, 1, 1); pvm_pkint(&packet, 1, 1); pvm_mcast(slave_tid, nslaves, HMMPVM_WORK); /* Collect stopwatch results; quit the VM; return. */ for (i = 0; i < nslaves; i++) { pvm_recv(-1, HMMPVM_RESULTS); pvm_upkint(&slaveidx, 1, 1); StopwatchPVMUnpack(&slavewatch); SQD_DPRINTF1(("Slave %d finished; says it used %.2f cpu, %.2f sys\n", slaveidx, slavewatch.user, slavewatch.sys)); StopwatchInclude(extrawatch, &slavewatch); } free(slave_tid); free(sc); pvm_exit(); *ret_hist = hist; *ret_max = max; *ret_nslaves = nslaves; return; }
/* Function: worker_thread() * Date: SRE, Thu Jul 16 10:41:02 1998 [St. Louis] * * Purpose: The procedure executed by the worker threads. * * Args: ptr - (void *) that is recast to a pointer to * the workpool. * * Returns: (void *) */ void * worker_thread(void *ptr) { struct plan7_s *hmm; struct workpool_s *wpool; char *seq; char *dsq; int len; float sc; int rtn; Stopwatch_t thread_watch; StopwatchStart(&thread_watch); wpool = (struct workpool_s *) ptr; hmm = wpool->hmm; for (;;) { /* 1. Synthesize a random sequence. * The input sequence number is a shared resource, * and sre_random() isn't thread-safe, so protect * the whole section with mutex. */ /* acquire a lock */ if ((rtn = pthread_mutex_lock(&(wpool->input_lock))) != 0) Die("pthread_mutex_lock failure: %s\n", strerror(rtn)); /* generate a sequence */ wpool->nseq++; if (wpool->nseq > wpool->nsample) { /* we're done; release input lock, break loop */ if ((rtn = pthread_mutex_unlock(&(wpool->input_lock))) != 0) Die("pthread_mutex_unlock failure: %s\n", strerror(rtn)); break; } if (wpool->fixedlen) len = wpool->fixedlen; else do len = (int) Gaussrandom(wpool->lenmean, wpool->lensd); while (len < 1); seq = RandomSequence(Alphabet, wpool->randomseq, Alphabet_size, len); /* release the lock */ if ((rtn = pthread_mutex_unlock(&(wpool->input_lock))) != 0) Die("pthread_mutex_unlock failure: %s\n", strerror(rtn)); /* 2. Score the sequence against the model. */ dsq = DigitizeSequence(seq, len); if (P7ViterbiSize(len, hmm->M) <= RAMLIMIT) sc = P7Viterbi(dsq, len, hmm, NULL); else sc = P7SmallViterbi(dsq, len, hmm, NULL); free(dsq); free(seq); /* 3. Save the output; hist and max_score are shared, * so protect this section with the output mutex. */ /* acquire lock on the output queue */ if ((rtn = pthread_mutex_lock(&(wpool->output_lock))) != 0) Die("pthread_mutex_lock failure: %s\n", strerror(rtn)); /* save output */ AddToHistogram(wpool->hist, sc); if (sc > wpool->max_score) wpool->max_score = sc; /* release our lock */ if ((rtn = pthread_mutex_unlock(&(wpool->output_lock))) != 0) Die("pthread_mutex_unlock failure: %s\n", strerror(rtn)); } StopwatchStop(&thread_watch); /* acquire lock on the output queue */ if ((rtn = pthread_mutex_lock(&(wpool->output_lock))) != 0) Die("pthread_mutex_lock failure: %s\n", strerror(rtn)); /* accumulate cpu time into main stopwatch */ StopwatchInclude(&(wpool->watch), &thread_watch); /* release our lock */ if ((rtn = pthread_mutex_unlock(&(wpool->output_lock))) != 0) Die("pthread_mutex_unlock failure: %s\n", strerror(rtn)); pthread_exit(NULL); return NULL; /* solely to silence compiler warnings */ }