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*322ea7e2SCameron Smith #include <mpi.h> 10*322ea7e2SCameron Smith 11*322ea7e2SCameron Smith #ifndef PHASTAIO_TIMERS_ON 12*322ea7e2SCameron Smith #define PHASTAIO_TIMERS_ON 0 13*322ea7e2SCameron Smith #endif 14*322ea7e2SCameron Smith 15*322ea7e2SCameron Smith namespace { 16*322ea7e2SCameron Smith inline double getTime() { 17*322ea7e2SCameron Smith return MPI_Wtime(); 18*322ea7e2SCameron Smith } 19*322ea7e2SCameron Smith inline bool isRankZero() { 20*322ea7e2SCameron Smith int rank = 0; 21*322ea7e2SCameron Smith MPI_Comm_rank(MPI_COMM_WORLD, &rank); 22*322ea7e2SCameron Smith return !rank; 23*322ea7e2SCameron Smith } 24*322ea7e2SCameron Smith inline printTime(const char* key, double t) { 25*322ea7e2SCameron Smith #if PHASTAIO_TIMERS_ON==1 26*322ea7e2SCameron Smith if( isRankZero() ) 27*322ea7e2SCameron Smith fprintf(stderr, "%s %f\n", key, t); 28*322ea7e2SCameron Smith #endif 29*322ea7e2SCameron Smith } 30*322ea7e2SCameron Smith } 31f262839cSCameron Smith 324c223e05SCameron Smith #define PHIO_TRACING 0 334c223e05SCameron Smith namespace { 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 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[] ) { 51*322ea7e2SCameron Smith const double t0 = getTime(); 52064bab1dSCameron Smith f->ops->readheader(f->file, keyphrase, valueArray, 53064bab1dSCameron Smith nItems, datatype, iotype); 54*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 55d1293ce9SCameron Smith } 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[] ) { 64*322ea7e2SCameron Smith const double t0 = getTime(); 659ec3dd51SCameron Smith f->ops->writeheader(f->file, keyphrase, valueArray, 669ec3dd51SCameron Smith nItems, ndataItems, datatype, iotype); 67*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 68fa18c524SCameron Smith } 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[] ) { 76*322ea7e2SCameron Smith const double t0 = getTime(); 77064bab1dSCameron Smith f->ops->readdatablock(f->file, keyphrase, valueArray, 78064bab1dSCameron Smith nItems, datatype, iotype); 79*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 80f262839cSCameron Smith } 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[]) { 88*322ea7e2SCameron Smith const double t0 = getTime(); 899ec3dd51SCameron Smith f->ops->writedatablock(f->file, keyphrase, valueArray, 909ec3dd51SCameron Smith nItems, datatype, iotype); 91*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 9266a3fa2cSCameron Smith } 93ab645d52SCameron Smith 94ab645d52SCameron Smith void phio_constructName( 95a93de25bSCameron Smith phio_fp f, 96ab645d52SCameron Smith const char inName[], 97ab645d52SCameron Smith char* outName) { 98*322ea7e2SCameron Smith const double t0 = getTime(); 99a93de25bSCameron Smith f->ops->constructname(inName, outName); 100*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 101ab645d52SCameron Smith } 102ab645d52SCameron Smith 103ab645d52SCameron Smith void phio_openfile( 10482f286aaSCameron Smith const char filename[], 105ab645d52SCameron Smith phio_fp f) { 106*322ea7e2SCameron Smith const double t0 = getTime(); 1074c223e05SCameron Smith trace("openfile",filename,f); 108ab645d52SCameron Smith f->ops->openfile(filename, f); 109*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 11092bfab9aSCameron Smith } 111ab645d52SCameron Smith 112ab645d52SCameron Smith void phio_closefile(phio_fp f) { 113*322ea7e2SCameron Smith const double t0 = getTime(); 1144c223e05SCameron Smith trace("closefile","unknown",f); 115ab645d52SCameron Smith f->ops->closefile(f); 116*322ea7e2SCameron Smith printTime(__func__, getTime()-t0); 117064bab1dSCameron Smith } 118ab645d52SCameron Smith 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