Sung-Eun Choi and Steve Deitz Cray Inc.

Similar documents
Note: There is more in this slide deck than we will be able to cover, so consider it a reference and overview

Fast prototyping. Produc2on- grade. writeln( Hello, world! ); module Hello { proc main() { writeln( Hello, world! ); } }

Base Language COMPUTE STORE ANALYZE

Productive Programming in Chapel:

Decaf Language Reference Manual

Lexical Considerations


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

Review of the C Programming Language for Principles of Operating Systems

Chapel: An Emerging Parallel Programming Language. Thomas Van Doren, Chapel Team, Cray Inc. Northwest C++ Users Group April 16 th, 2014

The SPL Programming Language Reference Manual

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Lexical Considerations

Chapel: Features. Brad Chamberlain Cray Inc. CSEP 524 May 20, 2010

Programming for Engineers Iteration

UNIT- 3 Introduction to C++

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Introduction to Parallel Programming

Scala : an LLVM-targeted Scala compiler

Ruby: Introduction, Basics

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Decaf Language Reference

Java Basic Programming Constructs

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Introduction to Programming Using Java (98-388)

The PCAT Programming Language Reference Manual

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Fundamentals of Programming

1 Lexical Considerations

The Warhol Language Reference Manual

Review of the C Programming Language

Steve Deitz Cray Inc.

LECTURE 18. Control Flow

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Computer System and programming in C

Intermediate Code Generation

Short Notes of CS201

C - Basics, Bitwise Operator. Zhaoguo Wang

.Net Technologies. Components of.net Framework

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

IC Language Specification

Coral Programming Language Reference Manual

CS201 - Introduction to Programming Glossary By

Chapter 7. Expressions and Assignment Statements ISBN

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Expressions and Assignment

An Overview of Chapel

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Ruby: Introduction, Basics

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Sung-Eun Choi and Steve Deitz Cray Inc.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

. Programming in Chapel. Kenjiro Taura. University of Tokyo

Chapter 2: Basic Elements of C++

Chapter 7. Expressions and Assignment Statements

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Chapter 7. Expressions and Assignment Statements

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSE 590o: Chapel. Brad Chamberlain Steve Deitz Chapel Team. University of Washington September 26, 2007

CSE I-Sem)

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

egrapher Language Reference Manual

Programming. Elementary Concepts

Introduction to Programming using C++

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

A Short Summary of Javali

Flow Language Reference Manual

Chapter 3: Operators, Expressions and Type Conversion

Summary of Go Syntax /

Outline. 1 About the course

Expressions and Assignment Statements

