# Cool Numbers 7 students got the lowest complexity: O(n 1/6 ) Well done! –Emily Vukovich –Priyank Malvania –Kunal Choudhary –Vincent Lo –Carl Lam –Jerry.

## Presentation on theme: "Cool Numbers 7 students got the lowest complexity: O(n 1/6 ) Well done! –Emily Vukovich –Priyank Malvania –Kunal Choudhary –Vincent Lo –Carl Lam –Jerry."— Presentation transcript:

Cool Numbers 7 students got the lowest complexity: O(n 1/6 ) Well done! –Emily Vukovich –Priyank Malvania –Kunal Choudhary –Vincent Lo –Carl Lam –Jerry Zhang –Weicheng Cao Best solution: not just coding, also thinking about the math of the problem –Typical of the best programs/algorithms

Administration Course evaluations –Due April 13 Response rate so far: 1.2% ! –Please fill out We read them all & take very seriously Completely redesigned course  What worked & what didn’t? Free-form questions –1 most interesting / important thing you learned –1 least interesting thing  what would you remove?

Thread Synchronization Problem –cout is a global variable –All threads writing to it without coordination / synchronization –Getting their output interleaved Solution –Only one thread should write to cout at a time –How? –mutex (mutual exclusion) variable –“lock” this variable Only 1 thread gets lock at a time Only that thread can write to cout

Who is Godfrey Hounsfield? 1.A British Electrical Engineer 2.Knighted (Sir Godfrey) 3.Winner of the Nobel Prize in medicine 4.Inventor of the CAT scanner Broad knowledge  innovate across disciplines!

Multithreading for Speed Vector dot product –prod = v1  v2 double dotProd (const vector & v1, const vector & v2) { double prod = 0; for (int i = 0; i < v1.size(); i++) { prod += v1[i] * v2[i]; } return (prod); } #define N 400000000 int main () { vector v1(1,N), v2(2,N); cout << “dot product: “ << dotProd (v1, v2) << endl; } Output: 8e8

Parallel Dot Product: Idea 111111111111111111111111 222222222222222222222222 v1 v2 myThread[0] 111111111111111111111111 222222222222222222222222 v1 v2 prod 02468 Serial myThread[1] myThread[2] myThread[3] Parallel on 4 CPUs prod 081624... 24

Parallel Dot Product #define NUM_THREADS 4 // 4 CPUs on each UG machine void dpHelper (const vector & v1, const vector & v2, int istart, int iend, double& prod) { for (int i = istart; i < iend; i++) prod += v1[i] * v2[i]; } Output: 2e8 or 3.3e8 or 4.6e8, ?? Race condition! Many threads updating one variable This is read, then a write! Some threads reading old value, adding and overwriting someone else’s addition