Converting a Cilk Arts Cilk++ application to Intel® Cilk™ Plus

This document summarizes the differences between the Cilk Arts implementation and the Intel® Cilk™ Plus implementation. It contains specific guidelines for porting Cilk Arts code to Intel® Cilk Plus code that will compile using Intel® Parallel Composer 2011 or Intel® C++ Composer XE.

The file extension has changed

The Intel C++ compiler does not recognize .cilk files.

What needs to be changed:

  • Rename any .cilk files to .cpp, or other C or C++ file extensions recognized by the compiler.

The Cilk keywords have changed

The Cilk keywords recognized by Parallel Composer and C++ Composer XE are _Cilk_spawn, _Cilk_sync, and _Cilk_for.  These keywords have a leading underscore because they are not a standard part of C++.

Customers should continue to use the old keywords, and include cilk/cilk.h to define macros to map the preferred, old keywords to the keywords recognized by Parallel Composer and C++ Composer XE.

What needs to be changed:

  • Include cilk/cilk.h to define macros mapping the old keywords to the new keywords.

No more Cilk context

The Cilk Arts implementation required you to create a cilk::context class and call methods on that class to set the number workers as well as to start executing Cilk code. A side effect of this is that if you had a multithreaded application and created a cilk::context in each thread, by default the Cilk runtime would create a worker for each processor on your system, resulting in an explosion of worker threads.

The Intel® Cilk Plus runtime does away with the entire concept of Cilk contexts. Instead, the runtime maintains a single, global context. The first time that a thread calls a function with a cilk_spawn, the thread will be bound to the Cilk runtime, allowing system worker threads to steal from it. The thread will automatically unbind from the Cilk runtime when the function exits, and will rebind as needed. In addition, the Intel Cilk Plus runtime guarantees that the application will be executing in the same thread that initially bound to the runtime when the thread exits.

What needs to be changed:

  • Remove any uses of cilk::context.
  • Replace any calls to cilk::run (or the templated variant) with simple calls. Any packing and unpacking of parameters should be removed.

cilk::set_worker_count() has been replaced

Since there is no longer a cilk::context, you can’t use it to set the number of workers.

What needs to be changed:

  • cilk::set_worker_count has been replaced by a generalized function to set parameters: int __cilkrts_set_param(<parameter-name>, <value>)

    For example, to have the runtime use 4 worker threads, use

    __cilkrts_set_param("nworkers", "4");

    Note that once the runtime has started, attempting to set the number of workers will fail, since the runtime is already running and the worker threads have already been created.

    Also note that calling __cilkrts_get_nworkers() will start the runtime. On Linux* OS, the Intel Cilk Plus runtime will call get_nprocs() to get the default for the number of worker threads to start. On Windows* OS, the equivalent information is obtained by calling GetSystemInfo().

The environment variable to set the number of workers has changed

The Cilk Arts implementation checked for the environment variable CILK_NPROC, used to override the default number of system workers threads created by the runtime. This has been replaced by the environment variable CILK_NWORKERS. __cilkrts_set_param("nworkers", "<N>") still overrides the default.

No more Cilk linkage

The Cilk Arts implementation allocated frames for Cilk functions in memory from the heap. This allowed frames to move between the threads used to implement the Cilk workers. Instead, the Intel implementation will create stacks as necessary (implemented as fibers on Windows* OS) when work is stolen from another worker. This means that you can call from a C++ function to a Cilk function without calling cilk::run().

What needs to be changed:

  • Remove any uses of extern "cilk".
  • Remove any uses of extern "c++".
  • Replace any calls to cilk::run (or the templated variant) with simple calls. Any packing and unpacking of parameters should be removed.

No more cilk_main()

cilk_main() was provided by the Cilk Arts compiler to make it easier to write small programs using Cilk. cilk_main() hid the boilerplate of setting up a cilk::context, packing the argc and argv into a struct, transitioning to a Cilk-linkage function, and unpacking the parameters. Because Intel Cilk Plus functions use standard calling conventions, this is no longer needed.

What needs to be changed:

  • Replace any uses of cilk_main() with main().

The Cilk include files have been moved

The Cilk include files are located in a subdirectory of the Composer include directory, which should automatically be searched by the compiler.

What needs to be changed:

  • Modify any #include <cilk.h> statements to be #include <cilk/cilk_api.h>.
  • Modify any include statements for reducer files to include the cilk/ prefix.

cilk.h no longer defines the API functions

cilk.h now only defines macros for the Cilk keywords implemented by Composer. If you use any of the Cilk runtime API functions, you’ll need to explicitly include the files that define them.

What needs to be changed:

  • Functions to manipulate the Intel Cilk Plus runtime are defined in cilk/cilk_api.h, which, in turn, pulls in OS specific variants. The runtime manipulation functions are documented in the OS specific include files.
  • Functions to manipulate the Cilkscreen race detector are defined in cilk/cilkscreen.h. Note that Cilkscreen is not available for code compiled with the Intel C++ compiler as of this writing.
  • Functions needed to implement reducers are defined in cilk/reducer.h

Command line options have changed

 Effect  Cilk Arts Windows  Cilk Arts gcc  Composer 
 Serialize the code  /cilkp cpp  -fcilk-stub  -Qcilk-serialize
 Compile as Cilk  /TK  -x cilk++  On by default; occurs automatically 

Predefined macros have changed

 Cilk Arts Macro Composer Macro
 __cilkplusplus  __cilk
 __cilk_cpp  None, though cilk_stub.h will define CILK_STUB 

The grainsize pragma has been renamed

Cilk Arts pragmaComposer pragma
#pragma cilk_grainsize = 1 #pragma cilk grainsize = 1

That is, the underscore turned into a space.

Cilkscreen and Cilkview tools

The Cilkscreen race detector and Cilkview performance analysis tool are now available as a separate download from the Intel Cilk Plus Software Development Kit dowload page.

* Denotes a trademark of a third party.

Einzelheiten zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.