【发布时间】:2018-12-23 17:00:09
【问题描述】:
在性能关键的并行代码中,我有一个向量,其元素是:
- 计算成本非常高,结果是确定性的(给定位置的元素值仅取决于位置)
- 随机访问(通常访问次数大于或远大于向量的大小)
- 集群访问(许多访问请求相同的值)
- 向量由不同的线程共享(竞争条件?)
- 为避免堆碎片整理,不应重新创建对象,而应尽可能重置和回收
- 向量中的值将由多态对象提供
目前,我预先计算了向量的所有可能值,因此竞态条件应该不是问题。 为了提高性能,我正在考虑创建一个惰性向量,这样代码仅在请求向量的元素时才执行计算。 在并行区域中,可能会发生多个线程同时请求并且可能计算相同元素的情况。 我该如何处理这种可能的竞争情况?
以下是我想要实现的示例。在Windows 10、Visual Studio 17下可以正常编译运行。我用的是C++17。
// Lazy.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <math.h>
const double START_SUM = 1;
const double END_SUM = 1000;
//base object responsible for providing the values
class Evaluator
{
public:
Evaluator() {};
~Evaluator() {};
//Function with deterministic output, depending on the position
virtual double expensiveFunction(int pos) const = 0;
};
//
class EvaluatorA: public Evaluator
{
public:
//expensive evaluation
virtual double expensiveFunction(int pos) const override {
double t = 0;
for (int j = START_SUM; j++ < END_SUM; j++)
t += log(exp(log(exp(log(j + pos)))));
return t;
}
EvaluatorA() {};
~EvaluatorA() {};
};
class EvaluatorB : public Evaluator
{
public:
//even more expensive evaluation
virtual double expensiveFunction(int pos) const override {
double t = 0;
for (int j = START_SUM; j++ < 10*END_SUM; j++)
t += log(exp(log(exp(log(j + pos)))));
return t;
}
EvaluatorB() {};
~EvaluatorB() {};
};
class LazyVectorTest //vector that contains N possible results
{
public:
LazyVectorTest(int N,const Evaluator & eval) : N(N), innerContainer(N, 0), isThatComputed(N, false), eval_ptr(&eval)
{};
~LazyVectorTest() {};
//reset, to generate a new table of values
//the size of the vector stays constant
void reset(const Evaluator & eval) {
this->eval_ptr = &eval;
for (int i = 0; i<N; i++)
isThatComputed[i] = false;
}
int size() { return N; }
//accessing the same position should yield the same result
//unless the object is resetted
const inline double& operator[](int pos) {
if (!isThatComputed[pos]) {
innerContainer[pos] = eval_ptr->expensiveFunction(pos);
isThatComputed[pos] = true;
}
return innerContainer[pos];
}
private:
const int N;
const Evaluator* eval_ptr;
std::vector<double> innerContainer;
std::vector<bool> isThatComputed;
};
//the parallel access will take place here
template <typename T>
double accessingFunction(T& A, const std::vector<int>& elementsToAccess) {
double tsum = 0;
int size = elementsToAccess.size();
//#pragma omp parallel for
for (int i = 0; i < size; i++)
tsum += A[elementsToAccess[i]];
return tsum;
}
std::vector<int> randomPos(int sizePos, int N) {
std::vector<int> elementsToAccess;
for (int i = 0; i < sizePos; i++)
elementsToAccess.push_back(rand() % N);
return elementsToAccess;
}
int main()
{
srand(time(0));
int minAccessNumber = 1;
int maxAccessNumber = 100;
int sizeVector = 50;
auto start = std::chrono::steady_clock::now();
double res = 0;
float numberTest = 100;
typedef LazyVectorTest container;
EvaluatorA eval;
for (int i = 0; i < static_cast<int>(numberTest); i++) {
res = eval.expensiveFunction(i);
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli>diff(end - start);
double benchmark = diff.count() / numberTest;
std::cout <<"Average time to compute expensive function:" <<benchmark<<" ms"<<std::endl;
std::cout << "Value of the function:" << res<< std::endl;
std::vector<std::vector<int>> indexs(numberTest);
container A(sizeVector, eval);
for (int accessNumber = minAccessNumber; accessNumber < maxAccessNumber; accessNumber++) {
indexs.clear();
for (int i = 0; i < static_cast<int>(numberTest); i++) {
indexs.emplace_back(randomPos(accessNumber, sizeVector));
}
auto start_lazy = std::chrono::steady_clock::now();
for (int i = 0; i < static_cast<int>(numberTest); i++) {
A.reset(eval);
double res_lazy = accessingFunction(A, indexs[i]);
}
auto end_lazy = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli>diff_lazy(end_lazy - start_lazy);
std::cout << accessNumber << "," << diff_lazy.count() / numberTest << ", " << diff_lazy.count() / (numberTest* benchmark) << std::endl;
}
return 0;
}
【问题讨论】:
-
您的描述以
std::map为基础的实现让我尖叫"Dynamic Programming"...有点渴望现在变成答案(如果有人不打败我,我会稍后再试)它),但我建议您以这些链接为起点。只需确保在第一次生成值时根据需要使用锁定,您就可以免受竞争条件的影响。 -
我真的没有看到。正在评估的函数中没有递归,所以我看不到 DP 将如何帮助(每个评估都独立于其他评估)。而且地图的访问速度比矢量要慢。
-
DP 不需要显式递归。您提到值被多次使用并且始终具有确定性,这就是证明 DP 合理性所需的全部内容,因为您基于先前计算的值进行了进一步的计算。关于
map,我同意。问题是您是否总是使用数组中的所有位置(在这种情况下,vector是要走的路)或者可能存在(相当高)数量的未使用值,这会使向量方法在内存方面过度浪费 -
向量大小在编译时是否已知?还是在运行时确定?而且,如果在运行时,它是在算法开始时就知道的,还是在整个执行过程中增长?
-
大小在运行时是已知的,但一旦建立,它就会保持不变。
标签: c++ parallel-processing openmp c++17