From 8c2f7758309223c84d75cf2b72fd4e6be6325104 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Sat, 10 Apr 2021 12:39:32 +0200
Subject: [PATCH] Added examples for segments printing.

---
 .../Segments/SegmentsPrintingExample-1.cpp    | 49 +++++++++++++
 .../Segments/SegmentsPrintingExample-1.cu     |  1 +
 .../Segments/SegmentsPrintingExample-2.cpp    | 68 +++++++++++++++++++
 .../Segments/SegmentsPrintingExample-2.cu     |  1 +
 4 files changed, 119 insertions(+)
 create mode 100644 Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cpp
 create mode 120000 Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cu
 create mode 100644 Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cpp
 create mode 120000 Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cu

diff --git a/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cpp b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cpp
new file mode 100644
index 0000000000..f5fc25337c
--- /dev/null
+++ b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cpp
@@ -0,0 +1,49 @@
+#include <iostream>
+#include <functional>
+#include <TNL/Containers/Vector.h>
+#include <TNL/Algorithms/Segments/CSR.h>
+#include <TNL/Algorithms/Segments/Ellpack.h>
+#include <TNL/Algorithms/Segments/ChunkedEllpack.h>
+#include <TNL/Algorithms/Segments/BiEllpack.h>
+#include <TNL/Devices/Host.h>
+#include <TNL/Devices/Cuda.h>
+
+template< typename Segments >
+void SegmentsExample()
+{
+   /***
+    * Create segments with given segments sizes and print their setup.
+    */
+   Segments segments{ 1, 2, 3, 4, 5 };
+   std::cout << "Segments sizes are: " << segments << std::endl << std::endl;
+}
+
+int main( int argc, char* argv[] )
+{
+   std::cout << "Example of CSR segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of Ellpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of BiEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Host, int > >();
+
+#ifdef HAVE_CUDA
+   std::cout << "Example of CSR segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of Ellpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of BiEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Cuda, int > >();
+#endif
+   return EXIT_SUCCESS;
+}
diff --git a/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cu b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cu
new file mode 120000
index 0000000000..42cd3852fc
--- /dev/null
+++ b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-1.cu
@@ -0,0 +1 @@
+SegmentsPrintingExample-1.cpp
\ No newline at end of file
diff --git a/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cpp b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cpp
new file mode 100644
index 0000000000..ceaff0ecd9
--- /dev/null
+++ b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cpp
@@ -0,0 +1,68 @@
+#include <iostream>
+#include <functional>
+#include <TNL/Containers/Vector.h>
+#include <TNL/Algorithms/Segments/CSR.h>
+#include <TNL/Algorithms/Segments/Ellpack.h>
+#include <TNL/Algorithms/Segments/ChunkedEllpack.h>
+#include <TNL/Algorithms/Segments/BiEllpack.h>
+#include <TNL/Devices/Host.h>
+#include <TNL/Devices/Cuda.h>
+
+template< typename Segments >
+void SegmentsExample()
+{
+   using Device = typename Segments::DeviceType;
+
+   /***
+    * Create segments with given segments sizes.
+    */
+   TNL::Containers::Vector< int, Device > sizes{ 1, 2, 3, 4, 5 };
+   Segments segments( sizes );
+   std::cout << "Segments sizes are: " << segments << std::endl;
+
+   /***
+    * Allocate array for the segments;
+    */
+   TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
+   data.forAllElements( [=] __cuda_callable__ ( int idx, double& value ) {
+      value = idx + 1.0;
+   } );
+
+   /***
+    * Print the data managed by the segments.
+    */
+   auto data_view = data.getView();
+   auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
+   printSegments( segments, fetch, std::cout );
+   std::cout << std::endl;
+}
+
+int main( int argc, char* argv[] )
+{
+   std::cout << "Example of CSR segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of Ellpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Host, int > >();
+
+   std::cout << "Example of BiEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Host, int > >();
+
+#ifdef HAVE_CUDA
+   std::cout << "Example of CSR segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of Ellpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Cuda, int > >();
+
+   std::cout << "Example of BiEllpack segments on host: " << std::endl;
+   SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Cuda, int > >();
+#endif
+   return EXIT_SUCCESS;
+}
diff --git a/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cu b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cu
new file mode 120000
index 0000000000..2f3149802f
--- /dev/null
+++ b/Documentation/Examples/Algorithms/Segments/SegmentsPrintingExample-2.cu
@@ -0,0 +1 @@
+SegmentsPrintingExample-2.cpp
\ No newline at end of file
-- 
GitLab