Newsgroups: comp.parallel.mpi
From: eddemain@neumann.uwaterloo.ca (Erik Demaine)
Subject: Re: MPICH: how to redirect stdout from separate executables?
Organization: University of Waterloo
Date: Sun, 26 Nov 1995 01:53:21 GMT
Message-ID: <DIMnwx.HFA@undergrad.math.uwaterloo.ca>

Pasha (cuno@bimacs.biu.ac.il) wrote:
: extern int ntasks, myid;
: void proc_report(char *formatStr, ...)
:   {
:      sprintf(file_name, "task.%d", myid);
:      buf = fopen (file_name, "a+");
:  
:      va_start(argList,formatStr);
:      vfprintf(buf, formatStr, argList);
:      va_end(argList);
:  
:      fclose(buf);    
:   }
: ---------------------------------------
: Now you may use the proc_report(...) function exactly
: as you are using the printf() function.
: ...

Another way to do this is follows.  If you put the following in the beginning
of your program, the printf's will automatically write to the file.  This
makes it easier to switch back and forth between piping/not piping (but an
"if" or an #ifdef around this).

	char filename[256];
	int p, fid;

	MPI_Comm_rank (MPI_COMM_WORLD, &p);  /* get processor number */
	sprintf (filename, "out%d", p);
	close (1);
	fid = open (filename, O_WRONLY | O_CREAT | O_APPEND);
	if (fid != 1)
	{
	   dup2 (fid, 1);
	   close (fid);
	}

If you want to pipe stderr (errors) to the same file, add this:

	close (2);
	dup2 (1, 2);

If you want stderr to go to a seperate file, use the same as above but
replace "out" with "err".

Add | O_TRUNC to the open() call if you want to overwrite any existing
output (otherwise it'll append).

Ciao!
Erik
--
Erik Demaine                 \/  e-mail: eddemain@neumann.uwaterloo.ca
Dept. of Computer Science    /\  URL   : http://barrow.uwaterloo.ca/~eddemain/
University of Waterloo       \/  Most jugglers do poorly while drunk, especially
Waterloo, ON Canada N2L 3G1  /\  in a large wavelength gravity field.

