Program threaded-fft-bakery.cc

Similar documents
Program threaded-fft-mutex.cc

Linked List using a Sentinel

Program template-smart-pointers-again.cc

Threads CS1372. Lecture 13. CS1372 Threads Fall / 10

Program template-smart-pointers.cc

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

Threads. Threads (continued)

Example Threads. compile: gcc mythread.cc -o mythread -lpthread What is the output of this program? #include <pthread.h> #include <stdio.

CSCI [4 6] 730 Operating Systems. Example Execution. Process [& Thread] Synchronization. Why does cooperation require synchronization?

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

CS 6400 Lecture 11 Name:

CSE 333 Section 9 - pthreads

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

There are, of course, many other possible solutions and, if done correctly, those received full credit.

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 3305 Intro to Threads. Lecture 6

Threaded Programming. Lecture 9: Alternatives to OpenMP

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

1 Algorithmic Solution

Lecture 19: Shared Memory & Synchronization

Program des-simple3.cc

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

C++11 threads -- a simpler interface than pthreads Examples largely taken from 013/02/24/investigating-c11-threads/

CS2141 Software Development using C/C++ C++ Basics

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

POSIX Threads. HUJI Spring 2011

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Process Synchronization

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

The following program computes a Calculus value, the "trapezoidal approximation of

Distributed Programming

Sample Code: OUTPUT Daily Highs & Lows

Increment and the While. Class 15

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

POSIX Threads and OpenMP tasks

Operating systems. Lecture 10

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

CSCE 110 PROGRAMMING FUNDAMENTALS

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Exceptions, Case Study-Exception handling in C++.

Cache Awareness. Course Level: CS1/CS2. PDC Concepts Covered: PDC Concept Locality False Sharing

PROGRAMOVÁNÍ V C++ CVIČENÍ. Michal Brabec

First Exam name: John Franco

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

CPSC 427: Object-Oriented Programming

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

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

CSE 333 Final Exam June 6, 2017 Sample Solution

Synchronization for Concurrent Tasks

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Programming. C++ Basics

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Physics 234: Computational Physics

Pointers, Dynamic Data, and Reference Types

Programming RT systems with pthreads

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

CMSC 202 Midterm Exam 1 Fall 2015

Programming RT systems with pthreads

Introduc)on to pthreads. Shared memory Parallel Programming

C++ Undefined Behavior What is it, and why should I care?

CS 376b Computer Vision

CSE 333 Final Exam June 8, 2016 Sample Solution

CS333 Intro to Operating Systems. Jonathan Walpole

Global variables and resources are shared between threads within the same process. Each thread has its own stack and program counter (see Fig 2-7).

C++ Namespaces, Exceptions

CS 450 Exam 2 Mon. 4/11/2016

Ch 6. Functions. Example: function calls function

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Shared memory parallel computing

Process Synchronization (Part I)

Threads Tuesday, September 28, :37 AM

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS 153 Lab6. Kishore Kumar Pusukuri

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CISC2200 Threads Spring 2015

20-EECE-4029 Operating Systems Spring, 2015 John Franco

Chapter 4 Concurrent Programming

CS533 Concepts of Operating Systems. Jonathan Walpole

C++ Structures Programming Workshop 2 (CSCI 1061U)

Problem Solving: Storyboards for User Interaction

True or False (15 Points)

CS510 Operating System Foundations. Jonathan Walpole

CPSC 427: Object-Oriented Programming

Object oriented programming

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Posix Threads (Pthreads)

Lab Instructor : Jean Lai

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score

Fast Introduction to Object Oriented Programming and C++

Function Overloading

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

