LvArray
output.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Lawrence Livermore National Security, LLC and LvArray contributors.
3  * All rights reserved.
4  * See the LICENSE file for details.
5  * SPDX-License-Identifier: (BSD-3-Clause)
6  */
7 
13 #pragma once
14 
15 // Source includes
16 #include "Array.hpp"
17 #include "SortedArray.hpp"
18 #include "ArrayOfArrays.hpp"
19 #include "CRSMatrix.hpp"
20 #include "Macros.hpp"
21 #include "limits.hpp"
22 
23 // TPL includes
24 #include <RAJA/RAJA.hpp>
25 
26 // System includes
27 #include <string>
28 #include <iostream>
29 
30 #if defined( LVARRAY_USE_CUDA )
31  #include <cuda_fp16.h>
32 #endif
33 
34 namespace LvArray
35 {
36 
47 // Sphinx start after Array stream IO
48 template< typename T, int NDIM, int USD, typename INDEX_TYPE >
49 std::ostream & operator<<( std::ostream & stream,
51 {
52  stream << "{ ";
53 
54  if( slice.size( 0 ) > 0 )
55  {
56  stream << slice[ 0 ];
57  }
58 
59  for( INDEX_TYPE i = 1; i < slice.size( 0 ); ++i )
60  {
61  stream << ", " << slice[ i ];
62  }
63 
64  stream << " }";
65  return stream;
66 }
67 // Sphinx end before Array stream IO
68 
69 
81 template< typename T,
82  int NDIM,
83  int USD,
84  typename INDEX_TYPE,
85  template< typename > class BUFFER_TYPE >
86 std::ostream & operator<<( std::ostream & stream,
88 { return stream << view.toSliceConst(); }
89 
98 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
99 std::ostream & operator<< ( std::ostream & stream,
101 {
102  if( view.size() == 0 )
103  {
104  stream << "{}";
105  return stream;
106  }
107 
108  stream << "{ ";
109 
110  if( view.size() > 0 )
111  {
112  stream << view[ 0 ];
113  }
114 
115  for( INDEX_TYPE i = 1; i < view.size(); ++i )
116  {
117  stream << ", " << view[ i ];
118  }
119 
120  stream << " }";
121  return stream;
122 }
123 
132 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
133 std::ostream & operator<< ( std::ostream & stream,
135 { return stream << array.toViewConst(); }
136 
145 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
146 std::ostream & operator<< ( std::ostream & stream,
148 {
149  stream << "{" << std::endl;
150 
151  for( INDEX_TYPE i = 0; i < view.size(); ++i )
152  {
153  stream << i << "\t{";
154  for( INDEX_TYPE j = 0; j < view.sizeOfArray( i ); ++j )
155  {
156  stream << view( i, j ) << ", ";
157  }
158 
159  stream << "}" << std::endl;
160  }
161 
162  stream << "}" << std::endl;
163  return stream;
164 }
165 
174 template< typename T, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
175 std::ostream & operator<< ( std::ostream & stream,
177 { return stream << array.toViewConst(); }
178 
187 template< typename T, typename COL_TYPE, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
188 std::ostream & operator<< ( std::ostream & stream, CRSMatrixView< T const, COL_TYPE const, INDEX_TYPE const, BUFFER_TYPE > const & view )
189 {
190  stream << "{" << std::endl;
191 
192  for( INDEX_TYPE row = 0; row < view.numRows(); ++row )
193  {
194  stream << "row " << row << std::endl;
195  stream << "\tcolumns: " << view.getColumns( row ) << std::endl;
196  stream << "\tvalues: " << view.getEntries( row ) << std::endl;
197  }
198 
199  stream << "}" << std::endl;
200  return stream;
201 }
202 
203 
213 template< typename POLICY, typename T, typename COL_TYPE, typename INDEX_TYPE, template< typename > class BUFFER_TYPE >
215 {
216  INDEX_TYPE const numRows = view.numRows();
217 
218  printf( "numRows = %4lld \n", integerConversion< long long >( numRows ) );
219  RAJA::forall< POLICY >( RAJA::TypedRangeSegment< INDEX_TYPE >( 0, 1 ), [=] LVARRAY_HOST_DEVICE ( INDEX_TYPE const )
220  {
221  INDEX_TYPE const * const ncols = view.getSizes();
222  INDEX_TYPE const * const row_indexes = view.getOffsets();
223  COL_TYPE const * const cols = view.getColumns();
224  T const * const values = view.getEntries();
225 
226  printf( "ncols = { " );
227  for( INDEX_TYPE i = 0; i < numRows; ++i )
228  {
229  printf( "%4lld, ", integerConversion< long long >( ncols[ i ] ) );
230  }
231  printf( " }\n" );
232 
233  printf( "row_indexes = { " );
234  for( INDEX_TYPE i = 0; i < numRows + 1; ++i )
235  {
236  printf( "%4lld, ", integerConversion< long long >( row_indexes[ i ] ) );
237  }
238  printf( " }\n" );
239 
240  printf( "row col value \n" );
241  printf( "---- --------- --------- \n" );
242  for( INDEX_TYPE i = 0; i < numRows; ++i )
243  {
244  printf( "%4lld\n", integerConversion< long long >( ncols[ i ] ) );
245  for( INDEX_TYPE j = 0; j < ncols[ i ]; ++j )
246  {
247  printf( "%4lld %9lld %9.2g\n",
248  integerConversion< long long >( i ),
249  integerConversion< long long >( cols[ row_indexes[ i ] + j ] ),
250  double( values[ row_indexes[ i ] + j ] ) );
251  }
252  }
253  } );
254 
255  std::cout << std::endl;
256 }
257 
266 template< typename T, int N >
267 std::enable_if_t< !std::is_same< T, char >::value, std::ostream & >
268 operator<< ( std::ostream & stream, T const ( &array )[ N ] )
269 {
270  stream << "{ " << array[ 0 ];
271  for( int i = 1; i < N; ++i )
272  {
273  stream << ", " << array[ i ];
274  }
275  stream << " }";
276  return stream;
277 }
278 
288 template< typename T, int M, int N >
289 std::ostream & operator<< ( std::ostream & stream, T const ( &array )[ M ][ N ] )
290 {
291  stream << "{ " << array[ 0 ];
292  for( int i = 1; i < M; ++i )
293  {
294  stream << ", " << array[ i ];
295  }
296  stream << " }";
297  return stream;
298 }
299 
300 } // namespace LvArray
301 
302 #if defined( LVARRAY_USE_CUDA )
303 
310 inline std::ostream & operator<<( std::ostream & stream, __half const x )
311 { return stream << float( x ); }
312 
319 inline std::ostream & operator<<( std::ostream & stream, __half2 const x )
320 {
321  float2 const c = __half22float2( x );
322  return stream << c.x << ", " << c.y;
323 }
324 
325 #endif
LVARRAY_HOST_DEVICE constexpr ArraySlice< T const, NDIM, USD, INDEX_TYPE > toSliceConst() const &noexcept
Definition: ArrayView.hpp:327
Contains the implementation of LvArray::ArrayOfArrays.
This class serves to provide a sliced multidimensional interface to the family of LvArray classes...
Definition: ArraySlice.hpp:89
This class provides an interface similar to an std::set.
Definition: SortedArray.hpp:35
void print(CRSMatrixView< T, COL_TYPE const, INDEX_TYPE const, BUFFER_TYPE > const &view)
Print a CRSMatrixView in a format that can be easily xxdiff&#39;ed on the console.
Definition: output.hpp:214
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE size() const noexcept
Definition: ArraySlice.hpp:170
Contains portable access to std::numeric_limits and functions for converting between integral types...
This class implements an array of arrays like object with contiguous storage.
Definition: ArrayOfArrays.hpp:33
This class serves to provide a "view" of a multidimensional array.
Definition: ArrayView.hpp:68
Contains the implementation of LvArray::Array.
This class provides a view into an array of arrays like object.
Definition: ArrayOfArraysView.hpp:170
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC const numRows() const
Definition: SparsityPatternView.hpp:197
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK INDEX_TYPE_NC sizeOfArray(INDEX_TYPE const i) const
Definition: ArrayOfArraysView.hpp:332
Contains the implementation of LvArray::SortedArray.
Contains the implementation of LvArray::CRSMatrix.
The top level namespace.
Definition: Array.hpp:24
LVARRAY_HOST_DEVICE SortedArrayView< T const, INDEX_TYPE, BUFFER_TYPE > toViewConst() const &
Definition: SortedArray.hpp:155
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE const * getOffsets() const
Definition: SparsityPatternView.hpp:289
Contains a bunch of macro definitions.
std::ostream & operator<<(std::ostream &os, MemorySpace const space)
Output a Platform enum to a stream.
Definition: bufferManipulation.hpp:39
LVARRAY_HOST_DEVICE constexpr ArraySlice< COL_TYPE const, 1, 0, INDEX_TYPE_NC > getColumns(INDEX_TYPE const row) const
Definition: SparsityPatternView.hpp:282
LVARRAY_HOST_DEVICE ArraySlice< T, 1, 0, INDEX_TYPE_NC > getEntries(INDEX_TYPE const row) const
Definition: CRSMatrixView.hpp:244
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE_NC size() const
Definition: ArrayOfArraysView.hpp:324
LVARRAY_HOST_DEVICE constexpr SIZE_TYPE const * getSizes() const
Definition: ArrayOfArraysView.hpp:342
This class provides a view into a SortedArray.
Definition: SortedArrayView.hpp:58
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.
Definition: Macros.hpp:600
LVARRAY_HOST_DEVICE constexpr INDEX_TYPE size() const
Definition: SortedArrayView.hpp:188
This class provides a view into a compressed row storage matrix.
Definition: CRSMatrixView.hpp:75
constexpr ArrayOfArraysView< T const, INDEX_TYPE const, true, BUFFER_TYPE > toViewConst() const &
Definition: ArrayOfArrays.hpp:142