<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mehrere OpenMP - Fragen]]></title><description><![CDATA[<p>Ich versuche gerade, ich in OpenMP einzuarbeiten, und werde hier in diesem Thread meine Schwierigkeiten und Fragen posten.</p>
<p>Da ich den Thread des Öfteren updaten werde, wenn ich auf neue Probleme stoße, lohnt es sich unter Umständen auch später nochmal reinzuschauen.</p>
<p>Falls das das falsche Subforum ist, bitte verschieben.</p>
<p><strong>Problem 1 - offen</strong></p>
<p>Was ich erreichen möchte:<br />
Umgang mit dem Keyword <strong>sections</strong>.</p>
<p>Das Problem, das ich mir dazu ausgedacht habe:<br />
2 Threads initialisieren parallel ein Array - ohne dass ich dabei die einfache Variante mit parallel for verwende.</p>
<p>Ansatz:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;omp.h&gt;

using namespace std;

int main()
{
  int a[20];

  #pragma omp parallel sections shared(a)
  { 
    #pragma omp section
    {
      for(int i=0; i&lt; 10; i++)
      {
        a[i] = i;
        #pragma omp critical
        cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; endl;
      }
    }
    #pragma omp section
    {
      for(int i=10; i&lt; 20; i++)
      {
        a[i] = i; 
        #pragma omp critical
        cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; endl;
      }
    }
  }

  for(int i=0; i&lt; 20; i++)
    cout &lt;&lt; i &lt;&lt; &quot; &quot; ;
  cout &lt;&lt; endl;
  return 0;
}
</code></pre>
<p>Leider wird das hier alles von EINEM Thread getan:</p>
<blockquote>
<p>Thread 1 calculating 0<br />
...<br />
Thread 1 calculating 19</p>
</blockquote>
<p>Wie kann ich erreichen, dass beide Sections von unterschiedlichen Threads ausgeführt werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326307/mehrere-openmp-fragen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 04:24:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326307.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Jun 2014 12:28:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Mehrere OpenMP - Fragen on Wed, 11 Jun 2014 12:28:32 GMT]]></title><description><![CDATA[<p>Ich versuche gerade, ich in OpenMP einzuarbeiten, und werde hier in diesem Thread meine Schwierigkeiten und Fragen posten.</p>
<p>Da ich den Thread des Öfteren updaten werde, wenn ich auf neue Probleme stoße, lohnt es sich unter Umständen auch später nochmal reinzuschauen.</p>
<p>Falls das das falsche Subforum ist, bitte verschieben.</p>
<p><strong>Problem 1 - offen</strong></p>
<p>Was ich erreichen möchte:<br />
Umgang mit dem Keyword <strong>sections</strong>.</p>
<p>Das Problem, das ich mir dazu ausgedacht habe:<br />
2 Threads initialisieren parallel ein Array - ohne dass ich dabei die einfache Variante mit parallel for verwende.</p>
<p>Ansatz:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;omp.h&gt;

using namespace std;

int main()
{
  int a[20];

  #pragma omp parallel sections shared(a)
  { 
    #pragma omp section
    {
      for(int i=0; i&lt; 10; i++)
      {
        a[i] = i;
        #pragma omp critical
        cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; endl;
      }
    }
    #pragma omp section
    {
      for(int i=10; i&lt; 20; i++)
      {
        a[i] = i; 
        #pragma omp critical
        cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; endl;
      }
    }
  }

  for(int i=0; i&lt; 20; i++)
    cout &lt;&lt; i &lt;&lt; &quot; &quot; ;
  cout &lt;&lt; endl;
  return 0;
}
</code></pre>
<p>Leider wird das hier alles von EINEM Thread getan:</p>
<blockquote>
<p>Thread 1 calculating 0<br />
...<br />
Thread 1 calculating 19</p>
</blockquote>
<p>Wie kann ich erreichen, dass beide Sections von unterschiedlichen Threads ausgeführt werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403463</guid><dc:creator><![CDATA[shisha]]></dc:creator><pubDate>Wed, 11 Jun 2014 12:28:32 GMT</pubDate></item><item><title><![CDATA[Reply to Mehrere OpenMP - Fragen on Wed, 11 Jun 2014 12:33:39 GMT]]></title><description><![CDATA[<p>Mh, also in einem Beispiel ist der erste Block (also deine erste Section) ohne #pragma omp section, sondern nur die { ... }.</p>
<p>Frag doch vor dem pragma Block nach den möglichen OpenMP-Threads mit omp_get_max__threads (oder so ähnlich heisst der Befehl). Umgekehrt kannst du ja auch die Threadanzahl einstellen mit omp-set_num_threads (oder so ähnlich ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403464</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403464</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 11 Jun 2014 12:33:39 GMT</pubDate></item><item><title><![CDATA[Reply to Mehrere OpenMP - Fragen on Wed, 11 Jun 2014 20:35:43 GMT]]></title><description><![CDATA[<p>Ich kann mich meinem Vorredner nur anschließen. Bei mir funktioniert folgendes.</p>
<p>Und warum willst du nicht <code>parallel for</code> verwenden?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;omp.h&gt;
#include &lt;vector&gt;

int main()
{
	int const n_threads = 2;

	omp_set_num_threads( n_threads );

	std::vector&lt;double&gt; sample( 20, 0.0 );

  #pragma omp parallel sections shared(sample)
  {
	  #pragma omp section
    {
			for(int i=0; i&lt; 10; i++)
		  {
				sample[i] = i;
				#pragma omp critical
				std::cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num() &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; std::endl;
			}
		}

		#pragma omp section
		{
			for(int i=10; i&lt; 20; i++)
			{
				sample[i] = i;
				#pragma omp critical
					std::cout &lt;&lt; &quot;Thread &quot; &lt;&lt; omp_get_thread_num() &lt;&lt; &quot; calculating &quot; &lt;&lt; i &lt;&lt; std::endl;
			}
		}
	}

	for(double x: sample) std::cout &lt;&lt; x &lt;&lt; &quot; &quot;;

	std::cout &lt;&lt; std::endl;

	return 0;
}
</code></pre>
<p>Ausgabe</p>
<pre><code>Thread 0 calculating 0
Thread 1 calculating 10
Thread 0 calculating 1
Thread 1 calculating 11
Thread 0 calculating 2
Thread 1 calculating 12
Thread 0 calculating 3
Thread 1 calculating 13
Thread 0 calculating 4
Thread 1 calculating 14
Thread 0 calculating 5
Thread 1 calculating 15
Thread 0 calculating 6
Thread 1 calculating 16
Thread 0 calculating 7
Thread 1 calculating 17
Thread 0 calculating 8
Thread 1 calculating 18
Thread 0 calculating 9
Thread 1 calculating 19
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
</code></pre>
<p>Gruß,<br />
-- Klaus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403515</guid><dc:creator><![CDATA[Klaus82]]></dc:creator><pubDate>Wed, 11 Jun 2014 20:35:43 GMT</pubDate></item></channel></rss>