• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

MLAPI_MATLABStream.h

Go to the documentation of this file.
00001 #ifndef MLAPI_MATLABSTREAM_H
00002 #define MLAPI_MATLABSTREAM_H
00003 
00013 /* ******************************************************************** */
00014 /* See the file COPYRIGHT for a complete copyright notice, contact      */
00015 /* person and disclaimer.                                               */        
00016 /* ******************************************************************** */
00017 
00018 #include "ml_common.h"
00019 
00020 #include "MLAPI_Error.h"
00021 #include "MLAPI_Operator.h"
00022 
00023 namespace MLAPI {
00024 
00037 class MATLABStream
00038 {
00039 public:
00040 
00041   // @{ \name Constructors and destructors.
00042   
00044   MATLABStream(const string& FileName, bool UseSparse = true) 
00045   {
00046     FileName_ = FileName;
00047     SetUseSparse(UseSparse);
00048 
00049     // Creates a new stream, deletes old stream with the same name.
00050     if (GetMyPID() == 0) {
00051       Open(true);
00052       fprintf(fp_, "%% beginning of MLAPI::MATLABStream\n");
00053       Close();
00054     }
00055   }
00056 
00058   ~MATLABStream()
00059   {
00060     if (GetMyPID() == 0) {
00061       Open();
00062       fprintf(fp_, "%% end of MLAPI::MATLABStream\n");
00063       Close();
00064     }
00065   }
00066 
00067   // @}
00068   // @{ \name Overloaded operators
00069 
00071   MATLABStream& operator << (const int obj)
00072   {
00073     if (GetMyPID() == 0) {
00074       Open();
00075       fprintf(fp_,"%d",obj);
00076       Close();
00077     }
00078     return(*this);
00079   }
00080 
00082   MATLABStream& operator << (const double obj)
00083   {
00084     if (GetMyPID() == 0) {
00085       Open();
00086       fprintf(fp_,"%f",obj);
00087       Close();
00088     }
00089     return(*this);
00090   }
00091 
00093   MATLABStream& operator << (const string obj)
00094   {
00095     if (GetMyPID() == 0) {
00096       Open();
00097       fprintf(fp_,"%s",obj.c_str());
00098       Close();
00099     }
00100     return(*this);
00101   }
00102 
00104   MATLABStream& operator << (const Operator& obj)
00105   {
00106     int    *bindx;
00107     double *val;
00108     int    allocated, row_length;
00109     ML_Operator* matrix = obj.GetML_Operator();
00110 
00111     if (matrix->getrow == NULL)
00112       ML_THROW("getrow() not set", -1);
00113 
00114     allocated = 100;
00115     bindx = (int    *)  ML_allocate(allocated*sizeof(int   ));
00116     val   = (double *)  ML_allocate(allocated*sizeof(double));
00117 
00118     int NumGlobalRows = obj.GetDomainSpace().GetNumGlobalElements();
00119     int NumGlobalCols = obj.GetRangeSpace().GetNumGlobalElements();
00120 
00121     if (GetMyPID() == 0) {
00122       Open();
00123       if (GetUseSparse())
00124         fprintf(fp_,"%s = sparse(%d,%d);\n",
00125                 obj.GetLabel().c_str(), NumGlobalRows, NumGlobalCols);
00126       else
00127         fprintf(fp_,"%s = zeros(%d,%d);\n",
00128                 obj.GetLabel().c_str(), NumGlobalRows, NumGlobalCols);
00129 
00130       Close();
00131     }
00132 
00133     for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
00134 
00135       if (GetMyPID() == iproc) {
00136 
00137         Open();
00138 
00139         for (int i = 0 ; i < matrix->getrow->Nrows; i++) {
00140           ML_get_matrix_row(matrix, 1, &i, &allocated, &bindx, &val,
00141                             &row_length, 0);
00142           for  (int j = 0; j < row_length; j++) {
00143             int GlobalRow = obj.GetGRID(i) + 1;
00144             int GlobalCol = obj.GetGCID(bindx[j]) + 1;
00145             fprintf(fp_,"%s(%d,%d) = %e;\n",
00146                     obj.GetLabel().c_str(), GlobalRow, GlobalCol, val[j]);
00147           }
00148         }
00149 
00150         Close();
00151       }
00152       Barrier();
00153     }
00154 
00155     ML_free(val);
00156     ML_free(bindx);
00157     return (*this);
00158   }
00159 
00161   MATLABStream& operator << (const MultiVector& obj)
00162   {
00163     int NumMyRows = obj.GetVectorSpace().GetNumMyElements();
00164     int NumGlobalRows = obj.GetVectorSpace().GetNumGlobalElements();
00165     int NumVectors = obj.GetNumVectors();
00166 
00167     if (GetMyPID() == 0) {
00168       Open();
00169       fprintf(fp_,"%s = zeros(%d, %d);\n",
00170               obj.GetLabel().c_str(), NumGlobalRows, NumVectors);
00171       Close();
00172     }
00173 
00174     for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
00175 
00176       if (GetMyPID() == iproc) {
00177 
00178         Open();
00179 
00180         for (int i = 0 ; i < NumMyRows ; ++i) {
00181           for (int j = 0 ; j < NumVectors ; ++j) {
00182             fprintf(fp_,"%s(%d, %d) = %e;\n",
00183                     obj.GetLabel().c_str(), obj.GetVectorSpace()(i) + 1, j + 1, obj(i,j));
00184           }
00185         }
00186         Close();
00187       }
00188       Barrier();
00189     }
00190 
00191     return (*this);
00192   }
00193 
00195   MATLABStream& operator << (const Space& obj)
00196   {
00197     int NumMyRows = obj.GetNumMyElements();
00198     int NumGlobalRows = obj.GetNumGlobalElements();
00199 
00200     if (GetMyPID() == 0) {
00201       Open();
00202       fprintf(fp_,"%s = zeros(%d);\n",
00203               obj.GetLabel().c_str(), NumGlobalRows);
00204       Close();
00205     }
00206 
00207     for (int iproc = 0 ; iproc < GetNumProcs() ; ++iproc) {
00208 
00209       if (GetMyPID() == iproc) {
00210 
00211         Open();
00212 
00213         for (int i = 0 ; i < NumMyRows ; ++i)
00214           fprintf(fp_,"%s(%d) = %d;\n",
00215                   obj.GetLabel().c_str(), i, obj(i) + 1);
00216         Close();
00217       }
00218       Barrier();
00219     }
00220 
00221     return (*this);
00222   }
00223 
00224   // @}
00225   // @{ \name Sets and gets methods
00226 
00228   bool GetUseSparse() const
00229   {
00230     return(UseSparse_);
00231   }
00232 
00234   void SetUseSparse(const bool UseSparse)
00235   {
00236     UseSparse_ = UseSparse;
00237   }
00238     
00240   inline string GetFileName() const
00241   {
00242     return(FileName_);
00243   }
00244 
00246   
00247 private:
00248 
00250   void Open(const bool FirstTime = false)
00251   {
00252     if (FirstTime)
00253       fp_ = fopen(FileName_.c_str(),"w");
00254     else
00255       fp_ = fopen(FileName_.c_str(),"a");
00256   }
00257   
00259   void Close() 
00260   {
00261     fclose(fp_);
00262   }
00263 
00265   string FileName_;
00267   bool UseSparse_;
00269   FILE* fp_;
00270 
00271 }; // class MATLABStream
00272 
00273 } // namespace MLAPI
00274 
00275 #endif