Commit d571235b authored by Cianciosa, Mark's avatar Cianciosa, Mark
Browse files

Fix multithreading issue where multiple threads would try to access the same...

Fix multithreading issue where multiple threads would try to access the same netcdf file at the same time.
parent deca429b
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -51,10 +51,10 @@
 * <a href="https://docs.jax.dev/en/latest/">JAX</a> due to it's popularity,
 * and <a href="https://kokkos.org">Kokkos</a> for its performance
 * portability. Source codes for this benchmark case is available in the
 * appendix. Figure \ref{fig:compare} shows the through put of pushing $10^{8}$
 * particles for $10^{3}$ time steps. The graph framework consistently shows the
 * best throughput on both CPUs and GPUs. Note MLX CPU throughput could by
 * improved by splitting the problem to multiple threads.
 * appendix. Figure \ref{fig:compare} shows the through put of pushing
 * @f$10^{8}@f$ particles for @f$10^{3}@f$ time steps. The graph framework
 * consistently shows the best throughput on both CPUs and GPUs. Note MLX CPU
 * throughput could by improved by splitting the problem to multiple threads.
 *
 * @subsection code_performance_comparison_codes Source codes for throughput benchmark comparison
 * @subsubsection code_performance_comparison_graph Graph Framework
+5 −3
Original line number Diff line number Diff line
@@ -2028,9 +2028,9 @@
					"EFIT_FILE=\\\"/Users/m4c/Projects/graph_framework/graph_tests/efit.nc\\\"",
					"VMEC_FILE=\\\"/Users/m4c/Projects/graph_framework/graph_tests/vmec.nc\\\"",
					USE_METAL,
					"\"CXX_ARGS=\\\"-I/Users/m4c/Projects/graph_framework/graph_framework -I/usr/local/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks -fgnuc-version=4.2.1 -std=gnu++2a\\\"\"",
					"\"CXX_ARGS=\\\"-I/Users/m4c/Projects/graph_framework/graph_framework -I/usr/local/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/21/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks -fgnuc-version=4.2.1 -std=gnu++2a\\\"\"",
					STATIC,
					"MACOS_LIB_RT=\\\"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17.0.0/lib/darwin/libclang_rt.osx.a\\\"",
					"MACOS_LIB_RT=\\\"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/21.0.0/lib/darwin/libclang_rt.osx.a\\\"",
					USE_INDEX_CACHE,
					"USE_VERBOSE=false",
					"$(inherited)",
@@ -2204,8 +2204,10 @@
					"EFIT_FILE=\\\"/Users/m4c/Projects/graph_framework/graph_tests/efit.nc\\\"",
					"VMEC_FILE=\\\"/Users/m4c/Projects/graph_framework/graph_tests/vmec.nc\\\"",
					USE_METAL,
					"MACOS_LIB_RT=\\\"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/21.0.0/lib/darwin/libclang_rt.osx.a\\\"",
					"USE_VERBOSE=false",
					"\"CXX_ARGS=\\\"-I/Users/m4c/Projects/graph_framework/graph_framework -I/usr/local/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks -fgnuc-version=4.2.1 -std=gnu++2a\\\"\"",
					USE_INDEX_CACHE,
					"\"CXX_ARGS=\\\"-I/Users/m4c/Projects/graph_framework/graph_framework -I/usr/local/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/21.0.0/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks -fgnuc-version=4.2.1 -std=gnu++2a\\\"\"",
					"$(inherited)",
				);
				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+2 −4
Original line number Diff line number Diff line
@@ -235,8 +235,7 @@ namespace absorption {
                {graph::zero<T, SAFE_MATH> (), graph::variable_cast(this->kamp)}
            };

            work.add_item(inputs, {}, setters,
                          graph::shared_random_state<T, SAFE_MATH> (),
            work.add_item(inputs, {}, setters, NULL,
                          "root_find_init_kernel", inputs.back()->size());

            inputs.push_back(graph::variable_cast(this->t));
@@ -265,8 +264,7 @@ namespace absorption {
            setters = {
                {klen + kamp, graph::variable_cast(this->kamp)}
            };
            work.add_item(inputs, {}, setters,
                          graph::shared_random_state<T, SAFE_MATH> (),
            work.add_item(inputs, {}, setters, NULL,
                          "final_kamp", inputs.back()->size());
        }

+0 −1
Original line number Diff line number Diff line
@@ -100,7 +100,6 @@ namespace commandline {
                }
            }
            std::cout << std::endl;
            sync.unlock();
            exit(0);
        }

+6 −8
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
///  @subsection equilibrium_splines_1D Cubic Splines
///  Cubic splines are 1D interpolation functions consisting of 4 coefficient
///  arrays. They take the form of
///  @f{equation}{y\left(x\right)=C_{0} + C_{1}x + C_{2}x^2 + C_{3}x^2@f}
///  @f{equation}{y\left(x\right)=C_{0} + C_{1}x + C_{2}x^{2} + C_{3}x^{3}@f}
///  where @f$x@f$ is a normalized radial index. Cubic splines coefficients can
///  be calculated using
///  <a href="https://mathworld.wolfram.com/CubicSpline.html">Linear Solvers</a>
@@ -219,12 +219,10 @@
#include "math.hpp"
#include "arithmetic.hpp"
#include "newton.hpp"
#include "output.hpp"

///  Name space for equilibrium models.
namespace equilibrium {
///  Lock to synchronize netcdf across threads.
    static std::mutex sync;

//******************************************************************************
//  Equilibrium interface
//******************************************************************************
@@ -1629,7 +1627,7 @@ namespace equilibrium {
    template<jit::float_scalar T, bool SAFE_MATH=false>
    shared<T, SAFE_MATH> make_efit(const std::string &spline_file) {
        int ncid;
        sync.lock();
        output::sync.lock();
        nc_open(spline_file.c_str(), NC_NOWRITE, &ncid);

//  Load scalar quantities.
@@ -1794,7 +1792,7 @@ namespace equilibrium {
        nc_get_var(ncid, varid, ne_c3_buffer.data());
                    
        nc_close(ncid);
        sync.unlock();
        output::sync.unlock();

        auto rmin = static_cast<T> (rmin_value);
        auto dr = static_cast<T> (dr_value);
@@ -2426,7 +2424,7 @@ namespace equilibrium {
    template<jit::float_scalar T, bool SAFE_MATH=false>
    shared<T, SAFE_MATH> make_vmec(const std::string &spline_file) {
        int ncid;
        sync.lock();
        output::sync.lock();
        nc_open(spline_file.c_str(), NC_NOWRITE, &ncid);

//  Load scalar quantities.
@@ -2593,7 +2591,7 @@ namespace equilibrium {
        nc_get_var(ncid, varid, xn_buffer.data());

        nc_close(ncid);
        sync.unlock();
        output::sync.unlock();

        auto sminf = static_cast<T> (sminf_value);
        auto sminh = static_cast<T> (sminh_value);