Computer Components. Software{ User Programs. Operating System. Hardware

COMP520 - GoLite Type Checking Specification

Motivation for typed languages

The role of semantic analysis in a compiler

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSCE 110 PROGRAMMING FUNDAMENTALS

Protection Levels and Constructors The 'const' Keyword

Introduction to Programming (Java) 2/12

Final CSE 131B Spring 2004

SECTION II: LANGUAGE BASICS

Fundamental of Programming (C)

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Lecture no

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Task: a unit of parallel work in a Chapel program all Chapel parallelism is implemented using tasks

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Transcription:

Sung-Eun Choi and Steve Deitz Cray Inc.

Fast prototyping writeln( hello, world ); Production-grade module HelloWorld { def main() { writeln( hello, world ); } } Chapel: Language Basics 2

Syntax Basics from C and Modula Influences from many other languages Semantics Imperative, block-structured Optional object-oriented programming (OOP) Elided types for convenience and generic coding Static typing for performance and safety Design points No pointers and few references No compiler-inserted array temporaries Chapel: Language Basics 3

ZPL, HPF: data parallelism, index sets, distributed arrays CRAY MTA C/Fortran: task parallelism, synchronization CLU, Ruby, Python: iterators ML, Scala, Matlab, Perl, Python, C#: latent types Java, C#: OOP, type safety C++: generic programming/templates Chapel: Language Basics 4

High-Level Comments Elementary Concepts Lexical structure Types, variables, and constants Input and output Data Structures and Control Miscellaneous Chapel: Language Basics 5

Comments /* standard C-style */ // standard C++ style Identifiers Composed of A-Z, a-z, 0-9, _, and $ Starting with A-Z, a-z, _, and $ Case-sensitive Whitespace-aware Composed of spaces, tabs, and linefeeds Separates tokens and ends //-comments Chapel: Language Basics 6

Type Syntax Description Default Value Default Bit Width Examples Supported Bit Widths bool logical value false impl-dep 8, 16, 32, 64 int signed integer 0 32 8, 16, 32, 64 uint unsigned integer 0 32 8, 16, 32, 64 real real floating point 0.0 64 32, 64 imag imaginary floating point 0.0i 64 32, 64 complex complex floating points 0.0 + 0.0i 128 64, 128 string character string N/A N/A primitive-type: type-name [( bit-width )] int(64) // 64-bit int real(32) // 32-bit real uint // 32-bit uint Chapel: Language Basics 7

Syntax declaration: var identifier [: type] [= init-expr] const identifier [: type] [= init-expr] param identifier [: type] [= init-expr] Semantics Constness: const run-time, param compile-time Omitted init-expr: value is assigned default for type Omitted type: type is inferred from init-expr Examples var count: int; // initialized to 0 const pi: real = 3.14159; param debug = true; // inferred to be bool Chapel: Language Basics 8

Syntax config-declaration: config declaration Semantics Supports command-line overrides Must be declared at module (file) scope Examples config param intsize = 32; config const start: int(intsize) = 1; config var epsilon = 0.01; % chpl -sintsize=16 myprogram.chpl % a.out --start=2 --epsilon=0.001 Chapel: Language Basics 9

Operator Description Associativity Overloadable : cast left no ** exponentiation right yes! ~ logical and bitwise negation right yes * / % multiplication, division and modulus left yes unary + - positive identity and negation right yes + - addition and subtraction left yes << >> shift left and shift right left yes <= >= < > ordered comparison left yes ==!= equality comparison left yes & bitwise/logical and left yes ^ bitwise/logical xor left yes bitwise/logical or left yes && short-circuiting logical and left via istrue short-circuiting logical or left via istrue Chapel: Language Basics 10

Kind Description = simple assignment += -= *= /= %= **= &= = ^= &&= = <<= >>= <=> swap compound assignment (e.g., x += y; is equivalent to x = x + y;) Chapel: Language Basics 11

Notes Type int(32) int(64) uint(32) uint(64) real(32) real(64) imag(32) imag(64) Valid Target Types int(64), real(64), complex(128) real(64), complex(128) int(64), uint(64), real(64), complex(128) real(64), complex(128) real(64), complex(64), complex(128) complex(128) imag(64), complex(64), complex(128) complex(128) Generally no loss of information (exceptions in red) Real values do not coerce to integers const threepointzero: real = 3; // coerces to real const c = 1.0 + 2; // uses + over real Chapel: Language Basics 12

Syntax cast-expr: expr : type Semantics Converts type of expr to type Supported between all primitive types Examples const three = pi:int; const pistring = pi:string; const c = (1.0, 2.0):complex; Chapel: Language Basics 13

Input read(expr-list): reads values into the arguments read(type-list): returns values read of given types readln(...) variant: also reads through new line Output write(expr-list): writes arguments writeln(...) variant: also writes new line Support for all types (including user-defined) File and string I/O via method variants of the above Chapel: Language Basics 14

High-Level Comments Elementary Concepts Data Structures and Control Tuples Ranges Arrays For loops Traditional constructs Miscellaneous Chapel: Language Basics 15

Syntax tuple-expr: ( expr, expr-list ) expr-list: expr expr, expr-list Semantics Light-weight first-class data structure Examples var i3: (int, int, int) = (1, 2, 3); var i3_2: 3*int = (4, 5, 6); var triple: (int, string, real) = (7, eight, 9.0); Chapel: Language Basics 16

Syntax range-expr: [low].. [high] [by stride] Semantics Regular sequence of integers stride > 0: low, low+stride, low+2*stride,... high stride < 0: high, high+stride, high+2*stride,... low Default stride = 1, default low or high is unbounded Examples 1..6 by 2 // 1, 3, 5 1..6 by -1 // 6, 5, 4, 3, 2, 1 3.. by 3 // 3, 6, 9, 12,... Chapel: Language Basics 17

Syntax array-type: [ index-set-expr ] elt-type Semantics Stores an element of elt-type for each index Examples var A: [1..3] int, // 3-element array of ints B: [1..3, 1..5] real, // 2D array of reals C: [1..3][1..5] real; // array of arrays of reals Much more on arrays in data parallelism part Chapel: Language Basics 18

Syntax for-loop: for index-expr in iteratable-expr { stmt-list } Semantics Executes loop body once per loop iteration Indices in index-expr are new variables Examples var A: [1..3] string = ( DO, RE, MI ); for i in 1..3 do write(a(i)); // DO RE MI for a in A { a += LA ; write(a); } // DOLA RELA MILA Chapel: Language Basics 19

Syntax zipper-for-loop: for index-expr in ( iteratable-exprs ) { stmt-list } tensor-for-loop: for index-expr in [ iteratable-exprs ] { stmt-list } Semantics Zipper iteration is over all yielded indices pair-wise Tensor iteration is over all pairs of yielded indices Examples for i in (1..2, 1..2) do // (1,1), (2,2) for i in [1..2, 1..2] do // (1,1), (1,2), (2,1), (2,2) Chapel: Language Basics 20

Conditional statements if cond then computea() else computeb(); While loops while cond { compute(); } Select statements do { compute(); } while cond; select key { when value1 do compute1(); when value2 do compute2(); otherwise compute3(); } Chapel: Language Basics 21

High-Level Comments Elementary Concepts Data Structures and Control Miscellaneous Functions and iterators Records and classes Generics Other basic language features Chapel: Language Basics 22

Example to compute the area of a circle def area(radius: real) return 3.14 * radius**2; writeln(area(2.0)); // 12.56 Example of function arguments def writecoord(x: real = 0.0, y: real = 0.0) { writeln( (, x,,, y, ) ); } writecoord(2.0); // (2.0, 0.0) writecoord(y=2.0); // (0.0, 2.0) Chapel: Language Basics 23

An abstraction for loop control Yields (generates) values for consumption Otherwise, like a function Example def string_chars(s: string) { for i in 1..length(s) do yield s.substring(i); } for c in string_chars(s) do... Chapel: Language Basics 24

Separation of concerns Loop logic is abstracted from computation Supports efficient implementations Simple iterators can be inlined Complex iterators also admit optimization Chapel: Language Basics 25

Value-based objects Value-semantics (assignment copies fields) Contain variable definitions (fields) Contain function definitions (methods) Similar to C++ classes Example record circle { var x, y, radius: real; } var c1, c2: circle; c1.x = 1.0; c1.y = 1.0; c1.radius = 2.0; c2 = c1; // copy of value Chapel: Language Basics 26

Reference-based objects Reference-semantics (assignment aliases) Dynamic allocation Dynamic dispatch Similar to Java classes Example class circle { var x, y, radius: real; } var c1, c2: circle; c1 = new circle(x=1.0, y=1.0, radius=2.0); c2 = c1; // c2 is an alias of c1 delete c1; Chapel: Language Basics 27

Methods are functions associated with types. def circle.area() return 3.14 * radius**2; writeln(c1.area()); Methods can be defined for any type. def int.square() return this**2; writeln(5.square()); Chapel: Language Basics 28

Generic functions can be defined by explicit type and param arguments: def foo(type t, x: t) {... def bar(param bitwidth, x: int(bitwidth)) {... Or simply by eliding an argument type (or type part): def goo(x, y) {... def sort(a: []) {... Generic functions are replicated for each unique instantiation: foo(int, x); // copy of foo() with t==int foo(string, x); // copy of foo() with t==string goo(4, 2.2); // copy of goo() with int and real args Chapel: Language Basics 29

Generic types can be defined by explicit type and param fields: class Table { param numfields: int;... class Matrix { type elttype;... Or simply by eliding a field type (or type part): record Triple { var x, y, z; } Generic types are replicated for each unique instantiation: // copy of Table with 10 fields var myt: Table(10); // copy of Triple with x:int, y:int, z:real var my3: Triple (int,int,real) = new Triple(1,2,3.0); Chapel: Language Basics 30

Unions Enumerated types Range and domain by and # operators Type select statements Function instantiation constraints (where clauses) Formal argument intents (in, out, inout, const) User-defined compiler warnings and errors Chapel: Language Basics 31

Fixed length strings Binary I/O Parallel I/O Interoperability with other languages More advanced OO features Chapel: Language Basics 32

High-Level Comments Elementary Concepts Lexical structure Types, variables, and constants Input and output Data Structures and Control Tuples Ranges Arrays For loops Traditional constructs Miscellaneous Functions and iterators Records and classes Generics Other basic language features Chapel: Language Basics 33