vtk-m/vtkm/worklet/testing/UnitTestZFPCompressor.cxx

184 lines
5.8 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.
//============================================================================
#include <vtkm/worklet/ZFPCompressor.h>
2018-11-27 19:05:55 +00:00
#include <vtkm/worklet/ZFPDecompress.h>
2018-07-20 22:29:14 +00:00
2018-12-07 21:02:16 +00:00
#include <vtkm/worklet/ZFP1DCompressor.h>
#include <vtkm/worklet/ZFP1DDecompress.h>
2018-12-07 17:02:57 +00:00
#include <vtkm/worklet/ZFP2DCompressor.h>
#include <vtkm/worklet/ZFP2DDecompress.h>
2018-07-20 22:29:14 +00:00
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
2018-12-11 01:58:00 +00:00
#include <vtkm/worklet/zfp/ZFPTools.h>
2018-07-20 22:29:14 +00:00
2018-12-18 00:48:57 +00:00
#include <algorithm>
2018-12-11 01:58:00 +00:00
#include <fstream>
2018-07-20 22:29:14 +00:00
#include <iostream>
2018-12-18 00:48:57 +00:00
2018-12-11 01:58:00 +00:00
template <typename T>
void writeArray(vtkm::cont::ArrayHandle<T>& field, std::string filename)
{
auto val = vtkm::worklet::zfp::detail::GetVTKMPointer(field);
2018-12-11 01:58:00 +00:00
std::ofstream output(filename, std::ios::binary | std::ios::out);
2018-12-11 18:07:44 +00:00
output.write(reinterpret_cast<char*>(val), field.GetNumberOfValues() * 8);
2018-12-11 01:58:00 +00:00
output.close();
for (int i = 0; i < field.GetNumberOfValues(); i++)
{
std::cout << val[i] << " ";
}
std::cout << std::endl;
}
2018-07-20 22:29:14 +00:00
using Handle64 = vtkm::cont::ArrayHandle<vtkm::Float64>;
2018-12-07 21:02:16 +00:00
template <typename Scalar>
void Test1D(int rate)
{
std::cout << "Testing ZFP 1d:" << std::endl;
2018-12-11 01:58:00 +00:00
vtkm::Id dims = 256;
2018-12-07 21:02:16 +00:00
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataset = testDataSet.Make1DUniformDataSet2();
auto dynField = dataset.GetField("pointvar").GetData();
2018-12-07 21:02:16 +00:00
vtkm::worklet::ZFP1DCompressor compressor;
vtkm::worklet::ZFP1DDecompressor decompressor;
if (vtkm::cont::IsType<Handle64>(dynField))
2018-12-07 21:02:16 +00:00
{
vtkm::cont::ArrayHandle<Scalar> handle;
2018-12-11 18:07:44 +00:00
const vtkm::Id size = dynField.Cast<Handle64>().GetNumberOfValues();
2018-12-07 21:02:16 +00:00
handle.Allocate(size);
2018-07-20 22:29:14 +00:00
2018-12-11 18:07:44 +00:00
auto fPortal = dynField.Cast<Handle64>().GetPortalControl();
2018-12-07 21:02:16 +00:00
auto hPortal = handle.GetPortalControl();
for (vtkm::Id i = 0; i < size; ++i)
{
hPortal.Set(i, static_cast<Scalar>(fPortal.Get(i)));
}
auto compressed = compressor.Compress(handle, rate, dims);
2018-12-11 18:07:44 +00:00
//writeArray(compressed, "output.zfp");
2018-12-07 21:02:16 +00:00
vtkm::cont::ArrayHandle<Scalar> decoded;
decompressor.Decompress(compressed, decoded, rate, dims);
2018-12-18 00:48:57 +00:00
auto oport = decoded.GetPortalConstControl();
for (int i = 0; i < 4; i++)
{
std::cout << oport.Get(i) << " " << fPortal.Get(i) << " " << oport.Get(i) - fPortal.Get(i)
<< std::endl;
}
2018-12-07 21:02:16 +00:00
}
}
2018-12-07 17:02:57 +00:00
template <typename Scalar>
void Test2D(int rate)
{
std::cout << "Testing ZFP 2d:" << std::endl;
2018-12-11 01:58:00 +00:00
vtkm::Id2 dims(16, 16);
2018-12-07 17:02:57 +00:00
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataset = testDataSet.Make2DUniformDataSet2();
auto dynField = dataset.GetField("pointvar").GetData();
vtkm::worklet::ZFP2DCompressor compressor;
vtkm::worklet::ZFP2DDecompressor decompressor;
if (vtkm::cont::IsType<Handle64>(dynField))
2018-12-07 17:02:57 +00:00
{
vtkm::cont::ArrayHandle<Scalar> handle;
2018-12-11 18:07:44 +00:00
const vtkm::Id size = dynField.Cast<Handle64>().GetNumberOfValues();
2018-12-07 17:02:57 +00:00
handle.Allocate(size);
2018-12-11 18:07:44 +00:00
auto fPortal = dynField.Cast<Handle64>().GetPortalControl();
2018-12-07 17:02:57 +00:00
auto hPortal = handle.GetPortalControl();
for (vtkm::Id i = 0; i < size; ++i)
{
hPortal.Set(i, static_cast<Scalar>(fPortal.Get(i)));
}
auto compressed = compressor.Compress(handle, rate, dims);
vtkm::cont::ArrayHandle<Scalar> decoded;
decompressor.Decompress(compressed, decoded, rate, dims);
2018-12-18 00:48:57 +00:00
auto oport = decoded.GetPortalConstControl();
for (int i = 0; i < 4; i++)
{
std::cout << oport.Get(i) << " " << fPortal.Get(i) << " " << oport.Get(i) - fPortal.Get(i)
<< std::endl;
}
2018-12-07 17:02:57 +00:00
}
}
2018-11-26 22:30:30 +00:00
template <typename Scalar>
void Test3D(int rate)
2018-07-20 22:29:14 +00:00
{
std::cout << "Testing ZFP 3d:" << std::endl;
2018-11-28 22:11:50 +00:00
vtkm::Id3 dims(4, 4, 4);
2018-11-26 22:30:30 +00:00
//vtkm::Id3 dims(4,4,7);
2018-11-28 22:11:50 +00:00
//vtkm::Id3 dims(8,8,8);
2018-10-29 18:12:35 +00:00
//vtkm::Id3 dims(256,256,256);
2018-08-31 15:50:51 +00:00
//vtkm::Id3 dims(128,128,128);
2018-07-20 22:29:14 +00:00
vtkm::cont::testing::MakeTestDataSet testDataSet;
vtkm::cont::DataSet dataset = testDataSet.Make3DUniformDataSet3(dims);
auto dynField = dataset.GetField("pointvar").GetData();
2018-10-27 15:18:23 +00:00
vtkm::worklet::ZFPCompressor compressor;
2018-11-27 19:05:55 +00:00
vtkm::worklet::ZFPDecompressor decompressor;
2018-07-20 22:29:14 +00:00
if (vtkm::cont::IsType<Handle64>(dynField))
2018-07-20 22:29:14 +00:00
{
2018-11-26 22:30:30 +00:00
vtkm::cont::ArrayHandle<Scalar> handle;
2018-12-11 18:07:44 +00:00
const vtkm::Id size = dynField.Cast<Handle64>().GetNumberOfValues();
2018-11-26 22:30:30 +00:00
handle.Allocate(size);
2018-12-11 18:07:44 +00:00
auto fPortal = dynField.Cast<Handle64>().GetPortalControl();
2018-11-26 22:30:30 +00:00
auto hPortal = handle.GetPortalControl();
for (vtkm::Id i = 0; i < size; ++i)
2018-08-27 16:00:46 +00:00
{
2018-11-26 22:30:30 +00:00
hPortal.Set(i, static_cast<Scalar>(fPortal.Get(i)));
2018-08-27 16:00:46 +00:00
}
2018-11-27 19:05:55 +00:00
auto compressed = compressor.Compress(handle, rate, dims);
vtkm::cont::ArrayHandle<Scalar> decoded;
decompressor.Decompress(compressed, decoded, rate, dims);
2018-12-18 00:48:57 +00:00
auto oport = decoded.GetPortalConstControl();
for (int i = 0; i < 4; i++)
{
std::cout << oport.Get(i) << " " << fPortal.Get(i) << " " << oport.Get(i) - fPortal.Get(i)
<< std::endl;
}
2018-07-20 22:29:14 +00:00
}
}
void TestZFP()
{
2018-12-11 18:07:44 +00:00
Test3D<vtkm::Float64>(4);
Test2D<vtkm::Float64>(4);
2018-12-11 01:58:00 +00:00
Test1D<vtkm::Float64>(4);
2018-11-28 22:11:50 +00:00
//Test3D<vtkm::Float32>(4);
2018-11-26 22:30:30 +00:00
//Test3D<vtkm::Int64>(4);
//Test3D<vtkm::Int32>(4);
2018-07-20 22:29:14 +00:00
}
2018-12-18 00:48:57 +00:00
int UnitTestZFPCompressor(int argc, char* argv[])
2018-07-20 22:29:14 +00:00
{
2018-12-18 00:48:57 +00:00
return vtkm::cont::testing::Testing::Run(TestZFP, argc, argv);
2018-07-20 22:29:14 +00:00
}