High Performance Computing MPI and C-Language Seminars 2009

Similar documents
High Performance Computing

C++ for Java Programmers

Work relative to other classes

CS2141 Software Development using C/C++ C++ Basics

Kurt Schmidt. October 30, 2018

CSE 303: Concepts and Tools for Software Development

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

COMP s1 Lecture 1

PRINCIPLES OF OPERATING SYSTEMS

Algorithms, Data Structures, and Problem Solving

Homework #3 CS2255 Fall 2012

CSE 303 Lecture 8. Intro to C programming

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CE221 Programming in C++ Part 1 Introduction

CSE 374 Programming Concepts & Tools

CS 253: Intro to Systems Programming 1/21

Array Initialization

Final CSE 131B Spring 2004

A brief introduction to C programming for Java programmers

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Programming refresher and intro to C programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Lecture 03 Bits, Bytes and Data Types

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

Pointers (continued), arrays and strings

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS Programming In C

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Type Checking and Type Equality

Welcome to... CS113: Introduction to C

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Introduction to Programming Block Tutorial C/C++

CS 261 Fall Mike Lam, Professor. Structs and I/O

Programming in C and C++

CS 241 Data Organization. August 21, 2018

Lecture 07 Debugging Programs with GDB

Pointers (continued), arrays and strings

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Procedures, Parameters, Values and Variables. Steven R. Bagley

CS201 - Lecture 1 The C Programming Language

Introduction to Programming using C++

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

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

Pointers and Arrays 1

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Complex data structures. Cedric Saule

Chapter IV Introduction to C for Java programmers

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Arrays and Pointers in C & C++

Lecture 3. More About C

Problem Solving and 'C' Programming

6.S096 Lecture 1 Introduction to C

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

ELEC 377 C Programming Tutorial. ELEC Operating Systems

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

COMP1917: 09 Arrays and Strings

Structure of this course. C and C++ Past Exam Questions. Text books

High Performance Computing in C and C++

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS61C : Machine Structures

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Computer Programming

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Programming in C. What is C?... What is C?

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Lectures 5-6: Introduction to C

Lab 03 - x86-64: atoi

CS61C : Machine Structures

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61c: Great Ideas in Computer Architecture

Undefined Behaviour in C

Week 1: Hello World! Muhao Chen

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Introduction to C. Systems Programming Concepts

HW1 due Monday by 9:30am Assignment online, submission details to come

Transcription:

High Performance Computing - Seminar Plan Welcome to the High Performance Computing seminars for 2009. Aims: Introduce the C Programming Language. Basic coverage of C and programming techniques needed for the High Performance Computing coursework Introduce C-bindings for the Message Passing Interface. Overview of Performance Modelling for a complex application. These seminars will not cover C++ (but many of the skills are transferable) High Performance Computing MPI and C-Language Seminars 2009 Photo Credit: NOAA (IBM Hardware) 1 2 High Performance Computing - Seminar Plan Public Health Warning! Seminar Plan for Weeks 1-5 The MEng students this year will have been the first to use C skills in the second year Operating Systems module. Week 1 - Introduction, Data Types, Control Flow, Pointers Week 2 - Arrays, Structures, Enumerations, I/O Week 3 - File I/O, Preprocessing, Compile Time Options The first three weeks of this course are based on developing basic C skills so much of this will be familiar. Week 4 - MPI in C Feel free to skip this if you feel confident enough. Week 5 - How to Build a Performance Model After week 4 we will move to MPI, Performance Modelling etc. Unless you ve done this course before or are on a PhD programme with HPSG then its probably best to come along. Week 6-9 - Coursework Troubleshooting (a number of seminar tutors will be available during these sessions - go to each tutor s office) 3 4

High Performance Computing - Contact Info Seminar/Coursework Books Si Hammond (Warwick Postgraduate Research Fellow) High Performance Systems Group sdh@dcs.warwick.ac.uk Room CS2.04 (Department of Computer Science) Slides & Resources are available at: http://go.warwick.ac.uk/ep-csrcbc/ Office Hours: 9-11, Wednesday morning Email if you want to see me outside of these hours Probably won t need a book solely for the coursework/seminars. Worth consulting (in the library): The C Programming Language, (2nd Edition), Kernighan & Ritchie, 1988 C - How to Program, (5th Edition), Deitel, 2006 Online Reference Book linked at: http://go.warwick.ac.uk/ep-csrcbc 5 6 Feedback for Seminars Building a Collection of Resources Seminar Feedback forms at: http://go.warwick.ac.uk/ep-csrcbc under teaching. You can submit this form as many times as you like. Help us gauge whether to speed seminars up or slow them down, what things are are getting right and what things we need to improve. Please make use of this (makes my job easier!) Or feel free to email me with suggestions as we go along through the course. Over the CS402 course I will add resources at: http:// go.warwick.ac.uk/ep-csrcbc under teaching. Feel free to make suggestions for this resource. Check it regularly - we will add papers, tutorials, links etc to help you in the coursework. This will be particularly useful for Performance Modelling and OpenMPI later in the course. 7 8

