Browse code

restructured matrix.h

Tom Sherman authored on 24/05/2018 18:17:14
Showing 12 changed files

... ...
@@ -63,7 +63,7 @@ std::string getBuildReport_cpp()
63 63
     std::string simd = "SIMD not enabled\n";
64 64
 #endif
65 65
 
66
-#if defined(_GAPS_OPENMP)
66
+#if defined(__GAPS_OPENMP__)
67 67
     std::string openmp = "Compiled with OpenMP\n";
68 68
 #else
69 69
     std::string openmp = "Compiler did not support OpenMP\n";
... ...
@@ -207,11 +207,6 @@ void GapsRunner::displayStatus(const std::string &type, unsigned nIterTotal)
207 207
 {
208 208
     if (mNumOutputs > 0 && (mCurrentIter + 1) % mNumOutputs == 0 && mPrintMessages)
209 209
     {
210
-        // print algo status
211
-        Rprintf("%s %d of %d, Atoms:%lu(%lu) Chi2 = %.2f\n", type.c_str(),
212
-            mCurrentIter + 1, nIterTotal, mASampler.nAtoms(),
213
-            mPSampler.nAtoms(), mASampler.chi2());
214
-
215 210
         // compute time
216 211
         bpt::time_duration diff = bpt_now() - mStartTime;
217 212
         double elapsed = diff.total_seconds();
... ...
@@ -221,6 +216,11 @@ void GapsRunner::displayStatus(const std::string &type, unsigned nIterTotal)
221 216
         printTime("Elapsed Time", elapsed);
222 217
         printTime("Estimated Total Time", estTotal);
223 218
         printTime("Estimated Remaining Time", estTotal - elapsed);
219
+
220
+        // print algo status
221
+        Rprintf("%s %d of %d, Atoms:%lu(%lu) Chi2 = %.2f\n\n", type.c_str(),
222
+            mCurrentIter + 1, nIterTotal, mASampler.nAtoms(),
223
+            mPSampler.nAtoms(), mASampler.chi2());
224 224
     }
225 225
 }
226 226
 
... ...
@@ -3,6 +3,7 @@
3 3
 
4 4
 #include "Archive.h"
5 5
 #include "data_structures/Matrix.h"
6
+#include "data_structures/Vector.h"
6 7
 #include "GibbsSampler.h"
7 8
 #include "GapsStatistics.h"
8 9
 
... ...
@@ -11,18 +11,16 @@ void GapsStatistics::update(const AmplitudeGibbsSampler &ASampler,
11 11
 const PatternGibbsSampler &PSampler)
12 12
 {
13 13
     mStatUpdates++;
14
-
15
-    Vector normVec(mNumPatterns);
16 14
     for (unsigned j = 0; j < mNumPatterns; ++j)
17 15
     {
18
-        normVec[j] = gaps::algo::sum(PSampler.mMatrix.getRow(j));
19
-        normVec[j] = normVec[j] == 0 ? 1.f : normVec[j];
16
+        float norm = gaps::algo::sum(PSampler.mMatrix.getRow(j));
17
+        norm = norm == 0.f ? 1.f : norm;
20 18
 
21
-        Vector quot(PSampler.mMatrix.getRow(j) / normVec[j]);
19
+        Vector quot(PSampler.mMatrix.getRow(j) / norm);
22 20
         mPMeanMatrix.getRow(j) += quot;
23 21
         mPStdMatrix.getRow(j) += gaps::algo::elementSq(quot);
24 22
 
25
-        Vector prod(ASampler.mMatrix.getCol(j) * normVec[j]);
23
+        Vector prod(ASampler.mMatrix.getCol(j) * norm);
26 24
         mAMeanMatrix.getCol(j) += prod;
27 25
         mAStdMatrix.getCol(j) += gaps::algo::elementSq(prod); 
28 26
     }
... ...
@@ -9,6 +9,7 @@ OBJECTS =   AtomicDomain.o \
9 9
             test-runner.o \
10 10
             math/Algorithms.o \
11 11
             data_structures/Matrix.o \
12
+            data_structures/Vector.o \
12 13
             math/Random.o \
13 14
             cpp_tests/testAlgorithms.o \
14 15
             cpp_tests/testAtomicDomain.o \
... ...
@@ -48,27 +48,5 @@ TEST_CASE("Test Matrix.h")
48 48
         REQUIRE(cmS.nRow() == 1363);
49 49
         REQUIRE(cmS.nCol() == 9);
50 50
     }
