File: BigMatrix.h

package info (click to toggle)
r-cran-bigmemory 4.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 648 kB
  • sloc: cpp: 4,930; ansic: 131; sh: 13; makefile: 2
file content (263 lines) | stat: -rw-r--r-- 7,307 bytes parent folder | download | duplicates (4)
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
// proposed new names, InMemorySharedBigMatrix, FileBackedSharedBigMatrix
#ifndef BIGMATRIX_H
#define BIGMATRIX_H

#ifdef INT
#undef INT
#endif

#include <boost/interprocess/mapped_region.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <string>
#include <vector>

#include "bigmemoryDefines.h"
#include "SharedCounter.h"

using namespace std;

typedef vector<std::string> Names;
typedef boost::interprocess::mapped_region MappedRegion;
typedef boost::shared_ptr<MappedRegion> MappedRegionPtr;
typedef vector<MappedRegionPtr> MappedRegionPtrs;
typedef vector<index_type> Columns;

class BigMatrix : public boost::noncopyable
{
  // Public types
  public:
    enum MatrixType {CHAR=1, SHORT=2, INT=3, DOUBLE=4, COMPLEX=5, FLOAT=6};

  // Constructor and Destructor
  public:
    BigMatrix():_ncol(0),_nrow(0), _totalRows(0), _totalCols(0),
                _colOffset(0), _rowOffset(0),_matType(0), _pdata(NULL),
                _sepCols(false), _readOnly(false), _allocationSize(0){}
    virtual ~BigMatrix(){}

    // The next function returns the matrix data.  It will generally be passed
    // to an appropriate templated function. 
    void* matrix() {return _pdata;}
    
    // Accessors
    index_type ncol() const {return _ncol;}
    index_type nrow() const {return _nrow;}
   
    // For a submatrix, total_* includes the rows in the supermatrix.
    index_type total_rows() const {return _totalRows;}
    index_type total_columns() const {return _totalCols;}
    index_type col_offset() const {return _colOffset;}
    index_type row_offset() const {return _rowOffset;}
    int matrix_type() const {return _matType;}
    bool shared() const {return _shared;}
    bool separated_columns() const {return _sepCols;}
    Names column_names() 
    {
      Names ret;
      if (!_colNames.empty())
      {
        std::copy( _colNames.begin()+col_offset(), 
                  _colNames.begin()+col_offset()+ncol(),
                  std::back_inserter(ret) );
      }
      return ret;
    }

    Names row_names() 
    {
      Names ret;
      if (!_rowNames.empty())
      {
        ret.reserve(nrow());
        std::copy( _rowNames.begin() + row_offset(), 
                  _rowNames.begin() + row_offset() + nrow(),
                  std::back_inserter(ret) );
      }
      return ret;
    }

    bool is_submatrix()
    {
      return total_rows() != nrow() || total_columns() != ncol();
    }

    // Mutators
    bool column_names( const Names &newColNames )
    {
      if ( !is_submatrix() && 
        ((newColNames.size() == static_cast<Names::size_type>(ncol())) ||
        newColNames.size() == 0) )
      {
        _colNames = newColNames; 
        return true;
      }
      if ( is_submatrix() && 
        (newColNames.size() == static_cast<Names::size_type>(ncol())) )
      {
        std::copy( newColNames.begin(), newColNames.end(),
          _colNames.begin() + col_offset());
        return true;
      }
      return false;
    }

    bool row_names( const Names &newRowNames )
    {
      if ( !is_submatrix() && 
        ((newRowNames.size() == static_cast<Names::size_type>(nrow())) ||
        newRowNames.size() == 0) )
      {
        _rowNames = newRowNames; 
        return true;
      }
      if ( is_submatrix() && 
        (newRowNames.size() == static_cast<Names::size_type>(nrow())) )
      {
        std::copy( newRowNames.begin(), newRowNames.end(),
          _rowNames.begin() + row_offset());
        return true;
      }
      return false;
    }
  