Overview of the C Language Much of the Java language builds on C/C++ - hence some of the language keywords and behaviour is very similar. Types, Control Flow & Pointers Beware - although the keywords and names of types may be similar they are often different. Mixing between languages can introduce lots of bugs unless you re sure of what you are doing (so be careful)! 9 10 Data Types in the C Language Data Types in the C Language There are only four basic types in the C language (although most implementations include more). char - a single byte (8-bits) capable of holding an ASCII character int - an integer value, the size of which is often based on the word size of the machine (most often 32-bit) float - a single precision floating point number (often 32-bit, IEEE) double - a double precision floating point number (often 64-bit, IEEE) The above are just commonly encountered situations, on some platforms (particularly non-intel/amd) these may be different) Two additional qualifiers in the language to control the size of integer values. short int a - defines a to be a shorter version of int long int b - defines b to be a longer version of int Rules: short int <= int <= long int short int >= 16 bits int >= 16 bits long int >= 32 bits 11 12

Data Types in the C Language Booleans in C So which Java type is missing? So which Java type is missing? There is no boolean type in C Booleans are implemented and evaluated as integer values. TRUE = 1 FALSE = 0 These may or may not be pre-defined in the implementation of C you are using. 13 14 Booleans in C Additional Qualifiers for Integer Types What are the boolean values of the following expressions? 1 && 1 0 0!1 (2-1) 0 2 % 2 There are two additional qualifiers for integer types: unsigned - interpret the value as starting from 0 (no negatives) signed - allow negative numbers These can be used to increase the range of values within the type system provided the programmer is sure negative values will not be required. Mixing unsigned and signed values can cause programmer induced bugs (so again, be careful!) 15 16

Arrays in C Arrays in C Arrays in C are just allocated contiguous blocks of memory. For instance: int myarray[5]; Allocates space for 5 integers in one block in memory (must be contiguous) Arrays are not objects (C does not have objects) so you cannot access properties such as myarray.length as you might do in Java. The name of the array variable can be used as a pointer. For instance: int myarray[5]; The myarray variable is a pointer to the first element in the array. int* pointtofirst = myarray; You have to record the maximum length of the array, if you exceed the array bounds your program will crash or corrupt its own memory 17 18 Arrays in C Strings in C We will return to arrays when we cover pointers later in the seminar series. How are strings implemented in Java? Remember to treat array bounds seriously - your program will crash or produce incorrect/inconsistent output if you do not. 19 20

Strings in C Strings in C How are strings implemented in Java? How are strings implemented in C? java.lang.string class String contents are held in a char[] Strings are immutable - if you want to change it you create a new version and delete the old 21 22 Strings in C Strings in C How are strings implemented in C? Using a char[] (as in Java) but with an additional element at the end of the string. Strings must terminate with the null character - \0 For instance: Hello = { H, e, l, l, o, \0 } All string functions in the C libraries will use the \0 element as a method of detecting the end of the string. The most important functions are implemented in the string library. To use these you need to put: #include <string.h> At the very top of your file. The functions you need will be imported by the compiler. The seminar sheet for this lab has exercises in using the string functions. 23 24

Control Flow in C Control Flow in the C language uses very similar keywords and code structures to Java. Control Flow if, while, do...while are identical (except the conditions must produce an integer boolean result) for loops are not quite the same. Depending on which language mode you are in you cannot declare variables inside the for-loop definition. int i; for(i = 0; i < 10; i++) { printf( %i \n, i); } 25 26 Operators Mathematical Operators are identical to Java. +, -, *, / <=, >=, ==,!=, <, >, &&,! % &, (modulus operator) Pointers ++i, i++, --i, i-- (pre/post increment/decrement) 27 28

Pointers Pointers Pointers are references to locations in memory. Instead of using a variable name to state which memory location you want to use, you are using a numerical value to locate the value. Pointers are one of the biggest causes of bugs in software construction. Worse - they are one of the biggest reasons students coursework does not produce the correct solution on marking day. They require a lot of care! To declare a pointer: int myvalue = 42; int* mypointer; mypointer = &myvalue; The int* creates a pointer to a location in memory which is treated as if it were an integer value (the compiler will generate code to read enough bytes for an integer). The &myvalue operator gets the location of myvalue in memory 29 30 Quick Recap Pointer Mistakes Remember: *ptr - means look at the address pointed to by ptr &v - means get the location at which v is stored. Be careful - every year pointers cause lots of problems. The compiler often detects some common bugs but there are others which are not caught. The only way to solve these is to inspect your code manually. There are two common mistakes: The pointer is not initialised and then it is used (by default it will have a value of 0). This causes a segmentation fault. Pointers are used to access arrays and it is pushed outside of the array bounds. This either corrupts memory or gives a segmentation fault. 31 32

Basic Program Structure Basic Structure of a C Program: #include <stdio.h> Getting Started main(int argc, char *argv[]) { printf( Hello World ); } The includes act as import statements for libraries which are required. 33 34 Compilation Compilation Save your code in a file, e.g. myfile.c To compile a C file to object code: gcc -c myfile.c To link object code in an executable gcc -o myprogram myfile.o To run your executable:./myprogram Which compiler? Linux/Mac OSX - GCC Windows - Microsoft Visual C++/Intel C Compiler The code and seminar contents should work on these compilers but no promises! You can download the compilers above (mostly free). Any ANSI compliant C compiler will be fine for learning the language. You will not be able to compile the MPI coursework at home unless you compile and install the MPI runtimes. 35 36

End of Seminar Thanks for coming, see you next week. 37