First omp
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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;
|
||||
//}
|
||||
@@ -54,10 +54,12 @@ namespace io{
|
||||
// Function Name : panic::io::print
|
||||
//
|
||||
// Description:
|
||||
// Prints a panic::tensor::vector out in the terminal
|
||||
// Prints a vector out in the terminal
|
||||
//
|
||||
// Inputs:
|
||||
// v const panic::tensor::vector&
|
||||
// v const panic::tensor::uint_vector&
|
||||
// const panic::tensor::int_vector&
|
||||
// const panic::tensor::real_vector&
|
||||
// vector to print
|
||||
//
|
||||
// Outputs:
|
||||
@@ -69,7 +71,9 @@ namespace io{
|
||||
// Notes:
|
||||
// The vector is printed out in square brackets.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
void print(const panic::tensor::vector& v);
|
||||
void print(const panic::tensor::uint_vector& v);
|
||||
void print(const panic::tensor::int_vector& v);
|
||||
void print(const panic::tensor::real_vector& v);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -66,9 +66,10 @@
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
template <typename T>
|
||||
struct vector{
|
||||
panic::uint_t length;
|
||||
panic::real_t* data;
|
||||
T* data;
|
||||
|
||||
// empty contructor
|
||||
vector();
|
||||
@@ -77,7 +78,7 @@ struct vector{
|
||||
vector(panic::uint_t size);
|
||||
|
||||
// contructor with size allocation and sets it all to a value
|
||||
vector(panic::uint_t size, panic::real_t value);
|
||||
vector(panic::uint_t size, T value);
|
||||
|
||||
// copy-contructor
|
||||
// vector a(3);
|
||||
@@ -101,22 +102,40 @@ struct vector{
|
||||
bool resize(panic::uint_t new_size);
|
||||
|
||||
// fill data with value
|
||||
bool fill(panic::real_t value);
|
||||
bool fill(T value);
|
||||
|
||||
// lets you read and write v[index]
|
||||
panic::real_t& operator[](panic::uint_t index);
|
||||
T& operator[](panic::uint_t index);
|
||||
|
||||
// lets you read v[index] from a const vector
|
||||
const panic::real_t& operator[](panic::uint_t index) const;
|
||||
const T& operator[](panic::uint_t index) const;
|
||||
|
||||
// lets you read and write v.at(index)
|
||||
panic::real_t& at(panic::uint_t index);
|
||||
T& at(panic::uint_t index);
|
||||
|
||||
// lets you read v.at(index) from a const vector
|
||||
const panic::real_t& at(panic::uint_t index) const;
|
||||
const T& at(panic::uint_t index) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE ALIASES
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
typedef vector<panic::real_t> real_vector;
|
||||
typedef vector<panic::int_t> int_vector;
|
||||
typedef vector<panic::uint_t> uint_vector;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE DECLARATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
extern template struct vector<panic::real_t>;
|
||||
extern template struct vector<panic::int_t>;
|
||||
extern template struct vector<panic::uint_t>;
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
|
||||
Reference in New Issue
Block a user