Commit 5c9d443a authored by Cianciosa, Mark's avatar Cianciosa, Mark
Browse files

Consolidate the caches to a single leaf node cache in an attempt to fix the...

Consolidate the caches to a single leaf node cache in an attempt to fix the issue with __tls_guard on gcc.
parent 2d3f3326
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -78,28 +78,28 @@ int main(int argc, const char * argv[]) {
//  Inital conditions.
            if constexpr (jit::is_complex<base> ()) {
                if constexpr (jit::is_float<base> ()) {
                    std::normal_distribution<float> norm_dist(static_cast<float> (590.0), static_cast<float> (10.0));
                    std::normal_distribution<float> norm_dist(static_cast<float> (1000.0), static_cast<float> (10.0));
                    for (size_t j = 0; j < local_num_rays; j++) {
                        omega->set(j, static_cast<base> (norm_dist(engine)));
                    }
                } else {
                    std::normal_distribution<double> norm_dist(static_cast<double> (590.0), static_cast<double> (10.0));
                    std::normal_distribution<double> norm_dist(static_cast<double> (1000.0), static_cast<double> (10.0));
                    for (size_t j = 0; j < local_num_rays; j++) {
                        omega->set(j, static_cast<base> (norm_dist(engine)));
                    }
                }
            } else {
                std::normal_distribution<base> norm_dist(static_cast<base> (590.0), static_cast<base> (10.0));
                std::normal_distribution<base> norm_dist(static_cast<base> (1000.0), static_cast<base> (10.0));
                for (size_t j = 0; j < local_num_rays; j++) {
                    omega->set(j, static_cast<base> (norm_dist(engine)));
                }
            }

            x->set(static_cast<base> (2.5));
            x->set(static_cast<base> (1.5));
            //x->set(static_cast<base> (0.0));
            y->set(static_cast<base> (0.0));
            z->set(static_cast<base> (0.0));
            kx->set(static_cast<base> (-600.0));
            kx->set(static_cast<base> (2000.0));
            //kx->set(static_cast<base> (600.0));
            ky->set(static_cast<base> (0.0));
            kz->set(static_cast<base> (0.0));
@@ -110,7 +110,7 @@ int main(int argc, const char * argv[]) {
            //auto eq = equilibrium::make_no_magnetic_field<base> ();

            //const base endtime = static_cast<base> (4.0);
            const base endtime = static_cast<base> (1.0);
            const base endtime = static_cast<base> (100.0);
            const base dt = endtime/static_cast<base> (num_times);

            //auto dt_var = graph::variable(num_rays, static_cast<base> (dt), "dt");
@@ -118,12 +118,13 @@ int main(int argc, const char * argv[]) {
            //solver::split_simplextic<dispersion::bohm_gross<base>>
            //solver::adaptive_rk4<dispersion::bohm_gross<base>>
            //solver::rk4<dispersion::simple<base>>
            solver::rk4<dispersion::ordinary_wave<base>>
            //solver::rk4<dispersion::extra_ordinary_wave<base>>
            //solver::rk4<dispersion::ordinary_wave<base>>
            solver::rk4<dispersion::extra_ordinary_wave<base>>
            //solver::rk4<dispersion::cold_plasma<base>>
            //solver::adaptive_rk4<dispersion::ordinary_wave<base>>
                solve(omega, kx, ky, kz, x, y, z, t, dt, eq);
                //solve(omega, kx, ky, kz, x, y, z, t, dt_var, eq);
            solve.init(kx);
            solve.init(omega);
            solve.compile();
            if (thread_number == 0 && false) {
                solve.print_dispersion();
+20 −35
Original line number Diff line number Diff line
@@ -369,9 +369,6 @@ namespace graph {
                std::cout << "\\right)";
            }
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -388,13 +385,13 @@ namespace graph {
                       shared_leaf<T> r) {
        auto temp = std::make_shared<add_node<T>> (l, r)->reduce();
        const size_t h = temp->get_hash();
        if (add_node<T>::cache.find(h) ==
            add_node<T>::cache.end()) {
            add_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return add_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -707,9 +704,6 @@ namespace graph {
                std::cout << "\\right)";
            }
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -723,13 +717,13 @@ namespace graph {
                            shared_leaf<T> r) {
        auto temp = std::make_shared<subtract_node<T>> (l, r)->reduce();
        const size_t h = temp->get_hash();
        if (subtract_node<T>::cache.find(h) ==
            subtract_node<T>::cache.end()) {
            subtract_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return subtract_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -1138,9 +1132,6 @@ namespace graph {
                this->right->to_latex();
            }
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -1154,13 +1145,13 @@ namespace graph {
                            shared_leaf<T> r) {
        auto temp = std::make_shared<multiply_node<T>> (l, r)->reduce();
        const size_t h = temp->get_hash();
        if (multiply_node<T>::cache.find(h) ==
            multiply_node<T>::cache.end()) {
            multiply_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return multiply_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -1482,9 +1473,6 @@ namespace graph {
            this->right->to_latex();
            std::cout << "}";
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -1498,13 +1486,13 @@ namespace graph {
                          shared_leaf<T> r) {
        auto temp = std::make_shared<divide_node<T>> (l, r)->reduce();
        const size_t h = temp->get_hash();
        if (divide_node<T>::cache.find(h) ==
            divide_node<T>::cache.end()) {
            divide_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return divide_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -1788,9 +1776,6 @@ namespace graph {
            this->right->to_latex();
            std::cout << "\\right)";
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -1806,13 +1791,13 @@ namespace graph {
                       shared_leaf<T> r) {
        auto temp = std::make_shared<fma_node<T>> (l, m, r)->reduce();
        const size_t h = temp->get_hash();
        if (fma_node<T>::cache.find(h) ==
            fma_node<T>::cache.end()) {
            fma_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return fma_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared add nodes.
+16 −28
Original line number Diff line number Diff line
@@ -178,9 +178,6 @@ namespace graph {
            this->arg->to_latex();
            std::cout << "}";
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -192,13 +189,13 @@ namespace graph {
    template<typename T> shared_leaf<T> sqrt(shared_leaf<T> x) {
        auto temp = std::make_shared<sqrt_node<T>> (x)->reduce();
        const size_t h = temp->get_hash();
        if (sqrt_node<T>::cache.find(h) ==
            sqrt_node<T>::cache.end()) {
            sqrt_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return sqrt_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared sqrt nodes.
@@ -346,9 +343,6 @@ namespace graph {
            this->arg->to_latex();
            std::cout << "\\right)}";
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -360,13 +354,13 @@ namespace graph {
    template<typename T> shared_leaf<T> exp(shared_leaf<T> x) {
        auto temp = std::make_shared<exp_node<T>> (x)->reduce();
        const size_t h = temp->get_hash();
        if (exp_node<T>::cache.find(h) ==
            exp_node<T>::cache.end()) {
            exp_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return exp_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared exp nodes.
@@ -510,9 +504,6 @@ namespace graph {
            this->arg->to_latex();
            std::cout << "\\right)}";
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -524,13 +515,13 @@ namespace graph {
    template<typename T> shared_leaf<T> log(shared_leaf<T> x) {
        auto temp = std::make_shared<log_node<T>> (x)->reduce();
        const size_t h = temp->get_hash();
        if (log_node<T>::cache.find(h) ==
            log_node<T>::cache.end()) {
            log_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return log_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared log nodes.
@@ -757,9 +748,6 @@ namespace graph {
                   (this->right->is_variable_like() ||
                    constant_cast(this->right).get());
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -773,13 +761,13 @@ namespace graph {
                       shared_leaf<T> r) {
        auto temp = std::make_shared<pow_node<T>> (l, r)->reduce();
        const size_t h = temp->get_hash();
        if (pow_node<T>::cache.find(h) ==
            pow_node<T>::cache.end()) {
            pow_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return pow_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared add nodes.
+15 −19
Original line number Diff line number Diff line
@@ -156,6 +156,10 @@ namespace graph {
            return hash;
        }

///  Cache for constructed nodes.
        inline thread_local static std::map<size_t,
                                            std::shared_ptr<leaf_node<T>>> cache;

///  Type def to retrieve the backend type.
        typedef T base;
    };
@@ -166,9 +170,6 @@ namespace graph {
///  Convenience type alias for a vector of output nodes.
    template<typename T>
    using output_nodes = std::vector<shared_leaf<T>>;
///  Convenience type alias for node caches.
    template<typename T>
    using node_cache = std::map<size_t, shared_leaf<T>>;

//******************************************************************************
//  Base straight node.
@@ -518,13 +519,11 @@ namespace graph {
        virtual shared_leaf<T> df(shared_leaf<T> x) {
            auto zero = std::make_shared<constant_node<T>> (static_cast<T> (0.0));
            const size_t h = zero->get_hash();
            if (constant_node<T>::cache.find(h) ==
                constant_node<T>::cache.end()) {
                constant_node<T>::cache[h] = zero;
            if (leaf_node<T>::cache.find(h) ==
                leaf_node<T>::cache.end()) {
                leaf_node<T>::cache[h] = zero;
                return zero;
            }
            
            return constant_node<T>::cache[h];
        }

//------------------------------------------------------------------------------
@@ -606,9 +605,6 @@ namespace graph {
        virtual bool is_variable_like() const {
            return false;
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
    };

//------------------------------------------------------------------------------
@@ -621,13 +617,13 @@ namespace graph {
    shared_leaf<T> constant(const T d) {
        auto temp = std::make_shared<constant_node<T>> (d)->reduce();
        const size_t h = temp->get_hash();
        if (constant_node<T>::cache.find(h) ==
            constant_node<T>::cache.end()) {
            constant_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return constant_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -640,13 +636,13 @@ namespace graph {
    shared_leaf<T> constant(const backend::buffer<T> &d) {
        auto temp = std::make_shared<constant_node<T>> (d)->reduce();
        const size_t h = temp->get_hash();
        if (constant_node<T>::cache.find(h) ==
            constant_node<T>::cache.end()) {
            constant_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return constant_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//  Define some common constants.
+12 −16
Original line number Diff line number Diff line
@@ -287,8 +287,6 @@ namespace graph {
            return false;
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
///  Cache for the backend buffers.
        inline thread_local static std::map<size_t, backend::buffer<T>> backend_cache;
    };
@@ -302,13 +300,13 @@ namespace graph {
    template<typename T> shared_leaf<T> piecewise_1D(const std::vector<T> &d) {
        auto temp = std::make_shared<piecewise_1D_node<T>> (d)->reduce();
        const size_t h = temp->get_hash();
        if (piecewise_1D_node<T>::cache.find(h) ==
            piecewise_1D_node<T>::cache.end()) {
            piecewise_1D_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return piecewise_1D_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
@@ -322,13 +320,13 @@ namespace graph {
                                                     shared_leaf<T> x) {
        auto temp = std::make_shared<piecewise_1D_node<T>> (d, x)->reduce();
        const size_t h = temp->get_hash();
        if (piecewise_1D_node<T>::cache.find(h) ==
            piecewise_1D_node<T>::cache.end()) {
            piecewise_1D_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return piecewise_1D_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

///  Convenience type alias for shared piecewise 1D nodes.
@@ -702,8 +700,6 @@ namespace graph {
            return false;
        }

///  Cache for constructed nodes.
        inline thread_local static node_cache<T> cache;
///  Cache for the backend buffers.
        inline thread_local static std::map<size_t, backend::buffer<T>> backend_cache;
    };
@@ -719,13 +715,13 @@ namespace graph {
                                                     const size_t n) {
        auto temp = std::make_shared<piecewise_2D_node<T>> (d, n)->reduce();
        const size_t h = temp->get_hash();
        if (piecewise_2D_node<T>::cache.find(h) ==
            piecewise_2D_node<T>::cache.end()) {
            piecewise_2D_node<T>::cache[h] = temp;
        if (leaf_node<T>::cache.find(h) ==
            leaf_node<T>::cache.end()) {
            leaf_node<T>::cache[h] = temp;
            return temp;
        }
        
        return piecewise_2D_node<T>::cache[h];
        return leaf_node<T>::cache[h];
    }

//------------------------------------------------------------------------------
Loading