/******************************************************************* * Function: ArrayIndexAnalysis::doAnalysis * Purpose : Analyze an If then else statement * Initial : Nurudeen A. Lameed on July 21, 2009 ******************************************************************** Revisions and bug fixes: */ void ArrayIndexAnalysis::doAnalysis(const IfElseStmt* pIfStmt, const FlowSet& startSet, FlowSet& exitSet, FlowSet& retSet, FlowSet& breakSet, FlowSet& contSet, FlowSetMap& flowSetMap) { // Get the condition expression Expression* pTestExpr = pIfStmt->getCondition(); flowSetMap[pTestExpr] = startSet; FlowSet ifSet; FlowSet elseSet; // analyze the if block doAnalysis( pIfStmt->getIfBlock(), startSet, ifSet, retSet, breakSet, contSet, flowSetMap ); // analyze the else block doAnalysis( pIfStmt->getElseBlock(), startSet, elseSet, retSet, breakSet, contSet, flowSetMap ); // merge set exitSet.insert(ifSet.begin(), ifSet.end()); exitSet.insert(elseSet.begin(), elseSet.end()); }
int main(void) { struct timeval tv1; struct timeval tv2; int i, cnt, steps = 0; Board b = NULL; StartBoard **sb = malloc(sizeof(StartBoard *)); cnt = getStartBoards(sb); b = sb[0]->board; if (!b) { fprintf(stderr, "failed to retrieve the start board\n"); return 1; } gettimeofday(&tv1,NULL); start(); steps = doAnalysis(b); gettimeofday(&tv2,NULL); if (steps <= 0) { fprintf(stderr, "failed to find way out\n"); return 1; } while (b!=NULL) { printBoard(b); b = getNextBoard(b); } for (i=0;i<cnt;i++) { free(sb[i]); } free(sb); end(); fprintf(stderr, "\nTotal steps %d\n", steps); fprintf(stderr, "Time consumption: %ld sec, %ld micro seconds\n", tv2.tv_sec-tv1.tv_sec, tv2.tv_usec-tv1.tv_usec); return 0; }
/******************************************************************* * Function: ArrayIndexAnalysis::doAnalysis * Purpose : Analyze a function body * Initial : Nurudeen A. Lameed on July 21, 2009 ******************************************************************** Revisions and bug fixes: */ void ArrayIndexAnalysis::doAnalysis(const StmtSequence* pFuncBody, FlowSetMap& flowSetMap) { // find all the index symbols in the current function findAllMatrixIndexSymbols(pFuncBody); // get the initial flow set const FlowSet& startSet = top(); // declare output sets FlowSet exitSet; FlowSet retSet; FlowSet breakSet; FlowSet contSet; // perform flow analyis on the function body doAnalysis(pFuncBody, startSet, exitSet, retSet, breakSet, contSet, flowSetMap); }
void NCL2MT::processTree(std::ostream *os, unsigned numTaxa, const NxsCharactersBlock * charsBlock, const NxsDiscreteDatatypeMapper * dataMapper, const NxsCDiscreteStateSet ** compressedMatrix, const vector<double> & patternWeights, const vector<int> &origToCompressed, const NxsSimpleTree & nxsTree, const ModelDescription & md, ::INIReader & iniReader) { assert(dataMapper != nullptr); const unsigned numRealChars = patternWeights.size(); unsigned firstPartLength = patternWeights.size(); const unsigned numStates = dataMapper->GetNumStates(); vector<unsigned> origToComp; for (auto otc : origToCompressed) { origToComp.push_back((unsigned) otc); } vector<mt::char_state_t> bogusChar; if (md.GetAscBiasMode() == mt::ModelDescription::VAR_ONLY_NO_MISSING_ASC_BIAS) { firstPartLength += numStates; for (auto i = 0U; i < numStates; ++i) { bogusChar.push_back(i); } } vector<vector<mt::char_state_t> > rawMatrix(numTaxa); NxsCDiscreteStateSet maxStateCode = 0; for (auto i = 0U; i < numTaxa; ++i) { rawMatrix[i].reserve(firstPartLength); for (auto j = 0U; j < numRealChars; ++j) { NxsCDiscreteStateSet r = compressedMatrix[i][j]; if (r < 0) { r = numStates; } if (r > maxStateCode) { maxStateCode = r; } rawMatrix[i].push_back((mt::char_state_t) compressedMatrix[i][j]); } for (auto k : bogusChar) { rawMatrix[i].push_back(k); } } vector<mt::char_state_t *> rowPtrs(numTaxa); for (auto i = 0U; i < numTaxa; ++i) { rowPtrs[i] = &(rawMatrix[i][0]); } if (maxStateCode < (NxsCDiscreteStateSet) numStates) { maxStateCode = numStates; } unsigned numStateCodes = maxStateCode + 1; mt::CharStateToPrimitiveInd cs2pi(numStateCodes); for (auto i = 0U; i < numStateCodes; ++i) { vector<mt::char_state_t> v; for (auto xs : dataMapper->GetStateSetForCode(i)) { v.push_back(static_cast<mt::char_state_t>(xs)); } cs2pi.SetStateCode(i, v); } std::vector<unsigned> partLengths(1, firstPartLength); mt::CharModel * cm; unsigned numRateCats = 1; if (md.GetAscBiasMode() == mt::ModelDescription::VAR_ONLY_NO_MISSING_ASC_BIAS) { cm = new mt::MkVarNoMissingAscCharModel(numStates, numRateCats); } else { cm = new mt::MkCharModel(numStates, numRateCats); } unsigned numNodes = 2 * numTaxa - 1; BitFieldMatrix bMat; cm->alphabet = convertToBitFieldMatrix(*charsBlock, bMat); mt::MTInstance mtInstance(numTaxa, partLengths, origToComp, patternWeights, cm); mt::PartitionedMatrix & partMat = mtInstance.partMat; partMat.fillPartition(0, const_cast<const mt::char_state_t**>(&(rowPtrs[0])), &cs2pi); mt::Tree &tree = mtInstance.tree; std::map<const NxsSimpleNode *, unsigned> ncl2nodeNumber; std::vector<const NxsSimpleNode *> pre = nxsTree.GetPreorderTraversal(); unsigned internalIndex = numTaxa; for (std::vector<const NxsSimpleNode *>::iterator ndIt = pre.begin(); ndIt != pre.end(); ++ndIt) { const NxsSimpleNode *nd = *ndIt; //std::cout << "address = " << (long) nd << " is leaf = " << nd->IsTip() << " index = " << nd->GetTaxonIndex() << " parent address = " << (long) nd->GetEdgeToParent().GetParent() << std::endl; unsigned num; if (nd->IsTip()) { num = nd->GetTaxonIndex(); } else { num = internalIndex++; } mt::Node * treeNode = tree.GetNode(num); const NxsSimpleNode * par = nd->GetEdgeToParent().GetParent(); if (par == nullptr) { tree.SetRoot(treeNode); } else { assert (ncl2nodeNumber.find(par) != ncl2nodeNumber.end()); unsigned parNodeNumber = ncl2nodeNumber[par]; mt::Node * parNode = tree.GetNode(parNodeNumber); parNode->AddChild(treeNode, nd->GetEdgeToParent().GetDblEdgeLen()); } ncl2nodeNumber[nd] = num; } for (auto li = 0U; li < numTaxa; ++li) { mt::Node * leaf = tree.GetLeaf(li); assert(leaf); for (auto j = 0U; j < partMat.GetNumPartitions(); ++j) { leaf->SetData(j, (void *) partMat.GetLeafCharacters(j, li)); leaf->SetWork(j, (void *) new mt::LeafWork(firstPartLength, numStateCodes, numStates, numRateCats)); } } for (auto li = numTaxa; li < numNodes; ++ li) { mt::Node * nd = tree.GetNode(li); for (auto j = 0U; j < partMat.GetNumPartitions(); ++j) { nd->SetWork(j, (void *) new mt::InternalNodeWork(firstPartLength, numStates, numRateCats)); } } mt::ProcessActionsEnum action = configureBasedOnINI(mtInstance, iniReader, std::cerr); try { doAnalysis(os, mtInstance, action); } catch (...) { delete cm; throw; } delete cm; }
/******************************************************************* * Function: ArrayIndexAnalysis::doAnalysis * Purpose : Analyze a block ( sequence of statements) * Initial : Nurudeen A. Lameed on July 21, 2009 ******************************************************************** Revisions and bug fixes: */ void ArrayIndexAnalysis::doAnalysis(const StmtSequence* pStmtSeq, const FlowSet& startSet, FlowSet& exitSet, FlowSet& retSet, FlowSet& breakSet, FlowSet& contSet, FlowSetMap& flowSetMap) { // declare a reference to the sequence of statements const StmtSequence::StmtVector& stmts = pStmtSeq->getStatements(); // declare and initalize the current with the input set FlowSet curSet = startSet; // store the in set for the current sequence of statements flowSetMap[pStmtSeq] = curSet; // examine the statements and take actions for (StmtSequence::StmtVector::const_iterator it = stmts.begin(), iEnd = stmts.end(); it != iEnd; ++it) { Statement* pStmt = *it; switch( pStmt->getStmtType() ) { case Statement::BREAK: { // copy the current flow set breakSet.insert(curSet.begin(), curSet.end()); } break; case Statement::CONTINUE: { // copy the current flow set contSet.insert(curSet.begin(), curSet.end()); } break; case Statement::RETURN: { // copy the current flow set retSet.insert(curSet.begin(), curSet.end()); } break; case Statement::IF_ELSE: { // store the in-set flowSetMap[pStmt] = curSet; // declare out-set for the analysis FlowSet exitSet; doAnalysis( dynamic_cast<const IfElseStmt*>(pStmt), curSet, exitSet, retSet, breakSet, contSet, flowSetMap ); // update the current flow set with the outset from this analysis curSet = exitSet; } break; case Statement::LOOP: { // store the in-set flowSetMap[pStmt] = curSet; // declare out-set for the analysis FlowSet exitSet; // perform flow analysis on a loop doAnalysis( dynamic_cast<const LoopStmt*>(pStmt), curSet, exitSet, retSet, flowSetMap ); // update the current flow set with the outset from this analysis curSet = exitSet; } break; default: { // store the in-set flowSetMap[pStmt] = curSet; // compute the out-set curSet = flowFunction(pStmt, curSet); } } } // the current out set exitSet.insert(curSet.begin(), curSet.end()); }
/******************************************************************* * Function: ArrayIndexAnalysis::doAnalysis * Purpose : Analyze a loop statement * Initial : Nurudeen A. Lameed on July 21, 2009 ******************************************************************** Revisions and bug fixes: */ void ArrayIndexAnalysis::doAnalysis(const LoopStmt* pLoopStmt, const FlowSet& startSet, FlowSet& exitSet, FlowSet& retSet, FlowSetMap& flowSetMap) { // declare out set FlowSet initExitSet; // declare other output sets FlowSet initRetSet; FlowSet initBreakSet; FlowSet initContSet; // perform flow analysis on the sequence of statements for loop initialization doAnalysis( pLoopStmt->getInitSeq(), startSet, initExitSet, initRetSet, initBreakSet, initContSet, flowSetMap ); // Declare a variable for the current loop array bound checks //FlowSetMap loopSetMap; // declare current increment stmt output set FlowSet curIncrExitSet; // loop until a fixed point is reached. while(true) { // Clear the loop info map //loopSetMap.clear(); // declare and intialize in set for loop test statement FlowSet testStartSet(initExitSet); testStartSet.insert(curIncrExitSet.begin(), curIncrExitSet.end()); // FlowSet testExitSet; FlowSet testRetSet; FlowSet testBreakSet; FlowSet testContSet; // perform flow analysis on the sequence of statements for loop test doAnalysis( pLoopStmt->getTestSeq(), testStartSet, testExitSet, testRetSet, testBreakSet, testContSet, flowSetMap ); FlowSet bodyExitSet; FlowSet breakSet; FlowSet contSet; // perform flow analysis on the loop's body doAnalysis( pLoopStmt->getBodySeq(), testExitSet, // in bodyExitSet, // out retSet, breakSet, contSet, flowSetMap ); // Merge the test exit map and the break map breakSet.insert(testExitSet.begin(), testExitSet.end()); FlowSet incrStartSet(bodyExitSet); incrStartSet.insert(contSet.begin(), contSet.end()); FlowSet incrExitSet; FlowSet incrRetSet; FlowSet incrBreakSet; FlowSet incrContSet; // perform flow analysis on the sequence of statements for the loop increment statement doAnalysis( pLoopStmt->getIncrSeq(), incrStartSet, incrExitSet, incrRetSet, incrBreakSet, incrContSet, flowSetMap ); // update exitSet = breakSet; // is fixed point reached? if (curIncrExitSet == incrExitSet) { break; } curIncrExitSet = incrExitSet; } // collect the result //flowSetMap.insert(loopSetMap.begin(), loopSetMap.end()); }
void AnalyserQueue::run() { unsigned static id = 0; //the id of this thread, for debugging purposes QThread::currentThread()->setObjectName(QString("AnalyserQueue %1").arg(++id)); // If there are no analyzers, don't waste time running. if (m_aq.size() == 0) return; m_progressInfo.current_track = TrackPointer(); m_progressInfo.track_progress = 0; m_progressInfo.queue_size = 0; m_progressInfo.sema.release(); // Initalise with one while (!m_exit) { TrackPointer nextTrack = dequeueNextBlocking(); // It's important to check for m_exit here in case we decided to exit // while blocking for a new track. if (m_exit) return; // If the track is NULL, try to get the next one. // Could happen if the track was queued but then deleted. // Or if dequeueNextBlocking is unblocked by exit == true if (!nextTrack) { m_qm.lock(); m_queue_size = m_tioq.size(); m_qm.unlock(); if (m_queue_size == 0) { emit(queueEmpty()); // emit asynchrony for no deadlock } continue; } Trace trace("AnalyserQueue analyzing track"); // Get the audio SoundSourceProxy soundSource(nextTrack); soundSource.open(); //Open the file for reading int iNumSamples = soundSource.length(); int iSampleRate = soundSource.getSampleRate(); if (iNumSamples == 0 || iSampleRate == 0) { qDebug() << "Skipping invalid file:" << nextTrack->getLocation(); continue; } QListIterator<Analyser*> it(m_aq); bool processTrack = false; while (it.hasNext()) { // Make sure not to short-circuit initialise(...) if (it.next()->initialise(nextTrack, iSampleRate, iNumSamples)) { processTrack = true; } } m_qm.lock(); m_queue_size = m_tioq.size(); m_qm.unlock(); if (processTrack) { emitUpdateProgress(nextTrack, 0); bool completed = doAnalysis(nextTrack, &soundSource); if (!completed) { //This track was cancelled QListIterator<Analyser*> itf(m_aq); while (itf.hasNext()) { itf.next()->cleanup(nextTrack); } queueAnalyseTrack(nextTrack); emitUpdateProgress(nextTrack, 0); } else { // 100% - FINALIZE_PERCENT finished emitUpdateProgress(nextTrack, 1000 - FINALIZE_PERCENT); // This takes around 3 sec on a Atom Netbook QListIterator<Analyser*> itf(m_aq); while (itf.hasNext()) { itf.next()->finalise(nextTrack); } emit(trackDone(nextTrack)); emitUpdateProgress(nextTrack, 1000); // 100% } } else { emitUpdateProgress(nextTrack, 1000); // 100% qDebug() << "Skipping track analysis because no analyzer initialized."; } m_qm.lock(); m_queue_size = m_tioq.size(); m_qm.unlock(); if (m_queue_size == 0) { emit(queueEmpty()); // emit asynchrony for no deadlock } } emit(queueEmpty()); // emit in case of exit; }
void processFile(HumdrumFile& infile, const string& filename) { int i; if (SEGMENTS && !summaryQ) { cout << "!!!!SEGMENT: " << infile.getFileName() << endl; } if (debugQ) { cout << "!! file: " << infile.getFileName() << endl; } if (validQ && !isValidFile(infile)) { return; } if (kernQ) { Array<int> ktracks; infile.getTracksByExInterp(ktracks, "**kern"); cout << ktracks.getSize() << endl; return; } if (mdurQ || summaryQ || nograceQ) { infile.analyzeRhythm("4"); } Array<int> analysis(infile.getNumLines()); analysis.setAll(0); analysis.allowGrowth(0); for (i=0; i<infile.getNumLines(); i++) { if (!infile[i].isData()) { continue; } analysis[i] = doAnalysis(infile, i); } if (summaryQ) { return; } // print analysis: int firstdata = 1; PerlRegularExpression pre; for (i=0; i<infile.getNumLines(); i++) { if (appendQ) { cout << infile[i]; } if (infile[i].isData()) { if (appendQ) { cout << '\t'; } if (measureQ && firstdata) { printMeasureData(analysis, infile, i); firstdata = 0; } else if (measureQ) { cout << "."; } else { if (nograceQ && (infile[i].getDuration() == 0)) { cout << "."; } else { cout << analysis[i]; } } if (prependQ) { cout << '\t'; } if (appendQ) { cout << '\n'; } } else if (infile[i].isInterpretation()) { if (appendQ) { cout << '\t'; } if (strcmp(infile[i][0], "*-") == 0) { cout << "*-"; } else if (strncmp(infile[i][0], "**", 2) == 0) { printExclusiveInterpretation(); } else { cout << "*"; } if (prependQ) { cout << '\t'; } if (appendQ) { cout << '\n'; } } else if (infile[i].isBarline()) { if (pre.search(infile[i][0], "\\d")) { firstdata = 1; } if (appendQ) { cout << '\t'; } cout << infile[i][0]; if (prependQ) { cout << '\t'; } if (appendQ) { cout << '\n'; } } else if (infile[i].isLocalComment()) { if (appendQ) { cout << '\t'; } cout << "!"; if (prependQ) { cout << '\t'; } if (appendQ) { cout << '\n'; } } else { if (!(appendQ || prependQ)) { cout << infile[i]; } if (appendQ) { cout << '\n'; } } if (prependQ) { cout << infile[i]; } if (!appendQ) { cout << '\n'; } } }