Skip to content

Commit d266726

Browse files
authored
[NeoML] CDnnSparseMatrix ctor (#1103)
Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>
1 parent 99d2f6e commit d266726

File tree

5 files changed

+32
-51
lines changed

5 files changed

+32
-51
lines changed

NeoML/include/NeoML/Dnn/DnnSparseMatrix.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright © 2017-2020 ABBYY Production LLC
1+
/* Copyright © 2017-2024 ABBYY
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -23,14 +23,13 @@ limitations under the License.
2323
namespace NeoML {
2424

2525
// A sparse matrix for IMathEngine
26-
class NEOML_API CDnnSparseMatrix {
26+
class NEOML_API CDnnSparseMatrix final {
2727
public:
2828
CDnnSparseMatrix( IMathEngine& _mathEngine, int rowCount, int columnCount );
2929
~CDnnSparseMatrix();
3030

3131
// Creates a matrix from the specified problem
3232
void Create( const IProblem* problem, int startVectorIndex, int batchCount );
33-
3433
// Destroys the matrix; the allocated memory is kept
3534
void Destroy();
3635

@@ -41,24 +40,27 @@ class NEOML_API CDnnSparseMatrix {
4140
CSparseMatrixDesc GetBatchDesc( int index ) const;
4241

4342
private:
44-
struct CMatrix {
43+
struct CMatrix final {
4544
int ElementCount;
4645
int RowPos;
4746
int ElementPos;
47+
48+
CMatrix( int elementCount, int rowPos, int elementPos ) :
49+
ElementCount( elementCount ), RowPos( rowPos ), ElementPos( elementPos ) {}
4850
};
4951

5052
IMathEngine& mathEngine; // the math engine
5153
const int rowCount; // the number of rows
5254
const int columnCount; // the number of columns
5355
CArray<CFloatVectorDesc> vectors; // the vectors (matrix columns)
5456
CArray<CMatrix> matrixes; // the matrix descriptions
55-
int totalElementSize; // the size of matrix elements (aligned)
56-
int totalRowSize; // the size of matrix rows (aligned)
57+
int totalElementSize = 0; // the size of matrix elements (aligned)
58+
int totalRowSize = 0; // the size of matrix rows (aligned)
5759
CIntHandle mathEngineData; // the data allocated for the matrix by IMathEngine
58-
size_t mathEngineDataSize; // the size of the data allocated by IMathEngine
60+
size_t mathEngineDataSize = 0; // the size of the data allocated by IMathEngine
5961

60-
CDnnSparseMatrix( const CDnnSparseMatrix& );
61-
CDnnSparseMatrix& operator=( const CDnnSparseMatrix& );
62+
CDnnSparseMatrix( const CDnnSparseMatrix& ) = delete;
63+
CDnnSparseMatrix& operator=( const CDnnSparseMatrix& ) = delete;
6264
};
6365

6466
} // namespace NeoML

NeoML/src/Dnn/DnnSparseMatrix.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright © 2017-2020 ABBYY Production LLC
1+
/* Copyright © 2017-2024 ABBYY
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -25,10 +25,7 @@ namespace NeoML {
2525
CDnnSparseMatrix::CDnnSparseMatrix( IMathEngine& _mathEngine, int _rowCount, int _columnCount ) :
2626
mathEngine( _mathEngine ),
2727
rowCount( _rowCount ),
28-
columnCount( _columnCount ),
29-
totalElementSize( 0 ),
30-
totalRowSize( 0 ),
31-
mathEngineDataSize( 0 )
28+
columnCount( _columnCount )
3229
{
3330
NeoAssert( rowCount > 0 );
3431
NeoAssert( columnCount > 0 );
@@ -64,12 +61,7 @@ void CDnnSparseMatrix::Create( const IProblem* problem, int startVectorIndex, in
6461
totalRowSize++;
6562
}
6663
totalRowSize = CeilTo( totalRowSize, 4 );
67-
68-
CMatrix matrix;
69-
matrix.ElementCount = 0;
70-
matrix.RowPos = totalRowSize;
71-
matrix.ElementPos = totalElementSize;
72-
matrixes.Add( matrix );
64+
matrixes.Add( CMatrix( /*elementCount*/0, totalRowSize, totalElementSize ) );
7365
}
7466
matrixes.Last().ElementCount += vectors.Last().Size;
7567
totalElementSize += vectors.Last().Size;
@@ -141,12 +133,10 @@ CSparseMatrixDesc CDnnSparseMatrix::GetBatchDesc( int index ) const
141133
NeoAssert( !vectors.IsEmpty() );
142134
NeoAssert( !matrixes.IsEmpty() );
143135

