Multidimensional Arrays in C++: Trying to get it right

Similar documents
CE221 Programming in C++ Part 1 Introduction

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

Functions and Recursion

CSE 303: Concepts and Tools for Software Development

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

CPSC 427: Object-Oriented Programming

Launchpad Lecture -10

Pointers, Dynamic Data, and Reference Types

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CPSC 427: Object-Oriented Programming

CS201 Some Important Definitions

Short Notes of CS201

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS201 - Introduction to Programming Glossary By

Fast Introduction to Object Oriented Programming and C++

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Exercise 1.1 Hello world

Object-Oriented Programming for Scientific Computing

COMP6771 Advanced C++ Programming

Introduction to Scientific Programming in C++

Programming, numerics and optimization

CS 376b Computer Vision

Operator overloading

Object-Oriented Programming

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

A brief introduction to C++

Chapter 1: Object-Oriented Programming Using C++

C++ Tutorial AM 225. Dan Fortunato

Classes and Pointers: Some Peculiarities

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Link-Based Implementations. Chapter 4

C++ for numerical computing

Vector and Free Store (Vectors and Arrays)

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Introduction to C++ Systems Programming

CSI33 Data Structures

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CSC1322 Object-Oriented Programming Concepts

Homework #3 CS2255 Fall 2012

Exam 3 Chapters 7 & 9

05-01 Discussion Notes

04-17 Discussion Notes

Pointers II. Class 31

Exercise 7 References, Arrays, Vectors

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

QUIZ. What is wrong with this code that uses default arguments?

CS2255 HOMEWORK #1 Fall 2012

Cpt S 122 Data Structures. Introduction to C++ Part II

Lesson 13 - Vectors Dynamic Data Storage

Part V. Memory and pointers. Philip Blakely (LSC) C++ Introduction 145 / 370

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Object-Oriented Principles and Practice / C++

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

AN OVERVIEW OF C++ 1

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

C++ Programming Lecture 11 Functions Part I

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

IS 0020 Program Design and Software Tools

Pointers and References

Dynamic Memory Allocation

CS24 Week 3 Lecture 1

PIC10B/1 Winter 2014 Exam I Study Guide

CS201- Introduction to Programming Current Quizzes

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Chapter 2: Basic Elements of C++

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

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Introduction to C++ Introduction to C++ 1

C Functions. Object created and destroyed within its block auto: default for local variables

Separate Compilation Model

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

For example, let s say we define an array of char of size six:

Chapter 15 - C++ As A "Better C"

Ch02. True/False Indicate whether the statement is true or false.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

C++ Code Structure. Cooperating with the Compiler

Introduction to Programming using C++

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

! Errors can be dealt with at place error occurs

Lecture on pointers, references, and arrays and vectors

Computer Science II Lecture 1 Introduction and Background

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

Object Oriented Design

Object-Oriented Programming in C++

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

04-19 Discussion Notes

10. Abstract Data Types

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Transcription:

Multidimensional Arrays in C++: Trying to get it right Ramses van Zon SciNet HPC Consortium June 10, 2015 Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 1 / 20

What s the problem? C++ sucks at multidimensional arrays. Fortran is excellent at it, but people scold me for using such an old language/it does not get me a job when I m done with my career/i really like templates/... C++ could be great at multidimensional arrays, and there are a plethora of libraries aiming to fill this void. Something must be missing for there not to be a winner. Let s look a bit more closely at what is wrong with C++ multidimensional arrays, what we d want from such entities, and a library that attempts to solve that. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 2 / 20

At first it seems so easy #include <iostream> int main() { const long n = 4; float a[n] = {1.0,2.0,3.0,10.0; for (long i=0; i<n; i++) std::cout << a[i] << " "; Even easier using C++11 features: #include <iostream> int main() { const long n = 4; float a[n] = {1.0,2.0,3.0,10.0; for (auto x: a) std::cout << x << " "; Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 3 / 20

Two issues, even here 1: Arbitrary size limits #include <iostream> int main() { const long n = 1e7; float a[n] = {1.0,2.0,3.0,10.0; for (auto x: a) std::cout << x << " "; This segfaults. There automatic arrays are allocated on stack memory, which is usually much smaller than the total memory. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 4 / 20

Two issues, even here 2. Function arguments #include <iostream> void printthis(float a[4]) { for (auto x: a) std::cout << x << " "; int main() { float a[4] = {1.0,2.0,3.0,10.0; printthis(a); This will not even compile. When passed to as a function argument, the array gets converted to a pointer, that does not know about the length of the array. The 4 in the argument list is ignored. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 5 / 20

To solve this, we should: Allocate dynamically with new That means we have to explicitly deallocate with delete And we should pass the size of the array to functions. Still cannot use the c++11 loop. Or we can use std::vector. This solve it all. We must be careful not to pass vectors by value to functions though! Only for the one dimensional case. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 6 / 20

