Why did the developers of deal.ii choose templates over variadic arguments?

27 views
Skip to first unread message

Krishnakumar Gopalakrishnan

unread,
Dec 20, 2019, 8:12:56 AM12/20/19
to deal.II User Group
With the highest deference to the amazing quality of software development that deal.ii  developers follow, I wish to ask a question about the particular development paradigm chosen (with no disrespect to any of the developers intended). This question is just out of personal curiosity.

deal.ii makes heavy use of templates for spatial-dimension independent programming.  However, as already acknowledged in the FAQ, compiling template-heavy code takes a long time.

Since the number cases are only 3 (possible spatial dimensions), it strikes me that the variadic arguments feature (facilitated by the cstdarg.h header file) might be a slightly bit more natural choice for this use case? Apologies if I am wrong about this, since I am a novice C++ user.
 
Can the developers/users of deal.ii provide an objective reasoning of why they went the template-route instead of variadic arguments?

Regards,
Krishna

Bruno Turcksin

unread,
Dec 20, 2019, 8:41:22 AM12/20/19
to deal.II User Group
Krishna,

You can find an extensive discussion on the design choices made here. I think section 2.4 answers your question. The short answer is that using template makes the code run faster because the compiler can do a better job at optimizing everything and it allows us greater flexibility in the choice of data structure.

Best,

Bruno

Krishnakumar Gopalakrishnan

unread,
Dec 20, 2019, 8:52:24 AM12/20/19
to dea...@googlegroups.com
Hi Bruno,

Thank you for your really quick reply.  I shall read the article.

Krishna






--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/8QoMVsf0Oi4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/15f96801-09e4-438b-b159-e7baf8b5a587%40googlegroups.com.

Wolfgang Bangerth

unread,
Dec 21, 2019, 12:14:17 AM12/21/19
to dea...@googlegroups.com

Krishna,

> deal.ii makes heavy use of templates for spatial-dimension independent
> programming.  However, as already acknowledged in the FAQ, compiling
> template-heavy code takes a long time.
>
> Since the number cases are only 3 (possible spatial dimensions), it strikes me
> that the variadic arguments feature (facilitated by the cstdarg.h header file)
> might be a slightly bit more natural choice for this use case? Apologies if I
> am wrong about this, since I am a novice C++ user.
> Can the developers/users of deal.ii provide an objective reasoning of why they
> went the template-route instead of variadic arguments?

It took me a good long while to think through how one would use variadic
arguments. Let's take a function like this one:

void
VectorTools::interpolate_boundary_values
(const Mapping< dim, spacedim > & mapping,
const DoFHandlerType< dim, spacedim > & dof,
const std::map< types::boundary_id, const Function< spacedim, number > *>
& function_map,
std::map< types::global_dof_index, number > & boundary_values,
const ComponentMask & component_mask = ComponentMask())

https://dealii.org/developer/doxygen/deal.II/namespaceVectorTools.html#a187aeb575be07bc47cb3dea1a47aaf88

If you want to avoid making this function a template, then the only real
option you have is to declare it as

void VectorTools::interpolate_boundary_values (...);

with variadic arguments. This way, you can pass in a Mapping<1>, Mapping<2>,
Mapping<3>, or if you want to Mapping<1,3> as first argument, and then
internally figure out what the argument actually represents. But there are
quite a number of downsides:
* Figuring out what you actually got has to happen at run-time, so it has
a performance impact
* The compiler can't do any type checking: If you pass a DoFHandler<1,3> as
first argument, nobody tells you that that's wrong. Or a pointer-to-mapping
instead of reference-to-mapping. You will only get an error message at
run-time, and the error is going to say "You passed something, but it
doesn't match what we expected". There is no further information what the
something might be.
* Arguments cannot be passed by reference to variadic functions. For big
objects such as triangulations, that means that one would have to use
pointers, but that's decidedly against the spirit of C++.

So it's a fun exercise to think through how one could use variadic functions
for what we want to do in deal.II. But it's not the most convenient way :-)

Cheers
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

Reply all
Reply to author
Forward
0 new messages