[Insight-users] Explicit Instantiation question

Rupert Brooks rupe.brooks at gmail.com
Mon Nov 5 10:02:26 EST 2007


Hi,

In order to speed up one of my builds, which was including a lot of
templated classes, i decided to use an explicit instantiation
approach.  That is, i would implicitly instantiate the templated
classes that did not change most of the time, but in one place i would
explicitly instantiate them, and link in the resulting object file.

I found this document
(http://www.itk.org/Wiki/Proposals:Explicit_Instantiation) but after
reading it i must confess i was still confused.  What i really wanted
to know was how, as a user, im supposed to manage this issue now.
After some difficulty, i got it to work by switching on
ITK_MANUAL_INSTANTIATION before including the relevant files.
Pseudocode follows.

Overall, it works, and i sped up my build by about a factor of 3,
which is what i wanted.  But i still have some problems, and i have a
gut feeling im doing it the "wrong way".  I wondered if people could
tell me what the "RIGHT" way to do this is.  Possibly some of these
problems are bugs, in which case i  should report them.  But im not
sure.

1. Not only did i have to explicitly instantiate all the classes that
i actually use, but i had to instantiate their parents in some cases.
Eventually, i just kept building, and whatever symbols the linker
couldnt find, i would manually instantiate.

2. After I have used ITK_MANUAL_INSTANTIATION to block the inclusion
of txx files i have to manually include them for templated classes i
do want to implicitly include.  Again, a minor annoyance, but makes me
wonder if im doing this wrong.

3. For at least one class, itkPoint.h, probably more, this approach
does not work because the helper functions to allow things like
std::cout << mypoint are not explicitly instantiated.  I made a few
unsuccessful attempts to explicitly instantiate them by using them in
dummy functions in my explicit.cxx file, but it didnt work.

4. Certain classes dont seem to have all the members available for
instantiation.  On Visual C++ this gives a warning, on gcc 4, it is
ignored, and on gcc 3 it is a build-stopping error.  Whats very wierd
is i dont seem to be getting the same missing functions on gcc3 and
VC++.  Are these missing members a bug, or is there some trick to
instantiateing the classes?  I'm still investigating this one on gcc
3.  Example warning message from VC++ follows.

So, i would be curious about the opinions of people who have used the
explicit instantiation mode of ITK a bit more that I, on how best to
do this.

Cheers,
Rupert B.

PS For the record, these problems are occuring with ITK 3.2, using
CMake 2.4 with Visual c++ 2003 on WinXPSP2, gcc 3.2 on Red Hat, and
gcc 4.0.3 on Ubuntu.

Pseudocode:
======================================
//common.h
// the MY_EXPLICIT define controls to build explicit or not
#include "itkPoint.h"  // have to include this here to get the helper functions

#ifdef MY_EXPLICIT
#define IMPDEC
#else
#define ITK_MANUAL_INSTANTIATION
#define IMPDEC extern
#endif

#include "headerfilesthatneverchange.h"
// declare all classes used
IMPDEC template class classthatneverchanges<template, parameter>;

// declare some parents of classes that are used
IMPDEC template class classthatneverchanges<template, parameter>;

#ifndef MY_EXPLICIT
#undef ITK_MANUAL_INSTANTIATION
#endif
#undef IMPDEC


=======================================
//frequentlychanged.h
#include <headerfilesthatdochange>
// have to include .txx files too here.

=====================================
//explicit.cxx
// compiling this creates all the necessary objects for
// the templated classes.
#define MY_EXPLICIT 1
#include "common.h"

======================================
//application.cxx
#include "common.h"
#include "frequenlychanged.h"

int main() {

// insert fantasticly creative program here.
return 0;
}

Example warning (from Visual C++)
c:\Documents and Settings\rupert\My
Documents\home\development\registration\src\common.h(400) : warning
C4661: 'void itk::ImageMaskSpatialObject<TDimension>::operator =(const
itk::ImageMaskSpatialObject<TDimension>::Self &)' : no suitable
definition provided for explicit template instantiation request
        with
        [
            TDimension=3
        ]
        C:\Program
Files\ITK\include\InsightToolkit\SpatialObject\itkImageMaskSpatialObject.h(88)
: see declaration of
'itk::ImageMaskSpatialObject<TDimension>::operator`=''
        with
        [
            TDimension=3
        ]



-- 
--------------------------------------------------------------
Rupert Brooks
McGill Centre for Intelligent Machines (www.cim.mcgill.ca)
Ph.D Student, Electrical and Computer Engineering
http://www.cyberus.ca/~rbrooks


More information about the Insight-users mailing list