xref: /phasta/phSolver/common/phIO.cc (revision 1e99f302ca5103688ae35115c2fefb7cfa6714f1)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <cstring>
4 #include <string>
5 #include <sstream>
6 #include "phIO.h"
7 #include "phComm.h"
8 #include "phio_base.h"
9 #include <mpi.h>
10 
11 #ifndef PHASTAIO_TIMERS_ON
12 #define PHASTAIO_TIMERS_ON 0
13 #endif
14 
15 namespace {
16   inline double getTime() {
17     return MPI_Wtime();
18   }
19   inline bool isRankZero() {
20     int rank = 0;
21     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22     return !rank;
23   }
24   inline void printTime(const char* key, double t) {
25 #if PHASTAIO_TIMERS_ON==1
26     if( isRankZero() )
27       fprintf(stderr, "%s %f\n", key, t);
28 #endif
29   }
30 }
31 
32 #define PHIO_TRACING 0
33 namespace {
34   void trace(const char* key, const char* aux="", void* obj=NULL) {
35     if(PHIO_TRACING)
36       fprintf(stderr, "PHIO_TRACE entering %s %s %p\n", key, aux, obj);
37   }
38 }
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 void phio_readheader(
45     phio_fp f,
46     const  char keyphrase[],
47     void* valueArray,
48     int*  nItems,
49     const char  datatype[],
50     const char  iotype[] ) {
51   const double t0 = getTime();
52   f->ops->readheader(f->file, keyphrase, valueArray,
53       nItems, datatype, iotype);
54   printTime(__func__, getTime()-t0);
55 }
56 void phio_writeheader(
57     phio_fp f,
58     const char keyphrase[],
59     const void* valueArray,
60     const int* nItems,
61     const int* ndataItems,
62     const char datatype[],
63     const char iotype[] ) {
64   const double t0 = getTime();
65   f->ops->writeheader(f->file, keyphrase, valueArray,
66       nItems, ndataItems, datatype, iotype);
67   printTime(__func__, getTime()-t0);
68 }
69 void phio_readdatablock(
70     phio_fp f,
71     const  char keyphrase[],
72     void* valueArray,
73     int*  nItems,
74     const char  datatype[],
75     const char  iotype[] ) {
76   const double t0 = getTime();
77   f->ops->readdatablock(f->file, keyphrase, valueArray,
78       nItems, datatype, iotype);
79   printTime(__func__, getTime()-t0);
80 }
81 void phio_writedatablock(
82     phio_fp f,
83     const char keyphrase[],
84     const void* valueArray,
85     const int* nItems,
86     const char datatype[],
87     const char iotype[]) {
88   const double t0 = getTime();
89   f->ops->writedatablock(f->file, keyphrase, valueArray,
90       nItems, datatype, iotype);
91   printTime(__func__, getTime()-t0);
92 }
93 
94 void phio_constructName(
95     phio_fp f,
96     const char inName[],
97     char* outName) {
98   const double t0 = getTime();
99   f->ops->constructname(inName, outName);
100   printTime(__func__, getTime()-t0);
101 }
102 
103 void phio_openfile(
104     const char filename[],
105     phio_fp f) {
106   const double t0 = getTime();
107   trace("openfile",filename,f);
108   f->ops->openfile(filename, f);
109   printTime(__func__, getTime()-t0);
110 }
111 
112 void phio_closefile(phio_fp f) {
113   const double t0 = getTime();
114   trace("closefile","unknown",f);
115   f->ops->closefile(f);
116   printTime(__func__, getTime()-t0);
117 }
118 
119 void phio_appendInt(char* dest, int v) {
120   std::stringstream ss;
121   ss << dest << v << '.';
122   std::string s = ss.str();
123   strcpy(dest, s.c_str());
124 }
125 
126 #ifdef __cplusplus
127 }
128 #endif
129