Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Preprocessor Directives
2.1.
Macros
2.2.
C++ Standard Features
2.3.
Assertions
2.4.
Alignment, Vectorization, and Performance Tweaking
2.5.
Plugins
2.6.
Macros for Eigen Developers
3.
Frequently Asked Questions
3.1.
Is Eigen a good library?
3.2.
Does Eigen use BLAS?
3.3.
Does Tensorflow use Eigen?
3.4.
What are the common operations that can be performed using the Eigen Library?
3.5.
Is Eigen open source?
4.
Conclusion
Last Updated: Mar 27, 2024

Preprocessor Tokens Recognized by Eigen

Author Sagar Mishra
0 upvote

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? 

Preprocessor tokens recognized by Eigen

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

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

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

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

Frequently Asked Questions

Is Eigen a good library?

Eigen is versatile. It supports all matrix sizes, including sparse matrices, small fixed-size matrices, and arbitrary huge dense matrices.

Does Eigen use BLAS?

When calling BLAS routines, some of Eigen's algorithms are quietly replaced. Only objects with one of the four following standard scalar types—float, double, complex<float>, and complex<double> - or objects that are large enough are eligible for these swaps.

Does Tensorflow use Eigen?

The Tensor module of Eigen is used by Tensorflow, which the primary author of TensorFlow mostly maintains.

What are the common operations that can be performed using the Eigen Library?

Functions such as the declaration of vectors and matrices can be performed using the Eigen Library. Some math operations, like solving vectors, dot product, and cross product, can be performed using Eigen's Library.

Is Eigen open source?

Since version 3.1, Eigen has been open-source software covered by Mozilla Public License 2.0. The GNU Lesser General Public License applied to earlier iterations. Version 1.0 became available in December 2006.

Conclusion

This article discusses the topic of the preprocessor Tokens Recognized by Eigen. In detail, we have seen the Eigen, Preprocessor Directives, Macros, C++ Standard Features, and Assertions. Along with this, we also discussed Plugins and Macros for Eigen developers.

We hope this blog has helped you enhance your knowledge of Preprocessor Tokens Recognized by Eigen. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass