File tree Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Expand file tree Collapse file tree 3 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,17 @@ void GpuMatrix::resetOne() {
202
202
CHECK (data_ != NULL );
203
203
one ();
204
204
}
205
+
206
+ void GpuMatrix::setDiag (real value) {
207
+ CHECK (data_ != NULL );
208
+ CHECK_EQ (height_, width_);
209
+
210
+ zeroMem ();
211
+ for (size_t i = 0 ; i < height_; i++) {
212
+ hl_memcpy_host2device (&data_[i * stride_ + i], &value, sizeof (real));
213
+ }
214
+ }
215
+
205
216
void GpuMatrix::resize (size_t newHeight, size_t newWidth) {
206
217
size_t newSize = newHeight * newWidth;
207
218
if (NULL == memoryHandle_.get () ||
@@ -1244,6 +1255,16 @@ void CpuMatrix::resetOne() {
1244
1255
BaseMatrix::one ();
1245
1256
}
1246
1257
1258
+ void CpuMatrix::setDiag (real value) {
1259
+ CHECK (data_ != NULL );
1260
+ CHECK_EQ (height_, width_);
1261
+
1262
+ zeroMem ();
1263
+ for (size_t i = 0 ; i < height_; i++) {
1264
+ data_[i * stride_ + i] = value;
1265
+ }
1266
+ }
1267
+
1247
1268
void CpuMatrix::copyFrom (const Matrix& src) {
1248
1269
CHECK (isContiguous ());
1249
1270
if (typeid (src) == typeid (GpuMatrix)) {
Original file line number Diff line number Diff line change @@ -195,6 +195,8 @@ class Matrix : public BaseMatrix {
195
195
196
196
virtual void resetOne () { LOG (FATAL) << " Not implemented" ; }
197
197
198
+ virtual void setDiag (real value) { LOG (FATAL) << " Not implemented" ; }
199
+
198
200
virtual void copyFrom (const Matrix& src) { LOG (FATAL) << " Not implemented" ; }
199
201
200
202
virtual void trimFrom (const CpuSparseMatrix& src) {
@@ -330,6 +332,7 @@ class Matrix : public BaseMatrix {
330
332
331
333
virtual MatrixPtr getInverse () {
332
334
LOG (FATAL) << " Not implemented" ;
335
+ return nullptr ;
333
336
}
334
337
335
338
/* *
@@ -1016,6 +1019,7 @@ class GpuMatrix : public Matrix {
1016
1019
1017
1020
void zeroMem ();
1018
1021
void resetOne ();
1022
+ void setDiag (real value);
1019
1023
1020
1024
void resize (size_t newHeight, size_t newWidth);
1021
1025
void resize (size_t newHeight, size_t newWidth,
@@ -1280,6 +1284,8 @@ class CpuMatrix : public Matrix {
1280
1284
1281
1285
void zeroMem ();
1282
1286
void resetOne ();
1287
+ void setDiag (real value);
1288
+
1283
1289
void resize (size_t newHeight, size_t newWidth);
1284
1290
void resize (size_t newHeight, size_t newWidth,
1285
1291
size_t newNnz, /* used to allocate space */
Original file line number Diff line number Diff line change @@ -647,20 +647,23 @@ void testMatrixInverse(int height) {
647
647
MatrixPtr cpuI = std::make_shared<CpuMatrix>(height, height);
648
648
MatrixPtr gpuI = std::make_shared<GpuMatrix>(height, height);
649
649
650
+ /* Make matrix well conditioned: cpu * cpuT + Identity */
650
651
cpu->randomizeUniform ();
652
+ MatrixPtr cpuT = cpu->getTranspose ();
653
+ MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, height);
654
+ outputCheck->mul (cpu, cpuT);
655
+ cpu->setDiag (1.0 );
656
+ cpu->add (*outputCheck);
657
+
651
658
gpu->copyFrom (*cpu);
652
659
cpu->inverse (cpuI, false );
653
660
gpu->inverse (gpuI, false );
654
661
655
- MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, height);
656
662
outputCheck->copyFrom (*gpuI);
657
663
MatrixCheckErr (*cpuI, *outputCheck);
658
664
659
665
outputCheck->mul (cpu, cpuI);
660
- cpu->zeroMem ();
661
- for (int i = 0 ; i < height; i++) {
662
- cpu->getRowBuf (i)[i] = 1.0 ;
663
- }
666
+ cpu->setDiag (1.0 );
664
667
MatrixCheckErr (*cpu, *outputCheck);
665
668
}
666
669
You can’t perform that action at this time.
0 commit comments