Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
anagainaru committed Mar 1, 2023
1 parent c5ea7e3 commit 8d47bdd
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 15 deletions.
2 changes: 1 addition & 1 deletion source/adios2/operator/compress/CompressBlosc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ size_t CompressBlosc::InverseOperate(const char *bufferIn, const size_t sizeIn,
const uint8_t bufferVersion =
GetParameter<uint8_t>(bufferIn, bufferInOffset);
bufferInOffset += 2; // skip two reserved bytes
headerSize += bufferInOffset;
headerSize = bufferInOffset;

if (bufferVersion == 1)
{
Expand Down
48 changes: 35 additions & 13 deletions source/adios2/operator/compress/CompressMGARD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,32 @@ size_t CompressMGARD::Operate(const char *dataIn, const Dims &blockStart,
}
}

// input size under this bound will not compress
size_t thresholdSize = 1024;

// let mgard know the output buffer size
size_t sizeOut =
helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));

if (sizeOut < thresholdSize)
{
/* disable compression and add marker in the header*/
PutParameter(bufferOut, bufferOutOffset, false);
headerSize = bufferOutOffset;
return 0;
}

PutParameter(bufferOut, bufferOutOffset, true);
void *compressedData = bufferOut + bufferOutOffset;
mgard_x::compress(mgardDim, mgardType, mgardCount, tolerance, s,
errorBoundType, dataIn, compressedData, sizeOut, true);

bufferOutOffset += sizeOut;

return bufferOutOffset;
}

size_t CompressMGARD::GetHeaderSize() const { return headerSize; }