51
-
52
-    SECTION("Matrix Update")
53
-    {
54
-        RowMatrix rm(10, 25);
55
-        ColMatrix cm(10, 25);
56
-
57
-        rm.update(MatrixChange('A', 3, 4, 3.0));
58
-        cm.update(MatrixChange('P', 1, 2, 13.0));
59
-
60
-        REQUIRE(rm(3,4) == 3.0);
61
-        REQUIRE(cm(1,2) == 13.0);
62
-        REQUIRE(rm.getRow(3)[4] == 3.0);
63
-        REQUIRE(cm.getCol(2)[1] == 13.0);
64
-
65
-        rm.update(MatrixChange('A', 3, 4, 3.0));
66
-        cm.update(MatrixChange('P', 1, 2, 5.0));
67
-        
68
-        REQUIRE(rm(3,4) == 6.0);
69
-        REQUIRE(cm(1,2) == 18.0);
70
-        REQUIRE(rm.getRow(3)[4] == 6.0);
71
-        REQUIRE(cm.getCol(2)[1] == 18.0);
72
-    }
73 51
 }
74 52
 
... ...
@@ -20,7 +20,7 @@ public:
20 20
     void insert(uint64_t n) {mSet[n] = mCurrentKey;}
21 21
 };
22 22
 
23
-// have sorted vector with at least some % of holes
23
+// TODO have sorted vector with at least some % of holes
24 24
 // even distribute entries along it
25 25
 // when shift happens, should be minimal
26 26
 
... ...
@@ -37,7 +37,7 @@ public:
37 37
     void insert(uint64_t p) {mVec.push_back(p);}
38 38
     void clear() {mVec.clear();}
39 39
 
40
-    // inclusive of a and b
40
+    // inclusive of a and b, TODO improve performance
41 41
     bool isEmptyInterval(uint64_t a, uint64_t b)
