psmile_redirstdout.c

Go to the documentation of this file.
00001 /* --------------------------------------------------------------------- */
00002 /* Copyright 2006-2010, NEC Europe Ltd., London, UK. */
00003 /* All rights reserved. Use is subject to OASIS4 license terms. */
00004 /* --------------------------------------------------------------------- */
00005 
00006 #include <math.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 
00011 #include "PSMILe_f2c.h"
00012 
00013 /*
00014 
00015 //BOP
00016  
00017 ! !ROUTINE: psmile_redirstdout
00018  
00019   !INTERFACE:
00020 
00021 */
00022 
00023 void psmile_redirstdout (char *filestem, INTEGER *lenstr,
00024                          INTEGER *parallel, INTEGER *my_pe,
00025                          INTEGER *npes, INTEGER *ierror )
00026 /*
00027 
00028   !INPUT PARAMETERS:
00029 
00030    filestem = Basename of stdout/stderr files
00031    lenstr   = Significant length (without \0) of "filestem"
00032    
00033    parallel = Flag for direction of output
00034               parallel = 1 : Create single output files for each process
00035               Otherwise    : Create output files only for process 0.
00036                              The output of other processes is not redirected.
00037 
00038    my_pe    = Rank
00039 
00040    npes     = number of processes
00041 
00042    !OUTPUT PARAMETERS:
00043 
00044    ierror   = Return code
00045               = 0 : No error
00046               = 1 : Error in malloc
00047 
00048   !DESCRIPTION:
00049 
00050    Redirect standard output
00051 
00052   !FILES USED:
00053 
00054          <math.h>
00055          <stdio.h>
00056          <stddef.h>
00057          "PSMILe_f2c.h"
00058 
00059   !REVISION HISTORY:
00060 
00061     Date      Programmer   Description
00062   ----------  ----------   -----------
00063   01.12.03    R. Redler    created
00064   01.12.05    H. Ritzdorf  revised
00065 
00066 //EOP
00067 
00068  ----------------------------------------------------------------------
00069   $Id
00070   $Author
00071  ---------------------------------------------------------------------- */
00072 
00073 {
00074    size_t len_alloc = *lenstr;
00075    register int n_digits, num;
00076    register int redirect = 1;
00077 
00078    char *sname, *ename;
00079    register char *p_err, *p_std;
00080 
00081    ASSERT (*lenstr > 0)
00082    ASSERT (*my_pe  >= 0)
00083 
00084    *ierror = 0;
00085 
00086    /* Get number of digits in rank */
00087 
00088    num = (*npes > 0) ? *npes : 1;
00089    n_digits = (int) (log10((double)(num) + (double) 0.5)) + 1;
00090    ASSERT (n_digits > 0)
00091 
00092    /* allocate file names */
00093 
00094 #define NUMBER_OF_EXTENSIONS 2
00095 #define LENGTH_OF_ERR 3
00096 
00097    len_alloc += n_digits + NUMBER_OF_EXTENSIONS + LENGTH_OF_ERR + 1;
00098 
00099    sname = (char *) MALLOC (len_alloc);
00100    ename = (char *) MALLOC (len_alloc);
00101 
00102    if (!sname || !ename) {
00103       fprintf (stderr, "PSMILe_redirstdout: Cannot allocate memory. %d bytes\n", len_alloc);
00104       *ierror = 1;
00105       return;
00106    }
00107 
00108    /* Copy base name to standard output and standard error file name */
00109 
00110    memcpy (sname, filestem, (size_t) *lenstr);
00111    memcpy (ename, filestem, (size_t) *lenstr);
00112 
00113    p_std = sname + (size_t) *lenstr;
00114    p_err = ename + (size_t) *lenstr;
00115 
00116    if(* parallel == 1) {
00117      /* all processes write into their own file */
00118 
00119      sprintf (p_std, ".%0*d",     n_digits, *my_pe);
00120      sprintf (p_err, ".err.%0*d", n_digits, *my_pe);
00121    }
00122    else {
00123      /* only local root redirectes stdout */
00124      redirect = (*my_pe == 0);
00125 
00126      if( redirect ) {
00127        sprintf (p_std, ".log");
00128        sprintf (p_err, ".err");
00129      }
00130    }
00131 
00132    /* Redirect STDOUT/STDERR */
00133 
00134    if (redirect) {
00135       freopen (sname, "w", stdout);
00136       freopen (ename, "w", stderr);
00137    }
00138 
00139    /* Free file names */
00140 
00141    FREE (ename);
00142    FREE (sname);
00143 }

Generated on 18 Mar 2011 for Oasis4 by  doxygen 1.6.1