Skip to content

Commit

Permalink
Merge pull request #2873 from JasonRuonanWang/operator
Browse files Browse the repository at this point in the history
Simplify operator classes
  • Loading branch information
JasonRuonanWang authored Sep 20, 2021
2 parents 254b6e8 + 7b4426a commit 8b2d2ab
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 55 deletions.
6 changes: 3 additions & 3 deletions source/adios2/helper/adiosMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ namespace adios2
namespace helper
{

size_t GetTotalSize(const Dims &dimensions) noexcept
size_t GetTotalSize(const Dims &dimensions, const size_t elementSize) noexcept
{
return std::accumulate(dimensions.begin(), dimensions.end(),
static_cast<size_t>(1), std::multiplies<size_t>());
return std::accumulate(dimensions.begin(), dimensions.end(), elementSize,
std::multiplies<size_t>());
}

bool CheckIndexRange(const int index, const int upperLimit,
Expand Down
3 changes: 2 additions & 1 deletion source/adios2/helper/adiosMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace helper
* @param dimensions input containing size on each dimension {Nx, Ny, Nz}
* @return product of all dimensions Nx * Ny * Nz
*/
size_t GetTotalSize(const Dims &dimensions) noexcept;
size_t GetTotalSize(const Dims &dimensions,
const size_t elementSize = 1) noexcept;

/**
* Populates min and max for a selection region inside
Expand Down
10 changes: 4 additions & 6 deletions source/adios2/operator/compress/CompressBZIP2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ size_t CompressBZIP2::Compress(const void *dataIn, const Dims &dimensions,
}
}

const size_t sizeIn = std::accumulate(dimensions.begin(), dimensions.end(),
helper::GetDataTypeSize(type),
std::multiplies<size_t>());
const size_t sizeIn =
helper::GetTotalSize(dimensions, helper::GetDataTypeSize(type));

const size_t batches = sizeIn / DefaultMaxFileBatchSize + 1;
info["batches"] = std::to_string(batches);
Expand Down Expand Up @@ -118,9 +117,8 @@ size_t CompressBZIP2::Decompress(const void *bufferIn, const size_t sizeIn,
const Params &parameters, Params &info)
{
// TODO: leave defaults at zero?
const size_t sizeOut = std::accumulate(blockCount.begin(), blockCount.end(),
helper::GetDataTypeSize(type),
std::multiplies<size_t>());
const size_t sizeOut =
helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));
int small = 0;
int verbosity = 0;

Expand Down
10 changes: 4 additions & 6 deletions source/adios2/operator/compress/CompressBlosc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ size_t CompressBlosc::Compress(const void *dataIn, const Dims &dimensions,
DataType type, void *bufferOut,
const Params &parameters, Params &info)
{
const size_t sizeIn = std::accumulate(dimensions.begin(), dimensions.end(),
helper::GetDataTypeSize(type),
std::multiplies<size_t>());
const size_t sizeIn =
helper::GetTotalSize(dimensions, helper::GetDataTypeSize(type));

bool useMemcpy = false;
/* input size under this bound would not compressed */
Expand Down Expand Up @@ -240,9 +239,8 @@ size_t CompressBlosc::Decompress(const void *bufferIn, const size_t sizeIn,
const Dims &blockStart, const Dims &blockCount,
const Params &parameters, Params &info)
{
size_t sizeOut = std::accumulate(blockCount.begin(), blockCount.end(),
helper::GetDataTypeSize(type),
std::multiplies<size_t>());
size_t sizeOut =
helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));

assert(sizeIn >= sizeof(DataHeader));
const bool isChunked =
Expand Down
9 changes: 4 additions & 5 deletions source/adios2/operator/compress/CompressMGARD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,14 @@ size_t CompressMGARD::Decompress(const void *bufferIn, const size_t sizeIn,
reinterpret_cast<unsigned char *>(const_cast<void *>(bufferIn)),
static_cast<int>(sizeIn), r[0], r[1], r[2], 0.0);

const size_t dataSizeBytes = std::accumulate(
blockCount.begin(), blockCount.end(), helper::GetDataTypeSize(type),
std::multiplies<size_t>());
std::memcpy(dataOut, dataPtr, dataSizeBytes);
const size_t sizeOut =
helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));
std::memcpy(dataOut, dataPtr, sizeOut);

