Optimize C++ to use for computational physics?

In summary: This goes into the programming realm, so you have to improve your programming skills (in general and in C++ that you use particularly), in order to write your programs in a more elegant and concise way. A good textbook on C++ and a lot of patience - as C++ is not a toy but has extreme capabilities, should do fine. Along the way, you will find good libraries that you can incorporate into your code, in the more efficient way. Although it's ultimately a hard thing to achieve, it can be done.Alternatively, if it's not mandatory for you to work your way through C++, you can use some mathematical package like Mathematica or Matlab. There are very decent counterparts in
  • #1
AHashemi
25
0
Hi
I'm studying 2nd year for a bachelor's degree in physics and I'm interested in programming (Beginner C++ programmer). I have already done simulations like harmonic motions, Ising model etc but I know this can go further and I need help with that. I use Ubuntu for programming with c++. (GCC compiler)

1- So far I just have used GNUPLOT for drawing plots and exporting as .xpm pictures and joining them to make .gif files to demonstrate models like Ising. I searched for a great amount of time and I found out there are libraries like Qt, Octave, SDL,... but honestly I couldn't use any of them. Seems like they are suited for game programming and I need something more simple.

2- What libraries can I use for more advanced mathematical functions like determinants, inverse matrices, integral calculations, solving ODEs etc so I don't have to write my own functions each time and I can simply use something like float a = det(B).

3- What else should I do to optimize my programming for computational physics? I'm taking courses in computational physics soon and already started to read books.

Thanks.
 
Technology news on Phys.org
  • #2
AHashemi said:
2- What libraries can I use for more advanced mathematical functions like determinants, inverse matrices, integral calculations, solving ODEs etc so I don't have to write my own functions each time and I can simply use something like float a = det(B).
Have a look at GSL https://www.gnu.org/software/gsl/

AHashemi said:
3- What else should I do to optimize my programming for computational physics? I'm taking courses in computational physics soon and already started to read books.
What do you mean by optimizing? Making your program run faster?
 
  • Like
Likes AHashemi
  • #3
DrClaude said:

Thanks! Looks like just what I want.

DrClaude said:
What do you mean by optimizing? Making your program run faster?

Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
In other words I don't want to start from inventing wheel and fire. :grin:
 
  • #4
AHashemi said:
Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
In other words I don't want to start from inventing wheel and fire. :grin:
Then there is no simple answer that can be given in a post. You'll have to pick up a good book on computation physics or numerical methods.
 
  • Like
Likes AHashemi
  • #5
AHashemi said:
Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
In other words I don't want to start from inventing wheel and fire.

This goes into the programming realm, so you have to improve your programming skills (in general and in C++ that you use particularly), in order to write your programs in a more elegant and concise way. A good textbook on C++ and a lot of patience - as C++ is not a toy but has extreme capabilities, should do fine. Along the way, you will find good libraries that you can incorporate into your code, in the more efficient way. Although it's ultimately a hard thing to achieve, it can be done.

Alternatively, if it's not mandatory for you to work your way through C++, you can use some mathematical package like Mathematica or Matlab. There are very decent counterparts in the open source world - like Octave, too.
 
  • Like
Likes AHashemi
  • #6
AHashemi said:
Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
In other words I don't want to start from inventing wheel and fire. :grin:
That'll come with experience. You just have to know what tools you need and that's not taught in school. Not only are there usually good tools for what you need, but there are often many of them. You'll end up with a list of tools that you can use for any given task and be able to identify the best one, or (sometimes) you'll have to write your own.
 
  • #7
