Code: Select all
void searchPosition(board& inputBoard) {
std::vector<int> moves;
generateMoves(inputBoard, moves, false);
board workerDatas[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) workerDatas[i] = inputBoard;
board& firstWorkerData = workerDatas[0];
negamax(firstWorkerData, 1, -20000, 20000, moves, true);
int threadsToSpawn = numberOfThreads - 1;
int currentDepth = 2;
while (true) {
std::thread workerThreads[threadsToSpawn];
for (int i = 0; i < threadsToSpawn; i++) {
board& inputWorkerData = workerDatas[i + 1];
workerThreads[i] = std::thread(negamax, std::ref(inputWorkerData), currentDepth + (i & 1), -20000, 20000, std::ref(moves), true);
}
int score = negamax(firstWorkerData, currentDepth, -20000, 20000, moves, true);
for (int i = 0; i < threadsToSpawn; i++) {
// Stop all the threads once main thread has finished.
workerDatas[i + 1].isSearchStopped = true;
if (workerThreads[i].joinable()) workerThreads[i].join();
}
// Set the threads as unstopped once they actually have stopped so that we can search again.
for (int i = 0; i < threadsToSpawn; i++) workerDatas[i + 1].isSearchStopped = false;
if (firstWorkerData.isSearchStopped) break;
std::chrono::steady_clock::time_point currentTime = std::chrono::steady_clock::now();
int timeElapsed = (int)std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count();
double ordering = 100.0 * ((double)firstWorkerData.failedHighFirst / (double)firstWorkerData.failedHigh);
std::cout << "Depth: " << currentDepth << " Time: " << timeElapsed << " Nodes: " << firstWorkerData.nodes << " Ordering: " << ordering << "%" << " Score: " << score << " Principal Variation: ";
principalVariation.clear();
std::vector<std::string> internalPrincipleVariationAlgebraic;
retrievePrincipleVariation(inputBoard, principalVariation, internalPrincipleVariationAlgebraic);
for (int i = 0; i < internalPrincipleVariationAlgebraic.size(); i++) std::cout << internalPrincipleVariationAlgebraic[i] << " ";
std::cout << std::endl;
currentDepth++;
}
}