|
| 1 | +/* |
| 2 | + * Distributed under the OSI-approved Apache License, Version 2.0. See |
| 3 | + * accompanying file Copyright.txt for details. |
| 4 | + * |
| 5 | + * TestLargeBlocks.cpp : |
| 6 | + * |
| 7 | + * Created on: Aug 5, 2025 |
| 8 | + * Author: Norbert Podhorszki |
| 9 | + */ |
| 10 | + |
| 11 | +#include <cstdint> |
| 12 | +#include <cstring> |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <stdexcept> |
| 16 | + |
| 17 | +#include <adios2.h> |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +std::string engineName; // comes from command line |
| 22 | + |
| 23 | +class LargeBlocks : public ::testing::Test |
| 24 | +{ |
| 25 | +public: |
| 26 | + LargeBlocks() = default; |
| 27 | +}; |
| 28 | + |
| 29 | +constexpr size_t DefaultMaxFileBatchSize = 2147381248; |
| 30 | + |
| 31 | +TEST_F(LargeBlocks, MultiBlock) |
| 32 | +{ |
| 33 | + // Write a small variable |
| 34 | + // Write a large (>2147381248 bytes) so that it's offset is >0 in the file. |
| 35 | + // Read the large variable back and confirm that the data on the |
| 36 | + // chunk boundary of 2147381248 bytes is correct. |
| 37 | + // Testing if the POSIX reader transport is reading in chunks of 2147381248 |
| 38 | + // bytes correctly (from the correct offsets) |
| 39 | + |
| 40 | + int rank = 0, nproc = 1; |
| 41 | + |
| 42 | + adios2::ADIOS adios; |
| 43 | + |
| 44 | + const std::string fname = "LargeBlocks.bp"; |
| 45 | + size_t NX = DefaultMaxFileBatchSize / 4 + |
| 46 | + 1024; // data must be > 2147381248 bytes to test chunked writing/reading |
| 47 | + |
| 48 | + std::vector<uint32_t> LargeData(NX); |
| 49 | + uint32_t myvalue = 0; |
| 50 | + for (size_t n = 0; n < NX; ++n) |
| 51 | + { |
| 52 | + LargeData[n] = myvalue++; |
| 53 | + } |
| 54 | + |
| 55 | + // Writer |
| 56 | + { |
| 57 | + adios2::IO outIO = adios.DeclareIO("Output"); |
| 58 | + if (!engineName.empty()) |
| 59 | + { |
| 60 | + outIO.SetEngine(engineName); |
| 61 | + } |
| 62 | + adios2::Engine writer = outIO.Open(fname, adios2::Mode::Write); |
| 63 | + auto varSmall = outIO.DefineVariable<uint32_t>("small", {2000}, {0}, {2000}); |
| 64 | + auto varLarge = outIO.DefineVariable<uint32_t>("large", {NX}, {0}, {NX}); |
| 65 | + |
| 66 | + std::cout << "Writing to " << fname << std::endl; |
| 67 | + writer.BeginStep(); |
| 68 | + writer.Put(varSmall, LargeData.data(), adios2::Mode::Deferred); |
| 69 | + writer.Put(varLarge, LargeData.data(), adios2::Mode::Deferred); |
| 70 | + writer.EndStep(); |
| 71 | + writer.Close(); |
| 72 | + } |
| 73 | + |
| 74 | + // Reader with streaming |
| 75 | + { |
| 76 | + adios2::IO inIO = adios.DeclareIO("Input"); |
| 77 | + if (!engineName.empty()) |
| 78 | + { |
| 79 | + inIO.SetEngine(engineName); |
| 80 | + } |
| 81 | + adios2::Engine reader = inIO.Open(fname, adios2::Mode::ReadRandomAccess); |
| 82 | + std::cout << "Reading as stream with BeginStep/EndStep:" << std::endl; |
| 83 | + |
| 84 | + auto varLarge = inIO.InquireVariable<uint32_t>("large"); |
| 85 | + EXPECT_TRUE(varLarge); |
| 86 | + std::cout << " large data shape " << varLarge.Shape()[0] << std::endl; |
| 87 | + |
| 88 | + varLarge.SetSelection({{0}, {NX}}); |
| 89 | + std::vector<uint32_t> data(NX); |
| 90 | + reader.Get(varLarge, data.data()); |
| 91 | + reader.PerformGets(); |
| 92 | + |
| 93 | + size_t TestOffset = DefaultMaxFileBatchSize / 4; |
| 94 | + std::cout << "large data from " << TestOffset - 10 << ":" << std::endl; |
| 95 | + std::cout << " written: " << std::endl; |
| 96 | + for (size_t i = TestOffset - 10; i < TestOffset + 10; ++i) |
| 97 | + { |
| 98 | + std::cout << static_cast<int>(LargeData[i]) << " "; |
| 99 | + } |
| 100 | + std::cout << std::endl; |
| 101 | + std::cout << " read: " << std::endl; |
| 102 | + for (size_t i = TestOffset - 10; i < TestOffset + 10; ++i) |
| 103 | + { |
| 104 | + std::cout << static_cast<int>(data[i]) << " "; |
| 105 | + } |
| 106 | + std::cout << std::endl; |
| 107 | + for (size_t i = TestOffset - 10; i < TestOffset + 10; ++i) |
| 108 | + { |
| 109 | + EXPECT_EQ(data[i], LargeData[i]); |
| 110 | + } |
| 111 | + reader.Close(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +int main(int argc, char **argv) |
| 116 | +{ |
| 117 | +#if ADIOS2_USE_MPI |
| 118 | + int provided; |
| 119 | + |
| 120 | + // MPI_THREAD_MULTIPLE is only required if you enable the SST MPI_DP |
| 121 | + MPI_Init_thread(nullptr, nullptr, MPI_THREAD_MULTIPLE, &provided); |
| 122 | +#endif |
| 123 | + |
| 124 | + int result; |
| 125 | + ::testing::InitGoogleTest(&argc, argv); |
| 126 | + |
| 127 | + if (argc > 1) |
| 128 | + { |
| 129 | + engineName = std::string(argv[1]); |
| 130 | + } |
| 131 | + |
| 132 | + result = RUN_ALL_TESTS(); |
| 133 | + |
| 134 | +#if ADIOS2_USE_MPI |
| 135 | + MPI_Finalize(); |
| 136 | +#endif |
| 137 | + |
| 138 | + return result; |
| 139 | +} |
0 commit comments