📄 decisionqueue.cpp
字号:
/********************************************************************************************MiraXT -- Copyright (c) 2007, Tobias Schubert, Matthew Lewis, Natalia Kalinnik, Bernd Becker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies orsubstantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.********************************************************************************************/// Definition of the decision queue, needed to dynamically // divide the overall search space among the threads.class DecisionQueue{private: int numThreads; // Number of involved threads. int numWaitingThreads; // Number of actual inactive threads, waiting for a new subproblem. int dqEI; // Decision Queue End Index, marking the end of the list of actual available subproblems. vec<vec<int> > DecsionQueue; // Array of decision stacks, conceptually similar to PSATO's "Guiding Paths". ControlStructure* masterControl; pthread_mutex_t decisionQueue_mutex; pthread_cond_t decisionQueue_cond; public: // Constructor. DecisionQueue(int Threads, ControlStructure* control) { numThreads = Threads; masterControl = control; dqEI = 0; numWaitingThreads = 0; DecsionQueue.growTo(numThreads); for(int i=0; i<numThreads; i++) { DecsionQueue[i].growTo(numLiterals); } pthread_mutex_init(&decisionQueue_mutex, NULL); pthread_cond_init(&decisionQueue_cond, NULL); } // Destructor. ~DecisionQueue(void) { // Is there still an unevaluated subproblem left? if (dqEI != 0) { fprintf(stdout,"c Decision Queue Error: Unchecked subproblem(s) left\n"); fprintf(stdout,"s UNKNOWN\n"); // Exit code 0: UNKNOWN. exit(0); } } // Each SAT-solving thread periodically calls "needMoreDecisionStacks" to // test if there is a need to divide its actual suproblem. bool needMoreDecisionStacks(void) { // dqEI counts the number of available subproblems. If this number is // less than the number of currently waiting SAT-solving threads, another // thread has to split its subproblem. if (dqEI < numWaitingThreads) { return true; } else { return false; } } // Used by the SAT-solving threads to divide their actual subproblem. void addDecisionStack(int& currentDL, int* DLLookupArray, int* DecisionStack) { pthread_mutex_lock(&decisionQueue_mutex); // Is there really a need for a new subproblem? if (dqEI < numWaitingThreads) { // Initialization. int i=0; int posDL1 = DLLookupArray[1]; // Copy the thread's submitted decision stack into the decision queue. for (i=0; i<=posDL1; i++) { DecsionQueue[dqEI][i] = DecisionStack[i]; } // Flip the last variable assignment, so that the two problems are disjoint. // See also PSATO's concept of "Guiding Paths". DecsionQueue[dqEI][posDL1] = DecsionQueue[dqEI][posDL1] ^ 1; // Use 0 to mark the end of the decision stack, since all literals have an index greater 0. DecsionQueue[dqEI][posDL1+1] = 0; // Update the "Decision Queue End Index". dqEI++; // Modify the submitting thread's actual decision stack, so that he is not allowed // to search for an satisfiable assignment in the subproblem generated above. currentDL--; for (i=1; i<=currentDL; i++) { DLLookupArray[i] = DLLookupArray[i+1]; } } pthread_cond_signal(&decisionQueue_cond); pthread_mutex_unlock(&decisionQueue_mutex); } // "getDecisionStack" can be used by any thread to get a new subproblem. bool getDecisionStack(int* newDecisionStack) { pthread_mutex_lock(&decisionQueue_mutex); // Increment the number of waiting threads. numWaitingThreads++; // Wait until a new subproblem is available (dqEI != 0), the end of the search has been // signaled (masterDone != 0), or all threads are waiting (numWaitingThreads == numThreads). while(dqEI == 0 && masterControl->masterDone == 0 && numWaitingThreads != numThreads) { // Wait for a signal that the current status has been changed. pthread_cond_wait(&decisionQueue_cond,&decisionQueue_mutex); } // If all threads are waiting, then the search is over. if (numWaitingThreads == numThreads) { masterControl->done(); } else { // New subproblem available? if (dqEI != 0) { dqEI--; int L = 0; while(DecsionQueue[dqEI][L] != 0) { newDecisionStack[L] = DecsionQueue[dqEI][L]; L++; } newDecisionStack[L] = 0; } } // Decrement the number of waiting threads. numWaitingThreads--; pthread_cond_signal(&decisionQueue_cond); pthread_mutex_unlock(&decisionQueue_mutex); // Is the search over? if (masterControl->masterDone != 0) { return false; } return true; }};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -