From 40db8e95f91a0c69ca485d45f7565cb33ac1d2ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Mon, 9 Dec 2019 22:17:47 +0100
Subject: [PATCH] Implementing SparseMatrix::getNumberOfNonzeroMatrixElements.

---
 src/TNL/Matrices/SparseMatrix.h   | 2 +-
 src/TNL/Matrices/SparseMatrix.hpp | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h
index 6b6a58f9a0..b510636f55 100644
--- a/src/TNL/Matrices/SparseMatrix.h
+++ b/src/TNL/Matrices/SparseMatrix.h
@@ -17,7 +17,7 @@ namespace TNL {
 namespace Matrices {
 
 template< typename Real,
-          template< typename, typename > class Segments,
+          template< typename Device_, typename Index_ > class Segments,
           typename Device = Devices::Host,
           typename Index = int,
           typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >,
diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp
index 37f59c0580..1c243bcea8 100644
--- a/src/TNL/Matrices/SparseMatrix.hpp
+++ b/src/TNL/Matrices/SparseMatrix.hpp
@@ -10,7 +10,9 @@
 
 #pragma once
 
+#include <functional>
 #include <TNL/Matrices/SparseMatrix.h>
+#include <TNL/Algorithms/Reduction.h>
 
 namespace TNL {
 namespace Matrices {
@@ -192,6 +194,12 @@ Index
 SparseMatrix< Real, Segments, Device, Index, RealAllocator, IndexAllocator >::
 getNumberOfNonzeroMatrixElements() const
 {
+   const auto columns_view = this->columnIndexes.getConstView();
+   const IndexType paddingIndex = this->getPaddingIndex();
+   auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType {
+      return ( columns_view[ i ] != paddingIndex );
+   };
+   return Algorithms::Reduction< DeviceType >::reduce( this->columnIndexes.getSize(), std::plus<>{}, fetch, 0 );
 }
 
 template< typename Real,
-- 
GitLab