CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

Best coding practice for c++ solvers

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By mprinkey

Reply
 
LinkBack Thread Tools Display Modes
Old   February 23, 2015, 12:49
Default Best coding practice for c++ solvers
  #1
Senior Member
 
Joachim
Join Date: Mar 2012
Location: Atlanta
Posts: 140
Rep Power: 6
Joachim is on a distinguished road
Hi everyone,

so far, I have been working mostly with Fortran for my CFD codes, and I would like to switch to c++. I already know the language (pointers, classes, etc), but I don't have a lot of experience developing large c++ codes.

Could you please recommend me some textbooks/tutorials/etc that detail good coding practice? I have seen a few books/lectures which focused on robustness, with lots of checks all the time. This looks really good, but isn't that slowing down the code a whole lot? I am basically looking for a book that would explain how to develop an elegant and fast code (speed similar to Fortran if possible).

Thank you very much for your help!

Joachim
Joachim is offline   Reply With Quote

Old   February 25, 2015, 05:24
Default
  #2
Senior Member
 
N/A
Join Date: Mar 2009
Posts: 188
Rep Power: 8
harishg is on a distinguished road
Theoretically, you can write a fast C++ code or slow Fortran code. You can also wrap c++ around your Fortran code like overture does.

I do not think that any book is a right place to learn good coding practice. Also, the comments will differ from book to book and person to person.

Is there a specific reason to switch to C++? Programming languages are not hard to pick-up. However, if you do not use them for a long time, you will end up forgetting the syntax and would need a reference. Best to stick to the language that you know and switch only if you need to.
harishg is offline   Reply With Quote

Old   February 25, 2015, 11:04
Default
  #3
Senior Member
 
Joachim
Join Date: Mar 2012
Location: Atlanta
Posts: 140
Rep Power: 6
Joachim is on a distinguished road
Well, actually I am getting closer to my graduation date, and I know that most companies use C++ rather than Fortran. I will stick to Fortran for my thesis, but I would like to be as ready as possible with C++ when I start looking for jobs.
Joachim is offline   Reply With Quote

Old   February 25, 2015, 11:16
Default
  #4
Senior Member
 
N/A
Join Date: Mar 2009
Posts: 188
Rep Power: 8
harishg is on a distinguished road
They look more into your work and less into programming skills. Many of my colleagues got into positions without writing a single c++ code.

Good luck.
harishg is offline   Reply With Quote

Old   February 26, 2015, 16:11
Default Mit ocw
  #5
New Member
 
Ankit Rohatgi
Join Date: Sep 2010
Posts: 4
Rep Power: 6
ankitrohatgi is on a distinguished road
You can always read these notes before going to any C++ specific interview: http://ocw.mit.edu/courses/electrica...lecture-notes/
ankitrohatgi is offline   Reply With Quote

Old   February 26, 2015, 23:56
Default
  #6
Senior Member
 
Michael Prinkey
Join Date: Mar 2009
Location: Pittsburgh PA
Posts: 121
Rep Power: 12
mprinkey will become famous soon enough
There are plenty of general style guides. Google's is pretty good:

http://google-styleguide.googlecode..../cppguide.html

That may give you some idea of what large codes use. It is a place to start. In reality, if you get a job writing CFD code, your employer will likely hand you a style guide before they give you repository access.

While CFD has to serve similar goals as general software (maintainability, extensibility, etc), performance for CFD is far more critical than it is for most general applications. I've written a lot of code in FORTRAN, C, and C++. There are performance pitfalls in every language. Some have a similar root (poor memory ordering, conditionals inside inner loops, etc.). Some are unique to a given language. C++ is a rich and flexible language--look at the OpenFOAM solver codes if you need any convincing of that. But, at the same time, the abstraction and elegance can make code difficult to understand and performance optimization difficult, both for you and for the compiler.

For performance, I tend to write C code, even in C++. I am wary of making use of more advanced features without careful benchmarking. This is important for scalar code, but it is absolutely critical for threaded and vectorized code. Extracting good performance from current and future processors will require OpenMP and SIMD code, because individual cores are not getting much faster--they are just getting more numerous and sprouting wider vector pipelines. See the Intel MIC processors or Nvidia Kepler as good examples. Whatever language you use and whatever coding style you apply needs to take threading and vector operations into primary consideration. They will drive your data structures and your coding style.

There are lots of "High Performance C++" webpages and books, but, honestly, you have to feel your way. I used to avoid using operator overloading in lieu of C-style macros similar to what FLUENT uses:
Code:
#define V_V(A,op,B) do{A[0] op B[0]; A[1] op B[1]; A[2] op B[2];}while(0)
to do basic vector operations. I did that for a good reason. Over the years, I tried to make use of operator overloading for vector operations only to find that the resulting code was much slower than macro'ed or hand-coded C. But with recent compilers, I've found that I can use inline functions and operator overloading to get similar (and even better) performance with something more elegant.
Code:
class vector
{
    private:
    scalar xComp,yComp,zComp;
    public:
    //......lots of other code ....
    inline vector operator+ (const vector A)    // V+A
    {
        return vector(xComp+A.xComp,yComp+A.yComp,zComp+A.zComp);
    }
    inline vector operator- (const vector A)    // V-A
    {
        return vector(xComp-A.xComp,yComp-A.yComp,zComp-A.zComp);
    }
    inline vector operator+= (const vector A)   // V+=A
    {
        xComp+=A.xComp;
        yComp+=A.yComp;
        zComp+=A.zComp;
        return *this;
    }
    inline vector operator-= (const vector A)   // V-=A
    {
        xComp-=A.xComp;
        yComp-=A.yComp;
        zComp-=A.zComp;
        return *this;
    }
}
This allows me to get the cleaner vector notation without compromising performance--that same code 10 years ago ran much slower. You can also use code like this to build chunks of objects to feed to vector operations (AVX/AVX2). It will be code like this that may allow your code to make full use of future CPU features without littering the entire codebase with #pragmas.

I don't know if this is helpful, except to serve as a warning. It is possible to dream up many elaborate tricks and syntactic constructs, but don't be surprised if they produce elegant, slow code. Whatever style/data structure/class/function choices you make, you should prototype and benchmark them thoroughly on the target platform, especially in the context of new and evolving CPU architectures. You then get to pick the most elegant fast one and use that.
RodriguezFatz likes this.
mprinkey is offline   Reply With Quote

Reply

Tags
c++, cfd, solver

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solid Mechanics Solvers added to OpenFOAM Extend bigphil OpenFOAM Announcements from Other Sources 15 May 27, 2015 11:32
FSI solvers - best solvers steven123 OpenFOAM Running, Solving & CFD 0 July 8, 2014 10:26
Possible turbulence modelling bug in SRF solvers otm OpenFOAM Running, Solving & CFD 3 May 29, 2012 04:03
Reaction solvers megacrout OpenFOAM 5 July 8, 2011 10:54
User coding on NT w2k Philip Jones CD-adapco 0 November 26, 2001 04:43


All times are GMT -4. The time now is 03:02.