    bool col_offset( const index_type &newOffset ) 
    {
      _colOffset=newOffset;
      return true;
    }
    
    bool row_offset( const index_type &newOffset )
    {
      _rowOffset=newOffset;
      return true;
    }
    
    bool ncol( const index_type &newNumCols )
    {
      _ncol=newNumCols;
      return true;
    }
    
    bool nrow( const index_type &newNumCols )
    {
      _nrow=newNumCols;
      return true;
    }
    
    const bool read_only() const
    {
      return _readOnly;
    }

    void read_only(bool newReadOnly)
    {
      _readOnly = newReadOnly;
    }
  
    void* data_ptr() {return _pdata;}
  
    const index_type allocation_size() const {return _allocationSize;}

  // Data Members

  protected:
    index_type _ncol;
    index_type _nrow;
    index_type _totalRows;
    index_type _totalCols;
    index_type _colOffset;
    index_type _rowOffset;
    index_type _nebytes;
    int _matType;
    void* _pdata;
    bool _shared;
    bool _sepCols;
    Names _colNames;
    Names _rowNames;
    bool _readOnly;
    index_type _allocationSize;
};

class LocalBigMatrix : public BigMatrix
{
  public:
    LocalBigMatrix() : BigMatrix() {_shared=false;}
    virtual ~LocalBigMatrix() {destroy();};
    virtual bool create( const index_type numRow, const index_type numCol,
      const int matrixType, const bool sepCols);

  protected:
    virtual bool destroy();
};
 
class SharedBigMatrix : public BigMatrix
{
  public:
    SharedBigMatrix() : BigMatrix() {_shared=true;}
    virtual ~SharedBigMatrix() {}
    std::string uuid() const {return _uuid;}
    std::string shared_name() const {return _sharedName;}

  protected:
    virtual bool destroy()=0;

  protected:
    // According to the documentation, shared memory has kernel or 
    // filesystem presistence (mechanism exists until the system reboots
    // or is deleted (kernel) or until the mechanism is explicitly deleted
    // (filesystem)).  As a result, we are going to need a usage counter
    // so that when the last object is done with the shared resource, it
    // can delete the resource.  The destructor will handle deletion
    // of the shared usage counter.
    bool create_uuid();
    bool uuid(const std::string &uuid) {_uuid=uuid; return true;}

  protected:
    std::string _uuid;
    std::string _sharedName;
    MappedRegionPtrs _dataRegionPtrs;
};

class SharedMemoryBigMatrix : public SharedBigMatrix
{
  public:
    SharedMemoryBigMatrix():SharedBigMatrix(){};
    virtual ~SharedMemoryBigMatrix(){destroy();};
    virtual bool create( const index_type numRow, const index_type numCol, 
      const int matrixType, const bool sepCols);
    virtual bool connect( const std::string &uuid, const index_type numRow, 
      const index_type numCol, const int matrixType,
      const bool sepCols, const bool readOnly=false);

  protected:
    virtual bool destroy();

  protected:
    SharedCounter _counter;
}; 

class FileBackedBigMatrix : public SharedBigMatrix
{
  // _sharedName is filename_uuid
  public:
    FileBackedBigMatrix():SharedBigMatrix(){}
    virtual ~FileBackedBigMatrix(){destroy();}
    virtual bool create( const std::string &fileName, 
      const std::string &filePath,const index_type numRow, 
      const index_type numCol, const int matrixType, const bool sepCols);
    virtual bool connect( const std::string &fileName, 
      const std::string &filePath, const index_type numRow, 
      const index_type numCol, const int matrixType, const bool sepCols,
      const bool readOnly=false);
    std::string file_name() const {return _fileName;}
    std::string file_path() const {return _filePath;}
    bool flush();
  protected:
    virtual bool destroy();

  protected:
    std::string _fileName, _filePath;
};

#endif // BIGMATRIX_H