1 /*
2    Copyright (c) 2009-2014, Jack Poulson
3    All rights reserved.
4 
5    This file is part of Elemental and is under the BSD 2-Clause License,
6    which can be found in the LICENSE file in the root directory, or at
7    http://opensource.org/licenses/BSD-2-Clause
8 */
9 #pragma once
10 #ifndef ELEM_WRITE_HPP
11 #define ELEM_WRITE_HPP
12 
13 #include "./Write/Ascii.hpp"
14 #include "./Write/AsciiMatlab.hpp"
15 #include "./Write/Binary.hpp"
16 #include "./Write/BinaryFlat.hpp"
17 #include "./Write/Image.hpp"
18 #include "./Write/MatrixMarket.hpp"
19 
20 namespace elem {
21 
22 template<typename T>
23 inline void
Write(const Matrix<T> & A,std::string basename="matrix",FileFormat format=BINARY,std::string title="")24 Write
25 ( const Matrix<T>& A, std::string basename="matrix", FileFormat format=BINARY,
26   std::string title="" )
27 {
28     DEBUG_ONLY(CallStackEntry cse("Write"))
29     switch( format )
30     {
31     case ASCII:         write::Ascii( A, basename, title );       break;
32     case ASCII_MATLAB:  write::AsciiMatlab( A, basename, title ); break;
33     case BINARY:        write::Binary( A, basename );             break;
34     case BINARY_FLAT:   write::BinaryFlat( A, basename );         break;
35     case MATRIX_MARKET: write::MatrixMarket( A, basename );       break;
36     case BMP:
37     case JPG:
38     case JPEG:
39     case PNG:
40     case PPM:
41     case XBM:
42     case XPM:
43         write::Image( A, basename, format ); break;
44     default:
45         LogicError("Invalid file format");
46     }
47 }
48 
49 template<typename T,Dist U,Dist V>
50 inline void
Write(const DistMatrix<T,U,V> & A,std::string basename="matrix",FileFormat format=BINARY,std::string title="")51 Write
52 ( const DistMatrix<T,U,V>& A, std::string basename="matrix",
53   FileFormat format=BINARY, std::string title="" )
54 {
55     DEBUG_ONLY(CallStackEntry cse("Write"))
56     if( U == A.UGath && V == A.VGath )
57     {
58         if( A.CrossRank() == A.Root() && A.RedundantRank() == 0 )
59             Write( A.LockedMatrix(), basename, format, title );
60     }
61     else
62     {
63         DistMatrix<T,CIRC,CIRC> A_CIRC_CIRC( A );
64         if( A_CIRC_CIRC.CrossRank() == A_CIRC_CIRC.Root() )
65             Write( A_CIRC_CIRC.LockedMatrix(), basename, format, title );
66     }
67 }
68 
69 template<typename T,Dist U,Dist V>
70 inline void
Write(const BlockDistMatrix<T,U,V> & A,std::string basename="matrix",FileFormat format=BINARY,std::string title="")71 Write
72 ( const BlockDistMatrix<T,U,V>& A, std::string basename="matrix",
73   FileFormat format=BINARY, std::string title="" )
74 {
75     DEBUG_ONLY(CallStackEntry cse("Write"))
76     if( U == A.UGath && V == A.VGath )
77     {
78         if( A.CrossRank() == A.Root() && A.RedundantRank() == 0 )
79             Write( A.LockedMatrix(), basename, format, title );
80     }
81     else
82     {
83         BlockDistMatrix<T,CIRC,CIRC> A_CIRC_CIRC( A );
84         if( A_CIRC_CIRC.CrossRank() == A_CIRC_CIRC.Root() )
85             Write( A_CIRC_CIRC.LockedMatrix(), basename, format, title );
86     }
87 }
88 
89 } // namespace elem
90 
91 #endif // ifndef ELEM_WRITE_HPP
92