Utility.h 3.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can 
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of 
//  the License, or (at your option) any later version.
//  
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT 
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
//  for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file utility.h
//! \ingroup blockforest
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#pragma once

#include "Types.h"

#include "core/EndianIndependentSerialization.h"
#include "core/math/KahanSummation.h"
#include "core/math/Uint.h"

#include <limits>
#include <ostream>
#include <string>
#include <vector>


namespace walberla {
namespace blockforest {


using math::uintPow2;
using math::uintMSBPosition;


//**********************************************************************************************************************
/*!
*   \brief Returns a string that stores the bitwise representation of 'value' (must be an unsigned integer)
*
48
*   \code{.unparsed}
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
*      8bit display: 0101_1101
*     16bit display: 1110_0101.1100_0001
*     32bit display: 1000_0011.0110_1101.0000_0001.1010_0110
*     64bit analogously ...
*   \endcode
*/
//**********************************************************************************************************************
template< typename UINT > std::string uintToBitString( const UINT value ) {

   static_assert_uint_t< UINT >();

   std::string bitsString;

   UINT exp = static_cast< UINT >( std::numeric_limits< UINT >::digits ) - 1;

   static const uint_t bytes = uint_c( std::numeric_limits< UINT >::digits ) >> 3;

   for( uint_t i = 0; i != bytes; ++i ) {

      for( uint_t j = 0; j != 4; ++j, --exp )
         bitsString.push_back( (value & uintPow2(exp)) ? '1' : '0' );

      bitsString.push_back('_');

      for( uint_t j = 0; j != 4; ++j, --exp )
         bitsString.push_back( (value & uintPow2(exp)) ? '1' : '0' );

      if( i != bytes-1 ) bitsString.append(".");
   }

   return bitsString;
}



template< typename T >
workload_t workloadSum( const T& array )
{
   math::KahanAccumulator< workload_t > acc;

   for( uint_t i = 0; i < array.size(); ++i )
      acc += array[i]->getWorkload();

   return acc.get();
}

template< typename T >
memory_t memorySum( const T& array )
{
   math::KahanAccumulator< memory_t > acc;

   for( uint_t i = 0; i < array.size(); ++i )
      acc += array[i]->getMemory();

   return acc.get();
}



inline memory_t bytesToMiB( memory_t bytes ) {
   return bytes / static_cast< memory_t >( 1024.0 * 1024.0 );
}



//**********************************************************************************************************************
/*!
*   \brief Returns the string representation of 'number', every three digits the character 'separator' is inserted
*          (172408725 -> "172 408 725")
*/
//**********************************************************************************************************************
std::string naturalNumberToGroupedThousandsString( const uint_t number, const char separator = ' ' );

inline std::string naturalNumberToGroupedThousandsString( const real_t number, const char separator = ' ' ) {

   return naturalNumberToGroupedThousandsString( static_cast< uint_t >( 0.5 + number ), separator );
}



inline void fillStream( std::ostream& ostream, const char fill, uint_t length ) {
   while( length-- > 0 ) ostream << fill;
}



} // namespace blockforest
} // namespace walberla