Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 08/06/2008, à 11:17

mathieu_38

probleme openmp et gcc 4.2.3

Bonjour,
je profite de ma mise à jour sous hardy pour me mettre à la programmation openmp.
J'ai commencé par suivre un petit tutorial sur openmp pour me faire la main: http://www.kallipolis.com/openmp

J'ai essayé les deux petit programmes proposés dans le tutorial taylor.c et lu.c, voici les codes ainsi que mon makefile:

/*
 * 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 <omp.h>
#include <stdio.h>
#include <time.h>

#define num_steps 20000000
#define USE_OPENMP
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 */
#ifdef USE_OPENMP
#pragma omp parallel sections shared(e, pi)
#endif
  {
#ifdef USE_OPENMP
#pragma omp section
#endif
    {
      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 */
#ifdef USE_OPENMP
#pragma omp section 
#endif
    {
      /* 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)/CLOCKS_PER_SEC);

  return 0;
}
/*
 * LU.c
 * 
 * A prgram to d an LU decomposition.
 *
 */

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 500
#define USE_OPENMP
int main(int argc, char *argv[])
{
  double start, stop; /* for keeping track of running time */
  double A[SIZE][SIZE];
  double col[SIZE], row[SIZE];
  int i, j, k, n;

  /* preload A with random values */
  for (i = 0; i<SIZE; i++)
    for (j = 0; j<SIZE; j++)
      A[i][j] = rand();

  /* time start now */
  start = clock();

  /* The core algorithm */
  // #pragma omp parallel shared(A, col, row)
  for (k = 0; k<SIZE-1; k++) {
    /* set col values to column k of A */
    for (n = k; n<SIZE; n++) {
      col[n] = A[n][k];
    }

    /* scale values of A by multiplier */
    for (n = k+1; n<SIZE; n++) {
      A[k][n] /= col[k];
    }

    /* set row values to row k of A */
    for (n = k+1; n<SIZE; n++) {
      row[n] = A[k][n];
    }

    /* Here we update A by subtracting the appropriate values from row
       and column.  Note that these adjustments to A can be done in
       any order */
#ifdef USE_OPENMP
#pragma omp parallel for shared(A, row, col)
#endif
    for (i = k+1; i<SIZE; i++) {
      for (j = k+1; j<SIZE; j++) {
	A[i][j] = A[i][j] - row[i] * col[j];
      }
    }
  }

  /* we're done so stop the timer */
  stop = clock();

  printf("Completed decomposition in %.3f seconds\n", (stop-start)/CLOCKS_PER_SEC);

  

  return 0;
}
all: taylor lu

taylor: taylor.c
	gcc -O3 -fopenmp -g taylor.c -o taylor -lgomp
	
lu: lu.c
	gcc -O3 -fopenmp lu.c -o lu -lgomp
	
clean:
	rm taylor lu

Tout compile correctement, je n'ai pas de warning mais les programmes ne tournent pascorrectement: je n'ai pas les même résultats avec et sans utilisations de deux cores (j'ai un dell latitude d820) et de plus cela va moins vite quand j'utilise les deux cores hmm
Sans openmp:
taylor "Reached result 8.539734 in 11.550 seconds"
lu "Completed decomposition in 0.000 seconds"
Avec openmp:
taylor Reached result 8.392250 in 14.460 seconds
lu: Completed decomposition in 6.620 seconds

Avec openmp, jai bien deux threads qui tournent (le proc est à 190%). Ayant copier les fichiers depuis le tutorials je ne comprends pas pourquoi cela ne marche pas.
J'ai fais aussi des tests avec mes progs perso (multiplications de matrices et algebre linaires). J'ai des résultats justes, mais cela va moins vite (je travaille pourtant sur des matrices 10000x10000.

Ai-je oublié une option dans la compilation? Si quelqu'un a eu ce genre de problèmes et l'a resolu je suis  preneur!!
Merci,
Mathieu

#2 Le 03/10/2008, à 19:44

Maxime Boissonneault

Re : probleme openmp et gcc 4.2.3

J'ai des résultats similaires. À mon avis, gcc ne supporte pas bien OpenMP. J'ai des codes qui fonctionnent très bien en compilant avec icpc (compilateur Intel), que ce soit avec ou sans openmp, et qui prennent beaucoup plus de temps à exécuter avec g++ openmp et qui en plus donnent des résultats erronés.