42 42
     {
43 43
         for (unsigned i = 0; i < mVec.size(); ++i)
... ...
@@ -1,26 +1,7 @@
1 1
 #include "Matrix.h"
2 2
 
3
-#include <stdexcept>
4
-
5
-static const float EPSILON = 1.e-10;
6
-
7 3
 /******************************** HELPER *******************************/
8 4
 
9
-static void updateHelper2(float& val, float delta)
10
-{
11
-    val = std::abs(val + delta) < EPSILON ? 0.0 : val + delta;
12
-}
13
-
14
-template<class GenericMatrix>
15
-static void updateHelper(GenericMatrix &mat, const MatrixChange &change)
16
-{
17
-    updateHelper2(mat(change.row1, change.col1), change.delta1);
18
-    if (change.nChanges > 1)
19
-    {
20
-        updateHelper2(mat(change.row2, change.col2), change.delta2);
21
-    }
22
-}
23
-
24 5
 template<class GenericMatrix>
25 6
 static Rcpp::NumericMatrix convertToRMatrix(const GenericMatrix &mat)
26 7
 {
... ...
@@ -35,118 +16,16 @@ static Rcpp::NumericMatrix convertToRMatrix(const GenericMatrix &mat)
35 16
     return rmat;
36 17
 }
37 18
 
38
-/******************************** VECTOR *******************************/
39
-
40
-void Vector::concat(const Vector& vec)
41
-{
42
-    mValues.insert(mValues.end(), vec.mValues.begin(), vec.mValues.end());
43
-}
44
-
45
-void Vector::operator+=(const Vector &vec)
46
-{
47
-    for (unsigned i = 0; i < size(); ++i)
48
-    {
49
-        mValues[i] += vec[i];
50
-    }
51
-}
52
-
53
-void Vector::operator=(const Vector &vec)
54
-{
55
-    mValues = vec.mValues;
56
-}
57
-
58
-Vector Vector::operator+(Vector vec) const
59
-{
60
-    for (unsigned i = 0; i < size(); ++i)
61
-    {
62
-        vec[i] += mValues[i];
63
-    }
64
-    return vec;
65
-}
66
-
67
-Vector Vector::operator-(Vector vec) const
68
-{
69
-    for (unsigned i = 0; i < size(); ++i)
70
-    {
71
-        vec[i] -= mValues[i];
72
-    }
73
-    return vec;
74
-}
75
-
76
-Vector Vector::operator*(Vector vec) const
77
-{
78
-    for (unsigned i = 0; i < size(); ++i)
79
-    {
80
-        vec[i] *= mValues[i];
81
-    }
82
-    return vec;
83
-}
84
-
85
-Vector Vector::operator/(Vector vec) const
86
-{
87
-    for (unsigned i = 0; i < size(); ++i)
88
-    {
89
-        vec[i] /= mValues[i];
90
-    }
91
-    return vec;
92
-}
93
-
94
-Vector Vector::operator+(float val) const
95
-{
96
-    Vector vec(*this);
97
-    for (unsigned i = 0; i < size(); ++i)
98
-    {
99
-        vec[i] += val;
100
-    }
101
-    return vec;
102
-}
103
-
104
-Vector Vector::operator-(float val) const
105
-{
106
-    Vector vec(*this);
107
-    for (unsigned i = 0; i < size(); ++i)
108
-    {
109
-        vec[i] -= val;
110
-    }
111
-    return vec;
112
-}
113
-
114
-Vector Vector::operator*(float val) const
115
-{
116
-    Vector vec(*this);
117
-    for (unsigned i = 0; i < size(); ++i)
118
-    {
119
-        vec[i] *= val;
120
-    }
121
-    return vec;
122
-}
123
-
124
-Vector Vector::operator/(float val) const
125
-{
126
-    Vector vec(*this);
127
-    for (unsigned i = 0; i < size(); ++i)
128
-    {
129
-        vec[i] /= val;
130
-    }
131
-    return vec;
132
-}
133
-
134
-Archive& operator<<(Archive &ar, Vector &vec)
19
+template<class MatA, class MatB>
20
+static void copyMatrix(MatA &dest, const MatB &source)
135 21
 {
136
-    for (unsigned i = 0; i < vec.size(); ++i)
22
+    for (unsigned i = 0; i < source.nRow(); ++i)
137 23
     {
138
-        ar << vec[i];
139
-    }
140
-    return ar;
141
-}
142
-
143
-Archive& operator>>(Archive &ar, Vector &vec)
144
-{
145
-    for (unsigned i = 0; i < vec.size(); ++i)
146
-    {
147
-        ar >> vec.mValues[i];
24
+        for (unsigned j = 0; j < source.nCol(); ++j)
25
+        {
26
+            dest(i,j) = source(i,j);
27
+        }
148 28
     }
149
-    return ar;
150 29
 }
151 30
     
152 31
 /****************************** ROW MATRIX *****************************/
... ...
@@ -173,36 +52,19 @@ RowMatrix::RowMatrix(const Rcpp::NumericMatrix &rmat)
173 52
     }
174 53
 }
175 54
 
