MPI and C-Language Seminars 2010. Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
MPI and C-Language Seminars Seminar Plan  Week 1 – Introduction, Data Types, Control Flow, Pointers  Week 2 – Arrays, Structures, Enums, I/O,
COSC 120 Computer Programming
Chapter 10.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to a Programming Environment
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
The Java Programming Language
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright © – Curt Hill Types What they do.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
C is a high level language (HLL)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Java Programming Language Lecture27- An Introduction.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Last week: We talked about: History of C Compiler for C programming
CSE 374 Programming Concepts & Tools
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
The Machine Model Memory
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
C Programming Tutorial – Part I
Java package classes Java package classes.
C Basics.
Instructor: Ioannis A. Vetsikas
Computer Science 210 Computer Organization
Introduction to Abstract Data Types
Differences between Java and C
C Programming Getting started Variables Basic C operators Conditionals
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Variables and Constants
Introduction to C CS 3410.
Presentation transcript:

MPI and C-Language Seminars 2010

Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques needed for HPC coursework. C-bindings for the Message Passing Interface (MPI). Performance modelling.  We won’t be covering C++ (but many of the skills are transferable).

Seminar Plan (2/3)  Week 1 – Introduction, Data Types, Control Flow, Pointers  Week 2 – Arrays, Structures, Enums, I/O, Memory  Week 3 – Compiler Options and Debugging  Week 4 – MPI in C and Using the HPSG Cluster  Week 5 – “How to Build a Performance Model”  Week 6-9 – Coursework Troubleshooting (Seminar tutors available in their office)

Seminar Plan (3/3)  The first three weeks focus on developing basic C skills; much of this will be familiar.  Feel free to skip this if you already feel confident.  Unless you have done this course before, or are on a PhD programme with HPSG, it is a good idea to attend from Week 4 onwards.  Seminars will usually be held on Wednesdays, 11:00 – 12:00.

Contact Info  For C-related queries: John Pennycook  For performance modelling queries: Oli Perks  Availability: Always on . High Performance Systems Group (CS 2.04).  Resources:

Books  Worth consulting (in the library): The C Programming Language, (2 nd Edition), Kernighan & Ritchie, 1988 C – How to Program, (5 th Edition), Deitel, 2006  Online reference book linked at:

Overview of C  Much of the Java language builds on C/C++. Some of the language keywords and behaviour is very similar.  Beware – Though keywords and ‘names’ of types may be similar, they are often different.  Mixing between languages can introduce lots of bugs unless you’re sure what you’re doing.

Data Types in C  There are only four basic data types in C: 1. char – a single byte (8-bits) capable of holding an ASCII character. 2. int – an integer value, the size of which is based on the word size of the machine (often 32-bits). 3. float – a single precision floating point number (often 32-bit, IEEE). 4. double – a double precision floating point number (often 64-bit, IEEE).  Note: These sizes are the most commonly encountered, but may be different on some platforms (non-Intel/AMD).

Integers in C (1/2)  Two additional qualifiers to control the size of integer values: 1. short int – a ‘shorter’ version of int. 2. long int – a ‘longer’ version of int.  Rules: short int <= int <= long int short int >= 16 bits. int >= 16 bits. long int >= 32 bits.

Integers in C (2/2)  Two (more) additional qualifiers for integer types: 1. unsigned – interpret the value as starting from signed – allow negative numbers.  Use unsigned to increase the range of values available, provided you are sure negative values will not be required.  Mixing unsigned and signed values can cause bugs, so be careful.

Booleans in C  There is no boolean ‘type’ in C.  Booleans are implemented and evaluated as integer values: TRUE = 1 FALSE = 0  Keywords may not be pre-defined in every implementation of C.  Conversely: 0 = FALSE Non-zero = TRUE

Arrays in C (1/2)  Arrays in C are just allocated contiguous blocks of memory.  For example: int myarray[5]; allocates space for 5 integers in one contiguous block.  Arrays are not objects, so you cannot access properties (like length). You must record the maximum length of the array.

Arrays in C (2/2)  The name of the array variable can be used as a pointer: int* pointer = myarray;  myarray is a pointer to the first element in the array.  We will come back to this when we start looking at pointers in more detail...

Strings in C (1/2)  Strings are implemented using an array of characters, but with an additional element at the end of the string.  Strings must terminate with the null character, ‘\0’.  For example: “Hello” = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ }

Strings in C (2/2)  Most important functions are implemented in the string library.  To use the string library, put: #include at the top of your file.  All string functions in the C libraries will use the ‘\0’ character to detect the end of a string.

Control Flow in C (1/2)  if, while, do... while are identical, except that conditions must produce an integer ‘boolean’ result.  Consider: if (2 % 2) {... }... Will not run, since 2 % 2 = 0.

Control Flow in C (2/2)  for loops are not quite identical.  Depending on the language mode, you may not be able to declare variable inside a for-loop definition: int i; for (i = 0; i < 10; i++) { printf(“%d \n”, i); }

Operators in C  Mathematical operators in C are identical to those in Java. +, -, *, / =, ==, !=,, &&, || ! %(modulus) &, |  ++i, i++, --i, i--(pre/post-increment/decrement)

Pointers (1/4)  Pointers are references to locations in memory.  They are one of the biggest causes of bugs in software construction.  Worse – they are one of the biggest reasons coursework does not produce correct solutions on marking day.

Pointers (2/4)  To declare a pointer: int myvalue = 42; int* mypointer; mypointer = &myvalue;  int* creates a pointer to a location in memory. This is treated as if it were an integer value.  &myvalue is the location of myvalue in memory.

Pointers (3/4)  Remember: *ptr – look at the address pointed to by ptr. &v – get the address at which v is stored.  Be careful – every year pointers cause problems. The compiler can detect some common bugs, but not all of them... The only way to fix these is to inspect your code manually.

Pointers (4/4)  Common mistakes: 1. Uninitialised Pointers The pointer is not initialised before it is used. An un-initialised pointer has a value of 0 by default. This causes a segmentation fault. 2. Out-of-bounds Memory Access A pointer is used to access an array, and pushed outside of the array’s bounds. This either corrupts memory or gives a segmentation fault.

Hello World  Typical “Hello World” program: #include main (int argc, char *argv[]) { printf(“Hello world!\n”); }  The #include acts like a Java ‘import’ statement.

Compilation (1/2)  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 the executable:./myprogram

Compilation (2/2)  Compilers: Linux/Mac OS X – gcc Windows – Microsoft Visual C++/Intel C Compiler  Code and seminar contents should work with these compilers (but no promises).  You may not be able to compile the MPI coursework at home unless you compile and install the MPI runtimes.  Your code will be tested running on department machines; it may be best to do your compilation here.