diff --git a/src/MountainRangeThreaded.hpp b/src/MountainRangeThreaded.hpp index e8dd4fe..72ba733 100644 --- a/src/MountainRangeThreaded.hpp +++ b/src/MountainRangeThreaded.hpp @@ -15,33 +15,36 @@ 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 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. } @@ -49,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