xref: /phasta/phSolver/common/phIO.cc (revision bdcb75b4969e4723a2ecee2aab5064e1a5473545)
1 #include <mpi.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <cassert>
5 #include <cstring>
6 #include <string>
7 #include <sstream>
8 #include "phIO.h"
9 #include "phComm.h"
10 #include "phio_base.h"
11 #include "ph_mpi_help.h"
12 
13 #ifndef PHASTAIO_TIMERS_ON
14 #define PHASTAIO_TIMERS_ON 0
15 #endif
16 struct phastaio_stats {
17   double readTime;
18   double writeTime;
19   double openTime;
20   double closeTime;
21   size_t readBytes;
22   size_t writeBytes;
23 };
24 phastaio_stats phio_global_stats;
25 
26 namespace {
27   inline double getTime() {
28     return MPI_Wtime();
29   }
30   inline size_t getSize(const char* t) {
31     std::string type(t);
32     if(type == "integer")
33       return sizeof(int);
34     else if(type == "double")
35       return sizeof(double);
36     else {
37       assert(0);
38       exit(EXIT_FAILURE);
39     }
40   }
41 }
42 
43 #define PHIO_TRACING 0
44 namespace {
45   void trace(const char* key, const char* aux="", void* obj=NULL) {
46     if(PHIO_TRACING)
47       fprintf(stderr, "PHIO_TRACE entering %s %s %p\n", key, aux, obj);
48   }
49   void printMinMaxAvg(const char* key, size_t v) {
50     int val = static_cast<int>(v);
51     int min = ph_min_int(val);
52     int max = ph_max_int(val);
53     long tot = ph_add_long(static_cast<long>(val));
54     double avg = tot/static_cast<double>(ph_peers());
55     if(!ph_self())
56       fprintf(stderr, "phio_%s min max avg %d %d %f\n",
57           key, min, max, avg);
58   }
59 
60   void printMinMaxAvg(const char* key, double v) {
61     double min = ph_min_double(v);
62     double max = ph_max_double(v);
63     double tot = ph_add_double(v);
64     double avg = tot/ph_peers();
65     if(!ph_self())
66       fprintf(stderr, "phio_%s min max avg %f %f %f\n",
67           key, min, max, avg);
68   }
69 }
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 void phio_printStats() {
76   printMinMaxAvg("readTime",phio_getReadTime());
77   printMinMaxAvg("writeTime", phio_getWriteTime());
78   printMinMaxAvg("openTime", phio_getOpenTime());
79   printMinMaxAvg("closeTime", phio_getCloseTime());
80   printMinMaxAvg("readBytes", phio_getReadBytes());
81   printMinMaxAvg("writeBytes", phio_getWriteBytes());
82 }
83 
84 void phio_initStats() {
85   phio_global_stats.readTime = 0;
86   phio_global_stats.writeTime = 0;
87   phio_global_stats.openTime = 0;
88   phio_global_stats.closeTime = 0;
89   phio_global_stats.readBytes = 0;
90   phio_global_stats.writeBytes = 0;
91 }
92 
93 double phio_getReadTime() {
94   return phio_global_stats.readTime;
95 }
96 
97 double phio_getWriteTime() {
98   return phio_global_stats.writeTime;
99 }
100 
101 double phio_getOpenTime() {
102   return phio_global_stats.openTime;
103 }
104 
105 double phio_getCloseTime() {
106   return phio_global_stats.closeTime;
107 }
108 
109 size_t phio_getReadBytes() {
110   return phio_global_stats.readBytes;
111 }
112 
113 size_t phio_getWriteBytes() {
114   return phio_global_stats.writeBytes;
115 }
116 
117 void phio_readheader(
118     phio_fp f,
119     const  char keyphrase[],
120     void* valueArray,
121     int*  nItems,
122     const char  datatype[],
123     const char  iotype[] ) {
124   const double t0 = getTime();
125   f->ops->readheader(f->file, keyphrase, valueArray,
126       nItems, datatype, iotype);
127   phio_global_stats.readBytes += (*nItems)*getSize(datatype);
128   phio_global_stats.readTime += getTime()-t0;
129 }
130 void phio_writeheader(
131     phio_fp f,
132     const char keyphrase[],
133     const void* valueArray,
134     const int* nItems,
135     const int* ndataItems,
136     const char datatype[],
137     const char iotype[] ) {
138   const double t0 = getTime();
139   f->ops->writeheader(f->file, keyphrase, valueArray,
140       nItems, ndataItems, datatype, iotype);
141   phio_global_stats.writeBytes += (*nItems)*getSize(datatype);
142   phio_global_stats.writeTime += getTime()-t0;
143 }
144 void phio_readdatablock(
145     phio_fp f,
146     const  char keyphrase[],
147     void* valueArray,
148     int*  nItems,
149     const char  datatype[],
150     const char  iotype[] ) {
151   const double t0 = getTime();
152   f->ops->readdatablock(f->file, keyphrase, valueArray,
153       nItems, datatype, iotype);
154   phio_global_stats.readBytes += (*nItems)*getSize(datatype);
155   phio_global_stats.readTime += getTime()-t0;
156 }
157 void phio_writedatablock(
158     phio_fp f,
159     const char keyphrase[],
160     const void* valueArray,
161     const int* nItems,
162     const char datatype[],
163     const char iotype[]) {
164   const double t0 = getTime();
165   f->ops->writedatablock(f->file, keyphrase, valueArray,
166       nItems, datatype, iotype);
167   phio_global_stats.writeBytes += (*nItems)*getSize(datatype);
168   phio_global_stats.writeTime += getTime()-t0;
169 }
170 
171 void phio_constructName(
172     phio_fp f,
173     const char inName[],
174     char* outName) {
175   f->ops->constructname(inName, outName);
176 }
177 
178 void phio_openfile(
179     const char filename[],
180     phio_fp f) {
181   const double t0 = getTime();
182   trace("openfile",filename,f);
183   f->ops->openfile(filename, f);
184   phio_global_stats.openTime += getTime()-t0;
185 }
186 
187 void phio_closefile(phio_fp f) {
188   const double t0 = getTime();
189   trace("closefile","unknown",f);
190   f->ops->closefile(f);
191   phio_global_stats.closeTime += getTime()-t0;
192 }
193 
194 void phio_appendInt(char* dest, int v) {
195   std::stringstream ss;
196   ss << dest << v << '.';
197   std::string s = ss.str();
198   strcpy(dest, s.c_str());
199 }
200 
201 #ifdef __cplusplus
202 }
203 #endif
204