Files
panic/include/config/omp.hpp
T
2026-06-23 15:07:55 +02:00

188 lines
6.7 KiB
C++

/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* PANIC
* Portable Algorithms and Numerics In C++
*
* Scientific computing from scratch, with feeling.
*
* Copyright (c) 2026 Michelle Bausager
*
* This file is part of PANIC.
*
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
* You may redistribute and/or modify it under the terms of the GPL.
*
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the LICENSE file for the full license text.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* Project Name: PANIC
* Module Name: config
* File Name: omp.hpp
* Revision: 0.1.0
* Date: 20-06-2026
* Author: Michelle Bausager
*
* Description:
* Defines which types will be used in the rest of the libraries.
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
// None.
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// OPENMP CONFIGURATION DEFAULTS
//---------------------------------------------------------------------------------------------------------------------------
// If CMake did not define PANIC_USE_OPENMP, define it as 0.
// This makes OpenMP disabled by default.
//
// CMake can override this with:
// PANIC_USE_OPENMP=1
#ifndef PANIC_USE_OPENMP
#define PANIC_USE_OPENMP 0
#endif
// If CMake did not define PANIC_OMP_NUM_THREADS, define it as 0.
//
// Meaning:
// 0 = let OpenMP choose the default number of threads
// 1 = use 1 thread
// 2 = use 2 threads
// 4 = use 4 threads
// etc.
#ifndef PANIC_OMP_NUM_THREADS
#define PANIC_OMP_NUM_THREADS 0
#endif
//---------------------------------------------------------------------------------------------------------------------------
// OPENMP AVAILABILITY CHECK
//---------------------------------------------------------------------------------------------------------------------------
// PANIC_HAS_OPENMP becomes 1 only if:
//
// 1. PANIC_USE_OPENMP is enabled by CMake
// 2. The compiler actually has OpenMP enabled
//
// _OPENMP is automatically defined by the compiler when OpenMP support is active.
// For example, GCC defines _OPENMP when compiling with -fopenmp.
#if PANIC_USE_OPENMP && defined(_OPENMP)
#define PANIC_HAS_OPENMP 1
#else
#define PANIC_HAS_OPENMP 0
#endif
//---------------------------------------------------------------------------------------------------------------------------
// STRINGIFY HELPERS
//---------------------------------------------------------------------------------------------------------------------------
// These macros turn macro values into text.
//
// Example:
// PANIC_OMP_NUM_THREADS = 4
//
// We need to turn that into:
//
// "omp parallel for num_threads(4)"
//
// The two-step version is needed so that macros expand before becoming text.
#define PANIC_STRINGIFY_DETAIL(x) #x
#define PANIC_STRINGIFY(x) PANIC_STRINGIFY_DETAIL(x)
//---------------------------------------------------------------------------------------------------------------------------
// OPENMP PARALLEL FOR MACROS
//---------------------------------------------------------------------------------------------------------------------------
#if PANIC_HAS_OPENMP
#if PANIC_OMP_NUM_THREADS > 0
// Expands to:
// #pragma omp parallel for num_threads(PANIC_OMP_NUM_THREADS)
#define PANIC_OMP_PARALLEL_FOR \
_Pragma(PANIC_STRINGIFY(omp parallel for num_threads(PANIC_OMP_NUM_THREADS)))
// Expands to:
// #pragma omp parallel for if(condition) num_threads(PANIC_OMP_NUM_THREADS)
//
// The loop only runs in parallel if condition is true.
#define PANIC_OMP_PARALLEL_FOR_IF(condition) \
_Pragma(PANIC_STRINGIFY(omp parallel for if(condition) num_threads(PANIC_OMP_NUM_THREADS)))
#else
// Expands to:
// #pragma omp parallel for
//
// OpenMP chooses the default number of threads.
#define PANIC_OMP_PARALLEL_FOR \
_Pragma("omp parallel for")
// Expands to:
// #pragma omp parallel for if(condition)
//
// OpenMP chooses the default number of threads.
// The loop only runs in parallel if condition is true.
#define PANIC_OMP_PARALLEL_FOR_IF(condition) \
_Pragma(PANIC_STRINGIFY(omp parallel for if(condition)))
#endif
#else
// If OpenMP is disabled or unavailable, these macros expand to nothing.
//
// So this:
//
// PANIC_OMP_PARALLEL_FOR
// for (...) {
// ...
// }
//
// becomes just:
//
// for (...) {
// ...
// }
//
// That means the same code still works on microcontrollers and non-OpenMP builds.
#define PANIC_OMP_PARALLEL_FOR
#define PANIC_OMP_PARALLEL_FOR_IF(condition)
#endif
//---------------------------------------------------------------------------------------------------------------------------
// TYPE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
// None.
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
// None.
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
// None.
//PANIC_OMP_PARALLEL_FOR
//for (panic::uint_t i = 0; i < length; ++i) {
// data[i] = value;
//}