Lab. 2 Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
A Very Short Introduction to OpenMP Basile Schaeli EPFL – I&C – LSP Vincent Keller EPFL – STI – LIN.
CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Lab. 2 (April 27th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
Programming with Shared Memory Introduction to OpenMP
CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options.
COM503 Parallel Computer Architecture & Programming
Lecture 5: Shared-memory Computing with Open MP. Shared Memory Computing.
Lecture 8: Caffe - CPU Optimization
Chapter 17 Shared-Memory Programming. Introduction OpenMP is an application programming interface (API) for parallel programming on multiprocessors. It.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
CS333 Intro to Operating Systems Jonathan Walpole.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Now that you know the pthread API…  How do you create threads?  How do you pass different values to them?  How do you return values from threads? 
Computer Science 320 A First Program in Parallel Java.
Classes - Intermediate
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Programming Model Dr. Xiao Qin Auburn University
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 ITCS4145 Parallel Programming B. Wilkinson March 23, hybrid-abw.ppt Hybrid Parallel Programming Introduction.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Embedded Systems MPSoC Architectures OpenMP: Exercises Alberto Bosio
Introduction to OpenMP
Lecture 5: Shared-memory Computing with Open MP
Lab. 2 (April 15th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Instructor: Lilian de Greef Quarter: Summer 2017
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing A bug in the rwlock program Dr. Xiao Qin.
Introduction to OpenMP
September 4, 1997 Parallel Processing (CS 667) Lecture 5: Shared Memory Parallel Programming with OpenMP* Jeremy R. Johnson Parallel Processing.
Lab. 3 (May 6st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Operating Systems Lecture 13.
Parallel Programming.
Parallel Programming with OpenMP
Lab. 2 (May 12th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
null, true, and false are also reserved.
Operating Systems Lecture 14.
Hybrid Parallel Programming
Lab. 3 (May 11th) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute :
CS510 Operating System Foundations
Programming with Shared Memory Introduction to OpenMP
Jonathan Walpole Computer Science Portland State University
Operating System Concepts
Parallelism for summation
Lab. 2 (May 2nd) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use.
DNA microarrays. Infinite Mixture Model-Based Clustering of DNA Microarray Data Using openMP.
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Hybrid Parallel Programming
Introduction to OpenMP
Programming with Shared Memory
Lab. 3 (May 1st) You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “>
Programming with Shared Memory - 2 Issues with sharing data
In this class, we will cover:
Exercise 1 : ex1.java Thread Creation and Execution
Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()
Computer Architecture Multi-threaded Matrix Multiply
Shared Memory Programming with Pthreads
OpenMP Parallel Programming
Presentation transcript:

Lab. 2 Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin for programming You may use Text editor : nano or vim In cygwin, compiling “> gcc ex1.c -lpthread” will generate a.exe Execute : “> ./a.exe”

Exercise 1 : ex1.java Modify following JAVA code ex1.java to C code with pthread library for Thread Creation and Execution class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread " + i + " says bye"); } class ex1 { public static void main(String[] args) { System.out.println("main thread start!"); for(int i=1; i <= 5; ++i) { C c = new C(i); c.start(); System.out.println("main thread end!");

Exercise 1 : lab2_ex1.c Sample C code with pthread library #include <pthread.h> #include <stdio.h> void *c(void* i) { printf("Thread %d says hi\n",i); printf("Thread %d says bye\n",i); } int main (int argc, char *argv[]) { pthread_t threads[20]; int rc, t; printf("main thread start\n"); for(t=1; t<=20; t++){ rc = pthread_create(&threads[t], NULL, c, (void *)t); if (rc) { printf("ERROR code is %d\n", rc); exit(-1); printf("main thread end\n"); pthread_exit(NULL);

Exercise 2 : parallel summation Modify following multithreaded JAVA code ex2.java to C code with pthread library class SumThread extends Thread { int lo, hi; // fields for communicating inputs int[] arr; int ans = 0; // for communicating result SumThread(int[] a, int l, int h) { lo=l; hi=h; arr=a; } public void run() { // insert your code here class ex2 { private static final int NUM_END = 10000; private static final int NUM_THREAD = 4; // assume NUM_END is divisible by NUM_THREAD public static void main(String[] args) { int[] int_arr = new int [NUM_END]; int i,s; for (i=0;i<NUM_END;i++) int_arr[i]=i+1; s=sum(int_arr); System.out.println("sum=" + s) ; static int sum(int[] arr) {

Exercise 3 : parallel integration Modify your multithreaded JAVA code ex3.java to C code with pthread library

Exercise 4 : Prime numbers Modify your multithreaded JAVA code ex4.java to C code with pthread library class ex4_serial { private static final int NUM_END = 100000; public static void main(String[] args) { int counter=0; int i; long startTime = System.currentTimeMillis(); for (i=0;i<NUM_END;i++) { if (isPrime(i)) counter++; } long endTime = System.currentTimeMillis(); long timeDiff = endTime - startTime; System.out.println("Execution Time : "+timeDiff+"ms"); System.out.println("1..."+(NUM_END-1)+" prime# counter=" + counter +"\n"); private static boolean isPrime(int x) { if (x<=1) return false; for (i=2;i<x;i++) { if ((x%i == 0) && (i!=x)) return false; return true;

Practice OpenMP You may use either cygwin or visual studio for using OpenMP Compiling in cygwin “> gcc –fopenmp ex1.c” will generate a.exe Execute : “> ./a.exe” Compiling in visual studio: set “OpenMP Support” to Yes

Exercise 5 : ex5.c Compute sum=1+2+3+…+10000 using 1,2,3,4 threads using OpenMP and measure time for each case. Use reduction // sample solution #include <omp.h> #include <stdio.h> #define NUM_THREADS 4 #define END_NUM 10000 int main () { int i; int sum=0; double start_time, end_time; omp_set_num_threads(NUM_THREADS); start_time = omp_get_wtime( ); #pragma omp parallel #pragma omp for reduction(+:sum) for (i = 1; i <= END_NUM; i++) { sum+=i; //printf_f("(%d/%d) : %d\n",omp_get_thread_num(),omp_get_num_threads(),i); } end_time = omp_get_wtime( ); printf("sum = 1+2+..+%d = %d\n",END_NUM,sum); printf("time elapsed: %lfs\n",end_time-start_time); return 1;

Exercise 6 : Numerical Integration Parallelize following code using OpenMP. Test the code with 1,2,3,4,8,16 threads and with different load balancing approach (e.g. static, dynamic, guided, try different chunk size). Measure the time for each case.

Exercise 7 : Matrix multiplication Compile and run omp_ex3.c (available on our class webpage) and try different number of threads (1,2,4,8,16,32) and different load balancing approaches (e.g. static, dynamic). Read and understand the source code omp_ex3.c

Exercise 8 : Prime numbers Modify following multithreaded JAVA code to C code with OpenMP library. Test your code with 1,2,4,8,16 threads and measure performance. Test your code with static load balancing and dynamic load balancing using schedule(static|dynamic|guided [,4]) class ex4_serial { private static final int NUM_END = 200000; public static void main(String[] args) { int counter=0; int i; long startTime = System.currentTimeMillis(); for (i=0;i<NUM_END;i++) { if (isPrime(i)) counter++; } long endTime = System.currentTimeMillis(); long timeDiff = endTime - startTime; System.out.println("Execution Time : "+timeDiff+"ms"); System.out.println("1..."+(NUM_END-1)+" prime# counter=" + counter +"\n"); private static boolean isPrime(int x) { if (x<=1) return false; for (i=2;i<x;i++) { if ((x%i == 0) && (i!=x)) return false; return true;