gpt4 book ai didi

c++ - 并发线程比单线程慢

转载 作者:行者123 更新时间:2023-11-28 04:42:40 24 4
gpt4 key购买 nike

我一直在比较两种从矩阵中找到最大值的方法(如果它们是重复的,则在它们之间随机选择),单线程与多线程。通常,假设我正确编码,多线程应该更快。因为它不是,它慢得多,我只能假设我做错了什么。有人可以指出我做错了什么吗?

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

提前致谢!

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;
}

最佳答案

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

关于c++ - 并发线程比单线程慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49905094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com