1 Introduction to C Language Programming Language: Syntax

Total Page:16

File Type:pdf, Size:1020Kb

1 Introduction to C Language Programming Language: Syntax Programming Language: Syntax Syntax is the grammar of the language Analogous to rules in English Language ¾Missin g a peri od a fte r se nt en ce Introduction to C Language ¾Rules using verbs, nouns etc.. Overview, variables, Operators, Statements Most languages provide syntax check errors (Interprets/Compilers) ¾Syntax error messages may be helpful ¾Often, they are not ¾You gain experience with error messages after a while Based on slides © McGraw-Hill Additional material © 2004/2005 Lewis/Martin Modified by Diana Palsetia CIT 593 2 Programming Language Semantics C Programs are Compiled When the computer carries out your instructions (program) (text file containing Running or Executing a program hello.c C source code) Semantics is the meaning of the program We learn the semantics after we run or execute the program Basically we observe the output (text file containing After the executing program, the semantics of the program hello.s may or may be correct assembler source code) Semantic errors cause your answers to be wrong You may or may not get error messages hello ¾ E.g. Error Message – Dividing a number by zero (a.out) (binary executable file If your program is not doing what you want it to do, though it Containing machine code ) runs, the error is semantic We will use the GNU gcc compiler (v4.0 and higher) CIT 593 3 CIT 593 4 1 Syntax Compiling and Executing C programs Compilation Process C Entire mechanism is usually called the compiler Source and In the folder that contains the source code do for: Header Files Preprocessor Compiling the program Acts upon C preprocessor directives “Source-level” transformations C Preprocessor gcc –Wall progname.c ¾ Output is still C gcc tool for compiling process Compiler Compiler Source Code -Wall to display all warning Analysis Generates object (.o) file for each file Symbol Table progname.c is file that contains C code Target Code ¾ Machine instructions (binaries) Synthesis Will produce a file called a.out This file is not seen unless special compiler options are used Library Linker Executing Object Files Linker ./a.out Combine object files (including libraries) Executable into executable image Image CIT 593 5 CIT 593 6 Comments in C Basic C Elements Variables Begins with /* and ends with */ A data item upon which the programmer performs an operation A named space in memory Can span multiple lines E.g. z, counter Operators Predefined actions performed on data items Begins with // and ends with “end of line” E.g. *, +, /, ||, |, && Single-line comment Expressions ¾E.g. //This is a comment Operators combined with variables/literals to form expressions E.g. x * y Introduced in C++, later back-ported to C Statements Compiling with ansi standards, gives error A defined behavior Constitutes a unit of work for high-level language Ends with a semicolon. E.g. z = x * y; Why use comments ? Functions Help readers understand programs better Named group of statements Provides modularity to program Enforces, DRY principle (Do not Repeat Yourself) CIT 593 7 CIT 593 8 2 C program structure Variable Properties int main(){ Identifier: variable name /* code goes here Type: how data is interpreted, and how much space it */ needs return 0; Syntax:<type> <identifier> } Every C program must have the above format to develop Later we will discuss: application program(s) One of files must contain above structure Scope: is the region of the program in which the variable is ¾ main is a special function in C alive and accessible ¾ similar to Java’s main method Starting point for every program Storage: how C compiler allocates storage and whether or All C programs start and finish execution in the main not the variable loses its value when the section that Note: int main(int argc, char **argv) – main function can contains it has completed execution also take arguments like in Java CIT 593 9 CIT 593 10 Identifier: Variable Names Identifier Examples Any combination of letters, numbers, and Legal underscore (_) i wordsPerSecond same identifier words_per_ second Case sensitive _green "sum" is different than "Sum" aReally_longName_moreThan31chars aReally_longName_moreThan31characters Cannot begin with a number Usually, variables beginning with underscore Illegal are used only in special library routines 10sdigit ten'sdigit Only first 31 characters are definitely used done? reserved keyword Implementations can consider more characters if they like double CIT 593 11 CIT 593 12 3 Types Additional to Data Type C has several basic data types Literal int integer (at least 16 bits, commonly 32 bits) Values we write in a conventional form whose value is long integer (at least 32 bits) obvious float floating point (at least 32 bits) double floating point (commonly 64 bits) char character (at least 8 bits) Constant Exact size can vary, depending on processor Variable whose values do not change during the int is supposed to be "natural" integer size execution of the program ¾ 32 bits for most modern processors This is done by appending “const” before the type So how do I know the size? ¾ Use unary operator called sizeof. E.g. sizeof(int) – returns answer in bytes Symbol Signed vs. unsigned: Like constants but is preprocessor directive Default is 2’s complement signed integers Use “unsigned” keyword for unsigned numbers CIT 593 13 CIT 593 14 Literals C program showing Basic Syntax Integer #define RADIUS 15.0 123 /* decimal */ symbol -123 int main(){ 0x123 /* hexadecimal */ double area; double circum; DlDeclara tion a var ibliable (declaration statement) Floating point const double PI = 3.14159; 6.023 constant 6.023e23 /* 6.023 x 1023 */ literal 5E12 /* 5.0 x 1012 */ area = PI * RADIUS * RADIUS; Expression Character circum = 2 * PI * RADIUS; 'c' Statement '\n' /* newline */ return 0; Operators '\xA' /* ASCII 10 (0xA) */ } CIT 593 15 CIT 593 16 4 Symbol is C Preprocessor Directive Expression Symbol start with #define Expression Must go before the “main function syntax” Any combination of literals, variables, constants, operators, and function calls Every expression has a type, derived from the types Example: #define RADIUS 15.0 of its components (according to C typing rules) Before compiling, replace all instances of the string “RADIUS“ in the code with the string “15.0" Examples: Also known as a macro PI * RADIUS * RADIUS; Used for values that won't change during execution, but might change if the program is reused(Must x + sqrt(y) //sqrt is function with input y recompile) x % 6 == 0 CIT 593 17 CIT 593 18 Statement Operators Expresses a complete unit of work Three things to know about each operator Executed in sequential order (1) Function Simple statement ends with semicolon (note: “;” is not a What does it do? comme nt in C) (2) Precedence z = x * y; /* assign product to z */ In which order are operators combined? Example: y = y + 1; /* update y */ "a * b + c * d" is the same as "(a * b) + (c * d)" ; /* null statement */ because multiply (*) has a higher precedence than addition (+) sqrt(y); /* containing a function call*/ (3) Associativity Compound statement formed with braces In which order are operators of the same precedence combined? Syntactically equivalent to a simple statement Example: { z = x * y; y = y + 1; } "a - b - c" is the same as "(a - b) - c" because add/sub associate left-to-right CIT 593 19 CIT 593 20 5 Assignment Operator Assignment Operator (contd..) Changes the value of a variable All expressions evaluate to a value x = x + 4; Even ones with the assignment operator e.g. y = x = 3 For assignment, the result is the value assigned 1. Evaluate right-hand side. Usually (but not always) the value of the right-hand 2. Set value of left-hand side variable to result. side ¾Type conversion might make assigned value different than computed value e.g. int x = 15.6/3 = 5 Assigggnment associates right to left y = x = 3; y gets the value 3, because (x = 3) evaluates to the value 3 y = (x = 3); CIT 593 21 CIT 593 22 Arithmetic Operators Arithmetic Expressions If mixed types, smaller type is "promoted" to larger Symbol Operation Usage Precedence Assoc Example: x + 4.3 * multiply x * y 6l-to-r if x is int, converted to double and result is double divide 6l-to-r / x / y Integer division -- fraction is dropped % modulo x % y 6l-to-r Example: x / 3 + addition x + y 7l-to-r if x is int and x=5, result is 1 (not 1.666666...) - subtraction x - y 7l-to-r Storing mixed type expression values int x = 45/7.1; All associate left to right C compiler will do automatic down grade if storage is small. Java compiler will complain have higher precedence than * /%/ % + - Thus C has less type sa fe ty fea tures compare d to Java Example 2 + 3 * 4 vs. (2 + 3) * 4 Modulo -- result is remainder Example: x % 3 2 * 4 % 5 if x is int and x=5, result is 2 CIT 593 23 CIT 593 24 6 Relational Operators AND, OR, NOT Symbol Operation Usage Precedence Assoc Two states: TRUE=1, FALSE=0 > greater than x > y 9l-to-r >= greater than or equal x >= y 9l-to-r A B A AND B A B A OR B A NOT A < less than x < y 9l-to-r 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 <= less than or equal x <= y 9l-to-r 1 0 0 1 0 1 == equal x == y 10 l-to-r 1 1 1 1 1 1 != not equal x != y 10 l-to-r View n-bit number as a collection of n logical values Result is non-zero (TRUE) or zero (FALSE) Operation applied to each bit independently (bitwise operators) No boolean data type in C ¾Applications e.g.
Recommended publications
  • PRE PROCESSOR DIRECTIVES in –C LANGUAGE. 2401 – Course
    PRE PROCESSOR DIRECTIVES IN –C LANGUAGE. 2401 – Course Notes. 1 The C preprocessor is a macro processor that is used automatically by the C compiler to transform programmer defined programs before actual compilation takes place. It is called a macro processor because it allows the user to define macros, which are short abbreviations for longer constructs. This functionality allows for some very useful and practical tools to design our programs. To include header files(Header files are files with pre defined function definitions, user defined data types, declarations that can be included.) To include macro expansions. We can take random fragments of C code and abbreviate them into smaller definitions namely macros, and once they are defined and included via header files or directly onto the program itself it(the user defined definition) can be used everywhere in the program. In other words it works like a blank tile in the game scrabble, where if one player possesses a blank tile and uses it to state a particular letter for a given word he chose to play then that blank piece is used as that letter throughout the game. It is pretty similar to that rule. Special pre processing directives can be used to, include or exclude parts of the program according to various conditions. To sum up, preprocessing directives occurs before program compilation. So, it can be also be referred to as pre compiled fragments of code. Some possible actions are the inclusions of other files in the file being compiled, definitions of symbolic constants and macros and conditional compilation of program code and conditional execution of preprocessor directives.
    [Show full text]
  • Glibc and System Calls Documentation Release 1.0
    Glibc and System Calls Documentation Release 1.0 Rishi Agrawal <[email protected]> Dec 28, 2017 Contents 1 Introduction 1 1.1 Acknowledgements...........................................1 2 Basics of a Linux System 3 2.1 Introduction...............................................3 2.2 Programs and Compilation........................................3 2.3 Libraries.................................................7 2.4 System Calls...............................................7 2.5 Kernel.................................................. 10 2.6 Conclusion................................................ 10 2.7 References................................................ 11 3 Working with glibc 13 3.1 Introduction............................................... 13 3.2 Why this chapter............................................. 13 3.3 What is glibc .............................................. 13 3.4 Download and extract glibc ...................................... 14 3.5 Walkthrough glibc ........................................... 14 3.6 Reading some functions of glibc ................................... 17 3.7 Compiling and installing glibc .................................... 18 3.8 Using new glibc ............................................ 21 3.9 Conclusion................................................ 23 4 System Calls On x86_64 from User Space 25 4.1 Setting Up Arguements......................................... 25 4.2 Calling the System Call......................................... 27 4.3 Retrieving the Return Value......................................
    [Show full text]
  • Preview Objective-C Tutorial (PDF Version)
    Objective-C Objective-C About the Tutorial Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. This is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. This reference will take you through simple and practical approach while learning Objective-C Programming language. Audience This reference has been prepared for the beginners to help them understand basic to advanced concepts related to Objective-C Programming languages. Prerequisites Before you start doing practice with various types of examples given in this reference, I'm making an assumption that you are already aware about what is a computer program, and what is a computer programming language? Copyright & Disclaimer © Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book can retain a copy for future reference but commercial use of this data is not allowed. Distribution or republishing any content or a part of the content of this e-book in any manner is also not allowed without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] ii Objective-C Table of Contents About the Tutorial ..................................................................................................................................
    [Show full text]
  • C and C++ Preprocessor Directives #Include #Define Macros Inline
    MODULE 10 PREPROCESSOR DIRECTIVES My Training Period: hours Abilities ▪ Able to understand and use #include. ▪ Able to understand and use #define. ▪ Able to understand and use macros and inline functions. ▪ Able to understand and use the conditional compilation – #if, #endif, #ifdef, #else, #ifndef and #undef. ▪ Able to understand and use #error, #pragma, # and ## operators and #line. ▪ Able to display error messages during conditional compilation. ▪ Able to understand and use assertions. 10.1 Introduction - For C/C++ preprocessor, preprocessing occurs before a program is compiled. A complete process involved during the preprocessing, compiling and linking can be read in Module W. - Some possible actions are: ▪ Inclusion of other files in the file being compiled. ▪ Definition of symbolic constants and macros. ▪ Conditional compilation of program code or code segment. ▪ Conditional execution of preprocessor directives. - All preprocessor directives begin with #, and only white space characters may appear before a preprocessor directive on a line. 10.2 The #include Preprocessor Directive - The #include directive causes copy of a specified file to be included in place of the directive. The two forms of the #include directive are: //searches for header files and replaces this directive //with the entire contents of the header file here #include <header_file> - Or #include "header_file" e.g. #include <stdio.h> #include "myheader.h" - If the file name is enclosed in double quotes, the preprocessor searches in the same directory (local) as the source file being compiled for the file to be included, if not found then looks in the subdirectory associated with standard header files as specified using angle bracket. - This method is normally used to include user or programmer-defined header files.
    [Show full text]
  • B-Prolog User's Manual
    B-Prolog User's Manual (Version 8.1) Prolog, Agent, and Constraint Programming Neng-Fa Zhou Afany Software & CUNY & Kyutech Copyright c Afany Software, 1994-2014. Last updated February 23, 2014 Preface Welcome to B-Prolog, a versatile and efficient constraint logic programming (CLP) system. B-Prolog is being brought to you by Afany Software. The birth of CLP is a milestone in the history of programming languages. CLP combines two declarative programming paradigms: logic programming and constraint solving. The declarative nature has proven appealing in numerous ap- plications, including computer-aided design and verification, databases, software engineering, optimization, configuration, graphical user interfaces, and language processing. It greatly enhances the productivity of software development and soft- ware maintainability. In addition, because of the availability of efficient constraint- solving, memory management, and compilation techniques, CLP programs can be more efficient than their counterparts that are written in procedural languages. B-Prolog is a Prolog system with extensions for programming concurrency, constraints, and interactive graphics. The system is based on a significantly refined WAM [1], called TOAM Jr. [19] (a successor of TOAM [16]), which facilitates software emulation. In addition to a TOAM emulator with a garbage collector that is written in C, the system consists of a compiler and an interpreter that are written in Prolog, and a library of built-in predicates that are written in C and in Prolog. B-Prolog does not only accept standard-form Prolog programs, but also accepts matching clauses, in which the determinacy and input/output unifications are explicitly denoted. Matching clauses are compiled into more compact and faster code than standard-form clauses.
    [Show full text]
  • BUGS Code for Item Response Theory
    JSS Journal of Statistical Software August 2010, Volume 36, Code Snippet 1. http://www.jstatsoft.org/ BUGS Code for Item Response Theory S. McKay Curtis University of Washington Abstract I present BUGS code to fit common models from item response theory (IRT), such as the two parameter logistic model, three parameter logisitic model, graded response model, generalized partial credit model, testlet model, and generalized testlet models. I demonstrate how the code in this article can easily be extended to fit more complicated IRT models, when the data at hand require a more sophisticated approach. Specifically, I describe modifications to the BUGS code that accommodate longitudinal item response data. Keywords: education, psychometrics, latent variable model, measurement model, Bayesian inference, Markov chain Monte Carlo, longitudinal data. 1. Introduction In this paper, I present BUGS (Gilks, Thomas, and Spiegelhalter 1994) code to fit several models from item response theory (IRT). Several different software packages are available for fitting IRT models. These programs include packages from Scientific Software International (du Toit 2003), such as PARSCALE (Muraki and Bock 2005), BILOG-MG (Zimowski, Mu- raki, Mislevy, and Bock 2005), MULTILOG (Thissen, Chen, and Bock 2003), and TESTFACT (Wood, Wilson, Gibbons, Schilling, Muraki, and Bock 2003). The Comprehensive R Archive Network (CRAN) task view \Psychometric Models and Methods" (Mair and Hatzinger 2010) contains a description of many different R packages that can be used to fit IRT models in the R computing environment (R Development Core Team 2010). Among these R packages are ltm (Rizopoulos 2006) and gpcm (Johnson 2007), which contain several functions to fit IRT models using marginal maximum likelihood methods, and eRm (Mair and Hatzinger 2007), which contains functions to fit several variations of the Rasch model (Fischer and Molenaar 1995).
    [Show full text]
  • Cobol Vs Standards and Conventions
    Number: 11.10 COBOL VS STANDARDS AND CONVENTIONS July 2005 Number: 11.10 Effective: 07/01/05 TABLE OF CONTENTS 1 INTRODUCTION .................................................................................................................................................. 1 1.1 PURPOSE .................................................................................................................................................. 1 1.2 SCOPE ...................................................................................................................................................... 1 1.3 APPLICABILITY ........................................................................................................................................... 2 1.4 MAINFRAME COMPUTER PROCESSING ....................................................................................................... 2 1.5 MAINFRAME PRODUCTION JOB MANAGEMENT ............................................................................................ 2 1.6 COMMENTS AND SUGGESTIONS ................................................................................................................. 3 2 COBOL DESIGN STANDARDS .......................................................................................................................... 3 2.1 IDENTIFICATION DIVISION .................................................................................................................. 4 2.1.1 PROGRAM-ID ..........................................................................................................................
    [Show full text]
  • The Glib/GTK+ Development Platform
    The GLib/GTK+ Development Platform A Getting Started Guide Version 0.8 Sébastien Wilmet March 29, 2019 Contents 1 Introduction 3 1.1 License . 3 1.2 Financial Support . 3 1.3 Todo List for this Book and a Quick 2019 Update . 4 1.4 What is GLib and GTK+? . 4 1.5 The GNOME Desktop . 5 1.6 Prerequisites . 6 1.7 Why and When Using the C Language? . 7 1.7.1 Separate the Backend from the Frontend . 7 1.7.2 Other Aspects to Keep in Mind . 8 1.8 Learning Path . 9 1.9 The Development Environment . 10 1.10 Acknowledgments . 10 I GLib, the Core Library 11 2 GLib, the Core Library 12 2.1 Basics . 13 2.1.1 Type Definitions . 13 2.1.2 Frequently Used Macros . 13 2.1.3 Debugging Macros . 14 2.1.4 Memory . 16 2.1.5 String Handling . 18 2.2 Data Structures . 20 2.2.1 Lists . 20 2.2.2 Trees . 24 2.2.3 Hash Tables . 29 2.3 The Main Event Loop . 31 2.4 Other Features . 33 II Object-Oriented Programming in C 35 3 Semi-Object-Oriented Programming in C 37 3.1 Header Example . 37 3.1.1 Project Namespace . 37 3.1.2 Class Namespace . 39 3.1.3 Lowercase, Uppercase or CamelCase? . 39 3.1.4 Include Guard . 39 3.1.5 C++ Support . 39 1 3.1.6 #include . 39 3.1.7 Type Definition . 40 3.1.8 Object Constructor . 40 3.1.9 Object Destructor .
    [Show full text]
  • Section “Common Predefined Macros” in the C Preprocessor
    The C Preprocessor For gcc version 12.0.0 (pre-release) (GCC) Richard M. Stallman, Zachary Weinberg Copyright c 1987-2021 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation. A copy of the license is included in the section entitled \GNU Free Documentation License". This manual contains no Invariant Sections. The Front-Cover Texts are (a) (see below), and the Back-Cover Texts are (b) (see below). (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. i Table of Contents 1 Overview :::::::::::::::::::::::::::::::::::::::: 1 1.1 Character sets:::::::::::::::::::::::::::::::::::::::::::::::::: 1 1.2 Initial processing ::::::::::::::::::::::::::::::::::::::::::::::: 2 1.3 Tokenization ::::::::::::::::::::::::::::::::::::::::::::::::::: 4 1.4 The preprocessing language :::::::::::::::::::::::::::::::::::: 6 2 Header Files::::::::::::::::::::::::::::::::::::: 7 2.1 Include Syntax ::::::::::::::::::::::::::::::::::::::::::::::::: 7 2.2 Include Operation :::::::::::::::::::::::::::::::::::::::::::::: 8 2.3 Search Path :::::::::::::::::::::::::::::::::::::::::::::::::::: 9 2.4 Once-Only Headers::::::::::::::::::::::::::::::::::::::::::::: 9 2.5 Alternatives to Wrapper #ifndef ::::::::::::::::::::::::::::::
    [Show full text]
  • A Quick Reference to C Programming Language
    A Quick Reference to C Programming Language Structure of a C Program #include(stdio.h) /* include IO library */ #include... /* include other files */ #define.. /* define constants */ /* Declare global variables*/) (variable type)(variable list); /* Define program functions */ (type returned)(function name)(parameter list) (declaration of parameter types) { (declaration of local variables); (body of function code); } /* Define main function*/ main ((optional argc and argv arguments)) (optional declaration parameters) { (declaration of local variables); (body of main function code); } Comments Format: /*(body of comment) */ Example: /*This is a comment in C*/ Constant Declarations Format: #define(constant name)(constant value) Example: #define MAXIMUM 1000 Type Definitions Format: typedef(datatype)(symbolic name); Example: typedef int KILOGRAMS; Variables Declarations: Format: (variable type)(name 1)(name 2),...; Example: int firstnum, secondnum; char alpha; int firstarray[10]; int doublearray[2][5]; char firststring[1O]; Initializing: Format: (variable type)(name)=(value); Example: int firstnum=5; Assignments: Format: (name)=(value); Example: firstnum=5; Alpha='a'; Unions Declarations: Format: union(tag) {(type)(member name); (type)(member name); ... }(variable name); Example: union demotagname {int a; float b; }demovarname; Assignment: Format: (tag).(member name)=(value); demovarname.a=1; demovarname.b=4.6; Structures Declarations: Format: struct(tag) {(type)(variable); (type)(variable); ... }(variable list); Example: struct student {int
    [Show full text]
  • Lecture 2: Introduction to C Programming Language [email protected]
    CSCI-UA 201 Joanna Klukowska Lecture 2: Introduction to C Programming Language [email protected] Lecture 2: Introduction to C Programming Language Notes include some materials provided by Andrew Case, Jinyang Li, Mohamed Zahran, and the textbooks. Reading materials Chapters 1-6 in The C Programming Language, by B.W. Kernighan and Dennis M. Ritchie Section 1.2 and Aside on page 4 in Computer Systems, A Programmer’s Perspective by R.E. Bryant and D.R. O’Hallaron Contents 1 Intro to C and Unix/Linux 3 1.1 Why C?............................................................3 1.2 C vs. Java...........................................................3 1.3 Code Development Process (Not Only in C).........................................4 1.4 Basic Unix Commands....................................................4 2 First C Program and Stages of Compilation 6 2.1 Writing and running hello world in C.............................................6 2.2 Hello world line by line....................................................7 2.3 What really happens when we compile a program?.....................................8 3 Basics of C 9 3.1 Data types (primitive types)..................................................9 3.2 Using printf to print different data types.........................................9 3.3 Control flow.......................................................... 10 3.4 Functions........................................................... 11 3.5 Variable scope........................................................ 12 3.6 Header files.........................................................
    [Show full text]
  • GPU Directives
    OPENACC® DIRECTIVES FOR ACCELERATORS NVIDIA GPUs Reaching Broader Set of Developers 1,000,000’s CAE CFD Finance Rendering Universities Data Analytics Supercomputing Centers Life Sciences 100,000’s Oil & Gas Defense Weather Research Climate Early Adopters Plasma Physics 2004 Present Time 2 3 Ways to Accelerate Applications with GPUs Applications Programming Libraries Directives Languages “Drop-in” Quickly Accelerate Maximum Acceleration Existing Applications Performance 3 Directives: Add A Few Lines of Code OpenMP OpenACC CPU CPU GPU main() { main() { double pi = 0.0; long i; double pi = 0.0; long i; #pragma acc parallel #pragma omp parallel for reduction(+:pi) for (i=0; i<N; i++) for (i=0; i<N; i++) { { double t = (double)((i+0.05)/N); double t = (double)((i+0.05)/N); pi += 4.0/(1.0+t*t); pi += 4.0/(1.0+t*t); } } printf(“pi = %f\n”, pi/N); printf(“pi = %f\n”, pi/N); } } OpenACC: Open Programming Standard for Parallel Computing “ OpenACC will enable programmers to easily develop portable applications that maximize the performance and power efficiency benefits of the hybrid CPU/GPU architecture of Titan. ” Buddy Bland Titan Project Director Easy, Fast, Portable Oak Ridge National Lab “ OpenACC is a technically impressive initiative brought together by members of the OpenMP Working Group on Accelerators, as well as many others. We look forward to releasing a version of this proposal in the next release of OpenMP. ” Michael Wong CEO, OpenMP http://www.openacc-standard.org/ Directives Board OpenACC Compiler directives to specify parallel regions
    [Show full text]