Applied C Fri

Similar documents
PHP Extension Development with C++

Hydra. A. Augusto Alves Jr and M.D. Sokoloff. University of Cincinnati

CS 7B - Fall Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm

Operating Systems. VII. Synchronization

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

Introduction to functions

Python. chrysn

Modern Functional Programming and Actors With Scala and Akka

Meta-programming & you

C++ For Science and Engineering Lecture 8

CompA - Complex Analyzer

Recursion. Recursion. Steven Zeil. November 25, 2013

NGsolve::Give me your element

Functional Programming with F# Overview and Basic Concepts

C++ For Science and Engineering Lecture 17

MiniMat: Matrix Language in OCaml LLVM

Python. Tutorial. Jan Pöschko. March 22, Graz University of Technology

C++ For Science and Engineering Lecture 14

Structural Induction

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

Lecture 16 More Profiling: gperftools, systemwide tools: oprofile, perf, DTrace, etc.

Language and Compiler Support for Auto-Tuning Variable-Accuracy Algorithms

Numerical differentiation

PetaBricks: Variable Accuracy and Online Learning

Olena Quick Reference Guide LRDE

The Viterbi Algorithm EECS 869: Error Control Coding Fall 2009

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

The Notion of the Concept Instance : Problems in Modeling Concept Change in SKOS

Linear Algebra (part 1) : Vector Spaces (by Evan Dummit, 2017, v. 1.07) 1.1 The Formal Denition of a Vector Space

Kirsten Lackner Solberg. Dept. of Math. and Computer Science. Odense University, Denmark

Policy Based Class Design

26. Januar Introduction to Computational Semantics

Logic and Philosophical Logic. 1 Inferentialism. Inferentialism and Meaning Underdetermination

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

Bloom Filter Redux. CS 270 Combinatorial Algorithms and Data Structures. UC Berkeley, Spring 2011

An Introduction to Z3

C++ For Science and Engineering Lecture 10

Monitoring System for ALICE Surface Areas

Programsystemkonstruktion med C++: Övning 1. Karl Palmskog Translated by: Siavash Soleimanifard. September 2011

Structure. Background. them to their higher order equivalents. functionals in Standard ML source code and converts

Logic: Propositional Logic (Part I)

Parametric Optimization with Evolutionary Strategies in Particle Physics

CSCI 490 problem set 6

COMP 204. Object Oriented Programming (OOP) Mathieu Blanchette

A Forgery Attack on the Candidate LTE Integrity Algorithm 128-EIA3 (updated version)

1 Introduction. 2 First Order Logic. 3 SPL Syntax. 4 Hoare Logic. 5 Exercises

CS 7B - Spring Assignment: Adapting the calculator for bitwise expressions. due 2/21/18

From syntax to semantics of Dependent Type Theories - Formalized

The DCMI Tools Community Dublin Core 2007 Singapore 29 August 2007

Functional Database Query Languages as. Typed Lambda Calculi of Fixed Order. Gerd G. Hillebrand and Paris C. Kanellakis

Supplementary Notes on Inductive Definitions

Typed Arithmetic Expressions

Parallel Polynomial Evaluation

Big-O Notation and Complexity Analysis

Quantile Precision Issues in CUDA

Business Analytics and Data Mining Modeling Using R Prof. Gaurav Dixit Department of Management Studies Indian Institute of Technology, Roorkee

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Applied Logic. Lecture 1 - Propositional logic. Marcin Szczuka. Institute of Informatics, The University of Warsaw

Econometrics I. Ricardo Mora

o is a type symbol. There are no other type symbols.

Congratulations you're done with CS103 problem sets! Please evaluate this course on Axess!

Last name Department Computer Name Legi No. Date Total. Always keep your Legi visible on the table.

Lecture Notes on Inductive Definitions

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia

Sunrise: Patrik Jonsson. Panchromatic SED Models of Simulated Galaxies. Lecture 2: Working with Sunrise. Harvard-Smithsonian Center for Astrophysics

