Skip to content

Commit

Permalink
Improve speed of OI
Browse files Browse the repository at this point in the history
Create Point objects for each observation point once at the beginning of
the function, so that they don't get created many times when passing
into corr().

This makes up for the lost time caused by 6f6a551, where there is extra
overhead dealing with the vector version of corr(). However, with this
fix, OI is even faster than v0.7.0 (about 5%).
  • Loading branch information
tnipen committed Mar 11, 2024
1 parent 2956a46 commit a4cd72c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/api/oi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ vec gridpp::optimal_interpolation_full(const gridpp::Points& bpoints,
// Compute the background value at observation points (Y)
vec gY = pbackground;

// Create all objects of type Point (to save time on creation later)
std::vector<Point> point_vec;
point_vec.reserve(nS);
for(int i = 0; i < nS; i++) {
point_vec.push_back(points.get_point(i));
}

#pragma omp parallel for
for(int y = 0; y < nY; y++) {
if(!gridpp::is_valid(background[y])) {
Expand Down Expand Up @@ -238,8 +245,7 @@ vec gridpp::optimal_interpolation_full(const gridpp::Points& bpoints,
p2.reserve(lLocIndices0.size());
for(int i = 0; i < lLocIndices0.size(); i++) {
int index = lLocIndices0[i];
Point p = points.get_point(index);
p2.push_back(p);
p2.push_back(point_vec[index]);
}
vec rhos = structure.corr_background(p1, p2);
for(int i = 0; i < lLocIndices0.size(); i++) {
Expand Down Expand Up @@ -295,12 +301,11 @@ vec gridpp::optimal_interpolation_full(const gridpp::Points& bpoints,
lY(i) = gY[index];
lR(i, i) = pratios[index];
lG(0, i) = lRhos(i);
Point p1 = points.get_point(index);
Point p1 = point_vec[index];
std::vector<Point> p2(lS, Point(0, 0));
for(int j = 0; j < lS; j++) {
int index_j = lLocIndices[j];
Point p = points.get_point(index_j);
p2[j] = p;
p2[j] = point_vec[index_j];
}
vec corr = structure.corr(p1, p2);
for(int j = 0; j < lS; j++) {
Expand Down
10 changes: 8 additions & 2 deletions src/api/oi_ensi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ vec2 gridpp::optimal_interpolation_ensi(const gridpp::Points& bpoints,
gYhat[i] = mean;
}

// Create all objects of type Point (to save time on creation later)
std::vector<Point> point_vec;
point_vec.reserve(nS);
for(int i = 0; i < nS; i++) {
point_vec.push_back(points.get_point(i));
}

// Calculate number of valid members
int nValidEns = 0;
ivec validEns;
Expand Down Expand Up @@ -219,8 +226,7 @@ vec2 gridpp::optimal_interpolation_ensi(const gridpp::Points& bpoints,
p2.reserve(lLocIndices0.size());
for(int i = 0; i < lLocIndices0.size(); i++) {
int index = lLocIndices0[i];
Point p = points.get_point(index);
p2.push_back(p);
p2.push_back(point_vec[index]);
}
vec rhos = structure.corr_background(p1, p2);
for(int i = 0; i < lLocIndices0.size(); i++) {
Expand Down

0 comments on commit a4cd72c

Please sign in to comment.