Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions cpp/book/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,14 @@ BookHash ConstSymBookNode::hash() {

vector<int> SymBookNode::getSymmetries() {
vector<int> symmetries;
symmetries.reserve(node->symmetries.size());
for(int symmetry: node->symmetries)
symmetries.push_back(SymmetryHelpers::compose(invSymmetryOfNode, symmetry, symmetryOfNode));
return symmetries;
}
vector<int> ConstSymBookNode::getSymmetries() {
vector<int> symmetries;
symmetries.reserve(node->symmetries.size());
for(int symmetry: node->symmetries)
symmetries.push_back(SymmetryHelpers::compose(invSymmetryOfNode, symmetry, symmetryOfNode));
return symmetries;
Expand Down Expand Up @@ -456,6 +458,7 @@ vector<BookMove> SymBookNode::getUniqueMovesInBook() {
vector<BookMove> ConstSymBookNode::getUniqueMovesInBook() {
assert(node != nullptr);
vector<BookMove> ret;
ret.reserve(node->moves.size());
for(std::pair<Loc,BookMove> kv: node->moves) {
ret.push_back(kv.second.getSymBookMove(symmetryOfNode, node->book->initialBoard.x_size, node->book->initialBoard.y_size));
}
Expand Down Expand Up @@ -677,7 +680,7 @@ SymBookNode SymBookNode::playAndAddMove(Board& board, BoardHistory& hist, Loc mo
else {
childIsTransposing = true;
}
child->parents.push_back(std::make_pair(node->hash, bestLoc));
child->parents.emplace_back(node->hash, bestLoc);

BookMove newBookMove(
bestLoc,
Expand Down Expand Up @@ -1021,8 +1024,9 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN

// Spawn threads
std::vector<std::thread> threads;
threads.reserve(numThreads);
for(int i = 0; i < numThreads; i++) {
threads.push_back(std::thread([this, &dirtyNodes, &mutexPool, visitedDoneValue, i]() {
threads.emplace_back([this, &dirtyNodes, &mutexPool, visitedDoneValue, i]() {
Rand rand;
bool allDirty = false;
iterateDirtyNodesPostOrder(
Expand Down Expand Up @@ -1051,8 +1055,9 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN

// Acquire locks in sorted order
std::vector<std::unique_lock<std::mutex>> locks;
locks.reserve(mutexIndices.size());
for(uint32_t idx : mutexIndices) {
locks.push_back(std::unique_lock<std::mutex>(mutexPool.getMutex(idx)));
locks.emplace_back(mutexPool.getMutex(idx));
}

// Now safe to recompute
Expand All @@ -1063,7 +1068,7 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN
&rand,
visitedDoneValue
);
}));
});
}

// Join all threads
Expand All @@ -1079,7 +1084,7 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN

threads.clear();
for(int i = 0; i < numThreads; i++) {
threads.push_back(std::thread([this, &mutexPool, visitedDoneValueForCosts, i]() {
threads.emplace_back([this, &mutexPool, visitedDoneValueForCosts, i]() {
Rand rand;
iterateEntireBookPreOrder(
[this, &mutexPool](BookNode* node) {
Expand All @@ -1105,8 +1110,9 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN

// Acquire locks in sorted order
std::vector<std::unique_lock<std::mutex>> locks;
locks.reserve(mutexIndices.size());
for(uint32_t idx : mutexIndices) {
locks.push_back(std::unique_lock<std::mutex>(mutexPool.getMutex(idx)));
locks.emplace_back(mutexPool.getMutex(idx));
}

// Now safe to recompute costs
Expand All @@ -1117,7 +1123,7 @@ void Book::recomputeMultiThreaded(const std::vector<SymBookNode>& newAndChangedN
&rand,
visitedDoneValueForCosts
);
}));
});
}

// Join all threads
Expand All @@ -1135,8 +1141,9 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads

// Spawn threads
std::vector<std::thread> threads;
threads.reserve(numThreads);
for(int i = 0; i < numThreads; i++) {
threads.push_back(std::thread([this, &mutexPool, visitedDoneValue, i]() {
threads.emplace_back([this, &mutexPool, visitedDoneValue, i]() {
Rand rand;
bool allDirty = true;
iterateDirtyNodesPostOrder(
Expand Down Expand Up @@ -1165,8 +1172,9 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads

// Acquire locks in sorted order
std::vector<std::unique_lock<std::mutex>> locks;
locks.reserve(mutexIndices.size());
for(uint32_t idx : mutexIndices) {
locks.push_back(std::unique_lock<std::mutex>(mutexPool.getMutex(idx)));
locks.emplace_back(mutexPool.getMutex(idx));
}

// Now safe to recompute
Expand All @@ -1177,7 +1185,7 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads
&rand,
visitedDoneValue
);
}));
});
}

// Join all threads
Expand All @@ -1193,7 +1201,7 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads

threads.clear();
for(int i = 0; i < numThreads; i++) {
threads.push_back(std::thread([this, &mutexPool, visitedDoneValueForCosts, i]() {
threads.emplace_back([this, &mutexPool, visitedDoneValueForCosts, i]() {
Rand rand;
iterateEntireBookPreOrder(
[this, &mutexPool](BookNode* node) {
Expand All @@ -1219,8 +1227,9 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads

// Acquire locks in sorted order
std::vector<std::unique_lock<std::mutex>> locks;
locks.reserve(mutexIndices.size());
for(uint32_t idx : mutexIndices) {
locks.push_back(std::unique_lock<std::mutex>(mutexPool.getMutex(idx)));
locks.emplace_back(mutexPool.getMutex(idx));
}

// Now safe to recompute costs
Expand All @@ -1231,7 +1240,7 @@ void Book::recomputeEverythingMultiThreaded(MutexPool& mutexPool, int numThreads
&rand,
visitedDoneValueForCosts
);
}));
});
}

// Join all threads
Expand Down Expand Up @@ -1286,6 +1295,7 @@ vector<SymBookNode> Book::getAllLeaves(double minVisits) {

std::vector<SymBookNode> Book::getAllNodes() {
vector<SymBookNode> ret;
ret.reserve(nodes.size());
for(BookNode* node: nodes) {
ret.push_back(SymBookNode(node,0));
}
Expand Down Expand Up @@ -1520,7 +1530,7 @@ void Book::iterateDirtyNodesPostOrder(

auto fillChildren = [&nextChildrenToTry,randToShuffle](const BookNode* node, size_t stackDepth) {
if(nextChildrenToTry.size() <= stackDepth) {
nextChildrenToTry.push_back(std::vector<Loc>());
nextChildrenToTry.emplace_back();
}
assert(nextChildrenToTry[stackDepth].size() == 0);
for(auto iter = node->moves.rbegin(); iter != node->moves.rend(); ++iter) {
Expand Down Expand Up @@ -3312,14 +3322,14 @@ Book* Book::loadFromFile(const std::string& fileName, int numThreadsForRecompute
size_t nodeIdx = parentData["id"].get<size_t>();
BookHash parentHash = hashDict[nodeIdx];
Loc loc = Location::ofString(parentData["loc"].get<string>(),book->initialBoard);
node->parents.push_back(std::make_pair(parentHash,loc));
node->parents.emplace_back(parentHash,loc);
}
}
else {
for(json& parentData: nodeData["parents"]) {
BookHash parentHash = BookHash::ofString(parentData["hash"].get<string>());
Loc loc = Location::ofString(parentData["loc"].get<string>(),book->initialBoard);
node->parents.push_back(std::make_pair(parentHash,loc));
node->parents.emplace_back(parentHash,loc);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/command/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ int MainCmds::analysis(const vector<string>& args) {
AsyncBot* bot = new AsyncBot(defaultParams, nnEval, humanEval, &logger, searchRandSeed);
bot->setCopyOfExternalPatternBonusTable(patternBonusTable);
bot->setExternalEvalCache(evalCache);
threads.push_back(std::thread(analysisLoopProtected,bot,threadIdx));
threads.emplace_back(analysisLoopProtected,bot,threadIdx);
bots.push_back(bot);
}

Expand Down Expand Up @@ -792,7 +792,7 @@ int MainCmds::analysis(const vector<string>& args) {
reportErrorForId(rbase.id, field, "Could not parse board location: " + s1);
return false;
}
buf.push_back(Move(loc,pla));
buf.emplace_back(loc,pla);
}
return true;
};
Expand Down
4 changes: 2 additions & 2 deletions cpp/command/contribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ int MainCmds::contribute(const vector<string>& args) {
//Just start based on selfplay games, rating games will poke in as needed
vector<std::thread> gameThreads;
for(int i = 0; i<maxSimultaneousGames; i++) {
gameThreads.push_back(std::thread(runGameLoopProtected,i));
gameThreads.emplace_back(runGameLoopProtected,i);
}

//-----------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1385,7 +1385,7 @@ int MainCmds::contribute(const vector<string>& args) {
int numTaskLoopThreads = 4;
vector<std::thread> taskLoopThreads;
for(int i = 0; i<numTaskLoopThreads; i++) {
taskLoopThreads.push_back(std::thread(taskLoopProtected));
taskLoopThreads.emplace_back(taskLoopProtected);
}

//Allocate thread using new to make sure its memory lasts beyond main(), and just let it leak as we exit.
Expand Down
4 changes: 2 additions & 2 deletions cpp/command/demoplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ static void initializeDemoGame(Board& board, BoardHistory& hist, Player& pla, Ra
Loc loc = chosenOpening[k].loc;
Player movePla = chosenOpening[k].pla;
if(loc == Board::NULL_LOC || loc == Board::PASS_LOC)
symmetric.push_back(Move(loc,movePla));
symmetric.emplace_back(loc,movePla);
else {
int x = Location::getX(loc,size);
int y = Location::getY(loc,size);
if(j & 1) x = size-1-x;
if(j & 2) y = size-1-y;
if(j & 4) std::swap(x,y);
symmetric.push_back(Move(Location::getLoc(x,y,size),movePla));
symmetric.emplace_back(Location::getLoc(x,y,size),movePla);
}
}
chosenOpenings.push_back(symmetric);
Expand Down
3 changes: 2 additions & 1 deletion cpp/command/gatekeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,9 @@ int MainCmds::gatekeeper(const vector<string>& args) {
std::thread newThread(dataWriteLoopProtected);
newThread.detach();
vector<std::thread> threads;
threads.reserve(numGameThreads);
for(int i = 0; i<numGameThreads; i++) {
threads.push_back(std::thread(gameLoopProtected,i));
threads.emplace_back(gameLoopProtected,i);
}

//Wait for all game threads to stop
Expand Down
23 changes: 16 additions & 7 deletions cpp/command/genbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ int MainCmds::genbook(const vector<string>& args) {
if(loc == Board::NULL_LOC || loc == moveLoc)
continue;
if(policyProbs[pos] > 0.0 && policyProbs[pos] > 1.5 * moveLocPolicy + 0.05f)
extraMoveLocsToExpand.push_back(std::make_pair(loc,policyProbs[pos]));
extraMoveLocsToExpand.emplace_back(loc,policyProbs[pos]);
}
std::sort(
extraMoveLocsToExpand.begin(),
Expand Down Expand Up @@ -1361,8 +1361,9 @@ int MainCmds::genbook(const vector<string>& args) {
for(SymBookNode node: allNodes)
positionsToTrace.forcePush(node);
vector<std::thread> threads;
threads.reserve(numGameThreads);
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads.push_back(std::thread(loopAddingVariations, gameThreadIdx));
threads.emplace_back(loopAddingVariations, gameThreadIdx);
}
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads[gameThreadIdx].join();
Expand Down Expand Up @@ -1433,8 +1434,9 @@ int MainCmds::genbook(const vector<string>& args) {
for(BookHash hash: nodesHashesToUpdate)
hashesToUpdate.forcePush(hash);
vector<std::thread> threads;
threads.reserve(numGameThreads);
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads.push_back(std::thread(loopUpdatingHashes, gameThreadIdx));
threads.emplace_back(loopUpdatingHashes, gameThreadIdx);
}
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads[gameThreadIdx].join();
Expand Down Expand Up @@ -1512,8 +1514,9 @@ int MainCmds::genbook(const vector<string>& args) {
};

vector<std::thread> threads;
threads.reserve(numGameThreads);
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads.push_back(std::thread(loopExpandingNodes, gameThreadIdx));
threads.emplace_back(loopExpandingNodes, gameThreadIdx);
}
for(int gameThreadIdx = 0; gameThreadIdx<numGameThreads; gameThreadIdx++) {
threads[gameThreadIdx].join();
Expand Down Expand Up @@ -1873,7 +1876,7 @@ int MainCmds::booktoposes(const vector<string>& args) {

std::vector<ConstSymBookNode> nodesToExplore;
std::vector<int> depthsToExplore;
nodesToExplore.push_back(book->getRoot());
nodesToExplore.emplace_back(book->getRoot());
depthsToExplore.push_back(0);

logger.write("Beginning book sweep");
Expand Down Expand Up @@ -2062,8 +2065,9 @@ int MainCmds::booktoposes(const vector<string>& args) {
};

vector<std::thread> threads;
threads.reserve(numThreads);
for(int threadIdx = 0; threadIdx<numThreads; threadIdx++) {
threads.push_back(std::thread(processPoses, threadIdx));
threads.emplace_back(processPoses, threadIdx);
}
for(int threadIdx = 0; threadIdx<numThreads; threadIdx++) {
threads[threadIdx].join();
Expand Down Expand Up @@ -2319,7 +2323,7 @@ int MainCmds::findbookbottlenecks(const vector<string>& args) {
{
std::vector<NodeAndDepthInfo> nodesToExplore;
std::set<BookHash> visited;
nodesToExplore.push_back(NodeAndDepthInfo(book->getRoot(), 0, 0.0, 0.0, 0.0));
nodesToExplore.emplace_back(book->getRoot(), 0, 0.0, 0.0, 0.0);

for(size_t i = 0; i<nodesToExplore.size(); i++) {
NodeAndDepthInfo info = nodesToExplore[i];
Expand Down Expand Up @@ -2388,6 +2392,7 @@ int MainCmds::findbookbottlenecks(const vector<string>& args) {

// Combine all groups into a single vector
std::vector<ThresholdGroup> allGroups;
allGroups.reserve(groupsByThresholdIncreasing.size());
for(auto& pair : groupsByThresholdIncreasing) {
allGroups.push_back(pair.second);
}
Expand Down Expand Up @@ -2495,15 +2500,18 @@ int MainCmds::findbookbottlenecks(const vector<string>& args) {
jsonEntry["increasing"] = group.increasing;

std::vector<std::string> nodesToChangeList;
nodesToChangeList.reserve(result.nodes.size());
for(const BookHash& h: result.nodes)
nodesToChangeList.push_back(h.toString());
jsonEntry["minSetToChange"] = nodesToChangeList;
std::vector<int> nodesToChangeBranchCountsList;
nodesToChangeBranchCountsList.reserve(result.nodes.size());
for(const BookHash& h: result.nodes)
nodesToChangeBranchCountsList.push_back(book->getByHash(h).numUniqueMovesInBook());
jsonEntry["minSetToChangeBranchCounts"] = nodesToChangeBranchCountsList;

std::vector<std::string> moveHistoryStrings;
moveHistoryStrings.reserve(moveHistory.size());
for(Loc move: moveHistory)
moveHistoryStrings.push_back(Location::toString(move, book->initialBoard));
jsonEntry["moveHistory"] = moveHistoryStrings;
Expand All @@ -2518,6 +2526,7 @@ int MainCmds::findbookbottlenecks(const vector<string>& args) {
testAssert(suc2);

std::vector<std::string> moveHistoryToChangeStrings;
moveHistoryToChangeStrings.reserve(moveHistoryToChange.size());
for(Loc move : moveHistoryToChange)
moveHistoryToChangeStrings.push_back(Location::toString(move, book->initialBoard));
moveHistoryToChangeStringss.push_back(moveHistoryToChangeStrings);
Expand Down
Loading
Loading