Introduction Slide 1/20. Introduction. Fall Semester. Parallel Computing

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

Array Initialization

Lectures 5-6: Introduction to C

PRINCIPLES OF OPERATING SYSTEMS

Work relative to other classes

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Algorithms, Data Structures, and Problem Solving

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

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CSE 303: Concepts and Tools for Software Development

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

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

Programming in C UVic SEng 265

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

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

CS 61c: Great Ideas in Computer Architecture

Lectures 5-6: Introduction to C

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

C programming basics T3-1 -

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

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

Crash Course into. Prof. Dr. Renato Pajarola

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Today s Learning Objectives

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

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

A brief introduction to C++

CSE 303 Lecture 8. Intro to C programming

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

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Arrays and Pointers in C. Alan L. Cox

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CSE 12 Spring 2018 Week One, Lecture Two

6.S096 Lecture 1 Introduction to C

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

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

High Performance Programming Programming in C part 1

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CSE 12 Spring 2016 Week One, Lecture Two

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

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Dynamic arrays / C Strings

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Introduction to C Language

Introduction to C Programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Midterm Exam Review Slides

CS 241 Data Organization Pointers and Arrays

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

CSC 2400: Computer Systems. Arrays and Strings in C

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

ECE264 Fall 2013 Exam 1, September 24, 2013

CSE 220: Systems Programming

CSCI 2021: Introduction

CS 211 Programming I for Engineers

CSE 124 Discussion (10/3) C/C++ Basics

CSC 1600 Memory Layout for Unix Processes"

CS 61c: Great Ideas in Computer Architecture

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Computers Programming Course 5. Iulian Năstac

CSC 2400: Computer Systems. Arrays and Strings in C

CSC231 C Tutorial Fall 2018 Introduction to C

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

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Arrays and Memory Management

Programming in C. Pointers and Arrays

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary


CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Computer Systems C S Cynthia Lee

Your first C and C++ programs

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

CS201 - Lecture 1 The C Programming Language

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

This is CS50. Harvard University Fall Quiz 0 Answer Key

Introduction to C An overview of the programming language C, syntax, data types and input/output

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Programming in C and C++

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

Personal SE. Functions, Arrays, Strings & Command Line Arguments

COMP1917: Computing 1 1. Introduction

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

COMP1917: Computing 1 1. Introduction

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

Chapter 11 Introduction to Programming in C

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Problem Solving and 'C' Programming

C Language, Token, Keywords, Constant, variable

EE 209: Programming Structures for Electrical Engineering

ENSC Multimedia Communications Engineering Topic 4: Huffman Coding 2

Transcription:

Introduction Slide 1/20 Introduction Fall Semester

Introduction Slide 2/20 Topic Outline Programming in C Pointers Input-Output Embarrassingly Parallel Message Passing Interface Projectile motion Fractal generation Cellular Automata OpenGL graphics Coupled Systems Heat Equation Nearest neighbor N-Body problem Round robin Additional Tools XMT and OpenMP Threads and CUDA Sockets

Introduction Slide 3/20 The C Programming Language: C is quirky, flawed, and an enormous success. Dennis Ritchie C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. Bjarne Stroustrup Some people...think that C is a real programming language, but they are sadly mistaken... almost-portable assembly language. Linus Torvalds

Introduction Slide 4/20 Hello World 1 #include <stdio.h> // comments int main(void) // entry point of program { printf("hello, world\n"); return 0; // successful completion } 1 Kernighan, Brian, and Dennis Ritchie. The C Programming Language. 1st ed. Englewood Cliffs, NJ: Prentice Hall, 1978.

Introduction Slide 5/20 Pointers (1) #include <stdio.h> int main(int argc, char* argv[]) { int arr[10]; arr[0]=37; printf("%d\n",*arr); return 0; } // String[] args in Java // array of ten integers // data is uninitialized // pointer dereference // output is thirty-seven

Introduction Slide 6/20 Pointers (2) Array variable names are nothing more than pointers to the first element stored in a contiguously allocated block of memory. A given slot can be accessed using pointer arithmetic; this is why the first element has index zero. The mystery is solved! Syntax: *arr = *(arr+0) = arr[0] = 37; arr 0 1 2 3 4 5 6 7 8 9 37??????????????????

Introduction Slide 7/20 Pointers (3) void disp(int* a,int n) // pass-by-value, but... { // copy the pointer only int k; // so data is not copied for(k=0;k<n;k++) printf("%d\n",a[k]); } main: disp(arr,10); // note, no arr.length

Introduction Slide 8/20 Pointers (4) int a[10],b[10],*c,k;... for(k=0;k<10;k++) *(b+k)=*(a+k); c[0]=12345; c=a; for(k=0;k<10;k++) printf("%d\n",*c++); // arrays, pointer, int // pointer arithmetic // uh-oh, need malloc // only copies pointer // tricky plus-plus