free(dataPtr);
dataPtr = nullptr;

return static_cast<size_t>(dataSizeBytes);
return sizeOut;
}

bool CompressMGARD::IsDataTypeValid(const DataType type) const
Expand Down
5 changes: 1 addition & 4 deletions source/adios2/operator/compress/CompressPNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ size_t CompressPNG::Decompress(const void *bufferIn, const size_t sizeIn,
const Dims &blockStart, const Dims &blockCount,
const Params &parameters, Params &info)
{
const size_t sizeOut = std::accumulate(blockCount.begin(), blockCount.end(),
helper::GetDataTypeSize(type),
std::multiplies<size_t>());
png_image image;
std::memset(&image, 0, sizeof(image));
image.version = PNG_IMAGE_VERSION;
Expand All @@ -200,7 +197,7 @@ size_t CompressPNG::Decompress(const void *bufferIn, const size_t sizeIn,
"to ADIOS2 PNG Decompress\n");
}

return sizeOut;
return helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));
}

bool CompressPNG::IsDataTypeValid(const DataType type) const { return true; }
Expand Down
40 changes: 10 additions & 30 deletions source/adios2/operator/compress/CompressZFP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ size_t CompressZFP::Decompress(const void *bufferIn, const size_t sizeIn,
const Dims &blockStart, const Dims &blockCount,
const Params &parameters, Params &info)
{
auto lf_GetTypeSize = [](const zfp_type zfpType) -> size_t {
size_t size = 0;
if (zfpType == zfp_type_int32 || zfpType == zfp_type_float)
{
size = 4;
}
else if (zfpType == zfp_type_int64 || zfpType == zfp_type_double)
{
size = 8;
}
return size;
};

Dims convertedDims = ConvertDims(blockCount, type, 3);

zfp_field *field = GetZFPField(dataOut, convertedDims, type);
Expand All @@ -105,7 +92,7 @@ size_t CompressZFP::Decompress(const void *bufferIn, const size_t sizeIn,
zfp_stream_close(stream);
stream_close(bitstream);

const size_t typeSizeBytes = lf_GetTypeSize(GetZfpType(type));
const size_t typeSizeBytes = helper::GetDataTypeSize(type);
const size_t dataSizeBytes =
helper::GetTotalSize(convertedDims) * typeSizeBytes;

Expand Down Expand Up @@ -169,44 +156,37 @@ zfp_type CompressZFP::GetZfpType(DataType type) const
zfp_field *CompressZFP::GetZFPField(const void *data, const Dims &dimensions,
DataType type) const
{
auto lf_CheckField = [](const zfp_field *field,
const std::string zfpFieldFunction, DataType type) {
if (field == nullptr || field == NULL)
{
throw std::invalid_argument(
"ERROR: " + zfpFieldFunction + " failed for data of type " +
ToString(type) +
", data pointer might be corrupted, from "
"class CompressZfp Transform\n");
}
};

zfp_type zfpType = GetZfpType(type);
zfp_field *field = nullptr;

if (dimensions.size() == 1)
{
field = zfp_field_1d(const_cast<void *>(data), zfpType, dimensions[0]);
lf_CheckField(field, "zfp_field_1d", type);
}
else if (dimensions.size() == 2)
{
field = zfp_field_2d(const_cast<void *>(data), zfpType, dimensions[0],
dimensions[1]);
lf_CheckField(field, "zfp_field_2d", type);
}
else if (dimensions.size() == 3)
{
field = zfp_field_3d(const_cast<void *>(data), zfpType, dimensions[0],
dimensions[1], dimensions[2]);
lf_CheckField(field, "zfp_field_3d", type);
}
else
{
throw std::invalid_argument(
"ERROR: zfp_field* failed for data of type " + ToString(type) +
", only 1D, 2D and 3D dimensions are supported, from "
"class CompressZfp Transform\n");
"class CompressZfp\n");
}

if (field == nullptr)
{
throw std::invalid_argument(
"ERROR: zfp_field_" + std::to_string(dimensions.size()) +
"d failed for data of type " + ToString(type) +
", data might be corrupted, from class CompressZfp\n");
}

return field;
Expand Down

0 comments on commit 8b2d2ab

Please sign in to comment.