WSE 187: INTRODUCTION TO PARALLEL PROGRAMMING* Lecture 2 Jesmin Jahan Tithi *Prepared with the help of free online resources.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

Connecting to GMT machine via Windows 7. Windows PuTTy GMT on Mac server int-038.geosci.usyd.edu.au To use GMT, you will connect to a Mac server via PuTTy.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Ch. 8 Functions.
1 Programming Explicit Thread-level Parallelism  As noted previously, the programmer must specify how to parallelize  But, want path of least effort.
Zhang Hongyi CSCI2100B Data Structures Tutorial 2
COSC 120 Computer Programming
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 CS 140 : Jan 27 – Feb 3, 2010 Multicore (and Shared Memory) Programming with Cilk++ Multicore and NUMA architectures Multithreaded Programming Cilk++
CS 202 Computer Science II Lab Fall 2009 September 3.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Assembler Compiler Interpreter ASSEMBLER To convert the assembly language into machine code. Translate mnemonic operation codes to their machine language.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Intro. to Game Programming Want to program a game?
Programming with Shared Memory Introduction to OpenMP
Chapter 3 Getting Started with C++
High-Level Programming Languages: C++
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
1 C/C++ UM/MCSR. 2 Logging into the system using ssh Logging into the system from Windows: –Start the secure shell client: Start->Programs->SSH.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
1 Lab 2 “Hello world” in Unix/Linux #include "std_lib_facilities_4.h" int main(){ cout
Implementation of the Hangman Game in C++
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
Previously Repetition Structures While, Do-While, For.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C++ Programming Lecture 12 Functions – Part IV
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Software Engineering Algorithms, Compilers, & Lifecycle.
Linux CSE 1222 CSE1222: Lecture 1BThe Ohio State University1.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
CONCURRENCY PLATFORMS
Introduction to parallel programming modelS
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
Review 1.
CS 140 : Numerical Examples on Shared Memory with Cilk++
CMPS 5433 Programming Models
Govt. Polytechnic,Dhangar
Introduction to CILK Some slides are from:
CONCURRENCY PLATFORMS
Control Structures Part 1
C++ Programming Lecture 3 C++ Basics – Part I
CS31 Discussion 1D Winter19: week 4
Introduction to CILK Some slides are from:
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

WSE 187: INTRODUCTION TO PARALLEL PROGRAMMING* Lecture 2 Jesmin Jahan Tithi *Prepared with the help of free online resources.

LOGIN TO SSH Steps (Windows)  Connect to the host  Give provided password when prompted For the first time users:  Accept security keys  Change password:  First provide the Old password  Then type new password  Repeat new password Do not afraid if you do not see any character On screen. But be careful when you are Typing.  Save the host info in SSH when prompted.  Mac uses:  Use terminal to directly connect to the server.  Other steps are the same.  You may try to use Filezilla to transfer file from mac to server.

HELLO PARALLEL WORLD! Intel Cilk Plus

CILK PLUS Intel® Cilk™ Plus = add-on to the C and C++, implemented by the Intel® C++ Compiler 3 keywords to C and C++: cilk_for, cilk_spawn, and cilk_sync cilk_spawn - Specifies that a function call can execute asynchronously, without requiring the caller to wait for it to return. This is an expression of an opportunity for parallelism, not a command that mandates parallelism. The Intel Cilk Plus runtime will choose whether to run the function in parallel with its caller.cilk_spawn cilk_sync - Specifies that all spawned calls in a function must complete before execution continues. There is an implied cilk_sync at the end of every function that contains a cilk_spawn.cilk_sync cilk_for - Allows iterations of the loop body to be executed in parallel.cilk_for cilk_spawn and cilk_for keywords express opportunities for parallelism.

CILK_SPAWN #include static void hello(){ int i=0; for(i=0;i< ;i++) printf(""); printf("Hello "); } static void world(){ int i=0; for(i=0;i< ;i++) printf(""); printf("world! "); } int main(){ cilk_spawn hello(); cilk_spawn world(); //cilk_sync; printf("Done! "); } Compile: icc –O3 –o hello Hello_parallel_world.cpp Run:./hello Run: CILK_NWORKERS=4./hello

CILK_SPAWN EXERCISE Order of placement Wheels, Chassis, Engine, Frame, Steering wheel #include void make(char* str){ int i=0; for(i=0;i< ;i++) printf(""); printf("%s has/have been created.\n",str); } void place(char* str){ int i=0; for(i=0;i< ;i++) printf(""); printf("%s has/have been placed.\n",str); } int main(){ //Place your code here }

CILK_FOR #include #include "cilktime.h" using namespace std; #define n int main(){ // First input vector. int A[n]; // Second input vector. int B[n]; // Sum vector. int C[n]; // Initialize the vectors or arrays with input. cilk_for (int i = 0; i <= n; i++){ A[i] = i; B[i] = i+1; } // Compute the sum unsigned long long tstart = cilk_getticks(); //beginning time stamp cilk_for (int i = 0; i <= n; i++){ C[i] = A[i] + B[i]; } unsigned long long tend = cilk_getticks(); //end time stamp cout<<"Time to run:"<<cilk_ticks_to_seconds(tend-tstart)<<endl; // Check the sum to verify. int pos; cout<<"Enter position of element to inspect"<<endl; cin>>pos; cout<<C[pos]<<endl; return 0; } for (int i = 0; i < 8; ++i) { cilk_spawn do_work(i); } cilk_sync; A better approach is to use a cilk_for loop: cilk_for (int i = 0; i < 8; ++i) { do_work(i); }

CILK_FOR #include int main(){ long int sum = 0; cilk_for (int i = 0; i <= ; i++) sum += i; printf("%ld\n",sum); return 0; } for (int i = 0; i < 8; ++i) { cilk_spawn do_work(i); } cilk_sync; A better approach is to use a cilk_for loop: cilk_for (int i = 0; i < 8; ++i) { do_work(i); } //wrong! race conditionrace condition

CILK_FOR #include #include //pthread library int main(){ long int sum = 0; pthread_mutex_t m; //define the lock pthread_mutex_init(&m,NULL); //initialize the lock cilk_for (int i = 0; i <= ; i++){ pthread_mutex_lock(&m); //lock - prevents other threads from running this code sum += i; pthread_mutex_unlock(&m); //unlock - allows other threads to access this code } printf("%ld\n",sum); } Several ways of dealing with race conditions. First option: Use locks! We will learn more later.

CHANGING NUMBER OF CORES/THREADS Run with: CILK_NWORKERS=4./executable Or change inside the main program: if (0!= __cilkrts_set_param("nworkers","16")) { cout<<"Failed to set worker count\n"<<endl; return 1; } Check to verify: int num_threads =__cilkrts_get_nworkers(); cout<< num_threads <<endl;