mixed-language automatic differentiation
TRANSCRIPT
Mixed-language Automatic Differentiation
Valérie Pascual – Laurent Hascoët AD 2016 Oxford - September 2016
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 2
Mixed Language AD - Outline
• Language standards and interoperability • Automatic Differentiation algorithms for interoperability • Performance discussion • Implementation • Further works
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 3
Mixed language Automatic Differentiation
function g(y) real y g = … y …
end function
void f(float *x) {
*x = g_(x)
+ fBar(10, x);
}
Existing works for mixed-language software are categorized in two categories: - Program understanding and analysis - Refactoring (IDE)
subroutine bar(a, b) bind(c, name='fBar') real, value :: a real :: b … end subroutine
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 4
Language standards and interoperability
• Compiler dependent – ad hoc mechanisms different conventions to associate procedure names
between languages (BIND_ C) portability using macro definitions • ISO Fortran 2003 interoperability standards for interaction with C (ISO_C_BINDING module)
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 5
Support for interoperability
• Mixed language applications are compiled using Separate compilation and are differentiated using Global differentiation • AD analyses must be global for accuracy • Differentiation of the complete source code without using hand-written stubs
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 6
Support for interoperability
Language independent internal representation
Cparser
Fortran95parser
Fortran77parser IL
Tree
Internal representationbuilder
InternalRepresentation
Data-Flowanalysis
Data-Flow infoDifferentiation
InternalRepresentation
IL Treerebuilder
ILTree
Cprinter
Fortran95printer
Fortran77printer
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 7
Support for interoperability
Call graphs of Flow graphs
Nested Symbol Tables
Unit: C1(procedure)
Unit: P7(procedure)
Unit: M6(module)
Unit: P5(procedure)
Unit: M3(module)
Unit: To p(root level)
i f (. . . )
m o d u le M3m o d u le M6
p ro c e d u re P5 (. . . )
re a l :: A(:),bin te g e r :: i
d o i=1 ,5 0
A(i ) = b *i
u s e M3re a l :: v = 2 .0p ro c e d u re P7 (. . . )
in te g e r :: ii f (. . . )
re a l :: tm pv = . . .c a l l P5 (. . . )
d o i=1 ,9
root Symbol Table:
root Symbol Table C:C1 : procedure
Symbol Table of C1:
root Symbol Table F:M6 : moduleM3 : module
Symbol Table of M3:
P5 : procedure
Symbol Table of P5:A: variable, real(:)b : variable, real
i : variable, integer
Symbol Table of M3 (copy):P5 : procedure
Symbol Table of M6:P7 : procedurev : variable, real
Symbol Table of P7:
i : variable, integer
Local Symbol Table:tm p : variable, real
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 8
Support for interoperability
• Interoperability of procedures: - Naming convention or bind attribute in Fortran 2003 - Parameter-passing strategies: by reference, by value • Propagation of data-flow information between a caller
procedure and a callee using a “Translator”
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 9
Support for interoperability
• Each argument is split into elementary components • For each elementary formal argument of the called procedure,
the Translator associates - the corresponding elementary actual argument - a boolean copy / no copy upon return • At entry from the called procedure: the data flow value of the
passed argument is copied to the formal argument • At return time, copy back to the actual parameter / no copy
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 10
Support for interoperability
• Fortran calls C: REAL X void foo(float *a) CALL FOO(X) Passed argument is address of X Upon return, no copy takes place into &X • C calls Fortran: float *y; SUBROUTINE BAR(V) bar(y); REAL V Passed argument is *y Upon return, value of V is copied back into *y
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 11
Performance discussion
• CalculiX http://www.calculix.de 38000 lines of Fortran + 160000 lines of C • tapenade -b -head "linstatic(co)>(maxvm_pnorm)" analyses total time # diff params *.c + external 30 seconds 35 minutes 36 *.c *.f 80 seconds 9 minutes 30
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 12
Implementation
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 13
Implementation
AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 14
Further works
• Fortran 2003 “bind” statement for types, variables • Generation of differentiated code following interoperability
conventions and standards • Separate differentiation for large mixed-language applications,
with automatic generation of stub corresponding to a library