<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic tricky cilkification in Software Archive</title>
    <link>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741530#M628</link>
    <description>&lt;P&gt;
You've already got a cilk_for on your loop searching for primes:
&lt;/P&gt;
&lt;PRE&gt;
cilk_for (i = 79000; i &amp;lt; 10081000; i++)
{
    if (isprime(i))
    {
        printf ( "%d ", i ); 
        fflush(stdout);
    }
}

&lt;/PRE&gt;
&lt;P&gt;Why do you think you need more parallelism?&lt;/P&gt;
&lt;P&gt;Also, I'm sure you know that you'll be racing on the calls to printf.  Fortunately, individual calls to printf seem to be thread-safe, unlike using cout.  You could get a correctly ordered list using a reducer_list and dump it when the loop concludes.  Of course, then you'll get no indication of progress until the application completes.&lt;/P&gt;

&lt;P&gt; - Barry &lt;/P&gt;</description>
    <pubDate>Thu, 29 Mar 2012 17:17:50 GMT</pubDate>
    <dc:creator>Barry_T_Intel</dc:creator>
    <dc:date>2012-03-29T17:17:50Z</dc:date>
    <item>
      <title>tricky cilkification</title>
      <link>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741529#M627</link>
      <description>&lt;P&gt;#include &lt;STDIO.H&gt;&lt;/STDIO.H&gt;&lt;/P&gt;&lt;P&gt;#include &lt;MATH.H&gt;&lt;/MATH.H&gt;&lt;/P&gt;&lt;P&gt;#include "cilkview.h"&lt;/P&gt;&lt;P&gt;#include &lt;CILK&gt;&lt;/CILK&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int isprime ( int n )&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;int m;&lt;/P&gt;&lt;P&gt;int sqrt_n = sqrt(n);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for (m = 2; m &amp;lt;= sqrt_n+1; m++) // +1 in case of obscure rounding error&lt;/P&gt;&lt;P&gt;if ((n % m) == 0) return 0;&lt;/P&gt;&lt;P&gt;return 1;&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;int main ( int argc, char** argv )&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;cilkview_data_t start;&lt;/P&gt;&lt;P&gt;int i;&lt;/P&gt;&lt;P&gt;__cilkview_query(start);&lt;/P&gt;&lt;P&gt;cilk_for (i = 79000; i &amp;lt; 10081000; i++)&lt;/P&gt;&lt;P&gt;if (isprime(i)) { printf ( "%d ", i ); &lt;/P&gt;&lt;P&gt;fflush(stdout); }&lt;/P&gt;&lt;P&gt;__cilkview_report(&amp;amp;start, NULL, "my_tag", CV_REPORT_WRITE_TO_RESULTS);&lt;/P&gt;&lt;P&gt;return 0;&lt;/P&gt;&lt;P&gt;}&lt;BR /&gt;&lt;BR /&gt;In the above program, how does one cilkify is prime and main? I am guessing thatthe &lt;BR /&gt;&lt;BR /&gt;the answer is not to take the following line:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;if (isprime(i)) { printf ( "%d ", i ); &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;and change it to&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;if (spawn isprime(i)) { printf ( "%d ", i ); &lt;BR /&gt;&lt;BR /&gt;That would generate an error; there must be a different way.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance.&lt;BR /&gt;&lt;BR /&gt;Newport_j&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2012 16:34:04 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741529#M627</guid>
      <dc:creator>newport_j</dc:creator>
      <dc:date>2012-03-29T16:34:04Z</dc:date>
    </item>
    <item>
      <title>tricky cilkification</title>
      <link>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741530#M628</link>
      <description>&lt;P&gt;
You've already got a cilk_for on your loop searching for primes:
&lt;/P&gt;
&lt;PRE&gt;
cilk_for (i = 79000; i &amp;lt; 10081000; i++)
{
    if (isprime(i))
    {
        printf ( "%d ", i ); 
        fflush(stdout);
    }
}

&lt;/PRE&gt;
&lt;P&gt;Why do you think you need more parallelism?&lt;/P&gt;
&lt;P&gt;Also, I'm sure you know that you'll be racing on the calls to printf.  Fortunately, individual calls to printf seem to be thread-safe, unlike using cout.  You could get a correctly ordered list using a reducer_list and dump it when the loop concludes.  Of course, then you'll get no indication of progress until the application completes.&lt;/P&gt;

&lt;P&gt; - Barry &lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2012 17:17:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741530#M628</guid>
      <dc:creator>Barry_T_Intel</dc:creator>
      <dc:date>2012-03-29T17:17:50Z</dc:date>
    </item>
    <item>
      <title>tricky cilkification</title>
      <link>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741531#M629</link>
      <description>&lt;BR /&gt;&lt;BR /&gt;Okay, let us drop the c for loop. I am really interested in the embedded ipsime in:&lt;BR /&gt;&lt;BR /&gt;if (isprime()) {printf(%d',i)};&lt;BR /&gt;&lt;BR /&gt;IfI want to spawn that function; I am sure that putting spawn next to it will not work. I must do somethng more elaborate.&lt;BR /&gt;&lt;BR /&gt;if (cilk_spawn (isprime())) {printf(%d',i)};&lt;BR /&gt;&lt;BR /&gt;will not work. &lt;BR /&gt;&lt;BR /&gt;Any help appreciated.&lt;BR /&gt;&lt;BR /&gt;Newport_j&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 29 Mar 2012 18:44:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741531#M629</guid>
      <dc:creator>newport_j</dc:creator>
      <dc:date>2012-03-29T18:44:57Z</dc:date>
    </item>
    <item>
      <title>tricky cilkification</title>
      <link>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741532#M630</link>
      <description>&lt;PRE&gt;&amp;gt; if (isprime()) {printf(%d',i)};
&amp;gt;
&amp;gt; If I want to spawn that function; I am  sure that putting spawn next to it will not work. I must do
&amp;gt; somethng more elaborate. 
&amp;gt;
&amp;gt; if (cilk_spawn (isprime())) {printf(%d',i)};
&amp;gt;
&amp;gt; will not work

&lt;/PRE&gt;&lt;P&gt;Correct. Because the continuation of the expression may try to evaluate the spawned function call before the call returns. You could moving the entire if statement into a function. try something like: &lt;/P&gt;&lt;PRE&gt;void check_for_prime(int i)
{
    if isprime(i)
    {
        printf("%d ", i);
    }
}

and

cilk_spawn check_for_prime(i);

&lt;/PRE&gt;&lt;P&gt;Lambda functions in the new C++ make this much easier. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color:red"&gt;&lt;B&gt;HOWEVER,&lt;/B&gt;&lt;/SPAN&gt; this suffers from spawning off small pieces of work. A Cilk application should always try to spawn off big pieces so that steals are infrequent.&lt;/P&gt;&lt;P&gt;- Barry&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2012 19:21:38 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/tricky-cilkification/m-p/741532#M630</guid>
      <dc:creator>Barry_T_Intel</dc:creator>
      <dc:date>2012-03-29T19:21:38Z</dc:date>
    </item>
  </channel>
</rss>