1 // 2D FFT Using threads 2 // George F. Riley, Georgia Tech, Fall 2009 3 // This illustrates how a mutex would be implemented using Leslie Lamport s 4 // "Bakery Algorithm". This algorithm implements a correct mutex 5 // without any specific "atomic" instruction support from the hardware. 6 7 #include <iostream> 8 9 #include "pthread.h" 10 #include "math.h" 11 #include <sys/time.h> 12 13 #include "complex.h" 14 #include "InputImage.h" 15 16 using namespace std; 17 18 // Define a helper class to compare a tuple (number, thread-id) 19 // for less than. 20 class NumberId { 21 public: 22 NumberId(int n, int t) : number(n), threadid(t) {} 23 bool operator< (const NumberId& rhs); 24 public: 25 int number; 26 int threadid; 27 }; 28 29 bool NumberId::operator < (const NumberId& rhs) 30 { 31 // Less than if lhs.number < rhs.number, or 32 // if lhs.number == rhs.number AND lhs.threadid < rhs.threadid 33 if (number < rhs.number) return true; 34 if (number == rhs.number && threadid < rhs.threadid) return true; 35 return false; 36 }; 37 38 class BakeryMutex { 39 public: 40 BakeryMutex(int nthreads); 41 void Lock(int myid); // Lock the mutex 42 void UnLock(int myid); // UnLock the mutex 43 private: 44 int N; // Number of threads 45 bool* choosing; // True if choosing a ticket, one per thread 46 int* number; // Ticket number chosen, 0 if no ticket 47 }; Program threaded-fft-bakery.cc 1