176
-RowMatrix::RowMatrix(const RowMatrix &mat)
177
-: mNumRows(mat.nRow()), mNumCols(mat.nCol())
55
+void RowMatrix::operator=(const RowMatrix &mat)
178 56
 {
179
-    for (unsigned i = 0; i < mNumRows; ++i)
180
-    {
181
-        mRows.push_back(mat.getRow(i));
182
-    }
183
-}
184
-
185
-RowMatrix::RowMatrix(const ColMatrix &mat)
186
-: mNumRows(mat.nRow()), mNumCols(mat.nCol())
187
-{
188
-    for (unsigned i = 0; i < mNumRows; ++i)
189
-    {
190
-        mRows.push_back(Vector(mNumCols));
191
-        for (unsigned j = 0; j < mNumCols; ++j)
192
-        {
193
-            this->operator()(i,j) = mat(i,j);
194
-        }
195
-    }
57
+    copyMatrix(*this, mat);
196 58
 }
197 59
 
198
-void RowMatrix::update(const MatrixChange &change)
60
+void RowMatrix::operator=(const ColMatrix &mat)
199 61
 {
200
-    updateHelper<RowMatrix>(*this, change);
62
+    copyMatrix(*this, mat);
201 63
 }
202 64
 
203 65
 Rcpp::NumericMatrix RowMatrix::rMatrix() const
204 66
 {
205
-    return convertToRMatrix<RowMatrix>(*this);
67
+    return convertToRMatrix(*this);
206 68
 }
207 69
 
208 70
 RowMatrix RowMatrix::operator/(float val) const
... ...
@@ -215,17 +77,6 @@ RowMatrix RowMatrix::operator/(float val) const
215 77
     return mat;
216 78
 }
217 79
 
218
-void RowMatrix::operator=(const ColMatrix &mat)
219
-{
220
-    for (unsigned i = 0; i < mNumRows; ++i)
221
-    {
222
-        for (unsigned j = 0; j < mNumCols; ++j)
223
-        {
224
-            this->operator()(i,j) = mat(i,j);
225
-        }
226
-    }
227
-}
228
-
229 80
 Archive& operator<<(Archive &ar, RowMatrix &mat)
230 81
 {
231 82
     for (unsigned i = 0; i < mat.nRow(); ++i)
... ...
@@ -268,20 +119,6 @@ ColMatrix::ColMatrix(const Rcpp::NumericMatrix &rmat)
268 119
     }
269 120
 }
270 121
 
271
-ColMatrix::ColMatrix(const ColMatrix &mat)
272
-: mNumRows(mat.nRow()), mNumCols(mat.nCol())
273
-{
274
-    for (unsigned j = 0; j < mNumCols; ++j)
275
-    {
276
-        mCols.push_back(mat.getCol(j));
277
-    }
278
-}
279
-
280
-void ColMatrix::update(const MatrixChange &change)
281
-{
282
-    updateHelper<ColMatrix>(*this, change);
283
-}
284
-
285 122
 ColMatrix ColMatrix::operator/(float val) const
286 123
 {
287 124
     ColMatrix mat(mNumRows, mNumCols);
... ...
@@ -292,20 +129,19 @@ ColMatrix ColMatrix::operator/(float val) const
292 129
     return mat;
293 130
 }
294 131
 
132
+void ColMatrix::operator=(const ColMatrix &mat)
133
+{
134
+    copyMatrix(*this, mat);
135
+}
136
+
295 137
 void ColMatrix::operator=(const RowMatrix &mat)
296 138
 {
297
-    for (unsigned i = 0; i < mNumRows; ++i)
298
-    {
299
-        for (unsigned j = 0; j < mNumCols; ++j)
300
-        {
301
-            this->operator()(i,j) = mat(i,j);
302
-        }
303
-    }
139
+    copyMatrix(*this, mat);
304 140
 }
305 141
 
306 142
 Rcpp::NumericMatrix ColMatrix::rMatrix() const
307 143
 {
308
-    return convertToRMatrix<ColMatrix>(*this);
144
+    return convertToRMatrix(*this);
309 145
 }
310 146
 
311 147
 Archive& operator<<(Archive &ar, ColMatrix &mat)
... ...
@@ -2,9 +2,9 @@
2 2
 #define __COGAPS_MATRIX_H__
3 3
 
4 4
 #include "../Archive.h"
5
+#include "Vector.h"
5 6
 
