Channels ▼
RSS

Tools

Performance Portable C++

Source Code Accompanies This Article. Download It Now.


Jeff is a computer scientist at Lawrence Livermore National Laboratory where he contributes to several software projects managed through the ASC program.


Programmers have two basic ways of organizing arrays of data; see Figure 1. The performance of each choice can vary greatly as code is ported from machine to machine and compiler to compiler.

It's easy to switch between the Array-like and Struct-like implementations in Figure 1 by hiding the array details behind a class API. Listing One shows how a coordinate array is implemented as a performance portable Point class. There are two important features of the Point class implementation:

  • Methods are inlined.
  • Methods return direct references to the underlying data.

Together, these two features let almost all compilers efficiently optimize most (if not all) class overhead, especially when interprocedural analysis has been enabled in the compiler optimization flags.

If you use classes having the above form, you can quickly switch between array layouts as you port code. The easiest way to do this is to create a configuration header file with system-specific layout choices, and #include that configuration file at the top of each array class header file.

If you don't hide the array implementation as I describe here, you can end up completely rewriting your software when switching from one form of array layout to the other.

<b></b>
(a) 

double x[10000] ;         
double y[10000] ; 
double z[10000] ;         

<b>(b)</b>

struct {           
  double x,y,z ;
} point[10000] ;   

Figure 1: (a) Array-like, (b) Struct-like.

  
#define ML_STRUCT 0
#define ML_ARRAY  1

#if POINT_MEM == ML_ARRAY
class Point {
public:
   Point(const int size) : m_x(size), m_y(size) {}
   inline double &x(const int idx) { return m_x[idx] ; }
   inline double &y(const int idx) { return m_y[idx] ; }
private:
  Point() ;
  std::vector<double> m_x ;
  std::vector<double> m_y ;
} ;
#else /* ML_STRUCT */
class Point {
public:
   Point(const int size) : m_p(size) {}
   inline double &x(const int idx) { return m_p[idx].x ; }
   inline double &y(const int idx) { return m_p[idx].y ; }
private:
  struct Coord { double x, y ; } ;
  Point() ;
  std::vector<Coord> m_p ;
} ;
#endif 
Listing One


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video