



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In all programming language only syntax is different not the logic. This course discuss core concepts for many different programming language and techniques. Key points for this lecture are: Multithreading and Fork-Join, Sequential Programming, Threads of Execution, Synchronize, Concurrent Access, Simplified View of History, Moore's Law, Parallelism and Concurrency, Analogy, Shared Memory
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























So far most or all of your study of computer science has assumed
Called sequential programming – everything part of one sequence
Removing this assumption creates major challenges & opportunities
Note: Terms not yet standard but the perspective is essential
There is some connection:
Parallelism:
Use extra resources to solve a problem faster
resources
Concurrency: Correctly and efficiently manage access to shared resources
work^ requests
resource
Parallelism: Use extra computational resources to solve a problem faster (increasing throughput via simultaneous execution)
Pseudocode for array sum
int sum(int[] arr){ res = new int[4]; len = arr.length; FORALL(i=0; i < 4; i++) { //parallel iterations res[i] = sumRange(arr,ilen/4,(i+1)len/4); } return res[0]+res[1]+res[2]+res[3]; } int sumRange(int[] arr, int lo, int hi) { result = 0; for(j=lo; j < hi; j++) result += arr[j]; return result; }
Concurrency: Correctly and efficiently manage access to shared resources (from multiple possibly-simultaneous clients)
Pseudocode for a shared chaining hashtable
class Hashtable<K,V> { … void insert(K key, V value) { int bucket = …; prevent-other-inserts/lookups in table[bucket] do the insertion re-enable access to table[bucket] } V lookup(K key) { (similar to insert, but can allow concurrent lookups to same bucket) } }
pc=…
pc=…
pc=…
Unshared: locals and control
Shared: objects and static fields
Threads each have own unshared call stack and current statement
Any objects can be shared, but most are not
We will focus on shared memory, but you should know several other models exist and have their own advantages
A node executes after all of its predecessors in the graph
First learn some basics built into Java via java.lang.Thread
To get a new thread running:
What if we instead called the run method of C?
Let’s see how to share memory and coordinate via an example…
ans0 ans1 ans2 ans
ans
class SumThread extends java.lang.Thread { int lo, int hi, int[] arr; // arguments int ans = 0; // result SumThread(int[] a, int l, int h ) { … } public void run (){ … } // override }
int sum(int[] arr){ // can be a static method int len = arr.length; int ans = 0; SumThread[] ts = new SumThread[4]; for(int i=0; i < 4; i++) // do parallel computations ts[i] = new SumThread(arr,ilen/4,(i+1)len/4); for(int i=0; i < 4; i++) // combine results ans += ts[i].ans; return ans; }
int sum(int[] arr){ // can be a static method int len = arr.length; int ans = 0; SumThread[] ts = new SumThread[4]; for(int i=0; i < 4; i++){// do parallel computations ts[i] = new SumThread(arr,ilen/4,(i+1)len/4); ts[i].start(); // start not run } for(int i=0; i < 4; i++) // combine results ans += ts[i].ans; return ans; }
class SumThread extends java.lang.Thread { int lo, int hi, int[] arr; // arguments int ans = 0; // result SumThread(int[] a, int l, int h ) { … } public void run (){ … } // override }