size_t CompressMGARD::DecompressV1(const char *bufferIn, const size_t sizeIn,
char *dataOut)
{
Expand All @@ -175,6 +189,8 @@ size_t CompressMGARD::DecompressV1(const char *bufferIn, const size_t sizeIn,
std::to_string(GetParameter<uint8_t>(bufferIn, bufferInOffset)) +
". Please make sure a compatible version is used for decompression.";

const bool isCompressed = GetParameter<bool>(bufferIn, bufferInOffset);

size_t sizeOut =
helper::GetTotalSize(blockCount, helper::GetDataTypeSize(type));

Expand All @@ -183,19 +199,24 @@ size_t CompressMGARD::DecompressV1(const char *bufferIn, const size_t sizeIn,
sizeOut /= 2;
}

try
{
void *dataOutVoid = dataOut;
mgard_x::decompress(bufferIn + bufferInOffset, sizeIn - bufferInOffset,
dataOutVoid, true);
}
catch (...)
{
helper::Throw<std::runtime_error>("Operator", "CompressMGARD",
"DecompressV1", m_VersionInfo);
}
if (isCompressed)
{
try
{
void *dataOutVoid = dataOut;
mgard_x::decompress(bufferIn + bufferInOffset, sizeIn - bufferInOffset,
dataOutVoid, true);
}
catch (...)
{
helper::Throw<std::runtime_error>("Operator", "CompressMGARD",
"DecompressV1", m_VersionInfo);
}
return sizeOut;
}

return sizeOut;
headerSize += bufferInOffset;
return 0;
}

size_t CompressMGARD::InverseOperate(const char *bufferIn, const size_t sizeIn,
Expand All @@ -205,6 +226,7 @@ size_t CompressMGARD::InverseOperate(const char *bufferIn, const size_t sizeIn,
const uint8_t bufferVersion =
GetParameter<uint8_t>(bufferIn, bufferInOffset);
bufferInOffset += 2; // skip two reserved bytes
headerSize = bufferInOffset;

if (bufferVersion == 1)
{
Expand Down
3 changes: 3 additions & 0 deletions source/adios2/operator/compress/CompressMGARD.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class CompressMGARD : public Operator

bool IsDataTypeValid(const DataType type) const final;

size_t GetHeaderSize() const;

private:
size_t headerSize = 0;
/**
* Decompress function for V1 buffer. Do NOT remove even if the buffer
* version is updated. Data might be still in lagacy formats. This function
Expand Down
9 changes: 9 additions & 0 deletions source/adios2/operator/compress/CompressMGARDPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ size_t CompressMGARDPlus::Operate(const char *dataIn, const Dims &blockStart,
CompressMGARD mgard(m_Parameters);
size_t mgardBufferSize = mgard.Operate(dataIn, blockStart, blockCount, type,
bufferOut + bufferOutOffset);
if (mgardBufferSize == 0)
{
headerSize += (bufferOutOffset + mgard.GetHeaderSize());
return 0;
}

if (*reinterpret_cast<OperatorType *>(bufferOut + bufferOutOffset) ==
COMPRESS_MGARD)
Expand Down Expand Up @@ -90,6 +95,8 @@ size_t CompressMGARDPlus::Operate(const char *dataIn, const Dims &blockStart,
return bufferOutOffset;
}

size_t CompressMGARDPlus::GetHeaderSize() const { return headerSize; }

size_t CompressMGARDPlus::DecompressV1(const char *bufferIn,
const size_t sizeIn, char *dataOut)
{
Expand All @@ -114,6 +121,7 @@ size_t CompressMGARDPlus::DecompressV1(const char *bufferIn,
// sizeOut. Here you may want to do your magic to change the decompressed
// data somehow to improve its accuracy :)

headerSize += (bufferInOffset + mgard.GetHeaderSize());
return sizeOut;
}

Expand All @@ -124,6 +132,7 @@ size_t CompressMGARDPlus::InverseOperate(const char *bufferIn,
const uint8_t bufferVersion =
GetParameter<uint8_t>(bufferIn, bufferInOffset);
bufferInOffset += 2; // skip two reserved bytes
headerSize = bufferInOffset;

if (bufferVersion == 1)
{
Expand Down
4 changes: 4 additions & 0 deletions source/adios2/operator/compress/CompressMGARDPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ class CompressMGARDPlus : public Operator

bool IsDataTypeValid(const DataType type) const final;

size_t GetHeaderSize() const;

private:
size_t headerSize = 0;

/**
* Decompress function for V1 buffer. Do NOT remove even if the buffer
* version is updated. Data might be still in lagacy formats. This function
Expand Down
133 changes: 132 additions & 1 deletion testing/adios2/engine/bp/operations/TestBPWriteReadMGARDCuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void MGARDAccuracy2D(const std::string tolerance)
bpWriter.BeginStep();
cudaMemcpy(gpu64s, r64s.data(), Nx * Ny * sizeof(double),
cudaMemcpyHostToDevice);
var_r64.SetMemorySpace(adios2::MemorySpace::CUDA);
var_r64.SetMemorySpace(adios2::MemorySpace::GPU);
bpWriter.Put<double>("r64", gpu64s);
bpWriter.EndStep();
}
Expand Down Expand Up @@ -140,6 +140,136 @@ void MGARDAccuracy2D(const std::string tolerance)
}
}

void MGARDAccuracySmall(const std::string tolerance)
{
// Each process would write a 1x8 array and all processes would
// form a mpiSize * Nx 1D array
const std::string fname("BPWRMGARD1D_" + tolerance + ".bp");

int mpiRank = 0, mpiSize = 1;
// Number of rows
const size_t Nx = 100;

// Number of steps
const size_t NSteps = 1;

std::vector<float> r32s(Nx);
std::iota(r32s.begin(), r32s.end(), 0.f);

#if ADIOS2_USE_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
MPI_Comm_size(MPI_COMM_WORLD, &mpiSize);
#endif

#if ADIOS2_USE_MPI
adios2::ADIOS adios(MPI_COMM_WORLD);
#else
adios2::ADIOS adios;
#endif
{
adios2::IO io = adios.DeclareIO("TestIO");

if (!engineName.empty())
{
io.SetEngine(engineName);
}

const adios2::Dims shape{static_cast<size_t>(Nx * mpiSize)};
const adios2::Dims start{static_cast<size_t>(Nx * mpiRank)};
const adios2::Dims count{Nx};

auto var_r32 = io.DefineVariable<float>("r32", shape, start, count,
adios2::ConstantDims);

// add operations
adios2::Operator mgardOp =
adios.DefineOperator("mgardCompressor", adios2::ops::LossyMGARD);

var_r32.AddOperation(mgardOp,
{{adios2::ops::mgard::key::tolerance, tolerance},
{adios2::ops::mgard::key::s, "inf"}});

adios2::Engine bpWriter = io.Open(fname, adios2::Mode::Write);

float *gpu32s = nullptr;
cudaMalloc(&gpu32s, Nx * sizeof(float));
for (size_t step = 0; step < NSteps; ++step)
{
bpWriter.BeginStep();
cudaMemcpy(gpu32s, r32s.data(), Nx * sizeof(float),
cudaMemcpyHostToDevice);
bpWriter.Put<float>("r32", gpu32s);
bpWriter.EndStep();
}

bpWriter.Close();
}

{
adios2::IO io = adios.DeclareIO("ReadIO");

if (!engineName.empty())
{
io.SetEngine(engineName);
}

adios2::Engine bpReader = io.Open(fname, adios2::Mode::Read);

unsigned int t = 0;
std::vector<float> decompressedR32s(Nx);

while (bpReader.BeginStep() == adios2::StepStatus::OK)
{
auto var_r32 = io.InquireVariable<float>("r32");
EXPECT_TRUE(var_r32);
ASSERT_EQ(var_r32.ShapeID(), adios2::ShapeID::GlobalArray);
ASSERT_EQ(var_r32.Steps(), NSteps);
ASSERT_EQ(var_r32.Shape()[0], mpiSize * Nx);

const adios2::Dims start{mpiRank * Nx};
const adios2::Dims count{Nx};
const adios2::Box<adios2::Dims> sel(start, count);
var_r32.SetSelection(sel);

float *gpu32s = nullptr;
cudaMalloc(&gpu32s, Nx * sizeof(float));
bpReader.Get(var_r32, gpu32s);
bpReader.EndStep();
cudaMemcpy(decompressedR32s.data(), gpu32s,
Nx * sizeof(float), cudaMemcpyDeviceToHost);

double maxDiff = 0, relativeMaxDiff = 0;

for (size_t i = 0; i < Nx; ++i)
{
std::stringstream ss;
ss << "t=" << t << " i=" << i << " rank=" << mpiRank;
std::string msg = ss.str();

double diff = std::abs(r32s[i] - decompressedR32s[i]);

if (diff > maxDiff)
{
maxDiff = diff;
}
}
++t;

auto r32s_Max = std::max_element(r32s.begin(), r32s.end());
relativeMaxDiff = maxDiff / *r32s_Max;

ASSERT_LT(relativeMaxDiff, std::stod(tolerance));
std::cout << "Relative Max Diff " << relativeMaxDiff
<< " tolerance " << tolerance << "\n";
}

EXPECT_EQ(t, NSteps);

bpReader.Close();
}
}


class BPWriteReadMGARD : public ::testing::TestWithParam<std::string>
{
public:
Expand All @@ -149,6 +279,7 @@ class BPWriteReadMGARD : public ::testing::TestWithParam<std::string>
};

TEST_P(BPWriteReadMGARD, BPWRMGARDCU2D) { MGARDAccuracy2D(GetParam()); }
TEST_P(BPWriteReadMGARD, BPWRMGARDCU1D) { MGARDAccuracySmall(GetParam()); }

INSTANTIATE_TEST_SUITE_P(MGARDAccuracy, BPWriteReadMGARD,
::testing::Values("0.01", "0.001", "0.0001",
Expand Down

0 comments on commit 8d47bdd

Please sign in to comment.