Designing and Evaluating Generic Ontologies

arxiv: v1 [math.co] 3 Feb 2014

Price Competition and Endogenous Valuation in Search Advertising

Discriminant Analysis Documentation

A High Level Programming Language for Quantum Computing

Complete problems for classes in PH, The Polynomial-Time Hierarchy (PH) oracle is like a subroutine, or function in

ASSIGNMENT 2 TIPS AND TRICKS

Hydra. A library for data analysis in massively parallel platforms. A. Augusto Alves Jr and Michael D. Sokoloff

ITI Introduction to Computing II

Type Inference. For the Simply-Typed Lambda Calculus. Peter Thiemann, Manuel Geffken. Albert-Ludwigs-Universität Freiburg. University of Freiburg

A BRIEF INTRODUCTION TO TYPED PREDICATE LOGIC

Runtime Analysis of 4 VA HiCuM Versions with and without Internal Solver

Introduction to lambda calculus Part 6

INF Models of concurrency

8.1 THE LANGUAGE OF MOTION

Scripting Languages Fast development, extensible programs

An Automotive Case Study ERTSS 2016

Lambert s problem, to find the unique conic trajectory that

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Industrial Technology: Intro to Industrial Technology Crosswalk to AZ Math Standards

Programming Languages and Types

Desirable properties of decompositions 1. Decomposition of relational schemes. Desirable properties of decompositions 3

Fall 1999 Formal Language Theory Dr. R. Boyer. 1. There are other methods of nding a regular expression equivalent to a nite automaton in

Kurt Schmidt. June 5, 2017

CISC 1100: Structures of Computer Science

2. Write a recursive function to add the first n terms of the alternating harmonic series:

Productivity-oriented software design for geoscientific modelling

Models of Computation,

CRISP: Capture-Recapture Interactive Simulation Package

Nested Differentiation and Symmetric Hessian Graphs with ADTAGEO

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Automated Reasoning. Lecture 9: Isar A Language for Structured Proofs

Concurrent Non-malleable Commitments from any One-way Function

Proofs Propositions and Calculuses

Transcription:

Applied C++11 2013-01-25 Fri

Outline Introduction Auto-Type Inference Lambda Functions Threading Compiling

C++11 C++11 (formerly known as C++0x) is the most recent version of the standard of the C++ Approved by ISO(International Organization for Standardization) on 12 August 2011 Includes several additions to the core language and extends the C++ standard library

Core language usability enhancements: Primary purpose is to make the language easier to use Improve type safety Minimize code repetition Make erroneous code less likely

Auto Type Inference The compiler can infer the type of a variable at the point of declaration: i n t x = 5 ; auto x = 4 ; Very helpful when working with templates and iterators: v e c t o r <i n t > vec ; auto i t r = vec. i t e r a t o r ( ) ; // i n s t e a d o f v e c t o r <i n t > : : i t e r a t o r i t r

