#include /* These are C io functions with error messages which can be called by Fortran programs. */ #define MAX_FILE 100 static FILE *fp[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ; void copen_(fd,fname,mode,length,name_len) int *fd ; /* (output) file descriptor (unit number). */ char *fname ; /* (input) filename */ int *mode ; /* (input) type of access 0: open for reading; 1: open for writing; 2 open for both */ int *length ; /* (output) file length in 4-byte words. = -1 for error. */ int *name_len ; /* length of character string fname. */ { int l ; char name[256] ; l = *name_len ; while(fname[--l]==' ') ; /* remove trailing blanks */ if(l>255){ fprintf(stderr,"copen error: file path name too long.\n") ; exit() ; } name[l+1] = '\0' ; for(; l>=0; l--) name[l] = fname[l] ; for(*fd=1; fp[*fd-1]!=0 && *fd<=MAX_FILE; (*fd)++) ; if(*fd > MAX_FILE){ fprintf(stderr,"copen: cannot have open more than %d files\n",MAX_FILE) ; exit() ; } if(*mode == 1){ /* open file for writing only */ if((fp[*fd-1]=fopen(name,"w"))==NULL) *length = -1 ; else *length = 0 ; } else if(*mode == 0){ if((fp[*fd-1]=fopen(name,"r"))==NULL) *length = -1 ; else *length = sizef(name)/4 ; } else if(*mode == 2){ if((fp[*fd-1]=fopen(name,"r+"))==NULL) *length = -1 ; else { *length = sizef(name)/4 ; fseek(fp[*fd-1],0,0) ; } } } void cread_(fd,x,n,nw) /* read n 4-byte words */ int *fd ; /* (input) file descriptor from copen */ float *x ; /* (output) array of words read (integer or real) */ int *n ; /* (input) number of 4-byte words to read */ int *nw ; /* (output) number of 4-byte words actually read */ { if((*nw = fread(x,4,*n,fp[*fd-1])) < 0){ fprintf(stderr,"\ncread: read error.\n") ; exit() ; } } void creade_(fd,x,n) /* exit on any error from cread_ */ int *fd ; float *x ; int *n ; { int nw ; cread_(fd,x,n,&nw) ; if(nw != *n){ fprintf(stderr,"creade: read error.") ; exit(-1) ; } } void cwrite_(fd,x,n) /* write n 4-byte words */ int *fd ; /* (input) file descriptor from copen */ float *x ; /* (input) array of words to write (integer or real) */ int *n ; /* number of 4-byte words to write */ { if(fwrite(x,4,*n,fp[*fd-1]) != *n){ fprintf(stderr,"\ncwrite: write error.\n") ; exit() ; } } void cclose_(fd) /* close a file */ int *fd ; /* (input) file descriptor from copen */ { if(fclose(fp[*fd-1]) == -1) fprintf(stderr,"\ncclose: invalid file descriptor") ; fp[*fd-1] = 0 ; } void cseek_(fd,offset,whence) int *fd ; /* file descriptor from copen */ int *offset ; /* # of 4-byte words to offset from */ int *whence ; /* =0: the beginning; =1: the current location; =2: the end */ { if(fseek(fp[*fd-1],(long)(4* *offset),*whence) == -1){ fprintf(stderr,"\ncseek error on unit %d\n",*fd) ; exit() ; } } canseek_(fd) int *fd ; { return(1) ; /* can always seek on unix */ } void cflush_(fd) int *fd ; { fflush(fp[*fd-1]) ; } void cstop_() { int i ; for(i=0; i #include #include /* returns the file size in bytes for file fname. */ sizef(fname) char *fname ; /* pointer to filename */ { struct stat buf ; if(stat(fname,&buf)==-1){ fprintf(stderr,"\nsizef error: invalid filename %s\n",fname) ; exit() ; } return(buf.st_size) ; } void reverse(s) /* reverse string s in place */ char s[] ; { int c,i,j ; for(i=0,j=strlen(s)-1; i 0) ; /* delete it */ if(sign < 0) s[i++] = '-' ; s[i] = '\0'; reverse(s) ; *ls = i ; } /* append the process id number to the end of the input string. there must be at least 5 bytes beyond the '\0' in the string. */ char *uniqnam(name) char *name ; { int getpid(),pid,nc ; static char cpid[6] ; if(*cpid=='\0'){ /* if this is the first call */ pid = getpid() ; itoa_(&pid,cpid,&nc) ; cpid[nc] = '\0' ; } strncat(name,cpid,5) ; return(cpid) ; } #include #include #include #include #include #include typedef struct TABLOG { long cpu; int step, day; struct TABLOG *next; }TABLOG; #define NLOG 15 #if defined (SPARC) #define IN_SEC 60 #elif defined (CRAY) || defined(CONVEX) #define IN_SEC CLK_TCK #else #define IN_SEC HZ #endif #define TO_SEC(t) ((float)(t)/(float)IN_SEC) static time_t start_cpu, prev_cpu, start_wll; static struct tms s_tms; int ipast_scpu_() { times (&s_tms); return (int)TO_SEC(s_tms.tms_utime + s_tms.tms_stime - start_cpu); } int ipast_swll_() { time_t tloc; if ( ! start_wll ) time(&start_wll); return (int)(time(&tloc) - start_wll); } void cpulog_(fname, nstep, nday) char *fname; int *nstep, *nday; /********************************************************* * Senya Basin, 1992 ********************************************************/ { register int i; static long loc1, loc2, cpu, date; static int items = 0, first = 1; static FILE *flog; static TABLOG *start, *curr; times (&s_tms); if (time(&date) == -1) {printf ("Error in cpulog\n"); return;} cpu = s_tms.tms_utime + s_tms.tms_stime; if (first) { flog = fopen (strtok(fname, " \t\n"), "w"); start = curr = calloc(1L, sizeof(struct TABLOG)); for (i = 1; i < NLOG; i++) curr = (curr->next = calloc(1L, sizeof(struct TABLOG))); curr = (curr->next = start); fprintf (flog, " MODEL started at %s", ctime (&date)); loc1 = ftell (flog); fprintf (flog, " step # %-5d at %s\n", *nstep, ctime(&date)); fprintf (flog, " model STEP/DAY SEC per dDAY TOTAL CPU\n"); loc2 = ftell (flog); first = 0; start_cpu = prev_cpu = cpu; } else { if (items < NLOG) { prev_cpu = start_cpu; items++; } else { prev_cpu = start->cpu; start = start->next; } curr->cpu = cpu; curr->step = *nstep; curr->day = *nday; fseek (flog, loc1, SEEK_SET); fprintf (flog, " step # %-5d at %s\n", *nstep, ctime(&date)); fseek (flog, loc2, SEEK_SET); curr = start; for (i = 0; i < items; i++) { cpu = (int)TO_SEC(curr->cpu); fprintf (flog, " %-6d/ %-5d %-10.3g %2d hour %2d min %2d sec\n", curr->step, curr->day, TO_SEC(curr->cpu - prev_cpu), cpu/3600, (cpu%3600)/60, cpu%60); prev_cpu = curr->cpu; curr = curr->next; } } fflush (flog); }