VTK-m  2.0
ZFPBlockWriter.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_worklet_zfp_block_writer_h
11 #define vtk_m_worklet_zfp_block_writer_h
12 
14 
15 namespace vtkm
16 {
17 namespace worklet
18 {
19 namespace zfp
20 {
21 
22 using Word = vtkm::UInt64;
23 
24 template <int block_size, typename AtomicPortalType>
26 {
27  union UIntInt {
28  vtkm::UInt64 uintpart;
29  vtkm::Int64 intpart;
30  };
31 
35  const int m_maxbits;
36  AtomicPortalType& Portal;
37 
38  VTKM_EXEC BlockWriter(AtomicPortalType& portal, const int& maxbits, const vtkm::Id& block_idx)
39  : m_current_bit(0)
40  , m_maxbits(maxbits)
41  , Portal(portal)
42  {
43  m_word_index = (block_idx * maxbits) / vtkm::Int32(sizeof(Word) * 8);
44  m_start_bit = vtkm::Int32((block_idx * maxbits) % vtkm::Int32(sizeof(Word) * 8));
45  }
46 
47  inline VTKM_EXEC void Add(const vtkm::Id index, Word& value)
48  {
49  UIntInt newval;
50  UIntInt old;
51  (void)old;
52  newval.uintpart = value;
53  Portal.Add(index, newval.intpart);
54  }
55 
56  inline VTKM_EXEC vtkm::UInt64 write_bits(const vtkm::UInt64& bits, const unsigned int& n_bits)
57  {
58  const int wbits = sizeof(Word) * 8;
59  unsigned int seg_start = (m_start_bit + m_current_bit) % wbits;
60  vtkm::Id write_index = m_word_index;
61  write_index += vtkm::Id((m_start_bit + m_current_bit) / wbits);
62  unsigned int seg_end = seg_start + n_bits - 1;
63  //int write_index = m_word_index;
64  unsigned int shift = seg_start;
65  // we may be asked to write less bits than exist in 'bits'
66  // so we have to make sure that anything after n is zero.
67  // If this does not happen, then we may write into a zfp
68  // block not at the specified index
69  // uint zero_shift = sizeof(Word) * 8 - n_bits;
70  Word left = (bits >> n_bits) << n_bits;
71 
72  Word b = bits - left;
73  Word add = b << shift;
74  Add(write_index, add);
75 
76  // n_bits straddles the word boundary
77  bool straddle = seg_start < sizeof(Word) * 8 && seg_end >= sizeof(Word) * 8;
78  if (straddle)
79  {
80  Word rem = b >> (sizeof(Word) * 8 - shift);
81  Add(write_index + 1, rem);
82  }
83  m_current_bit += n_bits;
84  return bits >> (Word)n_bits;
85  }
86 
87  // TODO: optimize
88  vtkm::UInt32 VTKM_EXEC write_bit(const unsigned int& bit)
89  {
90  const int wbits = sizeof(Word) * 8;
91  unsigned int seg_start = (m_start_bit + m_current_bit) % wbits;
92  vtkm::Id write_index = m_word_index;
93  write_index += vtkm::Id((m_start_bit + m_current_bit) / wbits);
94  unsigned int shift = seg_start;
95  // we may be asked to write less bits than exist in 'bits'
96  // so we have to make sure that anything after n is zero.
97  // If this does not happen, then we may write into a zfp
98  // block not at the specified index
99  // uint zero_shift = sizeof(Word) * 8 - n_bits;
100 
101  Word add = (Word)bit << shift;
102  Add(write_index, add);
103  m_current_bit += 1;
104 
105  return bit;
106  }
107 };
108 
109 } // namespace zfp
110 } // namespace worklet
111 } // namespace vtkm
112 #endif // vtk_m_worklet_zfp_block_writer_h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
ZFPTypeInfo.h
vtkm::worklet::zfp::BlockWriter::Portal
AtomicPortalType & Portal
Definition: ZFPBlockWriter.h:36
vtkm::worklet::zfp::BlockWriter::m_maxbits
const int m_maxbits
Definition: ZFPBlockWriter.h:35
vtkm::worklet::zfp::BlockWriter::m_current_bit
vtkm::Int32 m_current_bit
Definition: ZFPBlockWriter.h:34
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::worklet::zfp::BlockWriter::BlockWriter
VTKM_EXEC BlockWriter(AtomicPortalType &portal, const int &maxbits, const vtkm::Id &block_idx)
Definition: ZFPBlockWriter.h:38
vtkm::worklet::zfp::BlockWriter::UIntInt::intpart
vtkm::Int64 intpart
Definition: ZFPBlockWriter.h:29
vtkm::worklet::zfp::BlockWriter::m_start_bit
vtkm::Int32 m_start_bit
Definition: ZFPBlockWriter.h:33
vtkm::worklet::zfp::Word
vtkm::UInt64 Word
Definition: ZFPBlockReader.h:22
vtkm::worklet::zfp::BlockWriter::write_bits
VTKM_EXEC vtkm::UInt64 write_bits(const vtkm::UInt64 &bits, const unsigned int &n_bits)
Definition: ZFPBlockWriter.h:56
vtkm::worklet::zfp::BlockWriter::write_bit
vtkm::UInt32 VTKM_EXEC write_bit(const unsigned int &bit)
Definition: ZFPBlockWriter.h:88
vtkm::worklet::zfp::BlockWriter::UIntInt::uintpart
vtkm::UInt64 uintpart
Definition: ZFPBlockWriter.h:28
vtkm::worklet::zfp::BlockWriter::UIntInt
Definition: ZFPBlockWriter.h:27
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::worklet::zfp::BlockWriter
Definition: ZFPBlockWriter.h:25
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::worklet::zfp::BlockWriter::m_word_index
vtkm::Id m_word_index
Definition: ZFPBlockWriter.h:32
vtkm::worklet::zfp::BlockWriter::Add
VTKM_EXEC void Add(const vtkm::Id index, Word &value)
Definition: ZFPBlockWriter.h:47