Docsity

Understanding Cilk++: Language Features, Performance Measures, and Scheduling, Slides of Parallel Computing and Programming

Partial preview of the text

Download Understanding Cilk++: Language Features, Performance Measures, and Scheduling and more Slides Parallel Computing and Programming in PDF only on Docsity!

Cilk++, Parallel Performance, and

the Cilk Runtime System

Tuesday

  • Overview of Cilk language features
  • Divide and conquer programming in Cilk
  • Advanced Cilk

—inlet

—abort

—SYNCHED

  • Performance measures

Cilk++: Differences with Cilk

  • cilk_main^ instead of main
  • cilk_spawn^ instead of^ spawn
  • No need to mark procedures with the^ cilk^ keyword anymore

—can call procedures directly or with cilk_spawn

  • cilk_sync^ instead of^ sync
  • cilk::mutex^ instead of cilk lock variables

—methods: void lock(), void unlock(), and bool try_lock()

  • No support for^ abort
  • cilk_for
  • Reducers rather than^ inlet
  • Race detection with^ cilkscreen 4

Cilk++ Parallel Loop: cilk_for

  • Loop index^ v —type T can be an integer, ptr, or a^ C++ random access iterator
  • Some restrictions —must compare v with end value using <, <=, !=, >=, or > —loop increment must use ++, --, +=, v = v + incr, or v = v - incr
  • if v is not a signed integer, loop must count up —runtime must be able to compute total # of iterations 5 cilk_for (T v = begin; v < end; v++) {

statement_1;

statement_2;

}

cilk_for vs. Loop with a cilk_spawn

Figure credits: Intel Cilk++ Programmer’s Guide. Document # 322581-001US. 7

  • for (int i = 0; i < 8; i++) {^ cilk_spawn^ work(i); }^ cilk_sync;
  • cilk_for^ (int i = 0; i < 8; i++) { work(i);}

Cilk++ cilk_for Restrictions

  • No early exit

—no break or return statement within loop

—no goto in loop unless target is within loop body

  • Loop induction variable restrictions

—loop induction variable must be declared in loop header

—only one loop induction variable allowed

—can’t modify loop induction variable within loop

—can’t modify end bound within loop

Non-local Variable Example

Automaker: hierarchical 3D CAD representation of assemblies Example source: http://www.cilk.com/multicore-blog/bid/5672/Global-Variable-Reconsidered 10 Computing a cutaway view Node *target; std::list<Node *> output_list; ... void walk(Node *x) { switch (x->kind) { case Node::LEAF: if (target->collides_with(x)) output_list.push_back(x); break; case Node::INTERNAL: for (Node::const_iterator child = x->begin(); child != x->end(); ++child) walk(child); break; } }

Adding Cilk++ Parallelism

Global variable causes data races! 11 Computing a cutaway view in parallel Node *target; std::list<Node *> output_list; ... void walk(Node *x) { switch (x->kind) { case Node::LEAF: if (target->collides_with(x)) output_list.push_back(x); break; case Node::INTERNAL: cilk_for (Node::const_iterator child = x->begin(); child != x->end(); ++child) walk(child); break; } }

Solution 2: Refactor the Code

Node *target; std::list<Node *> output_list; ... void walk(Node *x, std::list<Node *> o_list) { switch (x->kind) { case Node::LEAF: if (target->collides_with(x)) o_list.push_back(x); break; case Node::INTERNAL: std::vector<std::list<Node *> > children_list(x.num_children); cilk_for (Node::const_iterator child = x->begin(); child != x->end(); ++child) walk(child, children_list[child]); for (int i=0; i < x.num_children; ++i) o_list.splice( o_list.end(), children_list[i]); break; } 13

  • Have each child accumulate results in a separate list
  • Splice them all together
  • Drawback: development time, debugging

Cilk++ Reducers

  • Reducers support update of nonlocal variables without races

—deterministic update using associative operations

  • e.g. list and output stream append; global sum, ...
  • result using is same as serial version independent of # processors or scheduling
  • Can be used without significant code restructuring
  • Can be used independently of the program's control structure

— unlike constructs defined only over loops

  • Implemented efficiently with minimal overhead

—they don’t use locks in their implementation

  • avoids loss of parallelism from enforcing mutual exclusion when updating shared variables 14

Reducers

Reducing Over List Concatenation

Using Cilk++ Reducers

  • Include the appropriate Cilk++ reducer header file

reducer_opor.h, reducer_opand.h, reducer_opxor, reducer_list.h,

reducer_max.h, reducer_min.h, reducer_ostream.h,

reducer_opadd.h

  • Declare a variable as a reducer rather than a standard type

—cilk::reducer_list_append sequence versus

—std::list sequence

  • Use the reducers in the midst of work that includes parallelism created with cilk_spawn or cilk_for
  • Retrieve the reducer's terminal value with the get_value() method after the parallelism terminates 19

Cilk++ Reducers Under the Hood

  • If no steal occurs, a reducer behaves like a normal variable
  • If a steal occurs

—the continuation receives a view with an identity value

—the child receives the reducer as it was prior to the spawn

—at the corresponding cilk_sync

  • the value in the continuation is merged into the reducer held by the child using the reducer’s reduce operation
  • the new view is destroyed
  • the original (updated) object survives 20