6 7
 #include <Rcpp.h>
7
-#include <boost/align/aligned_allocator.hpp>
8 8
 #include <vector>
9 9
 
10 10
 struct MatrixChange
... ...
@@ -32,46 +32,6 @@ struct MatrixChange
32 32
     {}
33 33
 };
34 34
 
35
-namespace bal = boost::alignment;
36
-typedef std::vector<float, bal::aligned_allocator<float,32> > aligned_vector;
37
-
38
-class Vector
39
-{
40
-private:
41
-
42
-    aligned_vector mValues;
43
-
44
-public:
45
-
46
-    Vector(unsigned size) : mValues(aligned_vector(size, 0.f)) {}
47
-    Vector(unsigned size, float val) : mValues(aligned_vector(size, val)) {}
48
-    Vector(const aligned_vector &v) : mValues(v) {}
49
-    Vector(const Vector &vec) : mValues(vec.mValues) {}
50
-
51
-    const float* ptr() const {return &mValues[0];}
52
-    float* ptr() {return &mValues[0];}
53
-
54
-    float& operator[](unsigned i) {return mValues[i];}
55
-    float operator[](unsigned i) const {return mValues[i];}
56
-    unsigned size() const {return mValues.size();}
57
-
58
-    Rcpp::NumericVector rVec() const {return Rcpp::wrap(mValues);}
59
-    void concat(const Vector& vec);
60
-    void operator+=(const Vector &vec);
61
-    void operator=(const Vector &vec);
62
-
63
-    Vector operator+(Vector vec) const;
64
-    Vector operator-(Vector vec) const;
65
-    Vector operator*(Vector vec) const;
66
-    Vector operator/(Vector vec) const;
67
-    Vector operator+(float val) const;
68
-    Vector operator-(float val) const;
69
-    Vector operator*(float val) const;
70
-    Vector operator/(float val) const;
71
-
72
-    friend Archive& operator<<(Archive &ar, Vector &vec);
73
-    friend Archive& operator>>(Archive &ar, Vector &vec);
74
-};
75 35
 
76 36
 class ColMatrix;
77 37
 
... ...
@@ -82,14 +42,11 @@ private:
82 42
     std::vector<Vector> mRows;
83 43
     unsigned mNumRows, mNumCols;
84 44
 
85
-    RowMatrix() {}
86
-
87 45
 public:
88 46
 
89 47
     RowMatrix(unsigned nrow, unsigned ncol);
90 48
     RowMatrix(const Rcpp::NumericMatrix &rmat);
91
-    RowMatrix(const RowMatrix &mat);
92
-    RowMatrix(const ColMatrix &mat);
49
+    RowMatrix(const std::string &path);
93 50
 
94 51
     unsigned nRow() const {return mNumRows;}
95 52
     unsigned nCol() const {return mNumCols;}
... ...
@@ -105,9 +62,9 @@ public:
105 62
 
106 63
     RowMatrix operator/(float val) const;
107 64
 
65
+    void operator=(const RowMatrix &mat);
108 66
     void operator=(const ColMatrix &mat);
109 67
 
110
-    void update(const MatrixChange &change);
111 68
     Rcpp::NumericMatrix rMatrix() const;
112 69
 
113 70
     friend Archive& operator<<(Archive &ar, RowMatrix &mat);
... ...
@@ -121,14 +78,11 @@ private:
121 78
     std::vector<Vector> mCols;
122 79
     unsigned mNumRows, mNumCols;
123 80
 
124
-    ColMatrix() {}
125
-
126 81
 public:
127 82
 
128 83
     ColMatrix(unsigned nrow, unsigned ncol);
129 84
     ColMatrix(const Rcpp::NumericMatrix &rmat);
130
-    ColMatrix(const ColMatrix &mat);
131
-    ColMatrix(const RowMatrix &mat);
85
+    ColMatrix(const std::string &path);
132 86
 
133 87
     unsigned nRow() const {return mNumRows;}
