Advanced School in High Performance and GRID Computing November 2008

Similar documents
How Compiling and Compilers Work

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Follow us on Twitter for important news and Compiling Programs

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Introduction to Supercomputing

Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

Introduction Presentation A

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

CS240: Programming in C

Maemo Diablo GNU Make and makefiles Training Material

CS2141 Software Development using C/C++ Compiling a C++ Program

Advanced School in High Performance and GRID Computing November Mathematical Libraries. Part I

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

2 Compiling a C program

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Introduction to Linux Scripting (Part 2) Brett Milash and Wim Cardoen CHPC User Services

Introduction to Linux Scripting (Part 2) Brett Milash and Wim Cardoen CHPC User Services

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

GNU make... Martin Ohlerich, Parallel Programming of High Performance Systems

Compila(on, Disassembly, and Profiling

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Software Lesson 2 Outline

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Introduction to Supercomputing

CMPT 300. Operating Systems. Brief Intro to UNIX and C

CPS104 Recitation: Assembly Programming

Workshop Agenda Feb 25 th 2015

SOFTWARE ARCHITECTURE 5. COMPILER

Assembly Language Programming - III

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

Tutorial: Compiling, Makefile, Parallel jobs

Program Building. Goals for this Lecture. Help you learn about: Program structure. Detecting and reporting failure

Compilation, Disassembly, and Profiling (in Linux)

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

x86 assembly CS449 Spring 2016

Introduction to Operating Systems Part I

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Executables and Linking. CS449 Spring 2016

Laboratorio di Tecnologie dell'informazione

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

x86 assembly CS449 Fall 2017

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CS241 Computer Organization Spring Introduction to Assembly

You may work with a partner on this quiz; both of you must submit your answers.

Department of Computer Science and Engineering Yonghong Yan

Lecture 3: Instruction Set Architecture

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

CMPSC 311- Introduction to Systems Programming Module: Build Processing

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Intel assembly language using gcc

Laboratorio di Programmazione. Prof. Marco Bertini

30 Nov Dec Advanced School in High Performance and GRID Computing Concepts and Applications, ICTP, Trieste, Italy

UNIX Makefile. C Project Library Distribution and Installation.

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Introduction to the C-Language and Programming Environment

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Conduite de Projet Cours 4 The C build process

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

Laboratory 1 Semester 1 11/12

PRINCIPLES OF OPERATING SYSTEMS

Review. Topics. Lecture 3. Advanced Programming Topics. Review: How executable files are generated. Defining macros through compilation flags

Data in Memory. variables have multiple attributes. variable

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Executables and Linking. CS449 Fall 2017

EE/CSCI 451 Introduction to Parallel and Distributed Computation. Discussion #4 2/3/2017 University of Southern California

Buffer Overflow Attack

GSI Fundamentals (1): Setup and Compilation

Compilation & linkage. Compilation & linkage. Make. Compilation & linkage. Explicit rules. What makefile contains

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Computer Systems Organization

Midterm. Median: 56, Mean: "midterm.data" using 1:2 1 / 37

CS3157: Advanced Programming. Outline

CS429: Computer Organization and Architecture

System calls and assembler

Data and File Structures Laboratory

GCC as Optimising Compiler

Introduction to Operating Systems Part I

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

C introduction: part 1

Instruction Set Architectures

Transcription:

1967-6 Advanced School in High Performance and GRID Computing 3-14 November 2008 From Source Code to Executable: Preprocessing / Compiling / Linking Makefiles (Part I) KOHLMEYER Axel University of Pennsylvania Department of Chemistry 231 South 34th Street PA 19104 Philadelphia U.S.A.

From Source Code to Executable: Preprocessing, Compiling, Linking, and Makefiles ICTP Advanced School in High Performance and GRID Computing Axel Kohlmeyer Center for Molecular Modeling ICTP, Trieste Italy, 04 November 2008

Overview / Compiler The pre-process/compile/link process: the individual steps in detail Preprocessing in C and Fortran The C-preprocessor, typical directives Compilers: Fortran 77/95, C, C++ Vendors: GNU, Intel Special Compiler flags Special Linker flags, Utilities

Pre-process / Compile / Link Consider the minimal C program 'hello.c': #include <stdio.h> int main(int argc, char **argv) { printf( hello world\n ); return 0; } What happens, if we do?: > cc -o hello hello.c (try: cc -v -o hello hello.c)

Step 1: Pre-processing Preprocessing will handle all '#' directives File inclusion Conditional compilation Macro expansion In this case: /usr/include/stdio.h is inserted and pre-processed Only pre-processing with -E flag: > cc -E -o hello.pp.c hello.c Last part of hello.pp.c and hello.c identical

Step 2: Compilation Compiler converts high-level language Output machine specific text (assembly) Parses text (lexical + syntactical analysis) Test for correctness, language dialect Translate to internal representation (IR) Optimization (reorder, merge, eliminate) Replace IRs with assembly blocks Crucial step: Performance, Correctness (Cross-compiler for different platform) Try: > cc -S hello.c (and look at hello.s)

Compilation cont'd.lc0:.string "hello world\n".text.globl main.type main,@function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp subl $12, %esp pushl $.LC0 call printf addl $16, %esp movl $0, %eax leave ret

Optimized Compilation.LC0:.string "hello world".text.p2align 2,,3.globl main.type main,@function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp subl $12, %esp pushl $.LC0 call puts xorl %eax, %eax leave ret

Assembler / Linker Assembler (as) translates assembly to binary Creates so-call object files Try: > cc -c hello.c Try: > nm hello.o 00000000 T main U printf Linker (ld) puts binary together with startup code and required libraries Final step, result is executable. Try: > ld -o hello hello.o

Adding Libraries Example 2: exp.c #include <math.h> #include <stdio.h> int main(int argc, char **argv) { printf("exp(2.0)=%f\n", exp(2.0)); return 0; } > cc -o exp exp.c Does not work. exp is part of math library => cc -o exp exp.c -lm

Pre-processing in C and Fortran Pre-processing mandatory in C/C++ Pre-processing optional in Fortran Fortran pre-processing often implicit via file name: name.f, name.f90, name.for Use -DSOME_SYS to conditionally compile Use -DDEF_ARR=200 to set defaults Use capital letters to signal a define Use -I/some/path to search for include files Legacy Fortran packages frequently do: /lib/cpp -C -P -traditional -o name.f name.f

C-Pre-processor directives #define MYVAL 100 #undef MYVAL #if defined(myval) && defined( linux) #elif (MYVAL < 200) #else #endif #include myfile.c #include <myotherfile.c> #define SQRD(a) (a*a)

Compilers: GNU, PGI, Intel We will cover only C/C++, Fortran 77/95 GNU: gcc, g++, g77(old), gfortran(new), g95 Free, C/C++ quite good, gfortran/g95 not bad 'native' Linux compilers Support for many platforms Support for cross-compilation PGI: pgcc, pgcc, pgf77, pgf90 Commercial with trial, x86 and x86_64 Intel: icc, icpc, ifort Commercial with trial and non-commercial, x86, ia64 (Itanium), EM64t (=x86_64/opteron)

Common Compiler Flags Optimization: -O, -O0, -O1, -O2, -O3, -O4,... Compiler will try to rearrange generated code so it executes faster High optimization will not always execute faster High optimization may alter semantics Compile only: -c Preprocessor flags: -I/some/dir -DSOM_SYS Linker flags: -L/some/other/dir -lm -> search for libm.so/libm.a also in /some/dir Read the documentation for details

Special Compiler Flags: GNU -mtune=i686 -march=i386 (-mcpu sets both) optimize for i686 cpu, use i386 instruction set -funroll-loops heuristic loop unrolling (for floating point codes) -ffast-math replace some constructs with faster alternatives -fomit-frame-pointer use stack pointer as general purpose register -mieee-fp turn on IEEE754 compliance / comparisons

Special Compiler Flags: Intel -tpp6, set cpu type, v10 supports GNU style -pc64, set floating point rounding to 64-bit -ip, ipo, interprocedural optimization -axpw, generate SSE, SSE3 instructions -unroll, heuristic loop unrolling -fpp -openmp, turn on OpenMP -i-static, link compiler runtime statically -mp, force IEEE floating point handling -mp1, almost force IEEE floating point -fast, shortcut for -xp -O3 -ipo -no-prec-div

Special Compiler Flags: PGI -tp=px, -tp=amd64, -tp=x64, -tp=piv generate architecture specific code -pc=64 set floating-point rounding mode to 64-bit -Munroll, loop unrolling, -Mvect, vectorization (loop scheduling) -fast, -fastsse, short cuts to optimization flags -mp for OpenMP support -Mipa, turn on interprocedural analysis -Kieee, turn on IEEE floating point

Linker Issues, Utilities Linux defaults to dynamic libraries: > ldd hello libc.so.6 => /lib/i686/libc.so.6 /lib/ld-linux.so.2 => /lib/ld-linux.so.2 /etc/ld.so.conf, LD_LIBRARY_PATH define where to search for shared libraries Ifort -Wl,-rpath,/some/dir will encode /some/dir into binary for searching for dynamical libraries. Same for icc, GNU gcc/g++/gfortran

Overview / Make The idea behind make General Syntax Rules Examples Variables Examples Pattern Rules Special Rules Dependencies Conventions

Makefiles: Concepts Simplify building large code projects Speed up re-compile on small changes Consistent build command: make Platform specific configuration via Variable definitions

Makefiles: Syntax Rules: target: prerequisites command ^this must be a 'Tab' ( <- -> ) Variables: NAME= VALUE1 VALUE2 value3 Comments: # this is a comment Special keywords: include linux.mk

Makefiles: Rules Examples # first target is default: all: hello sqrt hello: hello.c cc -o hello hello.c sqrt: sqrt.o f77 -o sqrt sqrt.o sqrt.o: sqrt.f f77 -o sqrt.o -c sqrt.f

Makefiles: Variables Examples # uncomment as needed CC= gcc #CC= icc -i-static LD=$(CC) CFLAGS= -O2 hello: hello.o $(LD) -o hello hello.o hello.o: hello.c $(CC)-c $(CFLAGS) hello.c

Makefiles: Automatic Variables CC= gcc CFLAGS= -O2 howdy: hello.o yall.o $(CC) -o $@ $^ hello.o: hello.c $(CC)-c $(CFLAGS) $< yall.o: yall.c $(CC)-c $(CFLAGS) $<

Makefiles: Pattern Rules OBJECTS=hello.o yall.o howdy: $(OBJECTS) $(CC) -o $@ $^ hello.o: hello.c yall.o: yall.c.c.o: $(CC)-o $@ -c $(CFLAGS) $<

Makefiles: Special Targets.SUFFIXES:.SUFFIXES:.o.F.PHONY: clean install.f.o: $(CPP) $(CPPFLAGS) $< -o $*.f $(FC)-o $@ -c $(FFLAGS) $*.f clean: rm -f *.f *.o

Makefiles: Calling make Override Variables: make CC=icc CFLAGS='-O2 -unroll' Dry run (don't execute): make -n Don't stop at errors (dangerous): make -i Parallel make (requires careful design) make -j2 Alternative Makefile make -f make.pgi

Makefiles: Building Libraries ARFLAGS= rcsv LIBOBJ= tom.o dick.o harry.o helloguys: hello.o libguys.a $(CC) -o $@ $< -L. -lguys libguys.a: $(LIBOBJ) ar $(ARFLAGS) $@ $? tom.o: tom.c guys.h dick.o: dick.c guys.h harry.o: harry.c guys.h hello.o: hello.c guys.h

Makefiles: Automatic Dependencies LIBSRC= tom.c dick.c harry.c LIBOBJ= $(LIBSRC:.c=.o) LIBDEP= $(LIBSRC:.c=.d).c.d: $(CC) $(CFLAGS) -MM $< > $@ include $(LIBDEP) alternatively (note, some makes require.depend to exist):.depend dep: $(CC) $(CFLAGS)-MM $(LIBSRC) >.depend include.depend

Makefile Portability Caveats Always set the SHELL variable: SHELL=/bin/sh (make creates shell scripts from rules). GNU make has many features, that other make programs don't have and vice versa Use only a minimal set of Unix commands: cp, ls, ln, rm, mv, mkdir, touch, echo,... Implement some standard 'phony' targets: all, clean, install