469,924 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

Using c++ template metaprogramming for high performance number crunching?

I have cross posted this to comp.lang.c++ and to sci.math.num-
in the belief that the topic is of interest to some in both groups.

I am building my toolkit, in support of my efforts in producing high
performance C++ code; code that is provably correct. The issue here
is orthogonal to the question of expression templates, which at
present seem to me to be relatively simple.
I began with the dimensional analysis as described in Abrahams and
Gurtovoy's book on template metaprogramming. From there, I developed
a template class hierarchy specific to simulation, that avoids
unnecessary copying of data during a simulation. All works fine, and
my simulation templates interact with the dimensional analysis
templates perfectly. The cost of using my templates is about the
as using the corresponding pods. At some point, if there is interest,
I can provide a little zip archive showing what I have done so far.
Just tell me how you want to receive it if you want to see some
trivially simple code.
Anyway, the problem I am wrestling with, so far unsuccessfully,
relates to the question of units of measurement, and especially how
avoid what ought to be unnecessary flops during the simulation.
Suppose I have a volume variable, with measurements in cubic meters,
and a density variable, expressed in ounces per cubic centimeter, and
a mass expressed in kilograms. Obviously contrived though this
example is, it is simple enough to illustrate what I need to do.
Obviously, templates adequate from the perspective of dimensional
analysis as described in Abrahams and Gurtovoy's book would allow me
to assign a quotient involving my mass and volume variables to the
density variable. Also obviously such an assignment, that ignores
units of measurement, is likely to be wrong in most cases.
What I want to do, ideally, is do a little extra metaprogramming to
provide for implicit conversions, both for situations where I have
length in miles and another in kilometers, and need to convert
the two, and for composite variables (e.g. converting between one
density expressed in ounces per cubic foot and another expressed in
kilograms per cubic meter), while correctly refusing to convert
obviously wrong attempts to convert, e.g., between a length in feet
and a mass in kilograms. And since all valid conversions are known a
priori, well before any attempt to compile code, one would want the
conversions to happen, ideally, at compile time, or at worst before
any simulation begins. If one thinks of the cost of iterating a
function a million times, and a particular conversion of a constant
used in the function involves several flops, the simulation wastes
millions of flops during the simulation if I fail to find a way to
automate the conversions so that it happens either at compile time or
at a minimum before the simulation begins.
I am not sure I can get what I want because most of the conversions I
am after require use of floating point numbers, and IIRC, they can't
be used as template arguments.
While one might argue that this is a waste of time, since one can
always do it manually, as one writes the code, failing to find a way
to automate it results in code that seems to me to be unnecessarily
rigid. Imagine that a simulation engine is developed using this,
embedded within an application that takes equations provided by the
user, parses them into a DLL that can be then loaded and used
(obviously such an application would need to be distributed with a
decent compiler able to produce a DLL, but that is another matter),
and concommitantly takes data from the user for use in parameterizing
his model. If the model involves energy, and the equations use it as
joules, while the only source of data the user has at hand expresses
values in calories, one imposes extra work, vulnerable to error, on
the user.
Is what I am after possible? If you think so, how would you do it?
My own efforts, along the lines of a traits template parameter,
representing units and operations on them, have not yet paid off. I
find myself floundering, in part on the problem of producing a
suitably flexible template class that provides correct implicit
conversions, while rejecting incorrect conversions. I am also having
trouble figuring out the correct partial specializations required. I
am now not convinced I am even on the right track for solving this
particular problem. It is frustrating to have achieved everything
else I wanted in my library intended to support high performance
simulation, and not to see clearly the solution to this problem. :-(
Any ideas?

Sep 21 '07 #1
1 2105
Ted wrote:
Is there a simple way to download your library, or do I have to do it
a file at a time?
You can :

a) Use subversion (TortoiseSVN)
b) Use a DAV client (WinXP has one).

Sep 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Dave | last post: by
4 posts views Thread by zzfreddybb | last post: by
21 posts views Thread by Protoman | last post: by
10 posts views Thread by Frederick Gotham | last post: by
10 posts views Thread by Ray | last post: by
7 posts views Thread by Joe | last post: by
4 posts views Thread by suman.nandan | last post: by
12 posts views Thread by nooneinparticular314159 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.