vtk-m2/vtkm/worklet/ZFPCompressor.h

122 lines
4.4 KiB
C
Raw Normal View History

2018-07-20 22:29:14 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_worklet_zfp_compressor_h
#define vtk_m_worklet_zfp_compressor_h
#include <vtkm/Math.h>
2018-10-27 15:18:23 +00:00
#include <vtkm/cont/Algorithm.h>
2018-07-20 22:29:14 +00:00
#include <vtkm/cont/ArrayHandle.h>
2018-10-27 15:18:23 +00:00
#include <vtkm/cont/ArrayHandleConstant.h>
2018-07-20 22:29:14 +00:00
#include <vtkm/cont/ArrayHandleCounting.h>
2018-08-27 16:00:46 +00:00
#include <vtkm/cont/AtomicArray.h>
2018-08-31 15:50:51 +00:00
#include <vtkm/cont/Timer.h>
2018-07-20 22:29:14 +00:00
#include <vtkm/worklet/DispatcherMapField.h>
2018-10-29 18:12:35 +00:00
#include <vtkm/worklet/zfp/ZFPEncode3.h>
2018-12-07 17:02:57 +00:00
#include <vtkm/worklet/zfp/ZFPTools.h>
2018-08-27 16:00:46 +00:00
2018-07-20 22:29:14 +00:00
using ZFPWord = vtkm::UInt64;
2018-08-27 16:00:46 +00:00
#include <stdio.h>
2018-07-20 22:29:14 +00:00
namespace vtkm
{
namespace worklet
{
2018-08-27 16:00:46 +00:00
2018-07-20 22:29:14 +00:00
class ZFPCompressor
{
public:
2018-11-26 22:30:30 +00:00
template <typename Scalar>
2018-11-27 19:05:55 +00:00
vtkm::cont::ArrayHandle<vtkm::Int64> Compress(const vtkm::cont::ArrayHandle<Scalar>& data,
const vtkm::Float64 requestedRate,
const vtkm::Id3 dims)
2018-07-20 22:29:14 +00:00
{
2018-09-13 03:19:24 +00:00
DataDump(data, "uncompressed");
zfp::ZFPStream stream;
2018-07-20 22:29:14 +00:00
const vtkm::Int32 topoDims = 3;
vtkm::Float64 actualRate = stream.SetRate(requestedRate, topoDims, vtkm::Float64());
//VTKM_ASSERT(
std::cout << "ArraySize " << data.GetNumberOfValues() << "\n";
std::cout << "Array dims " << dims << "\n";
std::cout << "requested rate " << requestedRate << " actual rate " << actualRate << "\n";
std::cout << "MinBits " << stream.minbits << "\n";
// Check to see if we need to increase the block sizes
// in the case where dim[x] is not a multiple of 4
vtkm::Id3 paddedDims = dims;
// ensure that we have block sizes
// that are a multiple of 4
if (paddedDims[0] % 4 != 0)
paddedDims[0] += 4 - dims[0] % 4;
if (paddedDims[1] % 4 != 0)
paddedDims[1] += 4 - dims[1] % 4;
if (paddedDims[2] % 4 != 0)
paddedDims[2] += 4 - dims[2] % 4;
2018-08-27 16:00:46 +00:00
const vtkm::Id four = 4;
vtkm::Id totalBlocks =
(paddedDims[0] / four) * (paddedDims[1] / (four) * (paddedDims[2] / four));
2018-07-20 22:29:14 +00:00
std::cout << "Padded dims " << paddedDims << "\n";
2018-10-29 18:12:35 +00:00
size_t outbits = detail::CalcMem3d(paddedDims, stream.minbits);
2018-07-20 22:29:14 +00:00
std::cout << "Total output bits " << outbits << "\n";
vtkm::Id outsize = outbits / sizeof(ZFPWord);
std::cout << "Output size " << outsize << "\n";
2018-08-27 16:00:46 +00:00
vtkm::cont::ArrayHandle<vtkm::Int64> output;
2018-10-27 15:18:23 +00:00
// hopefully this inits/allocates the mem only on the device
vtkm::cont::ArrayHandleConstant<vtkm::Int64> zero(0, outsize);
vtkm::cont::Algorithm::Copy(zero, output);
using Timer = vtkm::cont::Timer<vtkm::cont::DeviceAdapterTagSerial>;
2018-08-31 19:40:36 +00:00
{
2018-10-27 15:18:23 +00:00
Timer timer;
2018-08-31 19:40:36 +00:00
vtkm::cont::ArrayHandleCounting<vtkm::Id> one(0, 1, 1);
vtkm::worklet::DispatcherMapField<detail::MemTransfer> dis;
dis.Invoke(one, data);
vtkm::Float64 time = timer.GetElapsedTime();
std::cout << "Copy scalars " << time << "\n";
}
2018-08-27 16:00:46 +00:00
// launch 1 thread per zfp block
2018-07-20 22:29:14 +00:00
vtkm::cont::ArrayHandleCounting<vtkm::Id> blockCounter(0, 1, totalBlocks);
2018-10-27 15:18:23 +00:00
Timer timer;
2018-10-29 18:12:35 +00:00
vtkm::worklet::DispatcherMapField<zfp::Encode3> compressDispatcher(
zfp::Encode3(dims, paddedDims, stream.maxbits));
2018-07-20 22:29:14 +00:00
compressDispatcher.Invoke(blockCounter, data, output);
2018-10-27 15:18:23 +00:00
2018-08-31 15:50:51 +00:00
vtkm::Float64 time = timer.GetElapsedTime();
2018-08-31 19:40:36 +00:00
size_t total_bytes = data.GetNumberOfValues() * sizeof(vtkm::Float64);
vtkm::Float64 gB = vtkm::Float64(total_bytes) / (1024. * 1024. * 1024.);
vtkm::Float64 rate = gB / time;
std::cout << "Compress time " << time << " sec\n";
std::cout << "Compress rate " << rate << " GB / sec\n";
2018-08-27 16:00:46 +00:00
DataDump(output, "compressed");
2018-11-27 19:05:55 +00:00
return output;
2018-07-20 22:29:14 +00:00
}
};
} // namespace worklet
} // namespace vtkm
#endif // vtk_m_worklet_zfp_compressor_h