Cilk

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Cilk
Paradigm imperative (procedural), structured, parallel
Designed by MIT Laboratory for Computer Science
Developer Intel
Appeared in 1994
static, weak, manifest
Cilk Plus
Designed by Intel
Developer Intel
Appeared in 2010
1.2 / September 9, 2013; 18 months ago (2013-09-09)
(Same as C or C++)
Website www.cilkplus.org

Cilk, Cilk++ and Cilk Plus are general-purpose programming language designed for multithreaded parallel computing. They are based on the C and C++ programming languages and extend these with constructs to express parallel loops and the fork–join idiom.

Originally developed in the 1990s at the Massachusetts Institute of Technology (MIT) in the group of Charles E. Leiserson, Cilk was later commercialized as Cilk++ by a spinoff company, Cilk Arts. That company was subsequently acquired by Intel, which increased compatibility with existing C and C++ code, calling the result Cilk Plus.

History[edit]

The Cilk programming language grew out of three separate projects at the MIT Laboratory for Computer Science:[citation needed]

  • Theoretical work on scheduling multi-threaded applications.
  • StarTech – a parallel chess program built to run on the Thinking Machines Corporation's Connection Machine model CM-5.
  • PCM/Threaded-C – a C-based package for scheduling continuation-passing-style threads on the CM-5

In April 1994 the three projects were combined and christened "Cilk". The name Cilk is not an acronym, but an allusion to "nice threads" (silk) and the C programming language. The Cilk-1 compiler was released in September 1994.

The original Cilk is based on ANSI C, with the addition of just a handful of Cilk-specific keywords. When the Cilk keywords are removed from Cilk source code, the result is a valid C program, called the serial elision (or C elision) of the full Cilk program. Cilk is a faithful extension of C and the serial elision of any Cilk program is always a valid serial implementation in C of the semantics of the parallel Cilk program. Despite several similarities, Cilk is not directly related to AT&T Bell Labs' Concurrent C.

Cilk++, Cilk Arts and Intel[edit]

Prior to ~2006, the market for Cilk was restricted to high-performance computing. The emergence of multicore processors in mainstream computing means that hundreds of millions of new parallel computers are now being shipped every year. Cilk Arts was formed to capitalize on that opportunity: In 2006, Professor Leiserson launched Cilk Arts to create and bring to market a modern version of Cilk that supports the commercial needs of an upcoming generation of programmers. The company closed a Series A venture financing round in October 2007, and Cilk++ 1.0 shipped in December, 2008. Cilk++ differs from Cilk in several ways: support for C++, support for loops, and "Cilk hyperobjects" - a new construct designed to solve data race problems created by parallel accesses to global variables.

On July 31, 2009, Cilk Arts announced on its web site that its products and engineering team were now part of Intel Corp. Intel and Cilk Arts integrated and advanced the technology further resulting in a September 2010 release of Intel Cilk Plus.[1][2] Cilk Plus adopts simplifications, proposed by Cilk Arts in Cilk++, to eliminate the need for several of the original Cilk keywords while adding the ability to spawn functions and to deal with variables involved in reduction operations. Cilk Plus differs from Cilk and Cilk++ by adding array extensions, being incorporated in a commercial compiler (from Intel), and compatibility with existing debuggers.[3] Intel has stated its desire to refine Cilk Plus and to enable it to be implemented by other compilers to gain industry wide adoption.[4] Work has started on a GCC implementation based in part on Intel's run time code contributed to open source by Intel which includes code they acquired from Cilk Arts plus additions by Intel and former Cilk Arts employees.[5]

Intel has also released a specification to enable other compatible implementations, and has said the trademark will be usable by compliant implementations.[citation needed]

Differences between versions[edit]

In the original MIT Cilk implementation, the first Cilk keyword is in fact cilk, which identifies a function which is written in Cilk. Since Cilk procedures can call C procedures directly, but C procedures cannot directly call or spawn Cilk procedures, this keyword is needed to distinguish Cilk code from C code. Cilk Plus removes this restriction, as well as the cilk keyword, so C and C++ functions can call into Cilk Plus code and vice versa.

Language features[edit]

The principle behind the design of the Cilk language is that the programmer should be responsible for exposing the parallelism, identifying elements that can safely be executed in parallel; it should then be left to the run-time environment, particularly the scheduler, to decide during execution how to actually divide the work between processors. It is because these responsibilities are separated that a Cilk program can run without rewriting on any number of processors, including one.

Task parallelism: spawn and sync[edit]

Two keywords are needed to use the parallel features of Cilk:

  • spawn indicates that the procedure call it modifies can safely operate in parallel with other executing code. Note that the scheduler is not obligated to run this procedure in parallel; the keyword merely alerts the scheduler that it can do so.
  • sync indicates that execution of the current procedure cannot proceed until all previously spawned procedures have completed and returned their results to the parent frame. This is an example of a barrier method.

In Cilk Plus, the keywords are spelled _Cilk_spawn and _Cilk_sync, or cilk_spawn and cilk_sync if the Cilk Plus headers are included.

