Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first part of this is getting OpenMP up and running with Visual Studio 2005, which is quite old; it takes some doing, but it's described in the answer to <a href="https://stackoverflow.com/questions/865686/openmp-in-visual-studio-2005-standard">this question</a>.</p> <p>Once that's done, it's fairly easy to do this simple form of task parallelism if you have two methods which are genuinely completely independant. Note that qualifier; if the methods are <em>reading</em> the same data, that's ok, but if they're updating <em>any</em> state that the other method uses, or calling any other routines that do so, then things will break.</p> <p>As long as the methods are completly independant, you can use <a href="https://computing.llnl.gov/tutorials/openMP/#SECTIONS" rel="nofollow noreferrer">sections</a> for these (<a href="http://bisqwit.iki.fi/story/howto/openmp/#TaskDirectiveOpenmp%203%200Only" rel="nofollow noreferrer">tasks</a> are actually the more modern, OpenMP 3.0 way of doing this, but you probably won't be able to get OpenMP 3.0 support for such an old compiler); you will also see people misusing parallel for loops to achieve this, which at least has the advantage of letting you control the thread assignments, so I include that here for completeness even though I can't really recommend it:</p> <pre><code>#include &lt;omp.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; int f1() { int tid = omp_get_thread_num(); printf("Thread %d in function f1.\n", tid); sleep(rand()%10); return 1; } int f2() { int tid = omp_get_thread_num(); printf("Thread %d in function f2.\n", tid); sleep(rand()%10); return 2; } int main (int argc, char **argv) { int answer; int ans1, ans2; /* using sections */ #pragma omp parallel num_threads(2) shared(ans1, ans2, answer) default(none) { #pragma omp sections { #pragma omp section ans1 = f1(); #pragma omp section ans2 = f2(); } #pragma omp single answer = ans1+ans2; } printf("Answer = %d\n", answer); /* hacky appraoch, mis-using for loop */ answer = 0; #pragma omp parallel for schedule(static,1) num_threads(2) reduction(+:answer) default(none) for (int i=0; i&lt;2; i++) { if (i==0) answer += f1(); if (i==1) answer += f2(); } printf("Answer = %d\n", answer); return 0; } </code></pre> <p>Running this gives</p> <pre><code>$ ./sections Thread 0 in function f1. Thread 1 in function f2. Answer = 3 Thread 0 in function f1. Thread 1 in function f2. Answer = 3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload