Auto-generated explicit template instantiations

Issue #14 resolved
Sam Preston created an issue

I've been playing with some auto-generation craziness, but it's working well so I created a feature branch and am setting up this issue just to discuss it. If we have a source file that needs a bunch of explicit template instantiations in it (for instance FOpers.cxx), we create a file FOpers_inst.tpl that might look something like this:

def AllBG = [
BACKGROUND_STRATEGY_CLAMP,
BACKGROUND_STRATEGY_WRAP,
BACKGROUND_STRATEGY_ZERO,
BACKGROUND_STRATEGY_PARTIAL_ZERO,
BACKGROUND_STRATEGY_ID,
BACKGROUND_STRATEGY_PARTIAL_ID]

def BoolArg = [true, false]

template void Opers::
ApplyH<${AllBG}>(Field3D&, const Field3D&, const Field3D&, const float&, StreamT);

template void Opers::
Resample<${AllBG}, ${BoolArg}>(Field3D&, const Field3D&, StreamT);
.
.
.

During compilation FOpers_inst.tpl will be parsed into the expanded instantiations in FOpers_inst.cxx, and in FOpers.cxx we just add the line #include "FOpers_inst.cxx. In the CMakeLists.txt we just need to set up a list of .tpl files to process (as we do for source files) and I've written some code that correctly sets up the dependencies and generation to turn FOpers_inst.tpl into FOpers_inst.cxx (it's just a simple python script that does the parsing).

I've also set up the generation so that the file is first parsed by the C preprocessor and all definitions passed to the C compiler are also passed to the preprocessing of the .tpl files, so we can do things like:

#ifdef CUDA_ENABLED
def ExecMode = [EXEC_CPU, EXEC_GPU]
#else
def ExecMode = [EXEC_CPU]
#endif

which will help us as we work on issue #12.

Comments (5)

  1. Jacob Hinkle

    Wow. Awesome. So I assume we can also keep various other lists, like

    def AllDefBG = [
    BACKGROUND_STRATEGY_WRAP,
    BACKGROUND_STRATEGY_ID,
    BACKGROUND_STRATEGY_PARTIAL_ID]
    

    or similar, and one for vfields, and use those in the same way? This looks great. Since these are just lists of strings we can do lists of ints too right?

  2. Sam Preston reporter

    Yeah, have a look at Code/Cxx/inc/math/FieldOpers_inst.tpl -- that's the best example with multiple replacement lists and some #ifdefs. Resample, for instance, used to take 24 explicit template instantiations to cover all combinations of parameters, and now it's just written once. Edit: added link to FieldOpers_inst.tpl so that you don't have to go searching for it

  3. Jacob Hinkle

    Wow. that's awesome. I notice that the version of Resample for instance that takes in bg and rescaleVector as arguments still has a manual series of ifs. Could that be handled the same way? I'd imagine the CMake magic could also just generate that function definition as well.

  4. Sam Preston reporter

    Yeah, I'm sure it could be set up -- the actual parsing is just done by a python script I wrote. Parsing the template arguments and inserting them as function arguments is just more complicated.

  5. Log in to comment