syncffprintf.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2009 Werner Scholz
00005 
00006     www:   http://www.magpar.net/
00007     email: magpar(at)magpar.net
00008 
00009     magpar is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     magpar is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with magpar; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 static char const Id[] = "$Id: syncffprintf.c 2681 2009-07-31 04:30:53Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "util.h"
00028 
00029 #ifdef ZLIB
00030 EXTERN_C_BEGIN
00031 #include "zlib.h"
00032 EXTERN_C_END
00033 #endif
00034 
00035 int SynchronizedFastFPrintf(FILE *fd, int nchars, int nmax, char *wbufs, PetscTruth zip)
00036 {
00037   int          i;
00038 
00039   MPI_Status   status;
00040 
00041   MagparFunctionInfoBegin;
00042 
00043   int rank,size;
00044   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00045   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00046 
00047   /* set null character and send it even if nchars==0 */
00048   assert(nmax>0);   /* otherwise the assignment might cause a SEGV */
00049   if (nchars==0) {
00050     wbufs[0]=0;
00051   }
00052   
00053   /* to include the null character at the end */
00054   nchars++;
00055 
00056   if (!rank) {
00057 #ifdef ZLIB
00058     if (zip==PETSC_TRUE) {
00059       ierr = gzwrite(fd,wbufs,nchars-1); /* skip the trailing \000 */
00060 /*
00061       ierr = gzprintf(fd,"%s",wbufs);
00062 */
00063     }
00064     else {
00065 #endif
00066     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",wbufs);CHKERRQ(ierr);
00067 #ifdef ZLIB
00068     }
00069 #endif
00070     for (i=1;i<size;i++) {
00071       ierr = MPI_Recv(&nchars,1,MPI_INT,i,0,PETSC_COMM_WORLD,&status);CHKERRQ(ierr);
00072       /* check that we do not exceed the size of the buffer */
00073       assert(nchars<nmax);
00074       ierr = MPI_Recv(wbufs,nchars,MPI_CHAR,i,0,PETSC_COMM_WORLD,&status);CHKERRQ(ierr);
00075 
00076 #ifdef ZLIB
00077       if (zip==PETSC_TRUE) {
00078         ierr = gzwrite(fd,wbufs,nchars-1); /* skip the trailing \000 */
00079 /*
00080         ierr = gzprintf(fd,"%s",wbufs);
00081 */
00082       }
00083       else {
00084 #endif
00085       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",wbufs);CHKERRQ(ierr);
00086 #ifdef ZLIB
00087       }
00088 #endif
00089     }
00090   }
00091   else {
00092     ierr = MPI_Send(&nchars,1,MPI_INT,0,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
00093     ierr = MPI_Send(wbufs,nchars,MPI_CHAR,0,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
00094   }
00095 
00096   MagparFunctionInfoReturn(0);
00097 }
00098 

magpar - Parallel Finite Element Micromagnetics Package
Copyright (C) 2002-2009 Werner Scholz