Below is a recursive implementation of the Fibonacci function in Cilk, with parallel recursive calls, which demonstrates the cilk, spawn, and sync keywords. (Cilk program code is not numbered; the numbers have been added only to make the discussion easier to follow.)

  1.  cilk int fib (int n)
    
  2.  {
    
  3.      if (n < 2) return n;
    
  4.      else
    
  5.      {
    
  6.         int x, y;
    
  7.  
    
  8.         x = spawn fib (n-1);
    
  9.         y = spawn fib (n-2);
    
  10.  
    
  11.         sync;
    
  12.  
    
  13.         return (x+y);
    
  14.      }
    
  15.  }
    

If this code was executed by a single processor to determine the value of fib(2), that processor would create a frame for fib(2), and execute lines 01 through 05. On line 06, it would create spaces in the frame to hold the values of x and y. On line 08, the processor would have to suspend the current frame, create a new frame to execute the procedure fib(1), execute the code of that frame until reaching a return statement, and then resume the fib(2) frame with the value of fib(1) placed into fib(2)'s x variable. On the next line, it would need to suspend again to execute fib(0) and place the result in fib(2)'s y variable.

When the code is executed on a multiprocessor machine, however, execution proceeds differently. One processor starts the execution of fib(2); when it reaches line 08, however, the spawn keyword modifying the call to fib(n-1) tells the processor that it can safely give the job to a second processor: this second processor can create a frame for fib(1), execute its code, and store its result in fib(2)'s frame when it finishes; the first processor continues executing the code of fib(2) at the same time. A processor is not obligated to assign a spawned procedure elsewhere; if the machine only has two processors and the second is still busy on fib(1) when the processor executing fib(2) gets to the procedure call, the first processor will suspend fib(2) and execute fib(0) itself, as it would if it were the only processor. Of course, if another processor is available, then it will be called into service, and all three processors would be executing separate frames simultaneously.

(The preceding description is not entirely accurate. Even though the common terminology for discussing Cilk refers to processors making the decision to spawn off work to other processors, it is actually the scheduler which assigns procedures to processors for execution, using a policy called work-stealing, described later.)

If the processor executing fib(2) were to execute line 13 before both of the other processors had completed their frames, it would generate an incorrect result or an error; fib(2) would be trying to add the values stored in x and y, but one or both of those values would be missing. This is the purpose of the sync keyword, which we see in line 11: it tells the processor executing a frame that it must suspend its own execution until all the procedure calls it has spawned off have returned. When fib(2) is allowed to proceed past the sync statement in line 11, it can only be because fib(1) and fib(0) have completed and placed their results in x and y, making it safe to perform calculations on those results.

Inlets[edit]

The two remaining Cilk keywords are slightly more advanced, and concern the use of inlets. Ordinarily, when a Cilk procedure is spawned, it can return its results to the parent procedure only by putting those results in a variable in the parent's frame, as we assigned the results of our spawned procedure calls in the example to x and y.

The alternative is to use an inlet. An inlet is a function internal to a Cilk procedure which handles the results of a spawned procedure call as they return. One major reason to use inlets is that all the inlets of a procedure are guaranteed to operate atomically with regards to each other and to the parent procedure, thus avoiding the bugs that could occur if the multiple returning procedures tried to update the same variables in the parent frame at the same time.

  • The inlet keyword identifies a function defined within the procedure as an inlet.
  • The abort keyword can only be used inside an inlet; it tells the scheduler that any other procedures that have been spawned off by the parent procedure can safely be aborted.

Inlets were removed when Cilk became Cilk++, and are not present in Cilk Plus.

Reducers and hyperobjects[edit]

Cilk++ added a kind of objects called hyperobjects, that allow multiple strands to share state without race conditions and without using explicit locks. Each strand has a view on the hyperobject that it can use and update; when the strands synchronize, the views are combined in a way specified by the programmer.[6]

The most common type of hyperobject is a reducer, which corresponds to the reduction clause in OpenMP or to the algebraic notion of a monoid. Each reducer has an identity element and an associative operation that combines two values. The archetypal reducer is summation of numbers: the identity element is zero, and the associative reduce operation computes a sum. This reducer is built into Cilk++ and Cilk Plus:

// Compute ∑ foo(i) for i from 0 to N, in parallel.
cilk::reducer_opadd<float> result(0);
cilk_for (int i = 0; i < N; i++)
    result += foo(i);

Other reducers can be used to construct linked lists or strings, and programmers can define their own reducers.

Parallel loops[edit]

Cilk++ added an additional construct, the parallel loop, denoted cilk_for in Cilk Plus. These loops look like

  1. void loop(int *a, int n)
    
  2. {
    
  3.     #pragma cilk grainsize = K
    
  4.     cilk_for (int i = 0; i < n; i++) {
    
  5.         a[i] = f(a[i]);
    
  6.     }
    
  7. }
    

This implements the parallel map idiom: the body of the loop, here a call to f followed by an assignment to the array a, is executed for each value of i from zero to N in an indeterminate order. The optional "grain size" determines the coarsening: no work is spawned when less than K elements need to be processed. The implementation is a divide-and-conquer recursion, as if the programmer had written

