xref: /phasta/phSolver/common/phIO.cc (revision 624b53bd233868d46c15955a23c7c355badc298b)
1064bab1dSCameron Smith #include <stdio.h>
2064bab1dSCameron Smith #include <stdlib.h>
336adee64SCameron Smith #include <cstring>
4064bab1dSCameron Smith #include <string>
536adee64SCameron Smith #include <sstream>
6d1293ce9SCameron Smith #include "phIO.h"
7d1293ce9SCameron Smith #include "phComm.h"
8064bab1dSCameron Smith #include "phio_base.h"
9*624b53bdSCameron Smith #include <mpi.h>
10322ea7e2SCameron Smith 
11322ea7e2SCameron Smith #ifndef PHASTAIO_TIMERS_ON
12322ea7e2SCameron Smith #define PHASTAIO_TIMERS_ON 0
13322ea7e2SCameron Smith #endif
14322ea7e2SCameron Smith 
15322ea7e2SCameron Smith namespace {
getTime()16322ea7e2SCameron Smith   inline double getTime() {
17322ea7e2SCameron Smith     return MPI_Wtime();
18322ea7e2SCameron Smith   }
isRankZero()19*624b53bdSCameron Smith   inline bool isRankZero() {
20*624b53bdSCameron Smith     int rank = 0;
21*624b53bdSCameron Smith     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22*624b53bdSCameron Smith     return !rank;
23322ea7e2SCameron Smith   }
printTime(const char * key,double t)24*624b53bdSCameron Smith   inline void printTime(const char* key, double t) {
25*624b53bdSCameron Smith #if PHASTAIO_TIMERS_ON==1
26*624b53bdSCameron Smith     if( isRankZero() )
27*624b53bdSCameron Smith       fprintf(stderr, "%s %f\n", key, t);
28*624b53bdSCameron Smith #endif
29322ea7e2SCameron Smith   }
30322ea7e2SCameron Smith }
31f262839cSCameron Smith 
324c223e05SCameron Smith #define PHIO_TRACING 0
334c223e05SCameron Smith namespace {
trace(const char * key,const char * aux="",void * obj=NULL)344c223e05SCameron Smith   void trace(const char* key, const char* aux="", void* obj=NULL) {
354c223e05SCameron Smith     if(PHIO_TRACING)
364c223e05SCameron Smith       fprintf(stderr, "PHIO_TRACE entering %s %s %p\n", key, aux, obj);
374c223e05SCameron Smith   }
384c223e05SCameron Smith }
394c223e05SCameron Smith 
40d7abaf6cSCameron Smith #ifdef __cplusplus
41d7abaf6cSCameron Smith extern "C" {
42d7abaf6cSCameron Smith #endif
43d7abaf6cSCameron Smith 
phio_readheader(phio_fp f,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])4482f286aaSCameron Smith void phio_readheader(
45064bab1dSCameron Smith     phio_fp f,
46d1293ce9SCameron Smith     const  char keyphrase[],
47d1293ce9SCameron Smith     void* valueArray,
48d1293ce9SCameron Smith     int*  nItems,
49d1293ce9SCameron Smith     const char  datatype[],
50d1293ce9SCameron Smith     const char  iotype[] ) {
516e8aac1eSCameron Smith   const double t0 = getTime();
52064bab1dSCameron Smith   f->ops->readheader(f->file, keyphrase, valueArray,
53064bab1dSCameron Smith       nItems, datatype, iotype);
54*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
55d1293ce9SCameron Smith }
phio_writeheader(phio_fp f,const char keyphrase[],const void * valueArray,const int * nItems,const int * ndataItems,const char datatype[],const char iotype[])56fa18c524SCameron Smith void phio_writeheader(
5757517afcSCameron Smith     phio_fp f,
58fa18c524SCameron Smith     const char keyphrase[],
59fa18c524SCameron Smith     const void* valueArray,
60fa18c524SCameron Smith     const int* nItems,
61fa18c524SCameron Smith     const int* ndataItems,
62fa18c524SCameron Smith     const char datatype[],
63fa18c524SCameron Smith     const char iotype[] ) {
646e8aac1eSCameron Smith   const double t0 = getTime();
659ec3dd51SCameron Smith   f->ops->writeheader(f->file, keyphrase, valueArray,
669ec3dd51SCameron Smith       nItems, ndataItems, datatype, iotype);
67*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
68fa18c524SCameron Smith }
phio_readdatablock(phio_fp f,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])69f262839cSCameron Smith void phio_readdatablock(
70064bab1dSCameron Smith     phio_fp f,
71f262839cSCameron Smith     const  char keyphrase[],
72f262839cSCameron Smith     void* valueArray,
73f262839cSCameron Smith     int*  nItems,
74f262839cSCameron Smith     const char  datatype[],
75f262839cSCameron Smith     const char  iotype[] ) {
76322ea7e2SCameron Smith   const double t0 = getTime();
77064bab1dSCameron Smith   f->ops->readdatablock(f->file, keyphrase, valueArray,
78064bab1dSCameron Smith       nItems, datatype, iotype);
79*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
80f262839cSCameron Smith }
phio_writedatablock(phio_fp f,const char keyphrase[],const void * valueArray,const int * nItems,const char datatype[],const char iotype[])8166a3fa2cSCameron Smith void phio_writedatablock(
829ec3dd51SCameron Smith     phio_fp f,
8366a3fa2cSCameron Smith     const char keyphrase[],
8466a3fa2cSCameron Smith     const void* valueArray,
8566a3fa2cSCameron Smith     const int* nItems,
8666a3fa2cSCameron Smith     const char datatype[],
8766a3fa2cSCameron Smith     const char iotype[]) {
88322ea7e2SCameron Smith   const double t0 = getTime();
899ec3dd51SCameron Smith   f->ops->writedatablock(f->file, keyphrase, valueArray,
909ec3dd51SCameron Smith       nItems, datatype, iotype);
91*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
9266a3fa2cSCameron Smith }
93ab645d52SCameron Smith 
phio_constructName(phio_fp f,const char inName[],char * outName)94ab645d52SCameron Smith void phio_constructName(
95a93de25bSCameron Smith     phio_fp f,
96ab645d52SCameron Smith     const char inName[],
97ab645d52SCameron Smith     char* outName) {
98*624b53bdSCameron Smith   const double t0 = getTime();
99a93de25bSCameron Smith   f->ops->constructname(inName, outName);
100*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
101ab645d52SCameron Smith }
102ab645d52SCameron Smith 
phio_openfile(const char filename[],phio_fp f)103ab645d52SCameron Smith void phio_openfile(
10482f286aaSCameron Smith     const char filename[],
105ab645d52SCameron Smith     phio_fp f) {
106322ea7e2SCameron Smith   const double t0 = getTime();
1074c223e05SCameron Smith   trace("openfile",filename,f);
108ab645d52SCameron Smith   f->ops->openfile(filename, f);
109*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
11092bfab9aSCameron Smith }
111ab645d52SCameron Smith 
phio_closefile(phio_fp f)112ab645d52SCameron Smith void phio_closefile(phio_fp f) {
113322ea7e2SCameron Smith   const double t0 = getTime();
1144c223e05SCameron Smith   trace("closefile","unknown",f);
115ab645d52SCameron Smith   f->ops->closefile(f);
116*624b53bdSCameron Smith   printTime(__func__, getTime()-t0);
117064bab1dSCameron Smith }
118ab645d52SCameron Smith 
phio_appendInt(char * dest,int v)119e81a6dc1SCameron Smith void phio_appendInt(char* dest, int v) {
12036adee64SCameron Smith   std::stringstream ss;
12136adee64SCameron Smith   ss << dest << v << '.';
12236adee64SCameron Smith   std::string s = ss.str();
12336adee64SCameron Smith   strcpy(dest, s.c_str());
12436adee64SCameron Smith }
125d7abaf6cSCameron Smith 
126d7abaf6cSCameron Smith #ifdef __cplusplus
127d7abaf6cSCameron Smith }
128d7abaf6cSCameron Smith #endif
129