I see with the CPU profiler what compute_variances()is the bottleneck of my project.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
75.63 5.43 5.43 40 135.75 135.75 compute_variances(unsigned int, std::vector<Point, std::allocator<Point> > const&, float*, float*, unsigned int*)
19.08 6.80 1.37 readDivisionSpace(Division_Euclidean_space&, char*)
...
Here is the function body:
void compute_variances(size_t t, const std::vector<Point>& points, float* avg,
float* var, size_t* split_dims) {
for (size_t d = 0; d < points[0].dim(); d++) {
avg[d] = 0.0;
var[d] = 0.0;
}
float delta, n;
for (size_t i = 0; i < points.size(); ++i) {
n = 1.0 + i;
for (size_t d = 0; d < points[0].dim(); ++d) {
delta = (points[i][d]) - avg[d];
avg[d] += delta / n;
var[d] += delta * ((points[i][d]) - avg[d]);
}
}
kthLargest(var, points[0].dim(), t, split_dims);
}
where kthLargest(), it seems, is not a problem, as I see that:
0.00 7.18 0.00 40 0.00 0.00 kthLargest(float*, int, int, unsigned int*)
compute_variances()takes a vector of float vectors (i.e. a vector Pointswhere Pointsis the class I implemented) and calculates their variance in each dimension (in relation to the Knut algorithm).
This is how I call the function:
float avg[(*points)[0].dim()];
float var[(*points)[0].dim()];
size_t split_dims[t];
compute_variances(t, *points, avg, var, split_dims);
The question is, can I do better? I would be very happy to pay a trade-off between speed and the approximate calculation of variances. Or maybe I could make the code more convenient for the cache or something else?
Compiled as follows:
g++ main_noTime.cpp -std=c++0x -p -pg -O3 -o eg
, -o3, "o". ypnos, -o3. , , -.
, compute_variances !
[EDIT]
copute_variances() 40 .
10 :
points.size() = 1000 and points[0].dim = 10000
points.size() = 10000 and points[0].dim = 100
points.size() = 10000 and points[0].dim = 10000
points.size() = 100000 and points[0].dim = 100
.
: points[i][d]?
A: point[i] i- std::vector, [] , Point.
const FT& operator [](const int i) const {
if (i < (int) coords.size() && i >= 0)
return coords.at(i);
else {
std::cout << "Error at Point::[]" << std::endl;
exit(1);
}
return coords[0];
}
coords - std::vector float. , , , ? ( ). , , std::vector.at() ( ref). , .at() , , , .
compute_variances() - ! , , , .
, parallelism.
[EDIT.2]
Point (, - ):
class Point {
public:
typedef float FT;
...
size_t dim() const {
return coords.size();
}
FT& operator [](const int i) {
return coords.at(i);
}
const FT& operator [](const int i) const {
return coords.at(i);
}
private:
std::vector<FT> coords;
};