LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


View Poll Results: Which language would you use for HPC codes?
C 4 40.00%
C++ 4 40.00%
Fortran 2 20.00%
Python 2 20.00%
Multiple Choice Poll. Voters: 10. You may not vote on this poll

Reply
  Search this Thread
Old 08-18-2011, 05:53 PM   #1
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Post C and C++ performance comparison in numerical computation


Hi there!

I have been writing this library as a summer project and I thought on writing it on C++ and then write a C wrappers collection (interface) so that C user could use it.

But then I thought... shouldn't I write the numerical cores in C and do the interfaces in C++?

My question is: up to which point is C better than C++ for required-performance codes such as numerical PDEs?

I asked that question to the guy who developed Overture and he told me that as long as I could keep my C++ code just as a C code, performance shouldn't be compromised. Nonetheless, Overture has every numerical core written in C and Fortran and the interface written in C++.

Thanks in advanced! \m/

Last edited by ejspeiro; 08-18-2011 at 07:03 PM. Reason: Typo' fixed
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-18-2011, 06:54 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ejspeiro View Post
I thought on writing it on C++ and the write a C wrappers collection (interface) so that C user could use it.
Sounds good. I often do C++ code with C wrappers so the code can be delivered as a .a or .so file. C++ interface (name mangling and other factors) may be unstable across changes of compiler or version. C interface is much more stable. If delivering source code rather than binary, my reason for a C interface isn't important, but your reason still might be.

Quote:
But then I thought... shouldn't I write the numerical cores in C and do the interfaces in C++?
I don't think that is a good idea.

Quote:
My question is: up to which point is C better than C++ for required-performance codes such as numerical PDEs?
Depends on the skill and performance awareness of the programmer. If you are skilled and performance aware, it is never hard to get at least as good performance in C++ as you would have in C. But it is sometimes very hard to get the same high performance in C as might be easy in C++.

If you are less skilled, there are many performance pitfalls in C++, ways in which a less skilled programmer might unknowingly kill performance in C++, where a similar performance mistake in C would be more obvious.

Quote:
I asked that question to the guy who developed Overture and he told me that as long as I could keep my C++ code just as a C code, performance shouldn't be compromised.
Nor improved!

C++ does not inherently add any overhead. If you code the same thing in C++ as you would have in C, you get the same performance. I wouldn't write performance critical code that way. I would write it better in C++ than in C. But I'm unusual.

Quote:
Nonetheless, Overture has every numerical core written in C and Fortran and the interface written in C++.
Maybe for historical reasons? I don't know.

Lots of performance critical numerical code is written in Fortran, maybe because that is what the algorithm experts writing the code have learned to think in. It sure isn't from any real advantage of Fortran compared to C++

Last edited by johnsfine; 08-18-2011 at 06:56 PM.
 
2 members found this post helpful.
Old 08-18-2011, 07:08 PM   #3
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
johnsfine: Thank you!

That was an amazing reply and I appreciated it!
 
Old 08-18-2011, 07:51 PM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by ejspeiro View Post
My question is: up to which point is C better than C++ for required-performance codes such as numerical PDEs?
Well, if you don't know what are you doing, then you can mess up in either language. Both languages are compiled, so performance mostly depends on your programming skills/knowledge. If you want good performance, pick decent algorithm and profile your program frequently to detect bottlenecks. Measure your program's performance instead of trying to guess it.
 
1 members found this post helpful.
Old 08-18-2011, 08:35 PM   #5
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
SigTerm: Awesome!

Thank you and you are right! Performance should be measured and off course I will!

Thanks!
 
Old 08-19-2011, 07:31 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
I suggest to get acquainted with:

http://en.wikipedia.org/wiki/Armadil...%2B_library%29
http://eigen.tuxfamily.org/index.php?title=Main_Page
http://www.linalg.org/
http://en.wikipedia.org/wiki/Matrix_Template_Library
http://math.nist.gov/iml++/

, etc.
 
1 members found this post helpful.
Old 08-19-2011, 07:39 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
See:
http://shootout.alioth.debian.org/

I vote mostly for C, but Fortran is good for math as well.
 
1 members found this post helpful.
Old 08-19-2011, 08:51 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ejspeiro View Post
But then I thought... shouldn't I write the numerical cores in C and do the interfaces in C++?