Multidimensional case At first, just as easy: #include <iostream> int main() { const long n = 2; const long m = 3; const long k = 2; float a[n][m][k] = {{{1,2,{3,4,{5,6,{{7,8,{9,10,{11,12; for (auto x: a) std::cout << x << " "; While the allocation works, the cout statement does not. It prints out two pointers to the two sub-matrices! These automatic arrays suffer from the same troubles are their 1d counterparts. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 7 / 20

Solution 1. Manual allocation float*** a = new float**[n]; for (int i=0;i<n;i++) { a[i] = new float*[m]; for (int j=0;j<m;j++) a[i][j] = new float[k]; // Special tuning needed for contiguous elements (needed for 2. Vectors of vectors of vectors vector<vector<vector<float>>> a(n); for (int i=0;i<n;i++) { a[i].reserve(m); for (int j=0;j<m;j++) v[i][j].reserve(k); // No tuning possible to get contiguous elements Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 8 / 20

Something better Introducing rarray: A library for runtime-defined multidimensional arrays with none of these issues. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 9 / 20

Rarray A header-only template solution for multidimensional arrays with dimensions determined at runtime. Usually faster than alternatives. Has optional bounds checking (no longer fast then, though). All allocation on the heap, and contiguous. No hidden copies of the array elements. Can reuse existing buffers too. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 10 / 20

How? The header file rarray.h provides the type rarray<t,r>, where T is any type and R is the rank. Element access uses repeated square brackets. Copying rarrays or passing them to functions mean shallow copies, unless explicitly asking for a deep copy. For io, use the additional header rarrayio.h. For element-wise algebraic operations, use rarrayex.h git clone https://github.com/vanzonr/rarray Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 11 / 20

Rarray in a Nutshell Define a n m k array of floats: rarray<float,3> b(n,m,k); Define it with preallocated memory: rarray<float,3> c(ptr,n,m,k); Element i,j,k of the array b: b[i][j][k] Pointer to the contiguous data in b: b.data() Extent in the ith dimension in b: b.extent(i) Shallow copy of the array: rarray<float,3> d=b; Deep copy of the array: rarray<float,3> e=b.copy(); A rarray re-using an automatic array: float f[10][20][8]={...;. rarray<float,3> g=rarray(f); Output a rarray to screen: std::cout «h «endl; Read a rarray from keyboard: std::cin» h; Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 12 / 20

. Details Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 13 / 20

Copying and function arguments In C++, when we copy a variable of a built-in type to a new variable, the new copy is completely independent of the old variable. Likewise, the default way of passing arguments to a function involves a complete copy for built-in types. For C-style arrays, however, only the pointer to the first element gets copied, so you get a reference, not the whole array. The latter is called a shallow copy. Rarrays use shallow copies much like pointers, but memory allocated by the rarray gets released by the first created reference. What does this essentially mean? Well: 1 You can pass rarrays by value to function, which is as if you were passing a pointer. 2 When you assign one rarray to another, the other simply points to the old one. 3 If you wish to do a deep copy, i.e., create a new array independent of the old array, you need to use the copy method. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 14 / 20

Returning a rarray from a function Unless you re using C++11 context, the shallow copying causes problems: rarray<double,2> zeros(int n, int m) { rarray<double,2> r(n,m); r.fill(0.0); return r; int main() { rarray<double,2> s = zeros(100,100); return s[99][99]; The array would get destroyed just after it is returned in C++03. Solution: rarray<double,2>::return_type zeros(int n, int m) { rarray<double,2> r(n,m); r.fill(0.0); return r; Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 15 / 20

Optional Bounds checking If the preprocessor constant RA_BOUNDSCHECK is defined, an out of bounds exception is thrown if an index is too small or too large; the size of dimension is requested that does not exist (in a call to extent(int i)); a constructor is called with a zero pointer for the buffer or for the dimensions array; a constructor is called with too few or too many arguments (for R<= 11). RA_BOUNDSCHECK can be defined by adding the -DRA_BOUNDSCHECK argument to the compilation command, or by #define RA_BOUNDSCHECK before the#include rarray.h in the source. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 16 / 20

I/O In the header rarrayio.h Only ascii for now: not great Prints it out as you would initialize an automatic array, i.e., with curly braces. Doing so means it can read it back in as well Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 17 / 20

Expressions A new and mildly experimental featues allows you to do this: #include "rarray.h" #include "rarrayex.h" int main() { rarray<float,2> a(4,4); rarray<float,2> b(4,4); rarray<float,2> c(4,4); float s = 2.0; a = b + s*c; Should work, but still under development and needs tuning. Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 18 / 20

. A Dream Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 19 / 20

This should be in the language #include "rarray.h" #include "rarrayex.h" int main() { float[*][*] a(4,4); float[*][*] b(4,4); float[*][*] c(4,4); float s = 2.0; a = b + s*c; Ramses van Zon (SciNet HPC Consortium) Multidimensional Arrays in C++: Trying to get it right June 10, 2015 20 / 20