xref: /phasta/phSolver/common/phIO.cc (revision 5f3fbd8dc1cca8c2fa3024abefb508c65f61af5f)
1 #include "phIO.h"
2 #include "phComm.h"
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <phastaIO.h>
7 #include <sstream>
8 #include <string>
9 
10 namespace {
11   std::string appendSync(const char* phrase) {
12     std::stringstream ss;
13     ss << phrase << "@" << phcomm_rank()+1 << "?";
14     std::string s = ss.str();
15     return s;
16   }
17   std::string appendColor(const char* phrase, int numFiles) {
18     const int color = computeColor(phcomm_rank(), phcomm_size(), numFiles);
19     std::stringstream ss;
20     ss << phrase << color+1;
21     std::string s = ss.str();
22     return s;
23   }
24 }
25 
26 void phio_readheader(
27     int* fileDescriptor,
28     const  char keyphrase[],
29     void* valueArray,
30     int*  nItems,
31     const char  datatype[],
32     const char  iotype[] ) {
33   std::string syncPhrase = appendSync(keyphrase);
34   readheader(fileDescriptor, syncPhrase.c_str(),
35       valueArray, nItems, datatype, iotype);
36 }
37 
38 void phio_readdatablock(
39     int*  fileDescriptor,
40     const char keyphrase[],
41     void* valueArray,
42     int*  nItems,
43     const char  datatype[],
44     const char  iotype[] ) {
45   std::string syncPhrase = appendSync(keyphrase);
46   readdatablock(fileDescriptor, syncPhrase.c_str(),
47       valueArray, nItems, datatype, iotype);
48 }
49 
50 void phio_openfile_read(
51     const char filename[],
52     int* numFiles,
53     int* fileDescriptor) {
54   std::string syncName = appendColor(filename, *numFiles);
55   int nfields=0;
56   int nppf=0;
57   queryphmpiio(syncName.c_str(), &nfields, &nppf);
58   const char* mode = "read";
59   initphmpiio(&nfields, &nppf, numFiles, fileDescriptor, mode);
60   openfile(syncName.c_str(), mode, fileDescriptor);
61 }
62 
63 void phio_openfile_write(
64     const char filename[],
65     int* numFiles,
66     int* numFields,
67     int* numPPF,
68     int* fileDescriptor) {
69   std::string syncName = appendColor(filename, *numFiles);
70   //TODO - define a good upper bound
71   assert(*numFields > 0 && *numFields < 1024);
72   assert(*numPPF > 0 && *numPPF < 1024);
73   const char* mode = "write";
74   initphmpiio(numFields, numPPF, numFiles, fileDescriptor, mode);
75   openfile(syncName.c_str(), mode, fileDescriptor);
76 }
77 
78 void phio_restartname(int* step, char* filename) {
79   std::stringstream ss;
80   ss << "restart-dat." << *step << '.';
81   std::string s = ss.str();
82   strcpy(filename, s.c_str());
83 }
84