【问题标题】:c++ async only running on one core?c++ async 只在一个核心上运行?
【发布时间】:2017-07-21 02:13:54
【问题描述】:

我目前正在尝试为我的国际象棋引擎实现惰性 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;
}

是否有一个明显的原因让我错过了为什么它仍然只使用一个核心?

【问题讨论】:

  • 你怎么知道它只使用一个线程?
  • 处理器使用情况?除非我完全误解了。有没有其他方法可以强制它在核心之间拆分?
  • 观察处理器使用情况是线程创建的一个不良指标,特别是如果它们快速完成。
  • 我已经通过增加搜索的深度来强迫他们永远等待。搜索的总 CPU 使用率永远不会超过 30%。这与普通的非多线程搜索相同。我有一个四核,所以这与只使用一个核是一致的。关于如何强制它使用所有内核的任何想法?
  • 读完这个link之后看起来std::asyc加上std::launch::async应该强制并行处理。

标签: c++ multithreading chess


【解决方案1】:

找出解决我遇到的问题的方法。只要线程是在迭代深化循环之前创建并在我肯定得到多核使用之后加入的。

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多