Introduction
Hey Ninjas! You must have heard about the language C++. It comes with a massive list of libraries that make your coding simpler to execute. So have you ever thought about which library is perfect for maths calculations like matrix and algebra?
Yes, Eigen is the library used for such math calculations. Today we will discuss one of the Eigen functions, i.e., Preprocessor Tokens Recognized by Eigen. Let's start without wasting our time.
Preprocessor Directives
By using #define to define the preprocessor tokens, you can regulate some features of Eigen. It is best to describe these macros before including any Eigen headers. They are often best defined in the project options.
Macros
Our next topic in the "Preprocessor Tokens Recognized by Eigen" series is Macros.
A macro is a section of code that software replaces with its value. The #define directive defines a macro. When the compiler comes across a macro name, it substitutes the name with the macro definition.
There are both pros and cons to using Macros. Macros break the Application programming interface (API), which can be both helpful and harmful. If your program runs in two parts, then your program may fail to link or exhibit subtle bugs. Still, if you know the proper use of Macros, you can use this better.
There are some features in Macros discussed below:
- EIGEN2_SUPPORT
- EIGEN_DEFAULT_DENSE_INDEX_TYPE
- EIGEN_DEFAULT_IO_FORMAT
- EIGEN_INITIALIZE_MATRICES_BY_ZERO
- EIGEN_INITIALIZE_MATRICES_BY_NAN
- EIGEN_NO_AUTOMATIC_RESIZING
C++ Standard Features
Our next topic in the "Preprocessor Tokens Recognized by Eigen" series is C++ Standard Features.
Based on the data provided by the compiler, Eigen attempts to automatically recognize and enable language features at compile time by default. Some features of C++ are given below.
-
EIGEN_MAX_CPP_VER: It stops users from using C++ features that need a version higher than EIGEN_MAX_CPP_VER.
-
EIGEN_HAS_C99_MATH: It regulates the use of C99 math functions like lgamma, erf, and erfc.
-
EIGEN_HAS_CXX11_MATH: It regulates how some functions, like round, logp1, isinf, and isnan, are handled.
-
EIGEN_HAS_STD_RESULT_OF: It specifies the support for std::result of.
- EIGEN_NO_IO: It disables all support and usage of <iostreams>.
Assertions
Our next topic in the "Preprocessor Tokens Recognized by Eigen" series is Assertions.
Assertions are analyses used to test beliefs made by coders. The Eigen library has a lot of assertions to avoid errors in the code both during compilation and during runtime. These claims can yet be turned off as they take time.
-
EIGEN_NO_DEBUG: This feature can disable Eigen's assertions if defined.
-
EIGEN_NO_STATIC_ASSERT: Runtime assertions are used in place of static compile-time assertions if they are defined. This saves compilation time. Also, it is not defined by default.
-
eigen_assert: The primary purpose of using assert is to abort the program if the assertion is violated. You can avoid this by throwing an exception.
- EIGEN_MPL2_ONLY: It disables the features which are still under the LGPL.
Alignment, Vectorization, and Performance Tweaking
Our next topic in the "Preprocessor Tokens Recognized by Eigen" series is Alignment, Vectorization, and Performance Tweaking.
-
EIGEN_MALLOC_ALREADY_ALIGNED: It can be set to 0 or 1 to detect whether the default system malloc already returns aligned buffers. This data is automatically derived from the compiler and system preprocessor tokens if it is not defined.
-
EIGEN_MAX_ALIGN_BYTES: It must be a power of two or 0.
-
EIGEN_MAX_STATIC_ALIGN_BYTES: It is similar to EIGEN_MAX_ALIGN_BYTES but for statically allocated data only.
-
EIGEN_DONT_PARALLELIZE: It is used to disable multi-threading. You can only use this when the OpenMP is enabled.
-
EIGEN_DONT_VECTORIZE: It is used to disable explicit vectorization when defined.
-
EIGEN_UNALIGNED_VECTORIZE: It turns on/off vectorization with unaligned stores.
-
EIGEN_FAST_MATH: It enables some optimizations that might affect the result's accuracy.
-
EIGEN_UNROLLING_LIMIT: It defines the size of a loop to enable meta unrolling. You can turn it off or disable it by setting it to 0.
-
EIGEN_ALTIVEC_USE_CUSTOM_PACK: It regulates the use of Eigen's custom packing for Altivec.
- EIGEN_DONT_ALIGN: It disables alignment entirely.
Plugins
Our next topic in the "Preprocessor Tokens Recognized by Eigen" series is Plugins.
Plugins, also known as add-ons or extensions, are software that adds new functions to a host program without changing the host program itself. By creating a plugin, it is possible to extend several core Eigen classes with new functions.
Below is the list of plugins supported by Eigen.
-
EIGEN_ARRAY_PLUGIN: It is used as the plugin's filename for extending the Array class.
-
EIGEN_CWISE_PLUGIN: It is used as the plugin's filename for extending the Cwise class.
-
EIGEN_DENSEBASE_PLUGIN: It is used as the plugin's filename for extending the DenseBase class.
-
EIGEN_FUNCTORS_PLUGIN: It is used as the plugin's filename for adding new functors and specializations of functor_traits.
-
EIGEN_MATRIX_PLUGIN: It is used as the plugin's filename for extending the Matrix class.
-
EIGEN_QUATERNION_PLUGIN: It is used as the plugin's filename for extending the Quaternion class.
-
EIGEN_SPARSEMATRIX_PLUGIN: It is used as the plugin's filename for extending the SparseMatrix class.
- EIGEN_TRANSFORM_PLUGIN: It is used as the plugin's filename for extending the SparseMatrix class.
Macros for Eigen Developers
Our last topic in the "Preprocessor Tokens Recognized by Eigen" series is Macros for Eigen developers.
These macros are primarily designed for Eigen developers and testers. They shouldn't be used by real-world code, even if they might be helpful for power users and the curious for debugging and testing purposes.
-
EIGEN_DEFAULT_TO_ROW_MAJOR: When defined, matrices' default storage order switches from column-major to row-major. Not by default defined.
-
EIGEN_INTERNAL_DEBUGGING: When defined, assertions are allowed in Eigen's internal rules. For Eigen's own debugging, this is helpful. Not by default defined.
-
EIGEN_NO_MALLOC: If defined, an assertion fails whenever an internal request to allocate memory from the heap is made. This is useful for ensuring that a routine is not dynamically allocating memory. Not by default defined.
- EIGEN_RUNTIME_NO_MALLOC: Upon definition, a new switch is created that can be enabled and disabled by using "set is malloc allowed(bool)." An assertion fails if Eigen tries to allocate memory dynamically even when malloc is not permitted. Not by default defined.
Also see, Application of Oops