/usr/include/trilinos/MLAPI_MATLABStream.h is in libtrilinos-ml-dev 12.4.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #ifndef MLAPI_MATLABSTREAM_H
#define MLAPI_MATLABSTREAM_H
/*!
\file MLAPI_MATLABStream.h
\brief Basic stream to save in a MATLAB-compatible file MLAPI objects.
\author Marzio Sala, D-INFK/ETHZ.
\date Last updated on Mar-06.
*/
/* ******************************************************************** */
/* See the file COPYRIGHT for a complete copyright notice, contact */
/* person and disclaimer. */
/* ******************************************************************** */
#include "ml_common.h"
#include "MLAPI_Error.h"
#include "MLAPI_Operator.h"
namespace MLAPI {
/*!
\class MATLABStream
\brief Basic stream to save in a MATLAB-compatible file MLAPI objects.
For an example of usage, see \ref ml_blackboard_cpp
\author Marzio Sala, SNL 9214
\date Last updated on Feb-05.
*/
class MATLABStream
{
public:
// @{ \name Constructors and destructors.
//! Opens the specified file for writing.
MATLABStream(const std::string& FileName, bool UseSparse = true)
{
FileName_ = FileName;
SetUseSparse(UseSparse);
// Creates a new stream, deletes old stream with the same name.
if (GetMyPID() == 0) {
Open(true);
fprintf(fp_, "%% beginning of MLAPI::MATLABStream\n");
Close();
}
}
//! Finally closes the output file.
~MATLABStream()
{
if (GetMyPID() == 0) {
Open();
fprintf(fp_, "%% end of MLAPI::MATLABStream\n");
Close();
}
}
// @}
// @{ \name Overloaded operators
//! Writes on file the specified integer (on process 0 only).
MATLABStream& operator << (const int obj)
{
if (GetMyPID() == 0) {
Open();
fprintf(fp_,"%d",obj);
Close();
}
return(*this);
}
//! Writes on file the specified double (on process 0 only).
MATLABStream& operator << (const double obj)
{
if (GetMyPID() == 0) {
Open();
fprintf(fp_,"%f",obj);
Close();
}
return(*this);
}
//! Writes on file the specified std::string on process 0 only.
MATLABStream& operator << (const std::string obj)
{
if (GetMyPID() == 0) {
Open();
fprintf(fp_,"%s",obj.c_str());
Close();
}
return(*this);
}
//! Writes on file input Operator, one process at-a-time, using global ordering.
MATLABStream& operator << (const Operator& obj)
{
int *bindx;
double *val;
int allocated, row_length;
ML_Operator* matrix = obj.GetML_Operator();
if (matrix->getrow == NULL)
ML_THROW("getrow() not set", -1);
allocated = 100;
bindx = (int *) ML_allocate(allocated*sizeof(int ));
val = (double *) ML_allocate(allocated*sizeof(double));
int NumGlobalRows = obj.GetDomainSpace().GetNumGlobalElements();
int NumGlobalCols = obj.GetRangeSpace().GetNumGlobalElements();
if (GetMyPID() == 0) {
Open();
if (GetUseSparse())
fprintf(fp_,"%s = sparse(%d,%d);\n",
obj.GetLabel().c_str(), NumGlobalRows, NumGlobalCols);
else
fprintf(fp_,"%s = zeros(%d,%d);\n",
obj.GetLabel().c_str(), NumGlobalRows, NumGlobalCols);
Close();
}
for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
if (GetMyPID() == iproc) {
Open();
for (int i = 0 ; i < matrix->getrow->Nrows; i++) {
ML_get_matrix_row(matrix, 1, &i, &allocated, &bindx, &val,
&row_length, 0);
for (int j = 0; j < row_length; j++) {
int GlobalRow = obj.GetGRID(i) + 1;
int GlobalCol = obj.GetGCID(bindx[j]) + 1;
fprintf(fp_,"%s(%d,%d) = %e;\n",
obj.GetLabel().c_str(), GlobalRow, GlobalCol, val[j]);
}
}
Close();
}
Barrier();
}
ML_free(val);
ML_free(bindx);
return (*this);
}
//! Writes on file the input MultiVector, one process at-a-time.
MATLABStream& operator << (const MultiVector& obj)
{
int NumMyRows = obj.GetVectorSpace().GetNumMyElements();
int NumGlobalRows = obj.GetVectorSpace().GetNumGlobalElements();
int NumVectors = obj.GetNumVectors();
if (GetMyPID() == 0) {
Open();
fprintf(fp_,"%s = zeros(%d, %d);\n",
obj.GetLabel().c_str(), NumGlobalRows, NumVectors);
Close();
}
for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
if (GetMyPID() == iproc) {
Open();
for (int i = 0 ; i < NumMyRows ; ++i) {
for (int j = 0 ; j < NumVectors ; ++j) {
fprintf(fp_,"%s(%d, %d) = %e;\n",
obj.GetLabel().c_str(), obj.GetVectorSpace()(i) + 1, j + 1, obj(i,j));
}
}
Close();
}
Barrier();
}
return (*this);
}
//! Writes on file input Space, one process at-a-time.
MATLABStream& operator << (const Space& obj)
{
int NumMyRows = obj.GetNumMyElements();
int NumGlobalRows = obj.GetNumGlobalElements();
if (GetMyPID() == 0) {
Open();
fprintf(fp_,"%s = zeros(%d);\n",
obj.GetLabel().c_str(), NumGlobalRows);
Close();
}
for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
if (GetMyPID() == iproc) {
Open();
for (int i = 0 ; i < NumMyRows ; ++i)
fprintf(fp_,"%s(%d) = %d;\n",
obj.GetLabel().c_str(), i, obj(i) + 1);
Close();
}
Barrier();
}
return (*this);
}
// @}
// @{ \name Sets and gets methods
//! Returns \c true if the stream uses sparse MATLAB format.
bool GetUseSparse() const
{
return(UseSparse_);
}
//! Toggles the use of sparse MATLAB formats.
void SetUseSparse(const bool UseSparse)
{
UseSparse_ = UseSparse;
}
//! Returns the name of the output file.
inline std::string GetFileName() const
{
return(FileName_);
}
//@}
private:
//! Opens the file stream in append mode, or in write more if \c FirstTime == \c true.
void Open(const bool FirstTime = false)
{
if (FirstTime)
fp_ = fopen(FileName_.c_str(),"w");
else
fp_ = fopen(FileName_.c_str(),"a");
}
//! Closes the file stream.
void Close()
{
fclose(fp_);
}
//! Name of output file.
std::string FileName_;
//! If \c true, prints out using sparse MATLAB commands.
bool UseSparse_;
//! FILE pointer.
FILE* fp_;
}; // class MATLABStream
} // namespace MLAPI
#endif
|