首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并发线程比单线程慢

并发线程比单线程慢
EN

Stack Overflow用户
提问于 2018-04-19 00:54:23
回答 1查看 96关注 0票数 0

我一直在比较两种从矩阵中寻找最高值的方法(如果它们是重复的,在它们之间随机选择),单线程和多线程。通常情况下,多线程应该更快,假设我对此进行了正确的编码。因为它不是,它的速度要慢得多,我只能假设我做错了什么。谁能指出我做错了什么?

注意:我知道我不应该使用rand(),但出于这个目的,我觉得这样做没有太多问题,在它正常工作后,我会用mt19937_64替换它。

提前感谢!

代码语言:javascript
复制
double* RLPolicy::GetActionWithMaxQ(std::tuple<int, double*, int*, int*, int, double*>* state, long* selectedActionIndex, bool& isActionQZero)
{
    const bool useMultithreading = true;

    double* qIterator = Utilities::DiscretizeStateActionPairToStart(_world->GetCurrentStatePointer(), (long*)&(std::get<0>(*state)));

    // Represents the action-pointer for which Q-values are duplicated
    // Note: A shared_ptr is used instead of a unique_ptr since C++11 wont support unique_ptrs for pointers to pointers **
    static std::shared_ptr<double*> duplicatedQValues(new double*[*_world->GetActionsNumber()], std::default_delete<double*>());
    /*[](double** obj) {
    delete[] obj;
    });*/

    static double* const defaultAction = _actionsListing.get();// [0];
    double* actionOut = defaultAction; //default action
    static double** const duplicatedQsDefault = duplicatedQValues.get();

    if (!useMultithreading)
    {    
        const double* const qSectionEnd = qIterator + *_world->GetActionsNumber() - 1;

        double* largestValue = qIterator;
        int currentActionIterator = 0;

        long duplicatedIndex = -1;

        do {
            if (*qIterator > *largestValue)
            {
                largestValue = qIterator;
                actionOut = defaultAction + currentActionIterator;
                *selectedActionIndex = currentActionIterator;
                duplicatedIndex = -1;
            }
            // duplicated value, map it
            else if (*qIterator == *largestValue)
            {
                ++duplicatedIndex;
                *(duplicatedQsDefault + duplicatedIndex) = defaultAction + currentActionIterator;
            }
            ++currentActionIterator;
            ++qIterator;
        } while (qIterator != qSectionEnd);

        // If duped (equal) values are found, select among them randomly with equal probability
        if (duplicatedIndex >= 0)
        {
            *selectedActionIndex = (std::rand() % duplicatedIndex);
            actionOut = *(duplicatedQsDefault + *selectedActionIndex);
        }

        isActionQZero = *largestValue == 0;

        return actionOut;

    }
    else
    {
        static const long numberOfSections = 6;
        unsigned int actionsPerSection = *_world->GetActionsNumber() / numberOfSections;
        unsigned long currentSectionStart = 0;

        static double* actionsListing = _actionsListing.get();

        long currentFoundResult = FindActionWithMaxQInMatrixSection(qIterator, 0, actionsPerSection, duplicatedQsDefault, actionsListing);

        static std::vector<std::future<long>> maxActions;
        for (int i(0); i < numberOfSections - 1; ++i)
        {
            currentSectionStart += actionsPerSection;
            maxActions.push_back(std::async(&RLPolicy::FindActionWithMaxQInMatrixSection, std::ref(qIterator), currentSectionStart, std::ref(actionsPerSection), std::ref(duplicatedQsDefault), actionsListing));
        }

        long foundActionIndex;

        actionOut = actionsListing + currentFoundResult;

        for (auto &f : maxActions)
        {
            f.wait();

            foundActionIndex = f.get();

            if (actionOut == nullptr)
                actionOut = defaultAction;
            else if (*(actionsListing + foundActionIndex) > *actionOut)
                actionOut = actionsListing + foundActionIndex;
        }

        maxActions.clear();

        return actionOut;
    }
}

/*
    Deploy a thread to find the action with the highest Q-value for the provided Q-Matrix section.

    @return - The index of the action (on _actionListing) which contains the highest Q-value.
*/
long RLPolicy::FindActionWithMaxQInMatrixSection(double* qMatrix, long sectionStart, long sectionLength, double** dupListing, double* actionListing)
{
    double* const matrixSectionStart = qMatrix + sectionStart;
    double* const matrixSectionEnd = matrixSectionStart + sectionLength;
    double** duplicatedSectionStart = dupListing + sectionLength;

    static double* const defaultAction = actionListing;
    long maxValue = sectionLength;
    long maxActionIndex = 0;
    double* qIterator = matrixSectionStart;
    double* largestValue = matrixSectionStart;

    long currentActionIterator = 0;

    long duplicatedIndex = -1;

    do {
        if (*qIterator > *largestValue)
        {
            largestValue = qIterator;
            maxActionIndex = currentActionIterator;
            duplicatedIndex = -1;
        }
        // duplicated value, map it
        else if (*qIterator == *largestValue)
        {
            ++duplicatedIndex;
            *(duplicatedSectionStart + duplicatedIndex) = defaultAction + currentActionIterator;
        }
        ++currentActionIterator;
        ++qIterator;
    } while (qIterator != matrixSectionEnd);

    // If duped (equal) values are found, select among them randomly with equal probability
    if (duplicatedIndex >= 0)
    {
        maxActionIndex = (std::rand() % duplicatedIndex);
    }

    return maxActionIndex;
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-19 03:53:50

并行程序不一定比串行程序快;设置并行算法既有固定的时间成本,也有可变的时间成本,对于小的和/或简单的问题,这种并行开销成本可能大于串行算法的整体成本。并行开销的示例包括线程派生和同步、额外内存复制和内存总线压力。串行程序大约2毫秒,并行程序大约500微秒,很可能你的矩阵足够小,以至于设置并行算法的工作盖过了解决矩阵问题的工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49905094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档