1 / 40

Hands-on (Crash) Introduction to C++ for (Forensic) Scientists

Hands-on (Crash) Introduction to C++ for (Forensic) Scientists. Outline. C++ : The s tandard for high performance scientific computing Why bother learning C++? Web resources that help when you get stuck Getting a computer to do anything useful Compilers (Open-source ones at least…)

nariko
Download Presentation

Hands-on (Crash) Introduction to C++ for (Forensic) Scientists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hands-on (Crash) Introduction to C++ for (Forensic) Scientists

  2. Outline • C++ : The standard for high performance scientific computing • Why bother learning C++? • Web resources that help when you get stuck • Getting a computer to do anything useful • Compilers (Open-source ones at least…) • The IDE we’ll use: Netbeans • Common data types • Structure of a basic C/C++ program • Compiling and running your first program • Useful headers, the STL

  3. Why C++? • We live in oceans of data. Computers are essential to record and help analyse it. • Competent scientists speak C/C++, Java, MATLAB, Python, Perl, R and/or Mathematica • Codes can easily be made available for review • If you can speak C++, you can speak anything • Sometimes you need the speed of C++ • Using C/C++ with R is now trivial with Rcpp

  4. Consecutive Matching Striae (CMS)-Space Biasotti-Murdock Dictionary 1,786,980 “line” comparisions Took ~1 week on “beefy” Mac Pro Toolmarks (screwdriver striation profiles) form database

  5. Approximate-Consecutive Matching Striae (CMS)-Space Biasotti-Murdock Dictionary • Took ~20 min on same Mac Pro • Approximate intensive steps and code in C++ Toolmarks (screwdriver striation profiles) form database • Took ~3 min • Intensive steps in C++ • Parallel “foreach” in R

  6. Web resources • Google: “C++ how do I <your question here>” • Often answered on http://stackoverflow.com/ site • www.learncpp.com/ (Great tutorials) • http://www.cplusplus.com/ (Great reference) • https://www.youtube.com/user/voidrealms • (Nice, clear “bite-sized” video tutorial series)

  7. Getting a computer to do anything useful • All machines understand is on/off! • High/low voltage • High/low current • High/low charge • 1/0 binary digits (bits) • To make a computer do anything, you have to speak machine language to it: 000000 00001 00010 00110 00000 100000 Add 1 and 2. Store the result.Wikipedia

  8. Getting a computer to do anything useful • Machine language is not intuitive and can vary a great deal over designs • The basic operations operations however are the same, e.g.: • Move data here • Combine these values • Store this data • Etc. • “Human readable” language for basic machine operations: assembly language

  9. Getting a computer to do anything useful • Assembly is still cumbersome for (most) humans A machine encoding 10110000 01100001 Assembly MOV AL, 61h Move the number 97 over to “storage area” AL

  10. Getting a computer to do anything useful • Better yet is a more “Englishy”, “high-level” language • Enter: C, C++, Fortran, Java, … • Higher level languages like these are translated (“compiled”) to machine language • Not exactly true for Java, but it’s something analogus… • From now on we will just talk in C++

  11. Programming • All you need to program is a text editor and a compiler • There are both commercial and open-source compilers • Open-source compilers are really good and have been around forever. They are the de-facto standard in science • GCC: GNU compiler collection (Unix/Linux, OS X) • MinGW: a port for gcc for Windows • Clang: From the LLVM Project (OS X, Unix/Linux)

  12. Programming • Minimalist programming with just: • a text editor (people like vim or emacs) • a compiler • maybe some unix tools like make … sucks for beginners • To make life a tad easier, we’ll use an integrated development environment (IDE) called Netbeans.

  13. Programming In Netbeans Compile/Run button Compile button Code Window Console/Output

  14. Common Data Types • Computers are DUMB! We have to be explicit about the type of data we want to work with • int: 32 bit (4 byte) signed integer • double: 64 bit (8 byte) floating point (decimal) • std::string: a “string” of characters. Pretty high level representation however. More on this another day. • Chatacters between “ ” or ‘ ’ are recognized as C++ strings.

  15. Structure of a Basic C++ program /* A block of text comments Blah blah blah blah */ Comments are set off by: // (1 line each) -or- “Header” file include section All stand alone c++ programs must have an int main() function Your program executes commands from the “main” code block

  16. Common STL headers • STL: C++ standard template library • Template: mechanism to handle data of any type • Subject of templates goes VERY deep. For another day… • Common/handy STL headers: • <iostream>: Basic input/output • <vector>: Common container for numbers • <fstream>: Basic file handling • <cstdlib>: The old C standard library of functions • <chron>: Timers for code performance measurement

  17. Outline • Pointers and References • What’s the point?? • Arrays • Basic memory management trivia and tips • Basic control structures • Functions • Code files .cpp • Header files .hpp • Conditional blocks • Looping

  18. Pointers and References • (An art-world analogy…..) In computing: • Our “medium” is data. • Our “pottery wheel” is hardware. • We want to get the most utility from our hardware. • Want to do a lot of work on data • Don’t want the hardware to work too hard on each task • Copying data from place to place: • Is time intensive • Wasteful of precious memory (RAM) • We always want to minimize copying! • Pointers and references deal with memory addresses • REALLY handy for cutting down on copying

  19. Pointers and References • Pointers/references refer to (point at) where data is • Some notation gymnastics: • double x = 3.0; //A double • &x; //The address of the data in x • & is the address operator • double *a_ptr //A variable that will //hold a memory addr • a_ptr called a pointer • It DOESN’T point at anything yet! • a_ptr = &x; //a_ptr “points at” the //data in x. It holds //x’s memory address

  20. Pointers and References • a_ptr = &x; //a_ptr “points at” x. • *a_ptr; //REFERS to the data in x. It //is the same thing as x, 3.0 //in this case. • * operator serves two purposes! • double*a_ptrDECLARES a pointer to a double. • *a_ptrREFERS to what a_ptr is pointing at (called de-referencing). NOTE there is no type name in front. • The C++ kosher thing to do is point at the data as soon as the pointer is declared: • double *a_ptr = &x;

  21. Pointers and References • So we learned about * operator and & operator: • Getting used to using them requires practice!! • int y = 3; • int*a_ptr2 = &y; • cout << “What gets printed?: ” *a_ptr2 <<endl; • y = 9; • cout << “Now what?: ” *a_ptr2 <<endl; • *a_ptr2 = 14; • cout << “Now what?: ” *a_ptr2 <<endl; • Why are these BAD?: • int *a_ptr3 = 18; • int &z = 9;

  22. Arrays • Pointers are great because they refer to data in-place. • They prevent us having to copy data from place-to-place! • This is very convenient when working with: • files • large vectors and matrices (arrays, STL containers, etc.) • CAREFUL THOUGH: • Data pointer is pointing at can be changed unintentionally! • Memory should be FREED when you are done with it (more later) • We usually (in memory) store related data together • arrays • STL containers • For arrays (a little more primitive but sometimes offer a speed advantage) we will declare/free with the important operators • new and delete.

  23. Arrays Pointer “arithmetic”: Indexing! Output from code above

  24. Arrays • Matrices are allocated and freed in a similar way: • NOTE: We’ll not typically do this however. Usually we’ll use STL containers or the wonderful modern templated C++ linear algebra libraries: Armadillo (http://arma.sourceforge.net/) and Eigen (http://eigen.tuxfamily.org/)

  25. Functions • A simple function called from main(): “arguments” to the function would go here Define the function ABOVE where it is first used Use the function in main

  26. Functions • Here is a function with arguments: Dummy variables. They will be substituted with actual argument when the function is actually called

  27. Functions • You can define a function after it is used, but care must be taken: Explicit dummy variable names are not necessary. The function “signature” MUST be defined before it is used Define the function

  28. Functions • The C++ kosher way to organize a function. Use separate header and implementation files: Need to #include the header file here func_header_file.hpp (function signature) main.cpp func.cpp (function implementation)

  29. Functions • If the function implementation isn’t too complicated define it in the header file (cuts down on the C++ “bureaucracy”). func_header_file.hpp main.cpp

  30. Conditional Statement Blocks • If-else. These are equivalent:

  31. Looping • Repeat (essentially) the same actions over and over: for-loop • Fill up a matrix • This is the “matrix” • The main loop: • Fills up A This is a lot of code to do a simple thing… Later we’ll use libraries to cut down on the work and clean up the code!

  32. Looping • Repeat (essentially) the same actions over and over: for-loop • Dot product between vectors • Define a dot product function • The loop: . Note the return type • Call the function in main()

  33. Outline • Common C++ lingo • Objects • Classes • Structure of your average class • Inheritance/Polymorphism • Operator overloading • Basic templates • Great libraries/Tools/Building blocks: • Armadillo • Eigen • Qt • Boost • Rcpp, RInside

  34. Objects • An object: Pretty much any self contained entity in the program. • Common examples of objects: • Variables (We saw these already) • Functions(We saw these already) • Classes (These are new!) • Templates (These are new!) • Arbitrarily “typed” versions and combinations of the above! • Object oriented means we want to build a program out of these very general objects • We will learn to think about a program in terms of interacting class objects

  35. Classes • A classencapsulates (somehow) related data and functions (methods) to interact with it. • Let say we we collect evidence from a crime scene: • “Evidence” will be a class • Associated with the evidence class will be: • A case number • Location of collection (a string) • The evidence type • Number of items collected • These are the data of the class • We need to set and access this data with functions for the user • These are the methods of the class

  36. Classes Evidence class • We can decide in the level of exposure these members have to other parts of the program • Public • Private • Protected • Data members • Case # • Location • Type • # items • Method members • Get a data member • Set a data member Gets and Sets are common class methods

  37. Classes • What does this look like in C++ code??? Class declaration header file class keyword declares a class “override” default constructor/destructor by explicitly declaring a new one Constructors will create instances of the class Optional initialization list for class member variables Copy constructor to copy an instance of the class. We can override with a custom one. Explicit declaration is optional. Class methods. Usually public Destructor to delete an instantiated class instance Class variables. Usually private or protected

  38. Classes • evidence:: prefix indicated method of class • :: is the scope operator evidence.cppimplementation file

  39. Classes Using the class #include header for class Create an instance of the class Use public members of the class When class “goes out of scope” (here when program ends) destructor automatically deallocates resources for it

  40. Derived Classes • Let say we we collect evidence from a crime scene: • “Evidence” will be a class • XXXX • XXXX

More Related