static void recursion(int *a, int offset, int n)
{
    if (n - offset <= K)
        for (int i = offset; i < offset + n; i++)
            a[i] = f(a[i]);
    else {
        int midpoint = n / 2;
        cilk_spawn recursion(a, offset, n - midpoint);
        recursion(a, midpoint, n);
        cilk_sync;
    }
}
 
void loop(int *a, int n)
{
    recursion(a, 0, n);
}

Array notation[edit]

Intel Cilk Plus adds notation that allow users to express high-level operations on entire arrays or sections of arrays; e.g., an axpy-style function that is ordinarily written

 // y ← α x + y
 void axpy(int n, float alpha, const float *x, float *y)
 {
     for (int i = 0; i < n; i++) {
         y[i] += alpha * x[i];
     }
 }

can in Cilk Plus be expressed as

y[0:n] += alpha * x[0:n];

These notations help the compiler to effectively vectorize the application. Intel Cilk Plus allows C/C++ operations to be applied to multiple array elements in parallel, and also provides a set of built-in functions that can be used to perform vectorized shifts, rotates, and reductions. Similar functionality exists in Fortran 90; Cilk Plus differs in that it never allocates temporary arrays, so memory usage is easier to predict.

Elemental functions[edit]

In Cilk Plus, an elemental function is a regular function which can be invoked either on scalar arguments or on array elements in parallel. They are similar to the kernel functions of OpenCL.

#pragma simd[edit]

This pragma gives the compiler permission to vectorize a loop even in cases where auto-vectorization might fail. It is the simplest way to manually apply vectorization.

Work-stealing[edit]

Main article: Work stealing

The Cilk scheduler uses a policy called "work-stealing" to divide procedure execution efficiently among multiple processors. Again, it is easiest to understand if we look first at how Cilk code is executed on a single-processor machine.

The processor maintains a stack on which it places each frame that it has to suspend in order to handle a procedure call. If it is executing fib(2), and encounters a recursive call to fib(1), it will save fib(2)'s state, including its variables and where the code suspended execution, and put that state on the stack. It will not take a suspended state off the stack and resume execution until the procedure call that caused the suspension, and any procedures called in turn by that procedure, have all been fully executed.

With multiple processors, things of course change. Each processor still has a stack for storing frames whose execution has been suspended; however, these stacks are more like deques, in that suspended states can be removed from either end. A processor can still only remove states from its own stack from the same end that it puts them on; however, any processor which is not currently working (having finished its own work, or not yet having been assigned any) will pick another processor at random, through the scheduler, and try to "steal" work from the opposite end of their stack—suspended states, which the stealing processor can then begin to execute. The states which get stolen are the states that the processor stolen from would get around to executing last.

Implementations[edit]

The original MIT Cilk was implemented as a translator from Cilk to C, targeting the GNU C Compiler (GCC). The last version, Cilk-5.3, is available from the MIT Computer Science and Artificial Intelligence Laboratory (CSAIL), though it is no longer supported.

Cilk++ was proprietary software, available from Cilk Arts, and implemented like its predecessor as a Cilk-to-C compiler. It supported the Microsoft and GCC compilers.

Cilk Plus was first implemented in the Intel C++ Compiler with the release of the Intel compiler in Intel Composer XE 2010.[citation needed] An open source (BSD-licensed) implementation was contributed by Intel to the GNU Compiler Collection (GCC), which shipped Cilk Plus support in version 4.9,[7] except for the _Cilk_for keyword, which was added in GCC 5.0. In February 2013, Intel announced a Clang fork with Cilk Plus support.[8] The Intel Compiler, but not the open source implementations, come with a race detector and a performance analyzer.

See also[edit]

Notes[edit]

  1. ^ "Intel Flexes Parallel Programming Muscles", HPCwire (2010-09-02). Retrieved on 2010-09-14.
  2. ^ "Parallel Studio 2011: Now We Know What Happened to Ct, Cilk++, and RapidMind", Dr. Dobbs Journal (2010-09-02). Retrieved on 2010-09-14.
  3. ^ "Intel Cilk Plus: A quick, easy and reliable way to improve threaded performance", Intel. Retrieved on 2010-09-14.
  4. ^ "Cilk Plus specification and runtime ABI freely available for download", James Reinders. Retrieved on 2010-11-03.
  5. ^ Announcing the Port of Intel(r) Cilk(tm)Plus into GCC
  6. ^ Frigo, Matteo; Halpern, Pablo; Leiserson, Charles E.; Lewin-Berlin, Stephen (2009). Reducers and other Cilk++ hyperobjects. Proc. Annual Symposium on Parallelism in Algorithms and Architectures (SPAA). ACM. 
  7. ^ "GCC 4.9 Release Series Changes, New Features, and Fixes", Free Software Foundation, Inc. Retrieved on 2014-06-29.
  8. ^ Cilk Plus/LLVM

References[edit]

External links[edit]