LCOV - code coverage report
Current view: top level - dtwc - parallelisation.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 9 100.0 %
Date: 2024-09-07 20:53:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file parallelisation.hpp
       3             :  * @brief Header for parallelisation functions.
       4             :  *
       5             :  * @details This header file provides functionalities for parallelising tasks using standard parallelisation.
       6             :  * It includes functions for running individual tasks in parallel and adjusting
       7             :  * the level of parallelism. Functions are templated to support various task types.
       8             :  *
       9             :  * @date 15 Dec 2021
      10             :  * @author Volkan Kumtepeli
      11             :  * @author Becky Perriment
      12             :  */
      13             : 
      14             : #pragma once
      15             : 
      16             : #include "settings.hpp"
      17             : #include "types/Range.hpp"
      18             : 
      19             : #include <cstddef>
      20             : #include <omp.h>
      21             : 
      22             : namespace dtwc {
      23             : 
      24             : /**
      25             :  * @brief Runs a given task in parallel using OpenMP.
      26             :  *
      27             :  * This function executes the provided task in parallel, leveraging OpenMP's dynamic scheduling.
      28             :  * The dynamic scheduling is advantageous for tasks with varying completion times. It allows for
      29             :  * better load balancing across threads.
      30             :  *
      31             :  * @tparam Tfun The type of the task function.
      32             :  * @param task_indv Reference to the task function to be executed.
      33             :  * @param i_end The upper bound of the loop index.
      34             :  * @param isParallel Flag to enable/disable parallel execution (default is true).
      35             :  */
      36             : template <typename Tfun>
      37           6 : void run_openmp(Tfun &task_indv, size_t i_end, bool isParallel = true)
      38             : {
      39           6 :   if (isParallel) {
      40           4 : #pragma omp parallel for schedule(dynamic) // As some take less time static scheduling is 2x slower.
      41             :     for (int i = 0; i < i_end; i++)
      42             :       task_indv(i);
      43             :   } else
      44         202 :     for (int i = 0; i < i_end; i++)
      45         200 :       task_indv(i);
      46           6 : }
      47             : 
      48             : /**
      49             :  * @brief A wrapper function to control the degree of parallelism in task execution.
      50             :  *
      51             :  * @details This function provides a higher level of control for parallel task execution.
      52             :  * It decides whether to use parallelism based on the provided maximum number of parallel workers.
      53             :  * It delegates the task execution to 'run_openmp'.
      54             :  *
      55             :  * @tparam Tfun The type of the task function.
      56             :  * @param task_indv Reference to the task function to be executed.
      57             :  * @param i_end The upper bound of the loop index.
      58             :  * @param numMaxParallelWorkers The maximum number of parallel workers (default is 32).
      59             :  */
      60             : template <typename Tfun>
      61           2 : void run(Tfun &task_indv, size_t i_end, size_t numMaxParallelWorkers = 32)
      62             : {
      63           2 :   run_openmp(task_indv, i_end, numMaxParallelWorkers != 1);
      64           2 : }
      65             : } // namespace dtwc

Generated by: LCOV version 1.14