1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2010 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
#include <weipa/FileWriter.h> |
15 |
#include <sstream> |
16 |
#include <iostream> |
17 |
|
18 |
|
19 |
using namespace std; |
20 |
|
21 |
namespace weipa { |
22 |
|
23 |
FileWriter::FileWriter() : |
24 |
mpiRank(0), |
25 |
mpiSize(1) |
26 |
{ |
27 |
} |
28 |
|
29 |
#if HAVE_MPI |
30 |
FileWriter::FileWriter(MPI_Comm comm) : |
31 |
mpiComm(comm) |
32 |
{ |
33 |
MPI_Comm_rank(mpiComm, &mpiRank); |
34 |
MPI_Comm_size(mpiComm, &mpiSize); |
35 |
} |
36 |
#endif |
37 |
|
38 |
bool FileWriter::openFile(string filename) |
39 |
{ |
40 |
bool success=false; |
41 |
|
42 |
if (mpiSize>1) { |
43 |
#if HAVE_MPI |
44 |
// remove file first if it exists |
45 |
int error = 0; |
46 |
int mpiErr; |
47 |
if (mpiRank == 0) { |
48 |
ifstream f(filename.c_str()); |
49 |
if (f.is_open()) { |
50 |
f.close(); |
51 |
if (remove(filename.c_str())) { |
52 |
error=1; |
53 |
} |
54 |
} |
55 |
} |
56 |
MPI_Allreduce(&error, &mpiErr, 1, MPI_INT, MPI_MAX, mpiComm); |
57 |
if (mpiErr != 0) { |
58 |
cerr << "Error removing " << filename << "!" << endl; |
59 |
return false; |
60 |
} |
61 |
|
62 |
MPI_Info mpiInfo = MPI_INFO_NULL; |
63 |
int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN; |
64 |
mpiErr = MPI_File_open(mpiComm, const_cast<char*>(filename.c_str()), |
65 |
amode, mpiInfo, &fileHandle); |
66 |
if (mpiErr == MPI_SUCCESS) { |
67 |
mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR, |
68 |
const_cast<char*>("native"), mpiInfo); |
69 |
} |
70 |
if (mpiErr != MPI_SUCCESS) { |
71 |
cerr << "Error opening " << filename << " for parallel writing!" << endl; |
72 |
} else { |
73 |
success=true; |
74 |
} |
75 |
#endif |
76 |
} else { |
77 |
ofs.open(filename.c_str()); |
78 |
success = !ofs.fail(); |
79 |
} |
80 |
return success; |
81 |
} |
82 |
|
83 |
bool FileWriter::writeShared(ostringstream& oss) |
84 |
{ |
85 |
bool success=false; |
86 |
if (mpiSize>1) { |
87 |
#if HAVE_MPI |
88 |
MPI_Status mpiStatus; |
89 |
string contents = oss.str(); |
90 |
int mpiErr = MPI_File_write_shared( |
91 |
fileHandle, const_cast<char*>(contents.c_str()), |
92 |
contents.length(), MPI_CHAR, &mpiStatus); |
93 |
oss.str(""); |
94 |
success=(mpiErr==0); |
95 |
#endif |
96 |
} else { |
97 |
ofs << oss.str(); |
98 |
oss.str(""); |
99 |
success=!ofs.fail(); |
100 |
} |
101 |
return success; |
102 |
} |
103 |
|
104 |
bool FileWriter::writeOrdered(ostringstream& oss) |
105 |
{ |
106 |
bool success=false; |
107 |
if (mpiSize>1) { |
108 |
#if HAVE_MPI |
109 |
MPI_Status mpiStatus; |
110 |
string contents = oss.str(); |
111 |
int mpiErr = MPI_File_write_ordered( |
112 |
fileHandle, const_cast<char*>(contents.c_str()), |
113 |
contents.length(), MPI_CHAR, &mpiStatus); |
114 |
oss.str(""); |
115 |
success=(mpiErr==0); |
116 |
#endif |
117 |
} else { |
118 |
ofs << oss.str(); |
119 |
oss.str(""); |
120 |
success=!ofs.fail(); |
121 |
} |
122 |
return success; |
123 |
} |
124 |
|
125 |
void FileWriter::close() |
126 |
{ |
127 |
if (mpiSize>1) { |
128 |
#if HAVE_MPI |
129 |
MPI_File_close(&fileHandle); |
130 |
#endif |
131 |
} else { |
132 |
ofs.close(); |
133 |
} |
134 |
} |
135 |
|
136 |
} // namespace weipa |
137 |
|