Outline

22
Outline Outline • Announcements Add/drop today! HWI due Friday Static linking Dynamic linking Marrying FORTRAN and C

description

Outline. Announcements Add/drop today! HWI due Friday Static linking Dynamic linking Marrying FORTRAN and C. Anatomy of an Executable. An executable is a set of machine instruction Specific to OS & chip When you start an executable (double click or type name) - PowerPoint PPT Presentation

Transcript of Outline

Page 1: Outline

OutlineOutline• Announcements

– Add/drop today!– HWI due Friday

• Static linking• Dynamic linking• Marrying FORTRAN and C

Page 2: Outline

Anatomy of an ExecutableAnatomy of an Executable• An executable is a set of machine instruction

– Specific to OS & chip• When you start an executable (double click or

type name)– A special program called “start” or “load” copies the

executable into RAM– Start then sets program counter at the start of main– The instructions in your program are then executed

Page 3: Outline

Anatomy of an ExecutableAnatomy of an ExecutableFpca

main call ReadData call GetCov call SSYEV :GetCov call SSYRK :ReadData :SSYEV :SSYRK :Data C, Cov, …

Page 4: Outline

Building an ExecutableBuilding an Executable1. Translate each source file to object code

main.f

subs.f

g77 -c main.f

g77 -c subs.f

main.omain call ReadData call GetCov call SSYEV :

subs.oGetCov call SSYRK :ReadData :

Page 5: Outline

Building an ExecutableBuilding an Executable2. Link objects to form executable

g77 main.o subs.o -llapack -lblas -o Fpcamain.o

main call ReadData call GetCov call SSYEV :

Fpcamain call ReadData call GetCov call SSYEV :GetCov call SSYRK :ReadData :SSYEV :SSYRK :Data C, Cov, …

subs.oGetCov call SSYRK :ReadData :

liblapack.aSSYEV.o SSYEV

libblas.aSSYRK.o SSYRK

Page 6: Outline

Static LinkingStatic Linking• The process we just described is “static

linking”– Means that object code in libraries is copied

into executable• Consider C function printf

– Called by lots of UNIX programs • (ls, pwd, more, man, etc.)

– With static linking, each of these programs would contain a copy of printf object code

Page 7: Outline

Dynamic LinkingDynamic Linking• Seems inefficient for every C program to have

its own printf• Dynamic linking allows multiple programs to

“share” the same object code– Linking happens when program is run

• On UNIX, libraries that allow dynamic linking are known as “shared-object” libraries– libblas.so

• On windows, these are DLL’s (Dynamically Linked Libraries)

Page 8: Outline

Static vs. Dynamic LinkingStatic vs. Dynamic LinkingFpca

main call ReadData call GetCov call SSYEV :GetCov call SSYRK :ReadData :SSYEV :SSYRK :Data C, Cov, …

Fpcamain call ReadData call GetCov call SSYEV :GetCov call SSYRK :ReadData :Data C, Cov, …

liblapack.soSSYEV :

libblas.soSSYRK :

Page 9: Outline

Static vs. Dynamic LinkingStatic vs. Dynamic Linking• Static Linking

– Simpler– Faster (slightly)

• Dynamic Linking– Less disk & memory– Easier to maintain--changing libA.so will

affect any programs which use it

Page 10: Outline

A Dirty Little SecretA Dirty Little Secret• When we type g77 *.o -lblas what do we link

to?– libblas.a?– libblas.so?

• Documentation for gcc is ambiguous– On some systems, if two versions of a library exists,

the default is the .so version– Force static linking with -static option

• Bottom line: static-dynamic distinction is important for library developers, not library users

Page 11: Outline

Interlanguage OperabilityInterlanguage Operability• Object code contains low-level

instructions for system• Linking process just merges object code

together– Seems like we could link object code

created from different languages (say, C and FORTRAN)

– You can, but there are some wrinkles

Page 12: Outline

FORTRANFORTRAN

• FORmula TRANslator– One of the first programming languages– Most common strain was standardized in 1977– Designed for “Scientific Computing” (e.g. physics)– complex type fully implemented, integrated– lots of legacy code– simple– fast!

Page 13: Outline

• F77 is ancient– Missing “modern” features like pointers, novel data

structures (or even records)– Missing not-so-modern features like recursion!– Encourages bad programming:

• heavy use of goto-statement• common blocks

FORTRAN: DisadvantagesFORTRAN: Disadvantages

Page 14: Outline

• In many ways, C is similar to FORTRAN– Procedural– A few built-in types and data structures

• But more modern– pointers--allows you to manipulate memory directly– structs--allows you to implement records– Together, pointers and structs allow you to create new

data structures– supports recursion– can do everything you could ever want to do (math, CS, graphics)

CC

Page 15: Outline

C: Key disadvantagesC: Key disadvantages

• Programming with pointers can be complicated and even dangerous

• No complex type or functions• LESS LEGACY CODE!

– Calling old FORTRAN code from C (or C++, etc.) would allow us to have the best of both worlds!

Page 16: Outline

Calling FORTRAN from CCalling FORTRAN from C• In theory, we should be able to

– Compile FORTRAN code to object code (-c option)– Compile C code to object code– Link objects together

• However, there are a few wrinkles:– Namespace problem

• C object code needs to refer to the routines using the correct names• ANSI C code needs prototypes

– Call-by-value problem• C can use call-by-value, FORTRAN uses only call-by-reference• In general, need to make sure we’re sending the FORTRAN routines the

type of data they expect

Page 17: Outline

Namespace ProblemNamespace Problem

• The section of a .o file for a specific routine is given a name.

• The name is used by the linker to figure out how the executable is put together

• We must ensure that calls to FORTRAN routines in C object code use the same name as in the FORTRAN .o file

Page 18: Outline

Namespace ProblemNamespace Problem• Routine FooBar in a FORTRAN .o file could be

– FooBar_– FOOBAR_– foobar_ (g77)

• To call FooBar from C, you will need to use the correct case and add the underscore– Some compilers provide a -f option which forces the names

in the .o to be all lower case• CAUTION: Every system/compiler is different! Read the

documentation!

Page 19: Outline

Call-by-Value ProblemCall-by-Value Problem

• In C, a variable can be passed to a subroutine by value or reference.– call-by-value: the number stored in the variable is

passed to the subroutine. The value in the calling routine WILL NOT CHANGE!

• int m = 4• Foo(m); /* m won’t change */• prototype for Foo:

– void Foo(int m);

Page 20: Outline

– call-by-reference: the memory address is passed. If the subroutine modifies the value, the value WILL CHANGE in the calling routine.

• Use “&” to pass a scalar by value:– Foo(&m) /* m might change */– prototype for Foo:

» void Foo(int *m); /* “*”==pointer */• Arrays are already pointers, so they are automatically

passed by reference:– int m[10],tot;– tot=SumArray(m,10);– prototype for SumArray:

» int Foo(int *m, int n); /* n=length(m) */

Call-by-reference:Call-by-reference:

Page 21: Outline

Type EquivalencesType EquivalencesFORTRAN C

character*n c char c[n]integer (integer *4) intreal (real *4) floatdouble (real *8) doublecomplex *16 c struct dcomp{

double real; double cmplx;};struct dcomp c;

Page 22: Outline

• 1D arrays of equivalent type are represented identically by C and FORTRAN– not true for multidimensional arrays

Multidimensional ArraysMultidimensional Arrays

1 2 34 5 6

123456

142536

A=

C: Column-major FORTRAN: Row-major