Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some DimsArray issues and add unit testing #4221

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/adios2/helper/adiosType.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ class CoreDims
// memory remains valid as long as it is necessary. If you don't
// know the memory will be valid the entire time, use the
// DimsArray class which copies the dimension data.
CoreDims(std::vector<size_t> vec) : DimCount(vec.size()), DimensSpan(vec.data()) {}
CoreDims(const std::vector<size_t> &vec)
: DimCount(vec.size()), DimensSpan((size_t *)vec.data())
{
}
CoreDims(size_t count, size_t *span_val) : DimCount(count), DimensSpan(span_val) {}

size_t size() const { return DimCount; }
Expand Down Expand Up @@ -149,6 +152,11 @@ class DimsArray : public CoreDims
// constructor with no init of values
DimsArray(const size_t count) : CoreDims(count, &Dimensions[0]) {}

DimsArray(const DimsArray &d1) : CoreDims(d1.size(), &Dimensions[0])
{
std::copy(d1.begin(), d1.end(), &Dimensions[0]);
}

// constructor with single init value
DimsArray(const size_t count, const size_t init) : CoreDims(count, &Dimensions[0])
{
Expand All @@ -158,7 +166,7 @@ class DimsArray : public CoreDims
}
}
// constructor from vector
DimsArray(const std::vector<size_t> vec) : CoreDims(vec.size(), &Dimensions[0])
DimsArray(const std::vector<size_t> &vec) : CoreDims(vec.size(), &Dimensions[0])
{
for (size_t i = 0; i < vec.size(); i++)
{
Expand Down
30 changes: 21 additions & 9 deletions testing/adios2/engine/bp/TestBPReadMultithreaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ class BPReadMultithreadedTest : public ::testing::Test
return ss.str();
}

bool OutputWritten = false;
bool StreamOutputWritten = false;
bool FileOutputWritten = false;

void CreateOutput()
void CreateOutput(bool stream)
{
// This is not really a test but to create a dataset for all read tests
if (OutputWritten)
if (stream && StreamOutputWritten)
{
return;
}
if (!stream && FileOutputWritten)
{
return;
}
Expand All @@ -77,7 +82,11 @@ class BPReadMultithreadedTest : public ::testing::Test
#else
adios2::ADIOS adios;
#endif
std::string filename = "BPReadMultithreaded" + std::to_string(mpiSize) + ".bp";
std::string filename;
if (stream)
filename = "BPReadMultithreaded" + std::to_string(mpiSize) + "_Stream.bp";
else
filename = "BPReadMultithreaded" + std::to_string(mpiSize) + "_File.bp";
adios2::IO ioWrite = adios.DeclareIO("TestIOWrite");
ioWrite.SetEngine(engineName);
adios2::Engine engine = ioWrite.Open(filename, adios2::Mode::Write);
Expand All @@ -103,7 +112,10 @@ class BPReadMultithreadedTest : public ::testing::Test
engine.EndStep();
}
engine.Close();
OutputWritten = true;
if (stream)
StreamOutputWritten = true;
else
FileOutputWritten = true;
}
};

Expand Down Expand Up @@ -131,8 +143,8 @@ TEST_P(BPReadMultithreadedTestP, ReadFile)
#else
adios2::ADIOS adios;
#endif
CreateOutput();
std::string filename = "BPReadMultithreaded" + std::to_string(mpiSize) + ".bp";
CreateOutput(false);
std::string filename = "BPReadMultithreaded" + std::to_string(mpiSize) + "_File.bp";
adios2::IO ioRead = adios.DeclareIO("TestIORead");
ioRead.SetEngine(engineName);
ioRead.SetParameter("Threads", std::to_string(nThreads));
Expand Down Expand Up @@ -199,8 +211,8 @@ TEST_P(BPReadMultithreadedTestP, ReadStream)
#else
adios2::ADIOS adios;
#endif
CreateOutput();
std::string filename = "BPReadMultithreaded" + std::to_string(mpiSize) + ".bp";
CreateOutput(true);
std::string filename = "BPReadMultithreaded" + std::to_string(mpiSize) + "_Stream.bp";
adios2::IO ioRead = adios.DeclareIO("TestIORead");
ioRead.SetEngine(engineName);
ioRead.SetParameter("Threads", std::to_string(nThreads));
Expand Down
1 change: 1 addition & 0 deletions testing/adios2/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#------------------------------------------------------------------------------#

