Skip to content

FentonWave performance #84

@mgrouch

Description

@mgrouch

diff --git a/FentonWave.h b/FentonWave.h
index abcdef1..1234567 100644
--- a/FentonWave.h
+++ b/FentonWave.h
@@ class FentonWave {
+    struct ResidualCache {
+        Eigen::Array<Real, N + 1, 1> eta;
+        Eigen::Array<Real, N + 1, 1> x_m;
+        std::array<Eigen::Array<Real, N, 1>, N + 1> kj_eta;
+        std::array<Eigen::Array<Real, N, 1>, N + 1> sinh_by_cosh_vals;
+        std::array<Eigen::Array<Real, N, 1>, N + 1> cosh_by_cosh_vals;
+        std::array<Eigen::Array<Real, N, 1>, N + 1> sin_jx;
+        std::array<Eigen::Array<Real, N, 1>, N + 1> cos_jx;
+    };

@@ void optimize(...) {
-        for (int iter = 0; iter < 500 && error > Real(1e-8); ++iter) {
-            Eigen::Matrix<Real, NU, 1> f = compute_residual(coeffs, H, k, D);
+        ResidualCache cache;
+        for (int iter = 0; iter < 500 && error > Real(1e-8); ++iter) {
+            Eigen::Matrix<Real, NU, 1> f = compute_residual(coeffs, H, k, D, cache);
             error = f.cwiseAbs().maxCoeff();
-            Eigen::Matrix<Real, NU, NU> J = compute_jacobian(coeffs, H, k, D);
+            Eigen::Matrix<Real, NU, NU> J = compute_jacobian(coeffs, H, k, D, cache);
@@ Eigen::Matrix<Real, StateDim, 1>
-    compute_residual(const Eigen::Matrix<Real, StateDim, 1>& coeffs, Real H, Real k, Real D) {
+    compute_residual(const Eigen::Matrix<Real, StateDim, 1>& coeffs, Real H, Real k, Real D, ResidualCache& cache) {
...
-        for (int m = 0; m <= N; ++m) {
-            Real x_m = Real(M_PI) * m / N;
-            Real eta_m = eta(m);
+        for (int m = 0; m <= N; ++m) {
+            Real x_m = Real(M_PI) * m / N;
+            Real eta_m = eta(m);
+            cache.eta(m) = eta_m;
+            cache.x_m(m) = x_m;
+            for (int j = 0; j < N; ++j) {
+                Real kj_eta = kj(j) * eta_m;
+                Real kj_D   = kj(j) * D;
+                cache.kj_eta[m](j) = kj_eta;
+                cache.sinh_by_cosh_vals[m](j) = sinh_by_cosh(kj_eta, kj_D);
+                cache.cosh_by_cosh_vals[m](j) = cosh_by_cosh(kj_eta, kj_D);
+                Real jx = j_cache(j) * x_m;
+                cache.sin_jx[m](j) = std::sin(jx);
+                cache.cos_jx[m](j) = std::cos(jx);
+            }
+        }

         for (int m = 0; m <= N; ++m) {
-            Real x_m = Real(M_PI) * m / N;
-            Real eta_m = eta(m);
-            Eigen::Array<Real, N, 1> kj_eta = kj * eta_m;
-            Eigen::Array<Real, N, 1> kj_D   = kj * D;
-            Eigen::Array<Real, N, 1> sin_jx = (j * x_m).sin();
-            Eigen::Array<Real, N, 1> cos_jx = (j * x_m).cos();
-            Eigen::Array<Real, N, 1> S1 = kj_eta.binaryExpr(kj_D, sinh_by_cosh);
-            Eigen::Array<Real, N, 1> C1 = kj_eta.binaryExpr(kj_D, cosh_by_cosh);
+            Real x_m = cache.x_m(m);
+            Real eta_m = cache.eta(m);
+            const auto& S1 = cache.sinh_by_cosh_vals[m];
+            const auto& C1 = cache.cosh_by_cosh_vals[m];
+            const auto& sin_jx = cache.sin_jx[m];
+            const auto& cos_jx = cache.cos_jx[m];
@@ Eigen::Matrix<Real, StateDim, StateDim>
-    compute_jacobian(const Eigen::Matrix<Real, StateDim, 1>& coeffs, Real H, Real k, Real D) {
+    compute_jacobian(const Eigen::Matrix<Real, StateDim, 1>& coeffs, Real H, Real k, Real D, const ResidualCache& cache) {
...
         for (int m = 0; m <= N; ++m) {
-            Real eta_m = eta(m);
-            Real x_m = Real(M_PI) * m / N;
+            Real eta_m = cache.eta(m);
+            Real x_m   = cache.x_m(m);
-            for (int j = 0; j < N; ++j) {
-                Real kj_eta = kj(j) * eta_m;
-                Real kj_D   = kj(j) * D;
-                S1(j) = sinh_by_cosh(kj_eta, kj_D);
-                C1(j) = cosh_by_cosh(kj_eta, kj_D);
-                Real jx = j_arr(j) * x_m;
-                S2(j) = std::sin(jx);
-                C2(j) = std::cos(jx);
-            }
+            S1 = cache.sinh_by_cosh_vals[m];
+            C1 = cache.cosh_by_cosh_vals[m];
+            S2 = cache.sin_jx[m];
+            C2 = cache.cos_jx[m];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions