IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

POSIX C Discussion :

posix thread [Débutant(e)]


Sujet :

POSIX C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de sorry60
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    802
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 802
    Par défaut posix thread
    Bonjour,

    Je commence les POSIX Threads.
    Je dois realiser un produit matriciel, R = A * B, chaque R(i,j) devant être calculé par une thread differente.

    J'ai donc fait ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
     
    #include <pthread.h>
    #include <stdio.h>
     
    #define LIN 3
    #define COL 3
     
    void *thread_func(void *);
     
    int A[LIN][COL];
    int B[LIN][COL];
    int R[LIN][COL];
     
    int main(){
      pthread_t thread[LIN][COL];
      int i,j;
      int ind[2];
     
      /* initialisation des matrices */
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          A[i][j] = i + j;
          B[i][j] = i * j;
          R[i][j] = 0;
        }
      }
      /* matrice A */
      printf("Matrice A :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",A[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      /* matrice B */
      printf("Matrice B :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",B[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      /* calcul */
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          ind[0] = i;
          ind[1] = j;
          pthread_create(&thread[i][j], NULL, &thread_func,ind);
        }
      }
      /* On attend la fin de toutes les threads */
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          pthread_join(thread[i][j], NULL);
        }
      }
      /* matrice R */
      printf("\nMatrice R = A * B :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",R[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      return 0;
    }
     
    void *thread_func(void *args){
      int* ind = (int *)args;
      int l = ind[0];
      int c = ind[1];
      int i;
     
      for ( i=0; i<LIN; i++ ){
        R[l][c] += (A[l][i] * B[i][c]);
      }
      printf("Cette thread a calcule R[%d][%d] = %d\n",l,c,R[l][c]);
      pthread_exit(0);
    }
    Mais ça ne fonctionne pas, il y a un problème apparemment au passage de ind car c'est toujours R[2][2] qui est calculé...

    Matrice A :
    0 1 2
    1 2 3
    2 3 4

    Matrice B :
    0 0 0
    0 1 2
    0 2 4

    Cette thread a calcule R[2][2] = 22
    Cette thread a calcule R[2][2] = 44
    Cette thread a calcule R[2][2] = 66
    Cette thread a calcule R[2][2] = 88
    Cette thread a calcule R[2][2] = 110
    Cette thread a calcule R[2][2] = 132
    Cette thread a calcule R[2][2] = 154
    Cette thread a calcule R[2][2] = 176
    Cette thread a calcule R[2][2] = 198

    Matrice R = A * B :
    0 0 0
    0 0 0
    0 0 198
    Voilà, je pige pas trop d'où vient le problème

    Merci pour vos lumières
    Sorry

  2. #2
    Membre éclairé Avatar de sorry60
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    802
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 802
    Par défaut
    J'ai modifié mon code en faisant le pthread_join après chaque création...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
    #include <pthread.h>
    #include <stdio.h>
     
    #define LIN 3
    #define COL 3
     
    void *thread_func(void *);
     
    int A[LIN][COL];
    int B[LIN][COL];
    int R[LIN][COL];
     
    int main(){
      pthread_t thread[LIN][COL];
      int i,j;
      int ind[2];
     
      /* initialisation des matrices */
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          A[i][j] = i + j;
          B[i][j] = i * j;
          R[i][j] = 0;
        }
      }
      /* matrice A */
      printf("Matrice A :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",A[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      /* matrice B */
      printf("Matrice B :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",B[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      /* calcul */
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          ind[0] = i;
          ind[1] = j;
          pthread_create(&thread[i][j], NULL, &thread_func,ind);
          pthread_join(thread[i][j], NULL);
        }
      }
      /* matrice R */
      printf("\nMatrice R = A * B :\n");
      for ( i=0; i<LIN; i++ ){
        for ( j=0; j<COL; j++ ){
          printf("%d ",R[i][j]);
        }
        printf("\n");
      }
      printf("\n");
      return 0;
    }
     
    void *thread_func(void *args){
      int* ind = (int *)args;
      int l = ind[0];
      int c = ind[1];
      int i;
     
      for ( i=0; i<LIN; i++ ){
        R[l][c] += (A[l][i] * B[i][c]);
      }
      printf("Cette thread a calcule R[%d][%d] = %d\n",l,c,R[l][c]);
      pthread_exit(0);
    }
    Ca fonctionne.
    Mais est ce que c'est bien la solution attendue ? car là il n'y a pas vraiment LIN*COL threads qui calculent en concurrence, "en meme temps" chaque R[i][j]...

  3. #3
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par sorry60 Voir le message
    Ca fonctionne.
    Mais est ce que c'est bien la solution attendue ? car là il n'y a pas vraiment LIN*COL threads qui calculent en concurrence, "en meme temps" chaque R[i][j]...
    Non. Tu as masqué le problème en sérialisant les traitements. J'ai donné une solution.

  4. #4
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par sorry60 Voir le message
    Bonjour,

    Je commence les POSIX Threads.
    Je dois realiser un produit matriciel, R = A * B, chaque R(i,j) devant être calculé par une thread differente.
    <...>
    Mais ça ne fonctionne pas, il y a un problème apparemment au passage de ind car c'est toujours R[2][2] qui est calculé...
    Il faut que chaque tâche ait son contexte, sinon, les données sont joyeusement écrasées...
    EDIT : simplification de la structure de données
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
     
    #include <pthread.h>
    #include <stdio.h>
     
    #define LIN 3
    #define COL 3
     
    struct mat
    {
       int A[LIN][COL];
       int B[LIN][COL];
       int R[LIN][COL];
    };
     
    struct data
    {
       struct mat *pmat;
       int ind[2];
    };
     
    void *thread_func (void *args)
    {
       struct data *p = args;
       if (p != NULL)
       {
          int l = p->ind[0];
          int c = p->ind[1];
          int i;
     
          for (i = 0; i < LIN; i++)
          {
             if (p->pmat != NULL)
             {
                p->pmat->R[l][c] += (p->pmat->A[l][i] * p->pmat->B[i][c]);
             }
             else
             {
                puts ("p->pmat=NULL !");
             }
          }
          printf ("Cette tache a calcule R[%d][%d] = %d\n", l, c,
                  p->pmat->R[l][c]);
       }
       return NULL;
    }
     
    int main (void)
    {
       struct mat mat = { {{0}} };
     
       struct ctx
       {
          pthread_t thread;
          struct data data;
       }
       ctx[LIN][COL];
       int i, j;
     
       /* initialisation des matrices */
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             mat.A[i][j] = i + j;
             mat.B[i][j] = i * j;
             mat.R[i][j] = 0;
          }
       }
       /* matrice A */
       printf ("Matrice A :\n");
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             printf ("%d ", mat.A[i][j]);
          }
          printf ("\n");
       }
       printf ("\n");
     
       /* matrice B */
       printf ("Matrice B :\n");
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             printf ("%d ", mat.B[i][j]);
          }
          printf ("\n");
       }
       printf ("\n");
     
       /* calcul */
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             ctx[i][j].data.pmat = &mat;
             ctx[i][j].data.ind[0] = i;
             ctx[i][j].data.ind[1] = j;
     
             printf ("Cette tache calcule avec [%d][%d]\n"
             , ctx[i][j].data.ind[0]
             , ctx[i][j].data.ind[1]);
             pthread_create (&ctx[i][j].thread, NULL, &thread_func, &ctx[i][j].data);
          }
       }
     
       /* On attend la fin de toutes les threads */
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             pthread_join (ctx[i][j].thread, NULL);
             printf ("tache  [%d][%d] terminee\n", i, j);
          }
       }
       /* matrice R */
       printf ("\nMatrice R = A * B :\n");
       for (i = 0; i < LIN; i++)
       {
          for (j = 0; j < COL; j++)
          {
             printf ("%d ", mat.R[i][j]);
          }
          printf ("\n");
       }
       printf ("\n");
       return 0;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    Matrice A :
    0 1 2
    1 2 3
    2 3 4
     
    Matrice B :
    0 0 0
    0 1 2
    0 2 4
     
    Cette tache calcule avec [0][0]
    Cette tache calcule avec [0][1]
    Cette tache a calcule R[0][0] = 0
    Cette tache calcule avec [0][2]
    Cette tache calcule avec [1][0]
    Cette tache calcule avec [1][1]
    Cette tache calcule avec [1][2]
    Cette tache a calcule R[0][1] = 5
    Cette tache a calcule R[0][2] = 10
    Cette tache a calcule R[1][0] = 0
    Cette tache a calcule R[1][1] = 8
    Cette tache a calcule R[1][2] = 16
    Cette tache calcule avec [2][0]
    Cette tache calcule avec [2][1]
    Cette tache calcule avec [2][2]
    tache  [0][0] terminee
    tache  [0][1] terminee
    Cette tache a calcule R[2][0] = 0
    Cette tache a calcule R[2][1] = 11
    Cette tache a calcule R[2][2] = 22
    tache  [0][2] terminee
    tache  [1][0] terminee
    tache  [1][1] terminee
    tache  [1][2] terminee
    tache  [2][0] terminee
    tache  [2][1] terminee
    tache  [2][2] terminee
     
    Matrice R = A * B :
    0 5 10
    0 8 16
    0 11 22
     
     
    Press ENTER to continue.

  5. #5
    Membre éclairé Avatar de sorry60
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    802
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 802
    Par défaut
    Citation Envoyé par Emmanuel Delahaye Voir le message
    Il faut que chaque tâche ait son contexte, sinon, les données sont joyeusement écrasées...
    Ok merci

  6. #6
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par sorry60 Voir le message
    Ok merci
    J'ai un peu simplifié les structures de données (suppression de pind).

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. bot IRC: posix threads et autres remarques
    Par keikoz dans le forum Réseau
    Réponses: 6
    Dernier message: 19/11/2006, 00h52
  2. thread posix
    Par _matt_44 dans le forum POSIX
    Réponses: 9
    Dernier message: 11/05/2006, 12h07
  3. [POSIX][SIGNAL] envoyer un signal a thread avec sigqueue
    Par Mokhtar BEN MESSAOUD dans le forum POSIX
    Réponses: 3
    Dernier message: 09/02/2006, 18h07
  4. Thread Posix - Linux
    Par taron dans le forum Linux
    Réponses: 4
    Dernier message: 27/07/2005, 00h42
  5. question sur les variables globales et les thread posix
    Par souris_sonic dans le forum POSIX
    Réponses: 5
    Dernier message: 13/06/2003, 13h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo