EE717
Lecture 14: Cilk
The lecture is partly based on Charles Leiserson’s Slides on Cilk
Lecture 14: Cilk Shankar Balachandran bshankar@ee.iitb.ac.in The - - PowerPoint PPT Presentation
Lecture 14: Cilk Shankar Balachandran bshankar@ee.iitb.ac.in The lecture is partly based on Charles Leisersons Slides on Cilk EE717 Cilk A C language for programming dynamic multithreaded applications on shared-memory multiprocessors.
EE717
The lecture is partly based on Charles Leiserson’s Slides on Cilk
EE717
computations
simulation
EE717
P P P Network
Memory I/O $ $ $
EE717
EE717
int fib (int n) { if (n<2) return (n); else { int x,y; x = fib(n-1); y = fib(n-2); return (x+y); } }
cilk int fib (int n) { if (n<2) return (n); else { int x,y; x = spawn fib(n-1); y = spawn fib(n-2); sync; return (x+y); } }
EE717
cilk int fib (int n) { if (n<2) return (n); else { int x,y; x = spawn fib(n-1); y = spawn fib(n-2); sync; return (x+y); } }
EE717
cilk int fib (int n) { if (n<2) return (n); else { int x,y; x = spawn fib(n-1); y = spawn fib(n-2); sync; return (x+y); } }
4 3 2 2 1 1 1
EE717
sequence of instructions not containing parallel control (spawn, sync, return).
a continue edge.
spawn edge return edge continue edge initial thread final thread
EE717
EE717
EE717
EE717
* Also called critical-path length or computational depth.
EE717
* Also called critical-path length or computational depth.
EE717
EE717
EE717
3 4 5 6 1 2 7 8
EE717
Using many more than 2 processors makes little sense.
EE717
EE717
EE717
void vadd (real *A, real *B, int n){ int i; for (i=0; i<n; i++) A[i]+=B[i]; }
EE717
if (n<=BASE) { int i; for (i=0; i<n; i++) A[i]+=B[i]; } else { void vadd (real *A, real *B, int n){ vadd (A, B, n/2); vadd (A+n/2, B+n/2, n-n/2); } }
void vadd (real *A, real *B, int n){ int i; for (i=0; i<n; i++) A[i]+=B[i]; }
EE717
if (n<=BASE) { int i; for (i=0; i<n; i++) A[i]+=B[i]; } else {
void vadd (real *A, real *B, int n){ cilk spawn vadd (A, B, n/2); vadd (A+n/2, B+n/2, n-n/2); spawn
} } sync;
void vadd (real *A, real *B, int n){ int i; for (i=0; i<n; i++) A[i]+=B[i]; }
EE717
cilk void vadd (real *A, real *B, int n){ if (n<=BASE) { int i; for (i=0; i<n; i++) A[i]+=B[i]; } else { spawn vadd (A, B, n/2); spawn vadd (A+n/2, B+n/2, n-n/2); sync; } }
EE717
BASE
EE717
void vadd1 (real *A, real *B, int n){ int i; for (i=0; i<n; i++) A[i]+=B[i]; } void vadd (real *A, real *B, int n){ int j; for (j=0; j<n; j+=BASE) { vadd1(A+j, B+j, min(BASE, n-j)); } }
cilk void vadd1 (real *A, real *B, int n){ int i; for (i=0; i<n; i++) A[i]+=B[i]; } cilk void vadd (real *A, real *B, int n){ int j; for (j=0; j<n; j+=BASE) { spawn vadd1(A+j, B+j, min(BASE, n-j)); } sync; }
EE717
Work: T1 = ? Span: T1 = ? Parallelism: T1/T1 = ?
BASE
P U N Y !