Study Case t e m p l a t e <typename BuiltType, typename B u i l d e r > void makeandprocessobject ( const B u i l d e r& b u i l d e r ) { B u i l t T y p e v a l = b u i l d e r. makeobject ( ) ; // do s t u f f w i t h v a l two necessary template parameters type of the builder object type of the object being built MyObjBuilder b u i l d e r ; makeandprocessobject<myobj>( b u i l d e r ) ;

Study Case (cont.) By using `auto' no longer need to write the specic type of the built object t e m p l a t e <typename B u i l d e r > void makeandprocessobject ( const B u i l d e r& b u i l d e r ) { auto v a l = b u i l d e r. makeobject ( ) ; // do s t u f f w i t h v a l MyObjBuilder b u i l d e r ; makeandprocessobject ( b u i l d e r ) ;

Lambda Functions Lambda Function: is a function dened, and possibly called, without being bound to an identier you can write inline in your source code convenient to pass as an argument to a higher-order function allow to create entirely new ways of writing programs

Basic Lambda Syntax #i n c l u d e <i o s t r e a m > u s i n g namespace s t d ; i n t main ( ) { [ ] ( ) { cout << " H e l l o, World! " ; ( ) ; []: capture specier, species for the compiler creation of a lambda function (): argument list What about return values?

More on the Lambda Syntax If there is no return statement, uses void as default If you have a simple return expression, the compiler will deduce the type of the return value // c o m p i l e r knows t h i s r e t u r n s an i n t e g e r [ ] ( ) { r e t u r n 1 ; specify the return value explicitly // now we ' r e t e l l i n g t h e c o m p i l e r what we want [ ] ( ) > i n t { r e t u r n 1 ;

Example: Sorting a list We create a list of cities. // D e f i n e a c l a s s f o r a c i t y c l a s s C i t y { p u b l i c : i n t p o p u l a t i o n ; i n t f o u n d a t i o n _ y e a r ; i n t s u r f a c e _ a r e a ; ; i n t main ( ) { s t d : : l i s t <C i t y > c i t i e s ; // F i l l t h e l i s t w i t h d a t a To sort a set of objects, we have to dene a comparison function.

Example: Sorting a list (cont.) In the STL, comparison functions have two arguments, and return true if the rst argument goes before the second. To sort by population, we can dene a compare function: b o o l compare ( C i t y a, C i t y b ) { r e t u r n a. p o p u l a t i o n < b. p o p u l a t i o n ;... c i t i e s. s o r t ( compare ) ; Alternatively, we can use a lambda function: c i t i e s. s o r t ( [ ] ( C i t y a, C i t y b) >b o o l { r e t u r n a. p u p u l a t i o n < b. p o p u l a t i o n ; ) ;

Multithreading C++11 threads are more convenient than directly using Posix threads. #i n c l u d e <t h r e a d > i n t main ( ) {... // D e c l a r i n g t h r e a d s s t d : : t h r e a d t ; // A s s i g n a f u n c t i o n t = s t d : : t h r e a d ( f u n c t i o n, a r g u m e n t s ) ;... // F i n a l i z i n g t. j o i n ( ) ; Compile with the ag -pthread

Multithreading example: Dot product // D e f i n i t i o n o f a c l a s s t o s t o r e t h e v a l u e s c l a s s P a r t i a l D P { p u b l i c : double d o t p r o d u c t ; double * x ; double * y ; i n t f i r s t ; // F i r s t i n d e x o f a r r a y i n t l e n g t h ; // Count o f e l e m e n t s P a r t i a l D P ( ) : d o t p r o d u c t ( 0 ) { s e t ( double * x i n, double * y i n, i n t f, i n t l ) { x = x i n ; y = y i n ; f i r s t = f ; l e n g t h = l ; ; o p e r a t o r ( ) ( ) { f o r ( i n t i = f i r s t ; i < f i r s t + l e n g t h ; i ++) { d o t p r o d u c t += x [ i ] + y [ i ] ;

Multithreading example (cont.) #i n c l u d e <t h r e a d > const i n t s i z e = 1 0 0 0, n t h r e a d s = 4 ; i n t main ( ) { s t d : : t h r e a d t [ n t h r e a d s ] ; double x [ s i z e ], y [ s i z e ] ; i n t s u b s i z e = s i z e / n t h r e a d s ; // Assume no r e m a i n d e r P a r t i a l D P dp [ n t h r e a d s ] ; f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++){ dp [ i ]. s e t ( x, y, i * s u b s i z e, s u b s i z e ) ; t [ i ] = s t d : : t h r e a d ( dp [ i ] ) ; // Compute f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++) t [ i ]. j o i n ( ) ; // A c cumulate t h e p r o d u c t double r e s u l t = 0 ; f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++) r e s u l t += dp [ i ]. d o t p r o d u c t ;

Compiling C++11 With gcc, C++11 features can be compiled by using g++ -std=c++11 <args>