1 / 22

Outline

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)

lewis
Télécharger la présentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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) 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

  3. Anatomy of an Executable Fpca main call ReadData call GetCov call SSYEV : GetCov call SSYRK : ReadData : SSYEV : SSYRK : Data C, Cov, …

  4. Building an Executable 1. Translate each source file to object code main.o g77 -c main.f main.f main call ReadData call GetCov call SSYEV : subs.o g77 -c subs.f GetCov call SSYRK : ReadData : subs.f

  5. Building an Executable 2. Link objects to form executable g77 main.o subs.o -llapack -lblas -o Fpca Fpca main call ReadData call GetCov call SSYEV : GetCov call SSYRK : ReadData : SSYEV : SSYRK : Data C, Cov, … main.o main call ReadData call GetCov call SSYEV : subs.o liblapack.a GetCov call SSYRK : ReadData : SSYEV.o SSYEV libblas.a SSYRK.o SSYRK

  6. Static 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

  7. Dynamic 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)

  8. Static vs. Dynamic Linking liblapack.so libblas.so Fpca SSYEV : SSYRK : main call ReadData call GetCov call SSYEV : GetCov call SSYRK : ReadData : SSYEV : SSYRK : Data C, Cov, … Fpca main call ReadData call GetCov call SSYEV : GetCov call SSYRK : ReadData : Data C, Cov, …

  9. Static 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

  10. A 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

  11. Interlanguage 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

  12. FORTRAN 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!

  13. 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: Disadvantages

  14. 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) C

  15. C: 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!

  16. Calling 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

  17. Namespace 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

  18. Namespace 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!

  19. Call-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);

  20. 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:

  21. Type Equivalences

  22. 1D arrays of equivalent type are represented identically by C and FORTRAN not true for multidimensional arrays Multidimensional Arrays A= FORTRAN: Row-major C: Column-major

More Related