For advanced visualization you have visit ( https://wci.llnl.gov/simulation/computer-codes/visit ), paraview ( http://www.paraview.org ), vtk ( http://www.vtk.org ). For simple plotting you can just use python matplotlib ( http://matplotlib.org ), matlab, mathematica, idl, octave, R, etc.

for high performance you might want to use GPU acceleration through something like Cuda or OpenCL, or just a library which exploits it for you. You basically have many options https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Implementations
https://en.wikipedia.org/wiki/List_of_numerical_libraries#C.2B.2B

You also have GNU or Boost Multi-precision libraries for the case where you need more precision than double or long double. Although I understand that methodology is even more important than datatype (you'll need a lot of knowledge about numerical methods.

Since C++ is tedious to program, you will want to use a scripting language sometimes when you need compute some quick result.
 
Last edited:
  • Like
Likes aaroman and fluidistic
  • #8
You'll also probably learn that long calculations you will tend to do in two parts: baking and rendering.

Baking does all of the calculations and stores the results. You usually try not to render this at the same time since rendering itself is slow. You want to make sure that the end result is correct before committing all of that horsepower drawing.
 
  • Like
Likes FactChecker
  • #9
newjerseyrunner said:
You'll also probably learn that long calculations you will tend to do in two parts: baking and rendering.

Baking does all of the calculations and stores the results. You usually try not to render this at the same time since rendering itself is slow. You want to make sure that the end result is correct before committing all of that horsepower drawing.
I completely agree. One process does the physics calculations and dumps accurate, detailed data into files (or puts the data on a network if the process is "real-time"). Then you can have a multitude of possibilities for post-processing, analyzing, and displaying.
 
  • Like
Likes newjerseyrunner
  • #10
AHashemi said:
Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
In other words I don't want to start from inventing wheel and fire. :grin:
Since you are a beginner, the best you can do is to rely on libraries (made by experts) and slowly improve your coding skills / algorithms.
But you are actually doing that, so it's already OK. Just don't be impatient. At this point any 'advanced' method usually related with 'performance optimization' would be most likely just waste of time.
 
  • #11
AHashemi said:
1- So far I just have used GNUPLOT for drawing plots and exporting as .xpm pictures and joining them to make .gif files to demonstrate models like Ising. I searched for a great amount of time and I found out there are libraries like Qt, Octave, SDL,... but honestly I couldn't use any of them. Seems like they are suited for game programming and I need something more simple.

2- What libraries can I use for more advanced mathematical functions like determinants, inverse matrices, integral calculations, solving ODEs etc so I don't have to write my own functions each time and I can simply use something like float a = det(B).
Using a linear algebra library like armadillo could be a good start (uses lapack). Then you also have the nice feature of various prebuilt matrix structures and classes. Although it is fun to play with the nitty-gritty low level stuff yourself as well.
3- What else should I do to optimize my programming for computational physics? I'm taking courses in computational physics soon and already started to read books.
One handy tips is to use optimization levels during compilation (O3 option for example). Another tips is to try and implement different solutions and time them, what you think is intuitively fastest isn't always.
 
  • #12
You can optimize certain common functions to provide faster estimates.

For example, I often create an array of 360 floats and precalculate sin and cos functions. A lookup in ram is much faster than actually doing a sinf() and for my purposes, being off by a half a degree doesn't matter.

There is also tricks like this well known example
Code:
float Q_rsqrt( float number )
{
   long i;
   float x2, y;
   const float threehalfs = 1.5F;

   x2 = number * 0.5F;
   y  = number;
   i  = * ( long * ) &y;                       // evil floating point bit level hacking
   i  = 0x5f3759df - ( i >> 1 );               // what the ****?
   y  = * ( float * ) &i;
   y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//    y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

   return y;
}
Which approximates an inverse square root. (Everyone I know leaves Carmack's original comments in there too.

This is code that gets used by me a LOT. Any time I need to determine distance.
Code:
static float fastnorm(float diffx, float diffy){
        //need absolute values for distance measurement
        diffx = fabs(diffx); diffy = fabs(diffy);
        //There are two simple approximations to sqrt(x^2 + y^2):
        //  max(x, y) -> good for x >> y or x << y          (square-shaped unit circle)
        //  1/sqrt(2) * (x + y) -> good for x = y (approx.) (diamond-shaped unit circle)
        //The worse approximation is always bigger. By taking the maximum of both,
        //we get an octagon-shaped upper approximation of the unit circle. The
        //approximation can be refined by a prefactor (called a) below.
        static const float a = (1 + sqrt(4 - 2 * sqrt(2))) / 2;
        static const float b = sqrt(0.5);
        const float metricOne = diffx > diffy ? diffx : diffy;
        const float metricTwo = b * (diffx + diffy);
        const float bigger = metricOne > metricTwo ? metricOne : metricTwo;
        return a * bigger;
    }

    inline static float regnorm(float diffx, float diffy){
        return sqrt(diffx * diffx + diffy * diffy);
    }
 
Last edited:
  • #13
Use libraries that are developed by people dedicated to producing the best code for some goal they/you are interested in.
Much of that is open source, and one thing that C++ is really good at is incorporating external functions into C++ objects.
Inventing a better wheel is pointless, when you already have very good wheels.
 
  • Like
Likes DrClaude
  • #14
I don't know, I think sometimes it's good to write your own functions because it allows you to get a feeling about how the algorithm works [and so the limitations of the algorithm]. If you did computational physics you might have seen such examples in the integration parts...
Of course, writing a code more than once is retarded (not optimal), and for that reason you can always write the classes that contain those methods. That way you will also be able to determine how optimal your code is, in terms of time performance; by e.g. comparing to an existing code that does the same thing [or even sit over your code and think how you can make it run faster]... So for example the "divide and conquer" algorithmic thinking comes after you realize that just looping over all your input is causing extra, unnecessary calculations... also is the speed the only thing you are looking for? or precision as well? those two are the two opposite sides most of the time [at least for my experience]... all these are helpful for you in the beginning to build up your algorithmic thinking [afterall as a physicist you'll end up spending more time writing a code than expecting the results]...
Also C++ is more like a DIY project... It's like you need a chair, and C++ comes and gives you the raw material and tools you need for it, but you should make it yourself... in the end you'll end up with a chair that is optimal for you, since you know exactly how you wanted it to be... other languages provide better solutions [eg giving you a bunch of chairs they have siting around]. It's a point where python owns C++ [although I'm subjective and think it owns it in all points].
 
  • #15
Learning how to properly use tools like gprof and valgrind are probably the most important when it comes to optimizing your code. They show you exactly where the slow parts are.
 
  • #16
newjerseyrunner said:
Learning how to properly use tools like gprof and valgrind are probably the most important when it comes to optimizing your code. They show you exactly where the slow parts are.
At beginner phase the gain from the low level performance optimization is usually lower than from the algorithm level.
Save these for the time when the coding skills are already established.
No amount of gprof can save a weak algorithm.
 
  • Like
Likes aaroman and newjerseyrunner
  • #17
ARRRRGGGGGG

Please listen to me. I work for years in computer optimisation. Use Eigen3 for matrix computing, it is a ultra optimised library but with an high level of programming due to it's meta-template building. Everyone in intensive computing use MKL, direct Blas on Eigen. Eigen is best for simplicity and performance.

For C++ Optimisation you need to check the flag (-O3) of G++, and if you want you can use intel compiler to save 10% more and profile you executable.
 
  • Like
Likes aaroman
  • #18
this can go further and I need help with that
I know this reply comes quite late, but maybe it's still useful. I have a computational physics blog, here: https://compphys.go.ro
I'm using among other libraries, Eigen (which was mentioned above) for matrix-related things, VTK for visualization (but also some code written by me), FFTW for Fourier Transforms, to name a few.

Most of the posts come with a C++ project attached (the repositories are here: https://github.com/aromanro?tab=repositories ) but there is also some JavaScript code in there for the easier ones. For some of the projects I write prototypes with some scripting language (like Octave or python) but I do not put that code online.
 
  • Like
Likes Marc Rindermann

FAQ: Optimize C++ to use for computational physics?

1. How can I improve the performance of my C++ code for computational physics?

The best way to optimize C++ for computational physics is to use efficient algorithms and data structures. This includes minimizing the use of dynamic memory allocation, using appropriate data types, and avoiding unnecessary function calls. Additionally, utilizing multithreading and parallel processing techniques can greatly improve performance.

2. Is there a specific compiler that is best for optimizing C++ for computational physics?

There is no one "best" compiler for optimizing C++ for computational physics as different compilers may have different strengths and weaknesses. It is recommended to do some research and try out different compilers to see which one works best for your specific code.

3. Can I optimize my C++ code for computational physics without sacrificing readability?

Yes, it is possible to optimize your C++ code without sacrificing readability. By following good coding practices and properly structuring your code, you can improve performance without making the code overly complex or difficult to understand.

4. How can I ensure my optimized C++ code is still accurate for my computational physics calculations?

When optimizing your C++ code, it is important to test and validate the results to ensure accuracy. This can be done by comparing the results with known solutions or using test cases with known outcomes. It is also helpful to use debugging tools to identify and fix any errors that may arise during the optimization process.

5. Are there any resources or tools available to help optimize C++ for computational physics?

Yes, there are many resources and tools available to help optimize C++ for computational physics, such as profiling tools and libraries that are specifically designed for scientific computing. It is also helpful to consult with experienced programmers or join online communities to get advice and tips on optimizing C++ for computational physics.

Similar threads

Back
Top