From 67402908ede7f132df5be5b42dde9c394538077c Mon Sep 17 00:00:00 2001 From: Joshua Kelley <60236515+k-joshua-kelley@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:00:45 -0600 Subject: [PATCH 1/2] Update MountainRangeThreaded.hpp --- src/MountainRangeThreaded.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/MountainRangeThreaded.hpp b/src/MountainRangeThreaded.hpp index e8dd4fe..07ff152 100644 --- a/src/MountainRangeThreaded.hpp +++ b/src/MountainRangeThreaded.hpp @@ -15,13 +15,16 @@ namespace { // Create a vector of threads, each of which will run F(thread_id) in a while loop until F() returns false auto looping_threadpool(auto thread_count, auto F) { - std::vector threads; - threads.reserve(thread_count); + std::vector threads; // Creates an empty vector of threads. + threads.reserve(thread_count); // Creates thread_count threads for use later. Sets the size of the thread vector. Sets the capacity if the vector. for (size_t tid=0; tid Date: Mon, 28 Oct 2024 13:15:50 -0600 Subject: [PATCH 2/2] Update MountainRangeThreaded.hpp Added comments explaining what everything is doing. --- src/MountainRangeThreaded.hpp | 48 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/MountainRangeThreaded.hpp b/src/MountainRangeThreaded.hpp index 07ff152..72ba733 100644 --- a/src/MountainRangeThreaded.hpp +++ b/src/MountainRangeThreaded.hpp @@ -31,20 +31,20 @@ namespace { -class MountainRangeThreaded: public MountainRange { +class MountainRangeThreaded: public MountainRange { // Inherits from MountainRange, making all public things in MountainRange public in MountainRangeThreaded // Threading-related members bool continue_iteration; // used to tell the looping threadpool to terminate at the end of the simulation const size_type nthreads; - std::barrier<> ds_barrier, step_barrier; - std::vector ds_workers, step_workers; - std::atomic ds_aggregator; // used to reduce dsteepness from each thread + std::barrier ds_barrier, step_barrier; // creates two barrier objects. + std::vector ds_workers, step_workers; // creates two vectors of threads + std::atomic ds_aggregator; // used to reduce dsteepness from each thread // I'm not sure how this will be adapted to the wave function, I think dsteepness -> energy. value_type iter_dt; // Used to distribute dt to each thread // Determine which rows a certain thread is in charge of auto this_thread_cell_range(auto tid) { - return mr::split_range(cells, tid, nthreads); + return mr::split_range(cells, tid, nthreads); // chops up the matrix and divies it up to the various threads. } @@ -52,33 +52,35 @@ class MountainRangeThreaded: public MountainRange { public: // Help message to show that SOLVER_NUM_THREADS controls thread counts inline static const std::string help_message = - "Set the environment variable SOLVER_NUM_THREADS to a positive integer to set thread count (default 1)."; + "Set the environment variable SOLVER_NUM_THREADS to a positive integer to set thread count (default 1)."; // static means create only one variable to be shared with all instances. +// inline makes it so I don't have to go access this memory location each time, the complier just sticks it in wherever I call it (which is expensive unliess I only use it a couple times). // Run base constructor, then build threading infrastructure - MountainRangeThreaded(auto &&...args): MountainRange(args...), // https://tinyurl.com/byusc-parpack + // This constructor inherits the constructors from MountainRange and adds the following initializations. + MountainRangeThreaded(auto &&...args): MountainRange(args...), // https://tinyurl.com/byusc-parpack // &&..args is a fancy way of saying what ever you pass to MountainRangeThreaded gets passed to MountainRange continue_iteration{true}, nthreads{[]{ // https://tinyurl.com/byusc-lambdai size_type nthreads = 1; - auto nthreads_str = std::getenv("SOLVER_NUM_THREADS"); - if (nthreads_str != nullptr) std::from_chars(nthreads_str, nthreads_str+std::strlen(nthreads_str), nthreads); + auto nthreads_str = std::getenv("SOLVER_NUM_THREADS"); // Get the environment variable + if (nthreads_str != nullptr) std::from_chars(nthreads_str, nthreads_str+std::strlen(nthreads_str), nthreads); // If the environment exists, set nthreads to the value fo the variable. return nthreads; - }()}, - ds_barrier(nthreads+1), // worker threads plus main thread + }()}, // initialize the lambda function and then immediately call it. + ds_barrier(nthreads+1), // worker threads plus main thread // initialize the barrier with 8 worker threads plus the main/parent thread. step_barrier(nthreads+1), // worker threads plus main thread ds_workers(looping_threadpool(nthreads, [this](auto tid){ // https://tinyurl.com/byusc-lambda - ds_barrier.arrive_and_wait(); - if (!continue_iteration) return false; - auto [first, last] = this_thread_cell_range(tid); - auto gfirst = tid==0 ? 1 : first; - auto glast = tid==nthreads-1 ? last-1 : last; - value_type ds_local = 0; - for (size_t i=gfirst; i