public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "diego sandoval" <diego.sandoval@cif.org.co>
To: gcc-help@gcc.gnu.org
Subject: openMP gcc vs icc, erratic results with gcc
Date: Wed, 21 May 2008 11:29:00 -0000	[thread overview]
Message-ID: <faa182c80805210034u12afd7fepaa08e5dfce69b7eb@mail.gmail.com> (raw)

 Hi everybody,
I just started working with openMP,  i installed first gcc-4.2.3 and
then gcc-4.3.0,  both of them having  support for openMP.
I tried a code to calculate the product \pi*\e.  When i compile  the
code with gcc (both 4.2.3 and 4.3.0) withtout -fopenmp the result is
correct. When i try with the -fopenmp option the result is erroneous.
I also tried with the intel compiler icc  (with -openmp) in order to
verify the code correctness . There was no problem. I dont know what
is wrong with gcc and this particular code but the results are
erratic. If anyone of you can help me ... thanks in advance.

Let me ellaborate on this problem.

I am using gcc-4.3.0  in slackware 12.0 vanilla,  i have a quad core smp machine

$ uname -a
Linux ra 2.6.24.3-smp #1 SMP Wed Feb 27 18:46:56 COT 2008 i686
Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz GenuineIntel GNU/Linux

$ gcc-4.3.0 -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ./configure
--prefix=/home/medrano/compilers/gcc-4.3.0/ --enable-shared
--enable-languages=c,c++ --enable-threads=posix --enable-__cxa_atexit
--disable-checking --with-gnu-ld --verbose --program-suffix=-4.3.0
Thread model: posix
gcc version 4.3.0 (GCC)



I tried the  openMP code below from
http://www.kallipolis.com/openmp/taylor_mp.c   which is supposed to
calculate the product \pi*\e using the taylor series.

$ gcc-4.3.0 -O2 -fopenmp taylor_mp.c -o taylor.gcc.out
$ icc -O2 -openmp taylor_mp.c -o taylor.intel.out

The results are:

$ ./taylor.gcc.out
Reached result 5.142145 in 10640.000 seconds ### wrong result
$ ./taylor.gcc.out
Reached result 10.795894 in 10660.000 seconds ### wrong result

$ ./taylor.intel.out
Reached result 8.539734 in 9950.000 seconds ### right result
$ ./taylor.intel.out
Reached result 8.539734 in 9570.000 seconds  ### right result




/*
 * taylor.c
 *
 * This program calculates the value of e*pi by first calculating e
 * and pi by their taylor expansions and then multiplying them
 * together.
 */

#include <stdio.h>

#include <time.h>

#define num_steps 20000000

int main(int argc, char *argv[])
{
  double start, stop; /* times of beginning and end of procedure */
  double e, pi, factorial, product;
  int i;


  /* start the timer */
  start = clock();

  /* Now there is no first and seccond, we calculate e and pi */
#pragma omp parallel sections shared(e, pi)
  {
#pragma omp section
    {
      printf("e started\n");

      e = 1;
      factorial = 1; /* rather than recalculating the factorial from
			scratch each iteration we keep it in this varialbe
			and multiply it by i each iteration. */
      for (i = 1; i<num_steps; i++) {

	factorial *= i;
	e += 1.0/factorial;
      }
      printf("e done\n");
    } /* e section */

#pragma omp section
    {
      /* In this thread we calculate pi expansion */
      printf("pi started\n");


      pi = 0;
      for (i = 0; i < num_steps*10; i++) {
	/* we want 1/1 - 1/3 + 1/5 - 1/7 etc.
	   therefore we count by fours (0, 4, 8, 12...) and take
             1/(0+1) =  1/1
	   - 1/(0+3) = -1/3

             1/(4+1) =  1/5
	   - 1/(4+3) = -1/7 and so on */
	pi += 1.0/(i*4.0 + 1.0);
	pi -= 1.0/(i*4.0 + 3.0);
      }
      pi = pi * 4.0;
      printf("pi done\n");
    } /* pi section */


  } /* omp sections */
  /* at this point the threads should rejoin */

  product = e * pi;

  stop = clock();

  printf("Reached result %f in %.3f seconds\n", product, (stop-start)/1000);


  return 0;
}

             reply	other threads:[~2008-05-21  7:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-21 11:29 diego sandoval [this message]
2008-05-21 15:23 ` Tom St Denis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=faa182c80805210034u12afd7fepaa08e5dfce69b7eb@mail.gmail.com \
    --to=diego.sandoval@cif.org.co \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).