<?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[Pi Berechnung gibt falsche Werte]]></title><description><![CDATA[<p>Hallo, ich habe eine MPI-Funktion zur Berechnung der Zahl PI geschrieben. Der Serielle Code Funktioniert und sieht wie folgt aus:</p>
<pre><code>#include &lt;ctime&gt;
#include &lt;iostream&gt;

using namespace std;

int main()
{

clock_t begin = clock();
int i;
long num_steps=1000000000;
double step,pi;
double x,sum=0.0;
step=1.0/(double)num_steps;
for(i=0;i&lt;num_steps;i++)
{
x=(i+0.5)*step;
sum=sum+4.0/(1.0+x*x);
}
pi=step*sum;
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout&lt;&lt;&quot;Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled&quot;&lt;&lt;'\n';
cout&lt;&lt;&quot;Pi ist  &quot;&lt;&lt;pi&lt;&lt;'\n';
cout&lt;&lt;&quot;Die Serielle Berechnung dauerte &quot;&lt;&lt;elapsed_secs&lt;&lt;&quot; Sekunden&quot;&lt;&lt;'\n';
return 0;
}
</code></pre>
<p>Der Code gibt &quot;Pi ist 3.14... &quot; aus.</p>
<p>Den gleichen Code habe ich nun in MPI geschrieben:</p>
<pre><code>#include &quot;mpi.h&quot;   
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;ctime&gt;

using namespace std;

    static long num_steps = 1000000000;
    double step;

    int main (int argc, char** argv)
    {
        clock_t begin = clock();
        int i, id, num_procs;
        double x, pi, sum = 0.0;
        MPI_Init(&amp;argc, &amp;argv);
        step = 1.0/(double) num_steps;

        MPI_Comm_rank(MPI_COMM_WORLD, &amp;id);
        MPI_Comm_size(MPI_COMM_WORLD, &amp;num_procs);

        cout&lt;&lt;&quot;Es wurde   &quot;&lt;&lt;num_procs&lt;&lt;&quot;   Prozess generiert&quot;&lt;&lt;endl;
        cout&lt;&lt;&quot;My ID is :  &quot;&lt;&lt;id&lt;&lt;endl;
        for (i=(id+1);i&lt;= num_steps; i+=num_procs){
            x = (i+0.5)*step;
            sum = sum + 4.0/(1.0+x*x);
        }
        cout&lt;&lt;&quot;Pi ist vor der Reduce Operation &quot;&lt;&lt;pi&lt;&lt;endl;
        MPI_Reduce(&amp;sum, &amp;pi, 1, MPI_FLOAT, MPI_SUM, 0,MPI_COMM_WORLD);
        if(id == 0){
            pi *= step;
        }
        clock_t end = clock();
        double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
        cout&lt;&lt;&quot;Pi ist nach der Reduce Operation  &quot;&lt;&lt;pi&lt;&lt;'\n';
        cout&lt;&lt;&quot;Die Parallele Berechnung dauerte &quot;&lt;&lt;elapsed_secs&lt;&lt;&quot; Sekunden&quot;&lt;&lt;'\n';
cout&lt;&lt;&quot;Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled&quot;&lt;&lt;'\n';
    return 0;
    }
</code></pre>
<p>Die Ausgabe lautet wie folgt:</p>
<p>Es wurde 1 Prozess generiert<br />
My ID is : 0<br />
Pi ist vor der Reduce Operation 2.074e-317<br />
Pi ist nach der Reduce Operation 1.4822e-323<br />
Die Parallele Berechnung dauerte 4.37859 Sekunden<br />
Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled</p>
<p>Solange ich auf meinem Privat Rechner mit einem Prozess teste kriege ich natürlich keinen Geschwindigkeitsvorteil, doch warum wird Pi falsch berechnet? Es handelt sich doch um eine gewöhnliche serielle Berechnung oder sehe ich das falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/333528/pi-berechnung-gibt-falsche-werte</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 23:53:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333528.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 Jul 2015 08:50:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Pi Berechnung gibt falsche Werte on Tue, 14 Jul 2015 08:50:18 GMT]]></title><description><![CDATA[<p>Hallo, ich habe eine MPI-Funktion zur Berechnung der Zahl PI geschrieben. Der Serielle Code Funktioniert und sieht wie folgt aus:</p>
<pre><code>#include &lt;ctime&gt;
#include &lt;iostream&gt;

using namespace std;

int main()
{

clock_t begin = clock();
int i;
long num_steps=1000000000;
double step,pi;
double x,sum=0.0;
step=1.0/(double)num_steps;
for(i=0;i&lt;num_steps;i++)
{
x=(i+0.5)*step;
sum=sum+4.0/(1.0+x*x);
}
pi=step*sum;
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout&lt;&lt;&quot;Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled&quot;&lt;&lt;'\n';
cout&lt;&lt;&quot;Pi ist  &quot;&lt;&lt;pi&lt;&lt;'\n';
cout&lt;&lt;&quot;Die Serielle Berechnung dauerte &quot;&lt;&lt;elapsed_secs&lt;&lt;&quot; Sekunden&quot;&lt;&lt;'\n';
return 0;
}
</code></pre>
<p>Der Code gibt &quot;Pi ist 3.14... &quot; aus.</p>
<p>Den gleichen Code habe ich nun in MPI geschrieben:</p>
<pre><code>#include &quot;mpi.h&quot;   
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;ctime&gt;

using namespace std;

    static long num_steps = 1000000000;
    double step;

    int main (int argc, char** argv)
    {
        clock_t begin = clock();
        int i, id, num_procs;
        double x, pi, sum = 0.0;
        MPI_Init(&amp;argc, &amp;argv);
        step = 1.0/(double) num_steps;

        MPI_Comm_rank(MPI_COMM_WORLD, &amp;id);
        MPI_Comm_size(MPI_COMM_WORLD, &amp;num_procs);

        cout&lt;&lt;&quot;Es wurde   &quot;&lt;&lt;num_procs&lt;&lt;&quot;   Prozess generiert&quot;&lt;&lt;endl;
        cout&lt;&lt;&quot;My ID is :  &quot;&lt;&lt;id&lt;&lt;endl;
        for (i=(id+1);i&lt;= num_steps; i+=num_procs){
            x = (i+0.5)*step;
            sum = sum + 4.0/(1.0+x*x);
        }
        cout&lt;&lt;&quot;Pi ist vor der Reduce Operation &quot;&lt;&lt;pi&lt;&lt;endl;
        MPI_Reduce(&amp;sum, &amp;pi, 1, MPI_FLOAT, MPI_SUM, 0,MPI_COMM_WORLD);
        if(id == 0){
            pi *= step;
        }
        clock_t end = clock();
        double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
        cout&lt;&lt;&quot;Pi ist nach der Reduce Operation  &quot;&lt;&lt;pi&lt;&lt;'\n';
        cout&lt;&lt;&quot;Die Parallele Berechnung dauerte &quot;&lt;&lt;elapsed_secs&lt;&lt;&quot; Sekunden&quot;&lt;&lt;'\n';
cout&lt;&lt;&quot;Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled&quot;&lt;&lt;'\n';
    return 0;
    }
</code></pre>
<p>Die Ausgabe lautet wie folgt:</p>
<p>Es wurde 1 Prozess generiert<br />
My ID is : 0<br />
Pi ist vor der Reduce Operation 2.074e-317<br />
Pi ist nach der Reduce Operation 1.4822e-323<br />
Die Parallele Berechnung dauerte 4.37859 Sekunden<br />
Benchmark Testumfeld: Thinkpad i5 3320m Dual Core@3.1 GHZ HT enabled</p>
<p>Solange ich auf meinem Privat Rechner mit einem Prozess teste kriege ich natürlich keinen Geschwindigkeitsvorteil, doch warum wird Pi falsch berechnet? Es handelt sich doch um eine gewöhnliche serielle Berechnung oder sehe ich das falsch?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459670</guid><dc:creator><![CDATA[humanica]]></dc:creator><pubDate>Tue, 14 Jul 2015 08:50:18 GMT</pubDate></item><item><title><![CDATA[Reply to Pi Berechnung gibt falsche Werte on Tue, 14 Jul 2015 09:02:50 GMT]]></title><description><![CDATA[<p>Ich kann dein MPI Beispiel hier gerade nicht testen aber du hast auf jeden Fall die Zeile <code>pi=step*sum;</code> vergessen. Außerdem ist deine for Schleife um einen Index verschoben im Vergleich zu vorher. Sollte es nicht eher <code>for (i=id;i&lt;num_steps; i+=num_procs)</code> heißen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459672</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459672</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Tue, 14 Jul 2015 09:02:50 GMT</pubDate></item><item><title><![CDATA[Reply to Pi Berechnung gibt falsche Werte on Tue, 14 Jul 2015 09:07:48 GMT]]></title><description><![CDATA[<p>Ach quatsch. Du machst ja dann MPI_Reduce mit der Summe. Aber die Ausgabe &quot;Pi ist vor der Reduce Operation&quot; trotzdem schonmal schwachsinn weil bis dahin pi gar nichts zugewiesen wurde. Und sollte bei dem MPI_Reduce Funktionsaufruf nicht MPI_DOUBLE als Parameter auftauchen und nicht MPI_FLOAT?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459673</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459673</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Tue, 14 Jul 2015 09:07:48 GMT</pubDate></item><item><title><![CDATA[Reply to Pi Berechnung gibt falsche Werte on Tue, 14 Jul 2015 09:57:09 GMT]]></title><description><![CDATA[<p>Danke für die Tipps. Ich konnte das Problem lösen, die Berechnung im Hostprozess ist falsch. Es muss pi=sum/step; sein und nicht pi*=sum;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459689</guid><dc:creator><![CDATA[humanica]]></dc:creator><pubDate>Tue, 14 Jul 2015 09:57:09 GMT</pubDate></item><item><title><![CDATA[Reply to Pi Berechnung gibt falsche Werte on Tue, 14 Jul 2015 10:16:24 GMT]]></title><description><![CDATA[<p>Sicher? Wenn ich in der nicht MPI Version <code>pi=sum/step;</code> rechne kriege ich 3.14159e+18 raus. Also irgendwie nicht pi. Meiner Meinung nach muss es entweder <code>pi=sum/num_steps;</code> oder <code>pi=sum*step;</code> heißen. Bzw. nach deinem MPI_Reduce Aufruf steht die Summe ja in der pi Variable, also war <code>pi *= step;</code> schon korrekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459697</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459697</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Tue, 14 Jul 2015 10:16:24 GMT</pubDate></item></channel></rss>