gtest_add_tests_helper(ChunkV MPI_NONE "" Unit. "")
gtest_add_tests_helper(CoreDims MPI_NONE "" Unit. "")
if(UNIX)
gtest_add_tests_helper(PosixTransport MPI_NONE "" Unit. "")
endif()
Expand Down
105 changes: 105 additions & 0 deletions testing/adios2/unit/TestCoreDims.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <adios2/helper/adiosType.h>
#include <algorithm>
#include <iostream>
#include <vector>

#include <gtest/gtest.h>

namespace adios2
{
namespace helper
{

// Assuming CoreDims and DimsArray definitions are included here
using namespace adios2::helper;

TEST(CoreDims, Basic)
{
// Using CoreDims with a vector
std::vector<size_t> vec = {1, 2, 3, 4, 5};
CoreDims coreDims(vec);
std::cout << "CoreDims from vector: " << coreDims << std::endl;

// Using DimsArray with different constructors
DimsArray dims1(5); // Initialize with no values
std::cout << "DimsArray with no values: " << dims1 << std::endl;
ASSERT_EQ(dims1.size(), 5);

DimsArray dims2(5, 42); // Initialize with a single value
std::cout << "DimsArray with single value: " << dims2 << std::endl;
ASSERT_EQ(dims2.size(), 5);
ASSERT_EQ(dims2[0], 42);
ASSERT_EQ(dims2[1], 42);
ASSERT_EQ(dims2[2], 42);
ASSERT_EQ(dims2[3], 42);
ASSERT_EQ(dims2[4], 42);

DimsArray dims3(vec); // Initialize from vector
std::cout << "DimsArray from vector: " << dims3 << std::endl;
ASSERT_EQ(dims3.size(), 5);
ASSERT_EQ(dims3[0], 1);
ASSERT_EQ(dims3[1], 2);
ASSERT_EQ(dims3[2], 3);
ASSERT_EQ(dims3[3], 4);
ASSERT_EQ(dims3[4], 5);

size_t arr[] = {10, 20, 30, 40, 50};
DimsArray dims4(5, arr); // Initialize from array
std::cout << "DimsArray from array: " << dims4 << std::endl;
ASSERT_EQ(dims4.size(), 5);
ASSERT_EQ(dims4[0], 10);
ASSERT_EQ(dims4[1], 20);
ASSERT_EQ(dims4[2], 30);
ASSERT_EQ(dims4[3], 40);
ASSERT_EQ(dims4[4], 50);

// Copying from CoreDims
DimsArray dims5(coreDims);
std::cout << "DimsArray copied from CoreDims: " << dims5 << std::endl;
ASSERT_EQ(dims5.size(), 5);
ASSERT_EQ(dims5[0], 1);
ASSERT_EQ(dims5[1], 2);
ASSERT_EQ(dims5[2], 3);
ASSERT_EQ(dims5[3], 4);
ASSERT_EQ(dims5[4], 5);

DimsArray dims6(dims4);
ASSERT_EQ(dims6.size(), 5);
ASSERT_EQ(dims6[0], 10);
ASSERT_EQ(dims6[1], 20);
ASSERT_EQ(dims6[2], 30);
ASSERT_EQ(dims6[3], 40);
ASSERT_EQ(dims6[4], 50);

std::cout << "DimsArray copied from dims4: " << dims4 << std::endl;

dims6[1] = 100;
std::cout << "dims4: " << dims4 << std::endl;
std::cout << "dims6: " << dims6 << std::endl;
ASSERT_EQ(dims6.size(), 5);
ASSERT_EQ(dims6[0], 10);
ASSERT_EQ(dims6[1], 100);
ASSERT_EQ(dims6[2], 30);
ASSERT_EQ(dims6[3], 40);
ASSERT_EQ(dims6[4], 50);

// dims4 should be unchanged
ASSERT_EQ(dims4.size(), 5);
ASSERT_EQ(dims4[0], 10);
ASSERT_EQ(dims4[1], 20);
ASSERT_EQ(dims4[2], 30);
ASSERT_EQ(dims4[3], 40);
ASSERT_EQ(dims4[4], 50);
}
}
}

int main(int argc, char **argv)
{

int result;
::testing::InitGoogleTest(&argc, argv);
result = RUN_ALL_TESTS();

return result;
}
Loading