/[escript]/trunk/weipa/src/FileWriter.cpp
ViewVC logotype

Contents of /trunk/weipa/src/FileWriter.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3107 - (show annotations)
Thu Aug 26 03:41:28 2010 UTC (9 years, 1 month ago) by caltinay
File size: 3256 byte(s)
Removed ugly #defines in favour of a class.

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
17 using namespace std;
18
19 namespace weipa {
20
21 FileWriter::FileWriter() :
22 mpiRank(0),
23 mpiSize(1)
24 {
25 }
26
27 #if HAVE_MPI
28 FileWriter::FileWriter(MPI_Comm comm) :
29 mpiComm(comm)
30 {
31 MPI_Comm_rank(mpiComm, &mpiRank);
32 MPI_Comm_size(mpiComm, &mpiSize);
33 }
34 #endif
35
36 bool FileWriter::openFile(string filename)
37 {
38 bool success=false;
39
40 if (mpiSize>1) {
41 #if HAVE_MPI
42 // remove file first if it exists
43 int error = 0;
44 int mpiErr;
45 if (mpiRank == 0) {
46 ifstream f(filename.c_str());
47 if (f.is_open()) {
48 f.close();
49 if (remove(filename.c_str())) {
50 error=1;
51 }
52 }
53 }
54 MPI_Allreduce(&error, &mpiErr, 1, MPI_INT, MPI_MAX, mpiComm);
55 if (mpiErr != 0) {
56 cerr << "Error removing " << filename << "!" << endl;
57 return false;
58 }
59
60 MPI_Info mpiInfo = MPI_INFO_NULL;
61 int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
62 mpiErr = MPI_File_open(mpiComm, const_cast<char*>(filename.c_str()),
63 amode, mpiInfo, &fileHandle);
64 if (mpiErr == MPI_SUCCESS) {
65 mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR,
66 const_cast<char*>("native"), mpiInfo);
67 }
68 if (mpiErr != MPI_SUCCESS) {
69 cerr << "Error opening " << filename << " for parallel writing!" << endl;
70 } else {
71 success=true;
72 }
73 #endif
74 } else {
75 ofs.open(filename.c_str());
76 success = !ofs.fail();
77 }
78 return success;
79 }
80
81 bool FileWriter::writeShared(ostringstream& oss)
82 {
83 bool success=false;
84 if (mpiSize>1) {
85 #if HAVE_MPI
86 MPI_Status mpiStatus;
87 string contents = oss.str();
88 int mpiErr = MPI_File_write_shared(
89 fileHandle, const_cast<char*>(contents.c_str()),
90 contents.length(), MPI_CHAR, &mpiStatus);
91 oss.str("");
92 success=(mpiErr==0);
93 #endif
94 } else {
95 ofs << oss.str();
96 oss.str("");
97 success=!ofs.fail();
98 }
99 return success;
100 }
101
102 bool FileWriter::writeOrdered(ostringstream& oss)
103 {
104 bool success=false;
105 if (mpiSize>1) {
106 #if HAVE_MPI
107 MPI_Status mpiStatus;
108 string contents = oss.str();
109 int mpiErr = MPI_File_write_ordered(
110 fileHandle, const_cast<char*>(contents.c_str()),
111 contents.length(), MPI_CHAR, &mpiStatus);
112 oss.str("");
113 success=(mpiErr==0);
114 #endif
115 } else {
116 ofs << oss.str();
117 oss.str("");
118 success=!ofs.fail();
119 }
120 return success;
121 }
122
123 void FileWriter::close()
124 {
125 if (mpiSize>1) {
126 #if HAVE_MPI
127 MPI_File_close(&fileHandle);
128 #endif
129 } else {
130 ofs.close();
131 }
132 }
133
134 } // namespace weipa
135

  ViewVC Help
Powered by ViewVC 1.1.26