144-
CSparseMatrixDesc result;
145-
result.ElementCount = matrixes[index].ElementCount;
146-
result.Rows = mathEngineData + matrixes[index].RowPos;
147-
result.Columns = mathEngineData + totalRowSize + matrixes[index].ElementPos;
148-
result.Values = CFloatHandle( mathEngineData ) + totalRowSize + totalElementSize + matrixes[index].ElementPos;
149-
return result;
136+
return CSparseMatrixDesc( matrixes[index].ElementCount,
137+
mathEngineData + matrixes[index].RowPos,
138+
mathEngineData + totalRowSize + matrixes[index].ElementPos,
139+
CFloatHandle( mathEngineData ) + totalRowSize + totalElementSize + matrixes[index].ElementPos );
150140
}
151141

152142
} // namespace NeoML

NeoML/src/TraditionalML/PCA.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ static CSparseMatrixDesc getSparseMatrixDesc( IMathEngine& mathEngine, const CFl
7070
{
7171
const int height = data.Height;
7272
const int width = data.Width;
73-
CSparseMatrixDesc desc;
74-
desc.ElementCount = ( data.Columns == nullptr ) ? height * width : data.PointerE[height - 1];
75-
columns = CDnnBlob::CreateVector( mathEngine, CT_Int, desc.ElementCount );
73+
int elementCount = ( data.Columns == nullptr ) ? height * width : data.PointerE[height - 1];
74+
columns = CDnnBlob::CreateVector( mathEngine, CT_Int, elementCount );
7675
rows = CDnnBlob::CreateVector( mathEngine, CT_Int, height + 1 );
77-
values = CDnnBlob::CreateVector( mathEngine, CT_Float, desc.ElementCount );
76+
values = CDnnBlob::CreateVector( mathEngine, CT_Float, elementCount );
7877
if( data.Columns != nullptr ) {
7978
columns->CopyFrom( data.Columns );
8079
int* rowBuffer = rows->GetBuffer<int>( 0, height + 1, false );
@@ -84,7 +83,7 @@ static CSparseMatrixDesc getSparseMatrixDesc( IMathEngine& mathEngine, const CFl
8483
rowBuffer[height] = data.PointerE[height - 1];
8584
rows->ReleaseBuffer( rowBuffer, true );
8685
} else {
87-
desc.ElementCount = height * width;
86+
elementCount = height * width;
8887
int* colBuffer = columns->GetBuffer<int>( 0, height * width, false );
8988
int* rowBuffer = rows->GetBuffer<int>( 0, height + 1, false );
9089
for( int i = 0; i < height; i++ ) {
@@ -97,11 +96,8 @@ static CSparseMatrixDesc getSparseMatrixDesc( IMathEngine& mathEngine, const CFl
9796
rows->ReleaseBuffer( rowBuffer, true );
9897
columns->ReleaseBuffer( colBuffer, true );
9998
}
100-
desc.Columns = columns->GetData<int>();
10199
values->CopyFrom( data.Values );
102-
desc.Values = values->GetData<float>();
103-
desc.Rows = rows->GetData<int>();
104-
return desc;
100+
return CSparseMatrixDesc( elementCount, rows->GetData<int>(), columns->GetData<int>(), values->GetData<float>() );
105101
}
106102

107103
// copy blob with reduced width

NeoMathEngine/include/NeoMathEngine/SparseMatrixDesc.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright © 2017-2020 ABBYY Production LLC
1+
/* Copyright © 2017-2024 ABBYY
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -20,18 +20,19 @@ limitations under the License.
2020
namespace NeoML {
2121

2222
// Sparse matrix descriptor (CSR format)
23-
struct CSparseMatrixDesc {
23+
struct CSparseMatrixDesc final {
2424
// The number of elements
25-
int ElementCount;
25+
const int ElementCount;
2626
// This array of (matrix height + 1) length stores the indices where the new row starts, as (Columns + Values)
2727
// The last array element is equal to the number of elements in the matrix
28-
CIntHandle Rows;
28+
const CIntHandle Rows;
2929
// This array of ElementCount length contains the indices of the columns to which the corresponding values belong
30-
CIntHandle Columns;
30+
const CIntHandle Columns;
3131
// This array of ElementCount length contains the matrix elements' values
32-
CFloatHandle Values;
32+
const CFloatHandle Values;
3333

34-
CSparseMatrixDesc() : ElementCount( 0 ) {}
34+
CSparseMatrixDesc( int count, const CIntHandle& rows, const CIntHandle& columns, const CFloatHandle& values ) :
35+
ElementCount( count ), Rows( rows ), Columns( columns ), Values( values ) {}
3536
};
3637

3738
} // namespace NeoML

NeoMathEngine/test/src/common/TestFixture.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,7 @@ class CSparseMatrix final {
274274
mathEngine.DataExchangeTyped<float>( values, _values.data(), _values.size() );
275275
}
276276

277-
CSparseMatrixDesc Desc() const
278-
{
279-
CSparseMatrixDesc desc;
280-
desc.ElementCount = elementCount;
281-
desc.Rows = rows;
282-
desc.Columns = columns;
283-
desc.Values = values;
284-
return desc;
285-
}
277+
CSparseMatrixDesc Desc() const { return CSparseMatrixDesc( elementCount, rows, columns, values ); }
286278

287279
private:
288280
int elementCount;

0 commit comments

Comments
 (0)