134 88
     unsigned nCol() const {return mNumCols;}
... ...
@@ -144,9 +98,9 @@ public:
144 98
 
145 99
     ColMatrix operator/(float val) const;
146 100
 
101
+    void operator=(const ColMatrix &mat);
147 102
     void operator=(const RowMatrix &mat);
148 103
 
149
-    void update(const MatrixChange &change);
150 104
     Rcpp::NumericMatrix rMatrix() const;
151 105
 
152 106
     friend Archive& operator<<(Archive &ar, ColMatrix &mat);
153 107
new file mode 100644
... ...
@@ -0,0 +1,52 @@
1
+#include "Vector.h"
2
+
3
+void Vector::concat(const Vector& vec)
4
+{
5
+    mValues.insert(mValues.end(), vec.mValues.begin(), vec.mValues.end());
6
+}
7
+
8
+void Vector::operator+=(const Vector &vec)
9
+{
10
+    for (unsigned i = 0; i < size(); ++i)
11
+    {
12
+        mValues[i] += vec[i];
13
+    }
14
+}
15
+
16
+Vector Vector::operator*(float val) const
17
+{
18
+    Vector vec(*this);
19
+    for (unsigned i = 0; i < size(); ++i)
20
+    {
21
+        vec[i] *= val;
22
+    }
23
+    return vec;
24
+}
25
+
26
+Vector Vector::operator/(float val) const
27
+{
28
+    Vector vec(*this);
29
+    for (unsigned i = 0; i < size(); ++i)
30
+    {
31
+        vec[i] /= val;
32
+    }
33
+    return vec;
34
+}
35
+
36
+Archive& operator<<(Archive &ar, Vector &vec)
37
+{
38
+    for (unsigned i = 0; i < vec.size(); ++i)
39
+    {
40
+        ar << vec[i];
41
+    }
42
+    return ar;
43
+}
44
+
45
+Archive& operator>>(Archive &ar, Vector &vec)
46
+{
47
+    for (unsigned i = 0; i < vec.size(); ++i)
48
+    {
49
+        ar >> vec.mValues[i];
50
+    }
51
+    return ar;
52
+}
0 53
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+#ifndef __COGAPS_VECTOR_H__
2
+#define __COGAPS_VECTOR_H__
3
+
4
+#include "../Archive.h"
5
+
6
+#include <boost/align/aligned_allocator.hpp>
7
+#include <vector>
8
+
9
+namespace bal = boost::alignment;
10
+typedef std::vector<float, bal::aligned_allocator<float,32> > aligned_vector;
11
+
12
+class Vector
13
+{
14
+private:
15
+
16
+    aligned_vector mValues;
17
+
18
+public:
19
+
20
+    Vector(unsigned size) : mValues(aligned_vector(size, 0.f)) {}
21
+
22
+    const float* ptr() const {return &mValues[0];}
23
+    float* ptr() {return &mValues[0];}
24
+
25
+    float& operator[](unsigned i) {return mValues[i];}
26
+    float operator[](unsigned i) const {return mValues[i];}
27
+
28
+    unsigned size() const {return mValues.size();}
29
+
30
+    Rcpp::NumericVector rVec() const {return Rcpp::wrap(mValues);}
31
+    void concat(const Vector& vec);
32
+    void operator+=(const Vector &vec);
33
+
34
+    Vector operator*(float val) const;
35
+    Vector operator/(float val) const;
36
+
37
+    friend Archive& operator<<(Archive &ar, Vector &vec);
38
+    friend Archive& operator>>(Archive &ar, Vector &vec);
39
+};
40
+
41
+#endif
0 42
\ No newline at end of file
... ...
@@ -21,7 +21,7 @@
21 21
 
22 22
 #include <stdint.h>
23 23
 
24
-#if defined(__GAPS_OPENMP__)
24
+#ifdef __GAPS_OPENMP__
25 25
     #include <omp.h>
26 26
 #else
27 27
     typedef int omp_int_t;