Mixed-language Automatic Differentiation

14
Mixed-language Automatic Differentiation Valérie Pascual – Laurent Hascoët AD 2016 Oxford - September 2016

Transcript of Mixed-language Automatic Differentiation

Page 1: Mixed-language Automatic Differentiation

Mixed-language Automatic Differentiation

Valérie Pascual – Laurent Hascoët AD 2016 Oxford - September 2016

Page 2: Mixed-language Automatic Differentiation

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

Page 3: Mixed-language Automatic Differentiation

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

Page 4: Mixed-language Automatic Differentiation

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)

Page 5: Mixed-language Automatic Differentiation

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

Page 6: Mixed-language Automatic Differentiation

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

Page 7: Mixed-language Automatic Differentiation

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

Page 8: Mixed-language Automatic Differentiation

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”

Page 9: Mixed-language Automatic Differentiation

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

Page 10: Mixed-language Automatic Differentiation

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

Page 11: Mixed-language Automatic Differentiation

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

Page 12: Mixed-language Automatic Differentiation

AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 12

Implementation

Page 13: Mixed-language Automatic Differentiation

AD 2016 Oxford Valérie Pascual – Laurent Hascoët - 13

Implementation

Page 14: Mixed-language Automatic Differentiation

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