我目前正在尝试为我的国际象棋引擎实施懒惰的SMP,这涉及在不同的核心上运行搜索算法(尽可能不同步),并且(希望)获得共享哈希表的好处。它仍然只在一个核心上运行。下面的代码(我已删除了相关的部分)。
迭代加深循环:
for( distance; distance <= depth && IDTimeS < endTime; ) {
positionCount = 0;
clock_t currentTime = clock();
if( currentTime >= endTime ) {
distance - 1;
break;
}
//multi threading testing
int val = multi( distance, alpha, beta, false, currentTime, timeLimmit, currentDepth +1, true, z0, z1, z2 );
//increment distance to travel (same as depth at max depth)
distance++;
}
多线程功能:
int Ai_Logic::multi(
int distance,
int alpha,
int beta,
bool isWhite,
long currentTime,
long timeLimmit,
int currentDepth,
bool allowNull,
ZobristH *z0,
ZobristH *z1,
ZobristH *z2
)
{
auto f1 = std::async( std::launch::async, &Ai_Logic::alphaBeta, this, distance, alpha, beta, isWhite, currentTime, timeLimmit, currentDepth, allowNull, BB0, z0 );
auto f2 = std::async( std::launch::async, &Ai_Logic::alphaBeta, this, distance-1, alpha, beta, isWhite, currentTime, timeLimmit, currentDepth, allowNull, BB1, z1 );
auto f3 = std::async( std::launch::async, &Ai_Logic::alphaBeta, this, distance+1, alpha, beta, isWhite, currentTime, timeLimmit, currentDepth, allowNull, BB2, z2 );
auto val = f1.get();
auto val2 = f2.get();
auto val3 = f3.get();
return val;
}
是否有一个明显的原因,我错过了为什么它仍然只使用一个核心?
答案 0 :(得分:0)
找出至少解决我遇到的问题的方法。只要线程是在迭代加深循环之前创建的,并且在我确实获得多核使用之后加入。
std::thread t0(&Ai_Logic::alphaBeta, this, depth+1, alpha, beta, false, 0, timeLimmit, currentDepth, true, BB0, z0);
std::thread t1(&Ai_Logic::alphaBeta, this, depth-1, alpha, beta, false, 0, timeLimmit, currentDepth, true, BB1, z1);
for( distance; distance <= depth && IDTimeS < endTime; ) {
positionCount = 0;
clock_t currentTime = clock();
if( currentTime >= endTime ) {
distance - 1;
break;
}
//normal alpha beta call
int val = alphaBeta(distance, alpha, beta, false, currentTime, timeLimmit, currentDepth +1, true, newBoard, mZobrist);
//increment distance to travel (same as depth at max depth)
distance++;
}
t0.join();
t1.join();