Introduction Slide 9/20 Input char ch; while(1) { ch=getchar(); if(ch==eof) break; // not a pointer // infinite loop // simple input // CTRL-D is EOF } printf("***%c***\n",ch);

Introduction Slide 10/20 Output char* str="hello"; char tmp[6]; printf("%s\n",str); printf("%c\n",str[0]); sprintf(tmp,"%s",str); printf("%c\n",tmp[5]); printf("%d\n",tmp[5]); // array of characters // extra slot for \0 // print entire string // print first character // print onto buffer // output is a blank // output is zero

Introduction Slide 11/20 American Standard Code for Information Interchange (ASCII) One byte only so sizeof(char)==1 in C. char int binary char int binary \0 0 0000 0000 A 65 0100 0001 \n 10 0000 1010 B 66 0100 0010 32 0010 0000 C 67 0100 0011 0 48 0011 0000 Z 90 0101 1010 1 49 0011 0001 a 97 0110 0001 2 50 0011 0010 b 98 0110 0010 3 51 0011 0011 c 99 0110 0011 9 57 0011 1001 z 122 0111 1010

Introduction Slide 12/20 Example Program: Miscellaneous double, math, sqrt string, strlen printf, %f Compile gcc -lm miscellaneous 0.c Or, gcc -lm -o misc miscellaneous 0.c Run./a.out, or./misc Command-line file redirection with <, >, >>, and 2>.

Introduction Slide 13/20 Lab Assignment: Entropy Calculation Input a message as a char* and call its length n. Calculate the entropy of each symbol x where: The number of times x appears in the message is F x. The probability of drawing x at random is P x = F x /n. The entropy of x is H x = P x log 2 (P x ). Calculate the total entropy H by summing all the H x. Calculate the number of redundant bits in 8-bit ASCII. The theoretical minimum number of bits is H n. See also Null and Lobur pages 310-311.

Introduction Slide 14/20 Example Entropy Calculation (1) x F_x P_x H_x bits ============================ ===== h 1 0.100 0.332 3.322 e 1 0.100 0.332 3.322 l 3 0.300 0.521 1.737 o 2 0.200 0.464 2.322 w 1 0.100 0.332 3.322 r 1 0.100 0.332 3.322 d 1 0.100 0.332 3.322 Start simple and only once everything is working then try a more complicated input like the declaration.eng file.

Introduction Slide 15/20 Example Entropy Calculation (2) The bits column, not required by the lab, indicates the theoretical minimum number of bits for each symbol in the message. The entropy H x multiplies the bits times P x, essentially weighting the value so that the overall H gives the average number of bits needed across all symbols. Thus, H n is the total bits. Encoded, 011010000001001011011000111 contains 27 bits for a compression factor of 1 27/80 = 66.25%, plus overhead: Char Code Char Code Char Code Char Code h 011 l 00 w 0101 d 111 e 0100 o 10 r 110

Introduction Slide 16/20 Huffman Compression The big idea is to replace ASCII with a custom encoding scheme. Symbols that appear more often are given shorter codewords and symbols that appear less often are given longer codewords. Care must be taken that the encoded message can be uniquely decoded. For instance, we can t have a scheme where one codeword is 001 and another is 0010110. How would we know when to stop decoding the symbol? There is an overhead cost to represent the custom scheme that is not required for ASCII since it is an established standard.

Introduction Slide 17/20 Example Custom Scheme Symbol Codeword Symbol Codeword 01 O 10100 T 000 R 10101 L 0010 A 11011 Y 0011 U 110100 I 1001 N 110101 H 1011 F 1000100 P 1100 M 1000101 E 1110 C 1000110 G 1111 D 1000111 S 10000

Introduction Slide 18/20 Corresponding Message 1011100111111111001011100000011011100100111111111001011 1000000110111001010011000100010111110011000111101001111 0110111101110000011110110110001110110101010001011111001 1000101101001100010001011111001110010011111100000110011 1010101110110110111101001010110101001101000101111100110 0011011011000100000110011101010111011011000100001011010 0101011010100110110111001111111110010111000000110111001 0011111111100101110000001101110010100110001 Note. If these bits are stored in ASCII then 0 is 00110000 and 1 is 00110001 and we only compress the message in theory. Or, use bit operators to read and write a binary file.

Introduction Slide 19/20 Start of the Decode Process The first eight bits of the message are: 10111001 Since 1011 is the codeword for H and 1001 is I the decoded message begins with HI. A friendly start! Checking the custom scheme shows there is no ambiguity as to which symbols could be represented by this bit sequence. Representing these two symbols in ASCII would require twice the number of bits, hence compression.

Introduction Slide 20/20 Lab Assignment: Huffman Decode Input the number of symbols. Study how this is done in the decode SHELL.c file. Input the custom scheme and the message. Decode the message. There is a guaranteed unique way to do this. Output the decoded message. Start with decode.txt which contains the example scheme and message included in these slides. Additional examples are provided on the course website.