close

Enter

Log in using OpenID

Cilk

embedDownload
Cilk
CISC 879 Parallel Computation
Erhan Atilla Avinal
Introduction
What is Cilk ?
 Multithreaded Computation
 Language Specifications
 Cilk Runtime System
 Compiling & Running

2/21
What is Cilk?
A language for multithreaded parallel
programming based on C.
 Effective in highly asynchronous
parallelism.
 No message passing

3/21
Multithreaded Computation
Cilk guarantees that programs are
scheduled efficiently by its runtime system.
 Programmer does not deal with message
passing.

4/21
int fib (int n)
{
if (n<2) return (n);
else
{
int x, y;
x = fib (n-1);
y = fib (n-2);
return (x+y);
}
}
int main (int argc, char *argv[])
{
int n, result;
n = atoi(argv[1]);
result = fib (n);
printf ("Result: %d\n",
result);
return 0;
}
#include <cilk.h>
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);
}
}
cilk int main (int argc, char
*argv[]){
int n, result;
n = atoi(argv[1]);
result = spawn fib(n);
sync;
printf ("Result: %d\n",
result);
return 0;
}
5/21
Multithreaded Computation
6/21
Spawn

Spawns Cilk procedures to start parallel
computations
int i;
float j;
cilk int foo();
i= spawn foo();
j= spawn foo();
YES
NO
x = spawn foo() + spawn bar(); NO
7/21
Shared Memory
Cilk supports shared memory
 Sharing occurs by passing pointers to
spawned procedures (call by reference).

8/21
Shared Memory
Data Race
cilk int foo (void)
{
int x = 0, y;
spawn bar(&x);
y = x + 1;
sync;
return (y);
}
cilk int foo (void)
{
int x = 0;
spawn bar(&x);
x = x + 1;
sync;
return (x);
}
cilk void bar (int *px)
{
printf("%d", *px + 1);
return;
}
cilk void bar (int *px)
{
*px = *px + 1;
return;
}
9/21
Locking

Cilk_lockvar data type
#include <cilk-lib.h>
:
:Cilk_lockvar mylock;
:
{
Cilk_lock_init(mylock);
:
Cilk_lock(mylock); /* begin critical section */
:
Cilk_unlock(mylock); /* end critical section */
}
10/21
Inlets
C Functions internal to a Cilk procedure
 Cannot contain spawn or sync
 Cilk guarantees that all operations in inlets
are atomic

11/21
Inlets
cilk int fib (int n)
{
int x = 0;
inlet void summer (int result)
{
x += result;
return;
}
if (n<2) return n;
else {
summer(spawn fib (n-1));
summer(spawn fib (n-2));
sync;
return (x);
}
}
12/21
SYNCHED Keyword

Tests if procedure is synchronized.
state1 = alloca(state_size);
/* fill in *state1 with data */
spawn foo(state1);
if (SYNCHED)
state2 = state1;
else
state2 = alloca(state_size);
/* fill in *state2 with data */
spawn bar(state2);
sync;
13/21
Aborting
“abort” : causes all of the spawned
children to abort.
 Cilk does not guarantee that all children
will be aborted instantly.

14/21
Aborting
cilk void baz(int n);
cilk void foo(int n)
{
inlet void bar()
{
abort;
}
bar(spawn baz(17));
spawn baz(28);
sync;
}
15/21
Cilk Runtime System(CRS)
CRS implements a efficient scheduling
policy : work-stealing
 When a processor runs out of work it
steals work from another processor
randomly.
 Deque (Double-Ended Queue)

16/21
Cilk Runtime System(CRS)
17/21
Compiling

“cilk” command
 supports
gcc options
 cilk fib.cilk -o fib
 cilk -cilk-profile -cilk-critical-path -O2 fib.cilk
 -cilk-profile : how much memory is used, how
much time each processor spends, etc.
18/21
Compiling

“cilk” command
 -cilk-critical-path
: measures ideal execution
time of your program on an infinite number of
processors.
It may slow down the execution of the
program because of many calls to timing
routines.
19/21
Running
fib --nproc 4 --stats 1 30
 --stats : shows performance data if -cilkprofile is used incompilation.
 --nproc : number of processors used

20/21
Reading List
Cilk Main Page
http://supertech.lcs.mit.edu/cilk
Cilk 5.3.2 Reference Manual
http://supertech.lcs.mit.edu/cilk/manual-5.3.2.pdf
21/21
Author
Document
Category
Uncategorized
Views
7
File Size
84 KB
Tags
1/--pages
Report inappropriate content