ENERGY 211 / CME 211 Lecture 11 October 15, 2008.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Computer Architecture CSCE 350
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Fall 2011SYSC 5704: Elements of Computer Systems 1 SYSC 5704 Elements of Computer Systems Optimization to take advantage of hardware.
Stanford University CS243 Winter 2006 Wei Li 1 Loop Transformations and Locality.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
ECE669 L23: Parallel Compilation April 29, 2004 ECE 669 Parallel Computer Architecture Lecture 23 Parallel Compilation.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
ECE 454 Computer Systems Programming Memory performance (Part II: Optimizing for caches) Ding Yuan ECE Dept., University of Toronto
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Assembly - Arrays תרגול 7 מערכים.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
SIMD Programming CS 240A, Winter Flynn* Taxonomy, 1966 In 2013, SIMD and MIMD most common parallelism in architectures – usually both in same.
. A little bit about optimization, 2d array, 1d array used as 2d, register and volatile.
Arrays Chapter 7.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
INC 161 , CPE 100 Computer Programming
CSE 351 Section 9 3/1/12.
CS2100 Computer Organisation
Java Array Object Chuen-Liang Chen Department of Computer Science
Data Structure and Algorithms
Arrays.
EKT120 : Computer Programming
The Hardware/Software Interface CSE351 Winter 2013
Section 7: Memory and Caches
Dynamic Array Multidimensional Array Matric Operation with Array
Chapter 7 Part 1 Edited by JJ Shepherd
Numeric Arrays Numeric Arrays Chapter 4.
Optimization Code Optimization ©SoftMoore Consulting.
Array Array Array Dimension 3. One dinensional array
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
C-Programming, continued
CSC 253 Lecture 8.
C Passing arrays to a Function
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Arrays, For loop While loop Do while loop
CSC 253 Lecture 8.
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
EKT150 : Computer Programming
Java Programming Arrays
Lecture 18 Arrays and Pointer Arithmetic
Machine-Level Programming IV: Data
Instructors: Majd Sakr and Khaled Harras
EKT120: Computer Programming
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Machine-Level Programming 5 Structured Data
Database Design and Programming
Machine-Level Programming 5 Structured Data
Practical Session 8, Memory Management 2
Arrays.
Arrays.
Structured Data I: Homogenous Data Feb. 10, 2000
Instructor: Fatma CORUT ERGİN
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Arrays and Matrices Prof. Abdul Hameed.
Practical Session 9, Memory Management continues
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Writing Cache Friendly Code
Presentation transcript:

ENERGY 211 / CME 211 Lecture 11 October 15, 2008

Visualizing Pointers This assumes 32-bit addressing 0xffff00 0xffff04 0xffff08 4 0xffff00 6 int x = 4; int *p = &x; int y = *p + 2; This assumes 32-bit addressing p is of type “pointer to int”, not int! Modifying value of *p modifies x too

Matrix Allocation Strategies There are two commonly-used methods for allocating memory for an m-by-n matrix Nested array: 1-D array of mn elements Multi-level array: 1-D, m-element array of 1-D n-element arrays

Nested Arrays Declaration and allocation: double *A = new double[m*n]; To access (i,j) element: A[i*n+j] Advantages: contiguous memory efficient for iteration over whole matrix, efficient machine code for indexing Disadvantages: not intuitive, cumbersome to code element access

Visual: Nested Arrays Representation of a 3-by-4 matrix (0,0) (0,1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) Representation of a 3-by-4 matrix Declaration: double *A = new double[12]; Element indices zero-based To access (1,2) element, compute 1*4+2 and access A[6] Note row-major ordering

Multi-level Arrays Declaration and allocation: double **A = new double *[m]; for (int i=0; i<m; i++) A[i] = new double[n]; To access (I,j) element: A[I][j] Advantages: intuitive structure, indexing Disadvantages: more multiplications per element access, rows not contiguous (how to address latter problem?)

Visual: Multi-level Arrays double **A; A = new double *[3]; A[i] = new double[4]; Same matrix as in nested example A is an array of pointers! A[i] stores base address of array for row i

Improving Efficiency Consider a loop that accesses each element of an array in sequence To access x[i], machine code multiples i by size of element and adds to base address Instead, set pointer p equal to x Access element: *p, move to next: p++ Much more efficient: an increment instead of an addition and multiplication!

Loop Optimization Most of the running time is spent in loops, so focus optimizing efforts there The compiler can figure out many optimizations, but must be conservative Conditionals are very expensive, so handle special cases outside the loop Try to maintain locality: access data sequentially, to keep in cache But remember: “Premature optimization is the root of all evil” – Donald Knuth

Next Time Your questions! Pointers Project 2 Project 3 preview