Docsity

Shared-memory Parallel Programming with Cilk, Slides of Parallel Computing and Programming

Partial preview of the text

Download Shared-memory Parallel Programming with Cilk and more Slides Parallel Computing and Programming in PDF only on Docsity!

Shared-memory

Parallel Programming with Cilk

2

Topics for Today

  • Shared-memory systems
  • Cilk overview
  • Example programs
  • Data races and non-determinism
  • Advanced Cilk —inlets — SYNCHED —locks — abort
  • Performance measures

cilk n1 = spawn fib(n-1); n2 = spawn fib(n-2); sync; int fib(int n) { if (n < 2) return n; else { int n1, n2; n1 = fib(n-1); n2 = fib(n-2); return (n1 + n2); } } 4

Introducing Cilk

  • Cilk constructs —cilk: a Cilk function; without the keyword, a function is standard C —spawn: call can execute asynchronously in a concurrent thread —sync: current thread waits for all locally-spawned functions
  • Cilk constructs specify logical parallelism in the program —what computations can be performed in parallel —not the mapping of tasks to processes

5

Cilk Language

  • Cilk is a^ faithful^ extension of C —if Cilk keywords are elided^ →^ C program semantics
  • Idiosyncrasies —spawn keyword can only be applied to a cilk function —spawn keyword cannot be used in a C function —cilk function cannot be called with normal C call conventions - must be called with a spawn & awaited using a sync

7

Cilk Program Execution as a DAG

Legend continuation spawn return each circle represents a thread A B C continuation fib(4) A fib(1) A fib(0) A fib(1) A fib(0) A B C fib(2) A B C fib(3) A B C fib(2) A fib(1) spawn return

8

Task Scheduling in Cilk

  • Alternative strategies —work-sharing: thread scheduled to run in parallel at every spawn - benefit: maximizes parallelism - drawback: cost of setting up new threads is high^ →^ should be avoided —work-stealing: processor looks for work when it becomes idle - lazy parallelism: put off work for parallel execution until necessary - benefits: executes with precisely as much parallelism as needed minimizes the number of threads that must be set up runs with same efficiency as serial program on uniprocessor
  • Cilk uses^ work-stealing^ rather than^ work-sharing

10

Topics for Today

  • Shared-memory systems
  • Cilk overview
  • Example programs
  • Data races and non-determinism
  • Advanced Cilk —inlets — SYNCHED —locks — abort
  • Performance measures

11

Exercise: Sum of First n Integers

Solution sketch?

cilk int main(int argc, char *argv[]) { int n; double result; n = atoi(argv[1]); if (n <= 0) { printf(“'n = %d': ” “n must be positive\n”,n); } else { result = spawn sum(1, n); sync; printf("Result: %lf\n", result); } return 0; } #include <stdlib.h> #include <stdio.h> #include <cilk.h> cilk double sum(int L, int U) { if (L == U) return L; else { double lower, upper; int mid = (U+L)/2; lower = spawn sum(L, mid); upper = spawn sum(mid+ 1, U); sync; return (lower + upper); } }

13

Example: N Queens

  • Problem —place N queens on an N x N chess board —no 2 queens in same row, column, or diagonal
  • Example: a solution to 8 queens problem Image credit: http://en.wikipedia.org/wiki/Eight_queens_puzzle

14

N Queens: Many Solutions Possible

Example: 8 queens

—92 distinct solutions —12 unique solutions; others are rotations & reflections Image credit: http://en.wikipedia.org/wiki/Eight_queens_puzzle

16 cilk void nqueens(n, j, placement) { // precondition: placed j queens so far if (j == n) { /* found a placement */ process placement; return; } for (k = 1; k <= n; k++) if putting j+1 queen in k th position in row j+1 is legal copy placement into newplacement and add extra queen spawn nqueens(n,j+1,newplacement) sync discard placement }

Parallel N Queens Solution Sketch

Issues regarding placements —how can we report placements? —what if a single placement suffices? —no need to compute all legal placements —so far, no way to terminate children exploring alternate placement

17

Approaches to Managing Placements

  • Choices for reporting multiple legal placements —count them —print them on the fly —collect them on the fly; print them at the end
  • If only one placement desired, can skip remaining search

19

Race Conditions (Data Races)

  • Two or more concurrent accesses to the same variable
  • At least one is a write cilk int f() { int x = 0; spawn g(&x); spawn g(&x); sync; return x; } cilk void g(int *p) { *p += 1; } serial semantics? f returns 2 parallel semantics? let’s look closely read x add 1 write x

parallel execution of two instances of g: g, g

many interleavings possible

read x read x add 1 add 1 write x write x one interleaving f returns 1!

20 Erroneous Parallel N Queens Solution Sketch cilk void nqueens(n, j, placement) { // precondition: placed j queens so far if (j == n) { /* found a placement */ process placement; return; } for (k = 1; k <= n; k++) if putting j+1 queen in k th position in row j+1 is legal place j+1 queen in k th position in row j+1 in placement spawn nqueens(n, j+1,placement) remove queen in k th position in row j+1 in placement sync }

Data Races Can Be Subtle! (1)