void test2() { for(int i = 0; i < 2500; i++) { //std::unique_lock<std::mutex> mlock(this->bqMutex); std::cout<<"odtwarzam"<<i; Pa_WriteStream(outputStream, bq.pop().getSample(), FRAMES_PER_BUFFER); //mlock.unlock(); } }
int testBlockingQueue(void) { BlockingQueue<int> testQueue; testQueue.push(1); testQueue.push(2); testQueue.push(3); int val = testQueue.pop(); printf("First value is %d\n", val); val = testQueue.pop(); printf("Second value is %d\n", val); val = testQueue.pop(); printf("Third value is %d\n", val); bool isEmpty = testQueue.empty(); if(isEmpty){ printf("Queue is empty.\n"); } else { printf("Queue is not empty.\n"); } return 0; }
/* Test to see if the queue is sorted within reasonable limits */ bool is_sorted(BlockingQueue<timeout_t>& bq) { LogContext& log = LogManager::instance().getLogContext("Test", "Scheduler"); timeout_t last = 0; while(!bq.empty()) { timeout_t cur = bq.front(); bq.pop(); if(cur < last && (last - cur) > 5 MILLIS ) { /* we give a 5 ms grace period */ log.printfln(ERROR, "%ld is less than %ld", cur, last); return false; } last = cur; } return true; }
// Running test bool TwitchPlaysBlockingQueueTest::RunTest(const FString& Parameters) { BlockingQueue<int>* q = new BlockingQueue<int>(); int32 val = 42; q->push(val); int32 test = q->pop(); //Test val afther a pop TWITCH_CHECK(test == val, "Error on pop()"); //Test IsEmpty TWITCH_CHECK(q->IsEmpty(), "Error on IsEmpty()"); //Test clear q->clear(); TWITCH_CHECK(q->IsEmpty(), "Error on clear()"); return true; }
void multiGameWorker() { cerr << "Thread #" << this_thread::get_id() << " starting up." << endl; while(true) { // Blocking call. Job j = jobQueue.pop(); if(j.shutdown) { cerr << "Thread #" << this_thread::get_id() << " shutting down." << endl; return; } cerr << "Thread #" << this_thread::get_id() << " starting job." << endl; double score = runMultiGame(j.config); double scoreDiff = -(score - j.currentScore); double flip = (float) drand48() / RAND_MAX; double acc = exp(-scoreDiff / j.temp); bool accepted = scoreDiff < 0 || acc > flip; Result res = { j.config, score, accepted}; resultQueue.push(res); cerr << "Thread #" << this_thread::get_id() << " finished job. Score: " << score << endl; } }
ScoreFactors optimize() { // Start up the workers. vector<thread> workers; for(int i = 0; i < WORKER_COUNT; i++) { workers.push_back(thread(multiGameWorker)); } ScoreFactors current = AnnealingBot().sFactors;//startingSFs(); double currentScore = runMultiGame(current); double bestScore = currentScore; ScoreFactors bestFactors = current; double prevTemp = initialTemp; double temp = initialTemp; double sdPrev = initialSD; for(int i = 0; i < tempReductions; i++) { int acceptCount = 0; int count = 1; double mean = currentScore; double delta = currentScore - 0.0; double M2 = 0.0 + delta*(currentScore-mean); vector<Result> accepted; for(int j = 0; j < neighorhoodSize;) { // Feed the workers. for(int y = 0; y < WORKER_COUNT; y++) { // ScoreFactors altered = randomAlter(current); ScoreFactors altered = randomAlter2(current); Job job = {false, altered, temp, currentScore}; jobQueue.push(job); } for(int z = 0; z < WORKER_COUNT && j < neighorhoodSize; z++) { Result res = resultQueue.pop(); if(res.accepted) { acceptCount++; cerr << "Master accepted update." << endl; accepted.push_back(res); } // Start up more jobs until one is accepted. if(accepted.empty()) { // ScoreFactors altered = randomAlter(current); ScoreFactors altered = randomAlter2(current); Job job = {false, altered, temp, currentScore}; jobQueue.push(job); } // Online mean & variance. count++; delta = res.score - mean; mean += delta/count; M2 += delta*(res.score - mean); // Increment trial counter. j++; } // Choose random success, if any. Random produces better results than first in. if(!accepted.empty()) { int randomIdx = (int) (drand48() * accepted.size()); current = accepted[randomIdx].config; currentScore = accepted[randomIdx].score; if(currentScore > bestScore) { bestScore = currentScore; bestFactors = current; } accepted.clear(); } cerr << "Round " << j << " of " << i << endl; } cerr << "M2: " << M2 << " count: " << count << endl; double sd = sqrt(M2) / (count - 1); // Minus 1 from count when calculating accept ratio as the count includes the starting score/config. cerr << "Accept %:" << (double)acceptCount / (double)(count-1) << " at temperature: " << temp << " and SD: " << sd << endl; sd = (1-smoothingFactor) * sd + smoothingFactor * sdPrev * (temp / prevTemp); sdPrev = sd; prevTemp = temp; temp = nextTemperature(temp, sd, lambda); cerr << "Current score: " << currentScore << endl; cerr << "Current score factors: " << endl << printScoreFactors(current) << endl; cerr << "Best score: " << bestScore << endl; cerr << "Best factors: " << endl << printScoreFactors(bestFactors) << endl; } for(int i = 0; i < WORKER_COUNT; i++) { jobQueue.push({true, 0, 0, 0}); } for(int i = 0; i < WORKER_COUNT; i++) { workers[i].join(); } finalScore = currentScore; return current; }