48 BakeryMutex::BakeryMutex(int nthreads) 49 : N(nThreads) 50 { 51 // Allocate the two thread specific values, "choosing" and "number" 52 choosing = new bool[n]; 53 number = new int[n]; 54 // Initialize 55 for (int i = 0; i < N; ++i) 56 { 57 choosing[i] = false; 58 number[i] = 0; 59 } 60 } 61 62 void BakeryMutex::Lock(int myid) 63 { 64 // First note that we are in the process of "choosing" a ticket number 65 choosing[myid] = true; 66 // Find the maximum already chosen, and pick that number + 1 67 int maxticket = 0; 68 for (int i = 0; i < N; ++i) 69 { 70 if (number[i] > maxticket) maxticket = number[i]; 71 } 72 // Set my number to the maxticket + 1 73 number[myid] = maxticket + 1; 74 // Indicate we are no longer choosing 75 choosing[myid] = false; 76 // Now defer to anyone with a smaller ticket. If we have ties 77 // (choosing the same ticket) defer if their threadid is 78 // less than ours 79 for (int i = 0; i < N; ++i) 80 { 81 while(choosing[i]) {} // Spin if someone else is choosing 82 while(number[i]!= 0 && 83 NumberId(number[i], i) < NumberId(number[myId], myid)) 84 { // Spin while some other thread has a lower ticket number 85 } 86 } 87 // At this point, we have the lowest ticket number and have essentially 88 // claimed the lock. 89 } 90 91 void BakeryMutex::UnLock(int myid) 92 { // Release our ticket number 93 number[myid] = 0; 94 } 2

95 // We use global variables in lieu of member variables for this example 96 Complex** h; // Points to the 2D array of complex (the input) 97 Complex* W; // Weights (computed once in main 98 unsigned N; // Number of elements (both width and height) 99 unsigned nthreads; // Desired number of threads 100 unsigned activecount = 0; // Number of active threads 101 102 // pthread variables 103 // We will replace the activemutex and coutmutex with our 104 // implementation to observe effects. We can t replace the exit mutex 105 // since it is needed for the condition variable (which we did not 106 // implement a replacement for. 107 BakeryMutex* activemutex; 108 pthread_mutex_t exitmutex; 109 pthread_cond_t exitcondition; 110 BakeryMutex* coutmutex; 111 112 // Add a verbose flag to turn on/off extra outputs 113 bool verbose = false; 114 115 // Helper routines 116 void DumpTransformedValues() 117 { // Code omitted for brevity 118 } 119 120 void TransposeInPlace(Complex** m, int wh) 121 { // code omitted for brevity 122 } 123 124 125 void LoadWeights() 126 { // Compute the needed W values. Omitted for brevity 127 } 128 129 void Transform1D(Complex* h) 130 { // The simple 1D transform we did earlier. Code omitted for brevity 131 } 3

132 void* FFT_Thread(void* v) 133 { 134 unsigned long myid = (unsigned long)v; // My thread number 135 unsigned rowspercpu = N / nthreads; 136 unsigned myfirstrow = myid * rowspercpu; 137 // We have to do a mutex around the "activecount++". Why? 138 activemutex->lock(myid); 139 activecount++; 140 activemutex->unlock(myid); 141 if (verbose) 142 { 143 coutmutex->lock(myid); 144 cout << "MyId is " << myid << " myfirstrow " << myfirstrow << endl; 145 coutmutex->unlock(myid); 146 } 147 // Call the 1D FFT on each row 148 for (unsigned i = 0; i < rowspercpu; ++i) 149 { 150 Transform1D(h[myFirstRow + i]); 151 } 152 // Now notify the main thread we have completed the rows 153 pthread_mutex_lock(&exitmutex); // Insure only one thread signals the exit 154 activemutex->lock(myid); // Insure only one thread changes active 155 activecount--; 156 activemutex->unlock(myid); 157 // Don t need cout mutex here. Why? 158 cout << "Thread " << myid << " exited, activecount " << activecount << endl; 159 if (activecount == 0) 160 { // We are the last thread to exit. Signal the main thread 161 // that all threads are done 162 pthread_cond_signal(&exitcondition); 163 } 164 pthread_mutex_unlock(&exitmutex); 165 return 0; 166 } 4

167 int main( int argc, char** argv) 168 { 169 verbose = argc > 3; 170 InputImage image(argv[1]); 171 nthreads = atol(argv[2]); // Number of threads 172 N = image.getheight(); // Assume square, width = height 173 h = image.getrows(0, N); // In this case, we get all rows 174 175 // Start the timer here, after loading the image 176 struct timeval tp; 177 gettimeofday(&tp, 0); 178 double startsec = tp.tv_sec + tp.tv_usec/1000000.0; 179 180 LoadWeights(); // Only need to do this once 181 182 // Initialize the BakeryMutexes 183 activemutex = new BakeryMutex(nThreads + 1); 184 coutmutex = new BakeryMutex(nThreads + 1); 185 186 // Initialize the pthread mutex and condition variables 187 pthread_mutex_init(&exitmutex, 0); 188 pthread_cond_init(&exitcondition, 0); 189 190 // We lock the exitmutex to be sure no threads exit until 191 // all threads created, and we are waiting on the condition signal 192 pthread_mutex_lock(&exitmutex); 193 // Create the threads 194 for (unsigned i = 0; i < nthreads; ++i) 195 { 196 pthread_t t; 197 pthread_create(&t, 0, FFT_Thread, (void*)i); 198 } 199 // Now wait for them to finish pass 1 200 pthread_cond_wait(&exitcondition, &exitmutex); 201 if (verbose) cout << "All threads finished pass 1" << endl; 202 203 // Transpose the matrix and schedule threads to do rows again 204 TransposeInPlace(h, N); 205 // Start the threads again 206 for (unsigned i = 0; i < nthreads; ++i) 207 { 208 pthread_t t; 209 pthread_create(&t, 0, FFT_Thread, (void*)i); 210 } 211 // Now wait for them to finish pass 2 212 pthread_cond_wait(&exitcondition, &exitmutex); 213 if (verbose) cout << "All threads finished pass 2" << endl; 214 215 // Transpose back and write results 216 TransposeInPlace(h, N); 217 gettimeofday(&tp, 0); 218 cout << "Calculated FFT " 219 << (tp.tv_sec+tp.tv_usec/1000000.0) - startsec << " seconds" << endl; 220 DumpTransformedValues(); 221 } 222 5

6