[mpich-discuss] (no subject)

Junchao Zhang jczhang at mcs.anl.gov
Sat Nov 29 08:04:24 CST 2014


Don't know what you meant by "add a piece "B" of code aims to read a .txt
file  nothing appear".
The code you added in B in a normal file read/write. You can easily debug
it with gdb.

--Junchao Zhang

On Fri, Nov 28, 2014 at 6:29 PM, Chafik sanaa <san.chafik at gmail.com> wrote:

> 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:
>
> program A:
> ---------------------------------------
> #include <malloc.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <time.h>
> #include "math.h"
> #include "mpi.h"
>
> #define MAX_LIGNE 20   /* au maximum, 10 lignes */
> #define MAX_COL    20   /* au maximum 8 colonnes */
> #define NOM_A_LIRE "fichier1.txt" /*matrice a lire*/
> typedef int Matrice[MAX_LIGNE][MAX_COL];
> int main(int argc, char** argv)
> {
> int          taskid, ntasks;
> int          ierr, i, itask;
> int         sendcounts[2048], displs[2048], recvcount;
> double       **sendbuff, *recvbuff, buffsum, buffsums[2048];
> double       inittime, totaltime;
> const int nbr_etat = 10;
> double tab[nbr_etat];
>
>
>
> for (int i = 0; i < nbr_etat; i++)
> tab[i] = i;
>
> for (int i = 0; i < nbr_etat; i++)
> printf("%0.0f ", tab[i]);
> printf("\n");
> int nbr_elm[2] = { 4, 6 };
> int dpl[2] = { 0, 4 };
>
> MPI_Init(&argc, &argv);
> MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
> MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
>
> recvbuff = (double *)malloc(sizeof(double)*nbr_etat);
> if (taskid == 0)
> {
> sendbuff = (double **)malloc(sizeof(double *)*ntasks);// on execute pour
> deux proc
> sendbuff[0] = (double *)malloc(sizeof(double)*ntasks*nbr_etat);
> for (i = 1; i < ntasks; i++)
> {
> sendbuff[i] = sendbuff[i - 1] + nbr_etat;
> }
> }
> else
> {
> sendbuff = (double **)malloc(sizeof(double *)* 1);
> sendbuff[0] = (double *)malloc(sizeof(double)* 1);
> }
>
> if (taskid == 0){
>
> srand((unsigned)time(NULL) + taskid);
> for (itask = 0; itask<ntasks; itask++)
> {
> int k;
> displs[itask] = itask*nbr_etat;
> int s = dpl[itask];
> sendcounts[itask] = nbr_elm[itask];
>
>
> for (i = 0; i<sendcounts[itask]; i++)
> {
> k = i + s;
> sendbuff[itask][i] = tab[k];
> printf("+ %0.0f  ", sendbuff[itask][i]);
> }
> printf("\n");
> }
> }
>
> recvcount = nbr_elm[taskid];
>
> inittime = MPI_Wtime();
>
> ierr = MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,
> recvbuff, recvcount, MPI_DOUBLE,
> 0, MPI_COMM_WORLD);
>
> totaltime = MPI_Wtime() - inittime;
>
> printf("\n >>>> \n");
> buffsum = 0.0;
> printf("\n > %d < \n", taskid);
> for (i = 0; i<recvcount; i++)
> {
> buffsum = buffsum + recvbuff[i];
> printf("* %0.0f ", recvbuff[i]);
> }
> printf("\n");
> printf("(%d) %0.3f ", taskid, buffsum);
>
> ierr = MPI_Gather(&buffsum, 1, MPI_DOUBLE,
> buffsums, 1, MPI_DOUBLE,
> 0, MPI_COMM_WORLD);
> if (taskid == 0)
> {
> printf("\n");
> printf("##########################################################\n\n");
> printf("                --> APReS COMMUNICATION <-- \n\n");
> for (itask = 0; itask<ntasks; itask++)
> {
> printf("Processus %d : Somme du vecteur reçu : %0.0f\n",
> itask, buffsums[itask]);
> }
> printf("\n");
> printf("##########################################################\n\n");
> printf(" Temps total de communication : %f secondes\n\n", totaltime);
> printf("##########################################################\n\n");
> }
>
>  /* Libération de la mémoire                                      */
> if (taskid == 0)
> {
> free(sendbuff[0]);
> free(sendbuff);
> }
> else
> {
> free(sendbuff[0]);
> free(sendbuff);
> free(recvbuff);
> }
>
>  /* Finalisation de MPI                                           */
> MPI_Finalize();
> }
>
> program B:
> -------------------------
> #include <malloc.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <time.h>
> #include "math.h"
> #include "mpi.h"
>
> #define MAX_LIGNE 20   /* au maximum, 10 lignes */
> #define MAX_COL    20   /* au maximum 8 colonnes */
> #define NOM_A_LIRE "fichier1.txt" /*matrice a lire*/
> typedef int Matrice[MAX_LIGNE][MAX_COL];
> int main(int argc, char** argv)
> {
> int          taskid, ntasks;
> int          ierr, i, itask;
> int         sendcounts[2048], displs[2048], recvcount;
> double       **sendbuff, *recvbuff, buffsum, buffsums[2048];
> double       inittime, totaltime;
> const int nbr_etat = 10;
> double tab[nbr_etat];
>
>
>
> for (int i = 0; i < nbr_etat; i++)
> tab[i] = i;
>
> for (int i = 0; i < nbr_etat; i++)
> printf("%0.0f ", tab[i]);
> printf("\n");
> int nbr_elm[2] = { 4, 6 };
> int dpl[2] = { 0, 4 };
>
> MPI_Init(&argc, &argv);
> MPI_Comm_rank(MPI_COMM_WORLD, &taskid);
> MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
>
> recvbuff = (double *)malloc(sizeof(double)*nbr_etat);
> if (taskid == 0)
> {
> sendbuff = (double **)malloc(sizeof(double *)*ntasks);// on execute pour
> deux proc
> sendbuff[0] = (double *)malloc(sizeof(double)*ntasks*nbr_etat);
> for (i = 1; i < ntasks; i++)
> {
> sendbuff[i] = sendbuff[i - 1] + nbr_etat;
> }
> }
> else
> {
> sendbuff = (double **)malloc(sizeof(double *)* 1);
> sendbuff[0] = (double *)malloc(sizeof(double)* 1);
> }
>
> if (taskid == 0){
>
> srand((unsigned)time(NULL) + taskid);
> for (itask = 0; itask<ntasks; itask++)
> {
> int k;
> displs[itask] = itask*nbr_etat;
> int s = dpl[itask];
> sendcounts[itask] = nbr_elm[itask];
>
>
> for (i = 0; i<sendcounts[itask]; i++)
> {
> k = i + s;
> sendbuff[itask][i] = tab[k];
> printf("+ %0.0f  ", sendbuff[itask][i]);
> }
> printf("\n");
> }
> }
>
> recvcount = nbr_elm[taskid];
>
> inittime = MPI_Wtime();
>
> ierr = MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,
> recvbuff, recvcount, MPI_DOUBLE,
> 0, MPI_COMM_WORLD);
>
> totaltime = MPI_Wtime() - inittime;
>
> printf("\n >>>> \n");
> buffsum = 0.0;
> printf("\n > %d < \n", taskid);
> for (i = 0; i<recvcount; i++)
> {
> buffsum = buffsum + recvbuff[i];
> printf("* %0.0f ", recvbuff[i]);
> }
> printf("\n");
> printf("(%d) %0.3f ", taskid, buffsum);
>
> ierr = MPI_Gather(&buffsum, 1, MPI_DOUBLE,
> buffsums, 1, MPI_DOUBLE,
> 0, MPI_COMM_WORLD);
> if (taskid == 0)
> {
> printf("\n");
> printf("##########################################################\n\n");
> printf("                --> APReS COMMUNICATION <-- \n\n");
> for (itask = 0; itask<ntasks; itask++)
> {
> printf("Processus %d : Somme du vecteur reçu : %0.0f\n",
> itask, buffsums[itask]);
> }
> printf("\n");
> printf("##########################################################\n\n");
> printf(" Temps total de communication : %f secondes\n\n", totaltime);
> printf("##########################################################\n\n");
> }
>
> /*===============================================================*/
> /* Libération de la mémoire                                      */
> if (taskid == 0)
> {
> free(sendbuff[0]);
> free(sendbuff);
> }
> else
> {
> free(sendbuff[0]);
> free(sendbuff);
> free(recvbuff);
> }
>
> /*===============================================================*/
> /* Finalisation de MPI                                           */
> MPI_Finalize();
> Matrice A;
> int  m, n;
> /* ouvrir le fichier Matrices.dta sur le disque réseau pour la lecture */
> FILE * aLire = fopen(NOM_A_LIRE, "r");
> if (aLire == NULL)
> {
> printf("Le fichier n'existe pas");
> }
> else
> {
> fscanf(aLire, "%d%d\n", &m, &n);
> ////void lire(FILE * aLire, Matrice mat, int nbLigne, int nbCol)
> for (int i = 0; i < m; i++) {
> for (int j = 0; j < n; j++)
> fscanf(aLire, "%d", &A[i][j]);
> fscanf(aLire, "\n");
> }
> ////void afficher(Matrice mat, char nom[], int nbLigne, int nbCol)
>
> printf("\nContenu de la matrice de %d ligne(s) et %d colonne(s) :\n", m,
> n);
> for (int i = 0; i < m; i++) {
> for (int j = 0; j < n; j++)
> printf("%d ", A[i][j]);
> printf("\n");
> }
> printf("\n");
>
>
> }
> fclose(aLire);
>
> }
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mpich.org/pipermail/discuss/attachments/20141129/51b551d9/attachment.html>
-------------- next part --------------
_______________________________________________
discuss mailing list     discuss at mpich.org
To manage subscription options or unsubscribe:
https://lists.mpich.org/mailman/listinfo/discuss


More information about the discuss mailing list