Outline
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

Outline PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on
  • Presentation posted in: General

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)

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Outline

Outline

Announcements

Add/drop today!

HWI due Friday

Static linking

Dynamic linking

Marrying FORTRAN and C


Anatomy of an executable

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


Anatomy of an executable1

Anatomy of an Executable

Fpca

main

call ReadData

call GetCov

call SSYEV

:

GetCov

call SSYRK

:

ReadData

:

SSYEV

:

SSYRK

:

Data

C, Cov, …


Building an executable

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


Building an executable1

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


Static linking

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


Dynamic linking

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)


Static vs dynamic linking

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, …


Static vs dynamic linking1

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


A dirty little secret

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


Interlanguage operability

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


Fortran

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!


Fortran disadvantages

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


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)

C


C key disadvantages

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!


Calling fortran from c

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


Namespace problem

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


Namespace problem1

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!


Call by value problem

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);


Call by reference

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:


Type equivalences

Type Equivalences


Multidimensional arrays

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


  • Login