<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><div dir="ltr">Don't know what you meant by "add a piece "B" of code aims to read a .txt file nothing appear".<div>The code you added in B in a normal file read/write. You can easily debug it with gdb.<br><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr">--Junchao Zhang</div></div></div>
<br><div class="gmail_quote">On Fri, Nov 28, 2014 at 6:29 PM, Chafik sanaa <span dir="ltr"><<a href="mailto:san.chafik@gmail.com" target="_blank">san.chafik@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">when I execute the "A" program the result is displayed , but when i trying to add a piece "B" of code aims to read a .txt file nothing appear:<div><br><div>program A:</div><div>---------------------------------------</div><div><div>#include <malloc.h></div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div>#include <time.h></div><div>#include "math.h"</div><div>#include "mpi.h"</div><div><br></div><div>#define MAX_LIGNE 20 /* au maximum, 10 lignes */</div><div>#define MAX_COL 20 /* au maximum 8 colonnes */</div><div>#define NOM_A_LIRE "fichier1.txt" /*matrice a lire*/</div><div>typedef int Matrice[MAX_LIGNE][MAX_COL];</div><div>int main(int argc, char** argv)</div><div>{</div><div><span style="white-space:pre-wrap"> </span>int taskid, ntasks;</div><div><span style="white-space:pre-wrap"> </span>int ierr, i, itask;</div><div><span style="white-space:pre-wrap"> </span>int<span style="white-space:pre-wrap"> </span> sendcounts[2048], displs[2048], recvcount;</div><div><span style="white-space:pre-wrap"> </span>double **sendbuff, *recvbuff, buffsum, buffsums[2048];</div><div><span style="white-space:pre-wrap"> </span>double inittime, totaltime;</div><div><span style="white-space:pre-wrap"> </span>const int nbr_etat = 10;</div><div><span style="white-space:pre-wrap"> </span>double tab[nbr_etat];</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span></div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < nbr_etat; i++)</div><div><span style="white-space:pre-wrap"> </span>tab[i] = i;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < nbr_etat; i++)</div><div><span style="white-space:pre-wrap"> </span>printf("%0.0f<span style="white-space:pre-wrap"> </span>", tab[i]);</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>int nbr_elm[2] = { 4, 6 };</div><div><span style="white-space:pre-wrap"> </span>int dpl[2] = { 0, 4 };</div><div><br></div><div><span style="white-space:pre-wrap"> </span>MPI_Init(&argc, &argv);</div><div><span style="white-space:pre-wrap"> </span>MPI_Comm_rank(MPI_COMM_WORLD, &taskid);</div><div><span style="white-space:pre-wrap"> </span>MPI_Comm_size(MPI_COMM_WORLD, &ntasks);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>recvbuff = (double *)malloc(sizeof(double)*nbr_etat);</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff = (double **)malloc(sizeof(double *)*ntasks);// on execute pour deux proc</div><div><span style="white-space:pre-wrap"> </span>sendbuff[0] = (double *)malloc(sizeof(double)*ntasks*nbr_etat);</div><div><span style="white-space:pre-wrap"> </span>for (i = 1; i < ntasks; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff[i] = sendbuff[i - 1] + nbr_etat;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff = (double **)malloc(sizeof(double *)* 1);</div><div><span style="white-space:pre-wrap"> </span>sendbuff[0] = (double *)malloc(sizeof(double)* 1);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0){</div><div><br></div><div><span style="white-space:pre-wrap"> </span>srand((unsigned)time(NULL) + taskid);</div><div><span style="white-space:pre-wrap"> </span>for (itask = 0; itask<ntasks; itask++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>int k;</div><div><span style="white-space:pre-wrap"> </span>displs[itask] = itask*nbr_etat;</div><div><span style="white-space:pre-wrap"> </span>int s = dpl[itask];</div><div><span style="white-space:pre-wrap"> </span>sendcounts[itask] = nbr_elm[itask];</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (i = 0; i<sendcounts[itask]; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>k = i + s;</div><div><span style="white-space:pre-wrap"> </span>sendbuff[itask][i] = tab[k];</div><div><span style="white-space:pre-wrap"> </span>printf("+ %0.0f ", sendbuff[itask][i]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>recvcount = nbr_elm[taskid];</div><div><br></div><div><span style="white-space:pre-wrap"> </span>inittime = MPI_Wtime();</div><div><br></div><div><span style="white-space:pre-wrap"> </span>ierr = MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>recvbuff, recvcount, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>0, MPI_COMM_WORLD);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>totaltime = MPI_Wtime() - inittime;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>printf("\n<span style="white-space:pre-wrap"> </span>>>>><span style="white-space:pre-wrap"> </span>\n");</div><div><span style="white-space:pre-wrap"> </span>buffsum = 0.0;</div><div><span style="white-space:pre-wrap"> </span>printf("\n<span style="white-space:pre-wrap"> </span>> %d <<span style="white-space:pre-wrap"> </span>\n", taskid);</div><div><span style="white-space:pre-wrap"> </span>for (i = 0; i<recvcount; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>buffsum = buffsum + recvbuff[i];</div><div><span style="white-space:pre-wrap"> </span>printf("* %0.0f<span style="white-space:pre-wrap"> </span>", recvbuff[i]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("(%d) %0.3f ", taskid, buffsum);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>ierr = MPI_Gather(&buffsum, 1, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>buffsums, 1, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>0, MPI_COMM_WORLD);</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>printf(" --> APReS COMMUNICATION <-- \n\n");</div><div><span style="white-space:pre-wrap"> </span>for (itask = 0; itask<ntasks; itask++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>printf("Processus %d : Somme du vecteur reçu : %0.0f\n",</div><div><span style="white-space:pre-wrap"> </span>itask, buffsums[itask]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>printf(" Temps total de communication : %f secondes\n\n", totaltime);</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>/* Libération de la mémoire */</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff[0]);</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff[0]);</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff);</div><div><span style="white-space:pre-wrap"> </span>free(recvbuff);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>/* Finalisation de MPI */</div><div><span style="white-space:pre-wrap"> </span>MPI_Finalize();</div></div><div>}</div><div><br></div><div>program B:</div><div>------------------------- </div></div><div><div>#include <malloc.h></div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div>#include <time.h></div><div>#include "math.h"</div><div>#include "mpi.h"</div><div><br></div><div>#define MAX_LIGNE 20 /* au maximum, 10 lignes */</div><div>#define MAX_COL 20 /* au maximum 8 colonnes */</div><div>#define NOM_A_LIRE "fichier1.txt" /*matrice a lire*/</div><div>typedef int Matrice[MAX_LIGNE][MAX_COL];</div><div>int main(int argc, char** argv)</div><div>{</div><div><span style="white-space:pre-wrap"> </span>int taskid, ntasks;</div><div><span style="white-space:pre-wrap"> </span>int ierr, i, itask;</div><div><span style="white-space:pre-wrap"> </span>int<span style="white-space:pre-wrap"> </span> sendcounts[2048], displs[2048], recvcount;</div><div><span style="white-space:pre-wrap"> </span>double **sendbuff, *recvbuff, buffsum, buffsums[2048];</div><div><span style="white-space:pre-wrap"> </span>double inittime, totaltime;</div><div><span style="white-space:pre-wrap"> </span>const int nbr_etat = 10;</div><div><span style="white-space:pre-wrap"> </span>double tab[nbr_etat];</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span></div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < nbr_etat; i++)</div><div><span style="white-space:pre-wrap"> </span>tab[i] = i;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < nbr_etat; i++)</div><div><span style="white-space:pre-wrap"> </span>printf("%0.0f<span style="white-space:pre-wrap"> </span>", tab[i]);</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>int nbr_elm[2] = { 4, 6 };</div><div><span style="white-space:pre-wrap"> </span>int dpl[2] = { 0, 4 };</div><div><br></div><div><span style="white-space:pre-wrap"> </span>MPI_Init(&argc, &argv);</div><div><span style="white-space:pre-wrap"> </span>MPI_Comm_rank(MPI_COMM_WORLD, &taskid);</div><div><span style="white-space:pre-wrap"> </span>MPI_Comm_size(MPI_COMM_WORLD, &ntasks);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>recvbuff = (double *)malloc(sizeof(double)*nbr_etat);</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff = (double **)malloc(sizeof(double *)*ntasks);// on execute pour deux proc</div><div><span style="white-space:pre-wrap"> </span>sendbuff[0] = (double *)malloc(sizeof(double)*ntasks*nbr_etat);</div><div><span style="white-space:pre-wrap"> </span>for (i = 1; i < ntasks; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff[i] = sendbuff[i - 1] + nbr_etat;</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>sendbuff = (double **)malloc(sizeof(double *)* 1);</div><div><span style="white-space:pre-wrap"> </span>sendbuff[0] = (double *)malloc(sizeof(double)* 1);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0){</div><div><br></div><div><span style="white-space:pre-wrap"> </span>srand((unsigned)time(NULL) + taskid);</div><div><span style="white-space:pre-wrap"> </span>for (itask = 0; itask<ntasks; itask++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>int k;</div><div><span style="white-space:pre-wrap"> </span>displs[itask] = itask*nbr_etat;</div><div><span style="white-space:pre-wrap"> </span>int s = dpl[itask];</div><div><span style="white-space:pre-wrap"> </span>sendcounts[itask] = nbr_elm[itask];</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>for (i = 0; i<sendcounts[itask]; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>k = i + s;</div><div><span style="white-space:pre-wrap"> </span>sendbuff[itask][i] = tab[k];</div><div><span style="white-space:pre-wrap"> </span>printf("+ %0.0f ", sendbuff[itask][i]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>recvcount = nbr_elm[taskid];</div><div><br></div><div><span style="white-space:pre-wrap"> </span>inittime = MPI_Wtime();</div><div><br></div><div><span style="white-space:pre-wrap"> </span>ierr = MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>recvbuff, recvcount, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>0, MPI_COMM_WORLD);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>totaltime = MPI_Wtime() - inittime;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>printf("\n<span style="white-space:pre-wrap"> </span>>>>><span style="white-space:pre-wrap"> </span>\n");</div><div><span style="white-space:pre-wrap"> </span>buffsum = 0.0;</div><div><span style="white-space:pre-wrap"> </span>printf("\n<span style="white-space:pre-wrap"> </span>> %d <<span style="white-space:pre-wrap"> </span>\n", taskid);</div><div><span style="white-space:pre-wrap"> </span>for (i = 0; i<recvcount; i++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>buffsum = buffsum + recvbuff[i];</div><div><span style="white-space:pre-wrap"> </span>printf("* %0.0f<span style="white-space:pre-wrap"> </span>", recvbuff[i]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("(%d) %0.3f ", taskid, buffsum);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>ierr = MPI_Gather(&buffsum, 1, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>buffsums, 1, MPI_DOUBLE,</div><div><span style="white-space:pre-wrap"> </span>0, MPI_COMM_WORLD);</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>printf(" --> APReS COMMUNICATION <-- \n\n");</div><div><span style="white-space:pre-wrap"> </span>for (itask = 0; itask<ntasks; itask++)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>printf("Processus %d : Somme du vecteur reçu : %0.0f\n",</div><div><span style="white-space:pre-wrap"> </span>itask, buffsums[itask]);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>printf(" Temps total de communication : %f secondes\n\n", totaltime);</div><div><span style="white-space:pre-wrap"> </span>printf("##########################################################\n\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>/*===============================================================*/</div><div><span style="white-space:pre-wrap"> </span>/* Libération de la mémoire */</div><div><span style="white-space:pre-wrap"> </span>if (taskid == 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff[0]);</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff[0]);</div><div><span style="white-space:pre-wrap"> </span>free(sendbuff);</div><div><span style="white-space:pre-wrap"> </span>free(recvbuff);</div><div><span style="white-space:pre-wrap"> </span>}</div><div><br></div><div><span style="white-space:pre-wrap"> </span>/*===============================================================*/</div><div><span style="white-space:pre-wrap"> </span>/* Finalisation de MPI */</div><div><span style="white-space:pre-wrap"> </span>MPI_Finalize();</div><div><span style="white-space:pre-wrap"> </span>Matrice A;</div><div><span style="white-space:pre-wrap"> </span>int m, n;</div><div><span style="white-space:pre-wrap"> </span>/* ouvrir le fichier Matrices.dta sur le disque réseau pour la lecture */</div><div><span style="white-space:pre-wrap"> </span>FILE * aLire = fopen(NOM_A_LIRE, "r");</div><div><span style="white-space:pre-wrap"> </span>if (aLire == NULL)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>printf("Le fichier n'existe pas");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>else</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap"> </span>fscanf(aLire, "%d%d\n", &m, &n);</div><div><span style="white-space:pre-wrap"> </span>////void lire(FILE * aLire, Matrice mat, int nbLigne, int nbCol)<span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < m; i++) {</div><div><span style="white-space:pre-wrap"> </span>for (int j = 0; j < n; j++)</div><div><span style="white-space:pre-wrap"> </span>fscanf(aLire, "%d", &A[i][j]);</div><div><span style="white-space:pre-wrap"> </span>fscanf(aLire, "\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>////void afficher(Matrice mat, char nom[], int nbLigne, int nbCol)<span style="white-space:pre-wrap"> </span></div><div><br></div><div><span style="white-space:pre-wrap"> </span>printf("\nContenu de la matrice de %d ligne(s) et %d colonne(s) :\n", m, n);</div><div><span style="white-space:pre-wrap"> </span>for (int i = 0; i < m; i++) {</div><div><span style="white-space:pre-wrap"> </span>for (int j = 0; j < n; j++)</div><div><span style="white-space:pre-wrap"> </span>printf("%d ", A[i][j]);</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>printf("\n");</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>}</div><div><span style="white-space:pre-wrap"> </span>fclose(aLire);</div><div><br></div><div>}</div><div><br></div></div></div>
</blockquote></div><br></div></div></div>