My question is: up to which point is C better than C++ for required-performance codes such as numerical PDEs?
If you don't use allocations and deallocations in the iterative part of the solver there shouldn't be much of a difference, unless your algorithm is so tight that extra dereference operations (e.g. accessing vtables or nested structures, using non-pointer iterators) cause a noticeable degradation. I implemented a sorting algorithm several years ago that ran so tightly that I saw a performance difference between val = a? b : c; and if (a) val = b; else val = c; with simple iterator operations. With indefinite-iteration algorithms such as PDE solvers, however, it's better to reduce the number of iterations and number of copy operations first.
Kevin Barry
 
1 members found this post helpful.
Old 08-19-2011, 09:08 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ta0kira View Post
unless your algorithm is so tight that extra dereference operations (e.g. accessing vtables or nested structures, using non-pointer iterators) cause a noticeable degradation.
In the performance critical portions of numerical PDE code, if the programmer is competent, I would not expect any access to vtables nor non-pointer iterators, nor the type of structure nesting that takes extra accesses. This is a situation in which templating is very powerful for writing general purpose source code that has no run time cost for its generality.

Quote:
Originally Posted by ta0kira View Post
it's better to reduce the number of iterations and number of copy operations first.
There is typically more room for improvement in making the sequence of copy operations more cache friendly than in actually reducing the number of copy operations. Either way, you're in a very difficult area already studied by some very good algorithm experts. Unless you're part of a really strong professional team, you may be better off following published algorithms keeping your own focus on code quality vs. trying to beat the published experts on algorithm.

I don't want to get much more specific about what should or shouldn't be in numerical PDE code, because that could get too close to some of my closed source professional work.

Last edited by johnsfine; 08-19-2011 at 09:20 AM.
 
1 members found this post helpful.
Old 08-19-2011, 09:41 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
There is typically more room for improvement in making the sequence of copy operations more cache friendly than in actually reducing the number of copy operations. Either way, you're in a very difficult area already studied by some very good algorithm experts. Unless you're part of a really strong professional team, you may be better off following published algorithms keeping your own focus on code quality vs. trying to beat the published experts on algorithm.
Yes, I wasn't suggesting that the average engineer set out to come up with a PDE algorithm. I failed to be concise in my effort to generalize to whomever might be writing PDE software. Certainly there are numerous mathematicians specializing in PDE and numerical analysis who are better suited for such tasks than most individuals. OP could be on either side, which I was trying to account for.
Kevin Barry

Last edited by ta0kira; 08-19-2011 at 09:42 AM.
 
Old 08-19-2011, 10:46 AM   #11
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
Sergei: Wow! Those are excellent links, particularly I learned a lot while reading about Armadillo for C++. Similarly, IML with SparseLib++ seem both very useful.

Thank you!

H_TeXMeX_H: The website about benchmarks for programing languages is very cool! I could run a couple of them to back my desicion up about selecting the language.

Thank you!
 
Old 08-19-2011, 10:52 AM   #12
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
ta0kira: Your comment was very useful! I will take care of the iterative parts! Thank you for tailoring your answer toward PDE solvers development!

I believe that performance in numerical simulation is important but correctitud also matters. In early stages of development, I will focus of correctitud rather than in performance; nonetheless, performance will not be disregarded in an point!

johnsfine: Thank you for your remarks! YES, I like templates, specially in this kind of libraries where one generally keep different versions to account for different data types for the scalars involved in you algorithm.

Thank you guys!
 
Old 09-01-2011, 09:46 PM   #13
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Original Poster
Rep: Reputation: 26
Thanks to all of you! I'm calling this one SOLVED!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: A Performance Comparison of Mono vs .NET LXer Syndicated Linux News 0 09-10-2010 04:00 PM
Best platform for High performance engineering computation. Alexvader Linux - General 4 02-03-2010 11:14 PM
LXer: AMD R700 2D Driver Performance Comparison LXer Syndicated Linux News 0 01-23-2009 10:50 PM
[OS performance comparison] Synesthesia *BSD 4 12-05-2005 07:51 PM
Why does linux performance seem so terrible in comparison to windows? hermaphromike Linux - Newbie 17 04-30-2005 08:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration