Software Archive
Read-only legacy content
17061 Discussions

cilk plus tutorials and source code

giannhssdra
Beginner
1,479 Views
Hello all

I would like to ask if there are any good tutorials about cilk plus to start with and any kind of source code to use as example.
I managed to find some pdfs but since i dont know much about cilk i would like to start from the beggining.

Anyone who has anything fro share please give a link.
Thanks a lot
0 Kudos
12 Replies
Balaji_I_Intel
Employee
1,479 Views
Hello Giannhssdra,
Here is a link to a video that gives you an introduction (http://software.intel.com/en-us/videos/introduction-to-intel-cilk-plus/).

I hope that helps!

Balaji V. Iyer.
0 Kudos
Barry_T_Intel
Employee
1,479 Views

The Cilk Plus Evaluation Guide is a good start. It will walk you through the task and data parallelism features of Intel Cilk Plus. http://software.intel.com/sites/products/evaluation-guides/docs/cilk-plus-evaluation-guide.pdf.

If you have installed the Intel Compose XE C/C++ compiler, it also comes with some examples.

- Barry

0 Kudos
giannhssdra
Beginner
1,479 Views
Hello

Thanks for the answers.

Im very confused with the distros of cilk.
I've download the gcc branch installed and i tested it with the fibonnaci example.
Im reading cilk 5.4.6 reference manual and lot of the stuff in there do not work with the compiler.
For example i tried to set the number of proceccors with --nproc and it doesnt take it or other compiler options sush as -cilk-profile -cilk-span
I dont know if this an old pdf im reading but i cant find anything else to start with.
0 Kudos
Barry_T_Intel
Employee
1,479 Views

Here's a quick history lesson:

Cilk 5.4.6 is the latest MIT version of Cilk. It's a source-to-source translator which converts Cilk code into C and then compiles it. MIT Cilk is an extension of C. It's not supported by Intel.

Cilk++ was developed by Cilk Arts which licensed the Cilk technology from MIT. It provided a GCC variant and a "sandwich" around the Microsoft compiler. Cilk++ is an extension of C++. Cilk Arts was purchased by Intel and an updated version of Cilk++ was released as an unsupported "WhatIf" kit. The Cilk++ kit included the Cilkscreen race detector and the Cilkview scalability analyzer. Do NOT use Cilk++. It is unsupported.

Intel Cilk Plus is the merger of the Cilk technology from the Cilk Arts acquisition and data parallization. It has been implemented in the Intel C/C++ compiler as well as the "cilkplus" branch of GCC for the C and C++ compilers. While the Intel implementation is still ahead of the GCC implementation, you can use the documentation from the Intel Composer XE 2011 compilerfor your work. There will be some differences, but they should beminor. Intel Cilk Plus does not include the Cilkscreen race detector or Cilkview scalability analyzer. However, they've been ported to the new compiler and are available as a free download from the Intel Cilk Plus SDK download page.

- Barry

0 Kudos
Balaji_I_Intel
Employee
1,479 Views
Hello Gianhssdra,
This is just to make sure we are on the same path. I apologize in advance if this question is redundant. When you said you downloaded GCC branch, did you download the source from "gcc.gnu.org/svn/gcc/branches/cilkplus" and build it yourself?

Thanks,

Balaji V. Iyer.
0 Kudos
Barry_T_Intel
Employee
1,479 Views
To clarify a bit, assume that nothing in the Cilk 4.5.6 documentation applies to Intel Cilk Plus. They are totally different implementations.

- Barry
0 Kudos
giannhssdra
Beginner
1,479 Views
Thanks a lot Barry
This is a vary good start.At least im in the right direction now.Everything i tried from here are working.
You also told me to download this.I didnt install it yet ,but are these additional tools i can use with cilk through terminal?

Balaji V. Iyer.
I installed the gcc branch from gcc.gnu.org/svn/gcc/branches/cilkplus and followed the tutorial you gave me in this topic.It works fine.
Is there anything else i should install?

Also can you explain to me why this is a seg fault?

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. using namespace std;
  7. void g();
  8. void f() {
  9. //cilk_spawn g();
  10. //cilk_sync;
  11. for( int i = 0 ; i < 5 ; i++ ) {
  12. printf("function f() worker : %d second : %d\n" , __cilkrts_get_worker_number() , i );
  13. sleep(1);
  14. }
  15. }
  16. void g() {
  17. for( int i = 0 ; i < 5 ; i++ ){
  18. sleep(1);
  19. printf("function g() worker : %d second : %d\n" , __cilkrts_get_worker_number() , i );
  20. }
  21. }
  22. int main() {
  23. cout<<"start of main with worker : "<<__cilkrts_get_worker_number()<
  24. cout<<<"spawing f from main()\n";
  25. cilk_spawn f();
  26. cilk_spawn g();
  27. cilk_sync;
  28. return 0;
  29. }

If i run it this way its ok .Main thread waits for both f and g.

But if i uncomment the cilk_spawn and cilk_sync int f() and comment out the cilk_spawn g() in main it runs but gives a seg fault

Main knows nothing about g.Waits only for f() which waits for g().Shouldnt be ok?where is the problem?

Thanks
0 Kudos
Barry_T_Intel
Employee
1,479 Views

Yes, it should work correctly. When I made the changes you suggested they rancorrectly for me on Windows with ICL. Balaji is on his way back from the Cauldron conference and will need to look at this when he gets back.

As an aside, you don't need the second cilk_spawn on the call the g() in main(). When we discuss Cilk programs, we tend to talk about "strands." A strand is a sequence of code without anything that effects the parallelism (a spawn or a sync). So in main(), you've got 4 strands:

  1. From the function entry to the spawn of f()
  2. From the spawn of f() to the spawn of g()
  3. From the spawn of g() to the sync
  4. From the sync to the end of the routine

The 3rd strand is pretty much a waste. If a worker steals it, it will immediately hit the cilk_sync. So you've added (a small amount of) overhead for nothing.

- Barry

0 Kudos
giannhssdra
Beginner
1,479 Views
Yes i see what you mean with the extra strand.
But i dont know why it gives error with the cilk_spawn in f().

If wont use cilk_sync inside f() ,the f() thread will be caught by main with cilk_sync.But it wont catch the g() thread.Is this right?The parent knows only about his children but not his childrens children.
So what happens now?I'll have a zombie thread or it will be caught from the implicit cilk_sync in f();

Also why the cilkview and cilkscreen are not running?
i used this export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.../cilkplusutil/lib32/
it gives this E:3.2 is not a supported linux release.Im using ubuntu 12.04
Is there anything else i should do?

Thanks
0 Kudos
Barry_T_Intel
Employee
1,479 Views

> If wont use cilk_sync inside f() ,the f() thread will be caught by main with cilk_sync.But it wont catch
>the g() thread.Is this right?The parent knows only about his children but not his childrens children.
> So what happens now?I'll have a zombie thread or it will be caught from the implicit cilk_sync in f();

Nope. Cilk imposes strict fork/join semantics on your application. There is always an implied cilk_sync at the end of any function that contains a cilk_spawn to guarantee that all spawned functions have completed before the function returns.

And you'll note that I spoke of "strands" not "threads." When the Cilk runtime starts up, it starts up all of the worker threads that will ever be created. "spawning a function" gives permission for parallelism, it does not demand parallelism. It explicitly does not create a thread.The Cilk runtime will schedule strands on worker threads as it sees fit.

What actually happens is that when the compiler sees a cilk_spawn, it makes a notation in an internal table that the continuation of the function containing the spawn can be stolen, and then continues calling the spawned function.

So let's say that Worker0 is executing main() in your example. When it hits the cilk_spawn for f(), it makes a notation in it's table, and starts executing f(). Some time later, an idle worker which we'll call Worker1 may notice that the continuation is available and steal it. This sets the STOLEN flag in the frame and increments the join counter, indicating that there are outstanding children.

When the spawned function running on Worker0 returns, it notes whether it's parent has been stolen. Ifthe parenthas not been stolen,Worker0 returns from f()normally. If the parent has been stolen, the join counter for the parent is decremented. If the result is 0,then Worker0 is the last one to the sync, soWorker0 jumps to the code following the sync in the parent. Otherwise Worker0 goes into the scheduling loop to look for other work to steal.

On the other hand,when Worker1which stole the continuation in the parent is the last one to the sync, it will simply continue executing. If it's not the last one, it will simply decrement the join counter and go into the the scheduling loop to loop for other work to steal.

Of course, there's lots of details I'm glossing over, but that's the basic concept.

> Also why the cilkview and cilkscreen are not running?
> i used this export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.../cilkplusutil/lib32/
> Is there anything else i should do?

What sort of error message are you getting?

- Barry

0 Kudos
giannhssdra
Beginner
1,479 Views
Thanks that expains a lot
I thought that every spawn correspond to a creation of a new thread.
So when the compiler sees a spawn just lets the runtime system know that this strand may run in parallel.But its work of the runtime system to arrange this strand to run in an existing thread.

im using ubuntu 12.04

cilkview gives this
cilkview: generating scalability data
E:3.2 is not a supported linux release
cilkview error: command exited with error condition: 255

and cilkscreen gives this
E:3.2 is not a supported linux release
0 Kudos
Barry_T_Intel
Employee
1,479 Views

Hmm. Which version of Cilkscreen are you using? Try using the command "cilkscreen -v" to get the version number.

Also, this link may be helpful: http://software.intel.com/en-us/forums/showthread.php?t=101066&o=a&s=lr#166814.

- Barry

0 Kudos
Reply