advanced biopse ncrr dynamic compilation joint session #1: advanced biopse/scirun
TRANSCRIPT
Advanced BioPSE
NCRR
Dynamic Compilation
Joint Session #1: AdvancedBioPSE/SCIRun
Advanced BioPSE
NCRR
C++ BackgroundInheritance • Virtual methods
• Overloaded behavior• gen_fld_ptr->interp(Point *p);• Each field inherits from base and overloads their own
interp appropriate to the field
Templates• Compile time 'virtual'• Interp<FLD>(Point *p);
• Compiler provides replacement for FLD with exact field type at compile time
Advanced BioPSE
NCRR
Field Design
Field Design Library
Must have uniform interface to Fields• Algorithms can be general• Coders can expect uniform interface
Evaluate the following:• Inheritance and virtual methods• Template classes implementing field concept
Advanced BioPSE
NCRR
Templates vs. Virtual
10.21
2.84
30.43
10.1
0
5
10
15
20
25
30
35
MIPSpro 7.3.1.2m GNU g++ 3.0.0
Virtual Template
Test identical functions configured within a virtual method against a templated configuration.
Execution times in seconds
Advanced BioPSE
NCRR
SCIRun Fields
Fields and Meshes are Templated Datatypes that adhere to their concepts
• TetVolField<double>• LatVolField<Tensor>• Many more...
Modules implement algorithms• Multiple Inputs and outputs• Essentially algorithms parameterized on their
inputs and outputs
Advanced BioPSE
NCRR
Template Drawbacks
Template code tends to propogate
All permutations must be known at compile time
Not runtime extensible
SCIRun modules must discover the type• Ports pass all fields as base class type
• Massive amounts of type identification code
• Code maintenance nightmare
Advanced BioPSE
NCRR
Type DiscoveryAlgo dispatch1(FieldHandle field1, callback) { string disp_name = field1->get_type_name(0); if (disp_name == "TetVol") { // test all supported sub types if (field1->get_type_name(1) == "double") { TetVol<double> *f1 = 0; f1 = dynamic_cast<TetVol<double>*>(field1); if (f1) { return new ModuleAlgorithm<TetVol<double> >; } } else if { // The same for the rest of the TetVol types.
•TetVol<int>
•TetVol<unsigned int>
•TetVol<char>
•TetVol<unsigned char>
}
} else if (disp_name == "LatticeVol") { // test all supported sub types } else if (disp_name == "ContourField") { // test all supported sub types } else if (disp_name == "TriSurf") { // test all supported sub types } else { cerr << "Error: Field type not supported" << endl; }}
•TetVol<short>
•TetVol<unsigned short>
•TetVol<Vector>
•TetVol<Tensor>
Advanced BioPSE
NCRR
CombinationsField Types (4)
• TetVolField• LatVolField• TriSurfField• CountourField
Data Types (9)• double• int• Char• float• etc...
An Algorithm Parameterized on One Field Type
• Requires 36 Field instantiations
• And 36 Algorithm Instantiations
Advanced BioPSE
NCRR
CombinationsParameterized On
Two Fields• 362 = 1296 Versions of
the Algorithm
Parameterized On Three Fields
• 363 = 46656 Versions of the Algorithm
SCIRun supports more Field and Data types
Very few of these combinations are needed by any one SCIRun application
Advanced BioPSE
NCRR
Solution
We only compile the exact types we need• Extend C++ RTTI to determine types using strings• Generate C++ code to instantiate correct templates• Fork a process to compile the code• Dynamically link the resulting shared library• Call into the lib to get an instance of the algorithm• Call a single virtual function in the algorithm, passing in
fields and other required parameters• Execute the intended function, which uses
dynamic_cast to get the known underlying field type
Advanced BioPSE
NCRR
Calling the AlgorithmVoid ShowField::execute()
{ // Get a Field from input field port. field = (FieldIPort *)get_iport("Field"); field->get(field_handle); // Get the input field's type info. const TypeDescription *td = field_handle->get_type_description();
// Get the Algorithm. CompileInfo *ci = RenderFieldBase::get_compile_info(td); if (! DynamicLoader::scirun_loader().get(*ci, rend_algo)) { error("Could not compile algorithm for ShowField -");
return; } RenderFieldBase *rf = dynamic_cast<RenderFieldBase*>(rend_algo); // Let the templated algorithm render this field. rf->render(field_handle, /* any other parameters from gui */);
// Send results downstream...}
No Template Instantiations for the exact algorithm type!
Advanced BioPSE
NCRR
Summary
Customize Compilation• Only the algorithms and types you use are compiled• They are persistent (only create once)
Flexibility/Extensibility• Your Fields in your Package compile with existing
algorithms.
Reduce Code Bloat• Won't swamp your compiler• Smaller libraries• Shorter installation compiles