Skip to content
Snippets Groups Projects
TestBPWriteProfilingJSON.cpp 6.75 KiB
Newer Older
/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * TestBPWriteProfilingJSON.cpp
 *
 *  Created on: Jul 18, 2017
 *      Author: William F Godoy godoywf@ornl.gov
 */

#include <cstdint>
#include <cstring>

#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <adios2.h>

#include <gtest/gtest.h>
#include <json.hpp> //This fails to be included

#include "../SmallTestData.h"

using json = nlohmann::json;

class BPWriteProfilingJSONTest : public ::testing::Test
{
public:
    BPWriteProfilingJSONTest() = default;

    SmallTestData m_TestData;
};

//******************************************************************************
// 1D 1x8 test data
//******************************************************************************

// ADIOS2 write, native ADIOS1 read
TEST_F(BPWriteProfilingJSONTest, DISABLED_ADIOS2BPWriteProfilingJSON)
    // Use a relative path + file name to test path in file name capability
    std::string fname;
    fname = "foo/ADIOS2BPWriteProfilingJSON.bp";

    int mpiRank = 0, mpiSize = 1;
    // Number of rows
    const std::size_t Nx = 8;

    // Number of steps
    const std::size_t NSteps = 3;

#ifdef ADIOS2_HAVE_MPI
    MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
    MPI_Comm_size(MPI_COMM_WORLD, &mpiSize);
#endif

    // Write test data and profiling.json using ADIOS2
    {
#ifdef ADIOS2_HAVE_MPI
        adios2::ADIOS adios(MPI_COMM_WORLD, adios2::DebugON);
#else
        adios2::ADIOS adios(true);
        adios2::IO &io = adios.DeclareIO("TestIO");

        // Declare 1D variables (NumOfProcesses * Nx)
        // The local process' part (start, count) can be defined now or later
        // before Write().
            adios2::Dims shape{static_cast<unsigned int>(Nx * mpiSize)};
            adios2::Dims start{static_cast<unsigned int>(Nx * mpiRank)};
            adios2::Dims count{static_cast<unsigned int>(Nx)};
            auto &var_i8 = io.DefineVariable<int8_t>("i8", shape, start, count);
                io.DefineVariable<int16_t>("i16", shape, start, count);
                io.DefineVariable<int32_t>("i32", shape, start, count);
                io.DefineVariable<int64_t>("i64", shape, start, count);
                io.DefineVariable<uint8_t>("u8", shape, start, count);
                io.DefineVariable<uint16_t>("u16", shape, start, count);
                io.DefineVariable<uint32_t>("u32", shape, start, count);
                io.DefineVariable<uint64_t>("u64", shape, start, count);
                io.DefineVariable<float>("r32", shape, start, count);
                io.DefineVariable<double>("r64", shape, start, count);
        }

        // Create the BP Engine
        io.SetEngine("BPFileWriter");
        io.SetParameters({{"Threads", "2"}});
        io.AddTransport("file", {{"Library", "POSIX"}});

        auto engine = io.Open(fname, adios2::OpenMode::Write);
        ASSERT_NE(engine.get(), nullptr);

        for (size_t step = 0; step < NSteps; ++step)
            // Generate test data for each process uniquely
            SmallTestData currentTestData =
                generateNewSmallTestData(m_TestData, step, mpiRank, mpiSize);

            // Retrieve the variables that previously went out of scope
            auto &var_i8 = io.GetVariable<int8_t>("i8");
            auto &var_i16 = io.GetVariable<int16_t>("i16");
            auto &var_i32 = io.GetVariable<int32_t>("i32");
            auto &var_i64 = io.GetVariable<int64_t>("i64");
            auto &var_u8 = io.GetVariable<uint8_t>("u8");
            auto &var_u16 = io.GetVariable<uint16_t>("u16");
            auto &var_u32 = io.GetVariable<uint32_t>("u32");
            auto &var_u64 = io.GetVariable<uint64_t>("u64");
            auto &var_r32 = io.GetVariable<float>("r32");
            auto &var_r64 = io.GetVariable<double>("r64");

            // Make a 1D selection to describe the local dimensions of the
            // variable we write and its offsets in the global spaces
            adios2::SelectionBoundingBox sel({mpiRank * Nx}, {Nx});
            var_i8.SetSelection(sel);
            var_i16.SetSelection(sel);
            var_i32.SetSelection(sel);
            var_i64.SetSelection(sel);
            var_u8.SetSelection(sel);
            var_u16.SetSelection(sel);
            var_u32.SetSelection(sel);
            var_u64.SetSelection(sel);
            var_r32.SetSelection(sel);
            var_r64.SetSelection(sel);

            // fill in the variable with values from starting index to
            // starting index + count
            engine->Write(var_i8, currentTestData.I8.data());
            engine->Write(var_i16, currentTestData.I16.data());
            engine->Write(var_i32, currentTestData.I32.data());
            engine->Write(var_i64, currentTestData.I64.data());
            engine->Write(var_u8, currentTestData.U8.data());
            engine->Write(var_u16, currentTestData.U16.data());
            engine->Write(var_u32, currentTestData.U32.data());
            engine->Write(var_u64, currentTestData.U64.data());
            engine->Write(var_r32, currentTestData.R32.data());
            engine->Write(var_r64, currentTestData.R64.data());

            // Advance to the next time step
            engine->Advance();
        }
        // Close the file
        engine->Close();
    }

    // open json file, parse it to a json structure, and verify a few things
    {
        std::ifstream profilingJSONFile(fname + ".dir/profiling.json");
        const json profilingJSON = json::parse(profilingJSONFile);
        const int rank = profilingJSON[mpiRank].value("rank", -1);
        ASSERT_EQ(rank, mpiRank);
        const int threads = profilingJSON[mpiRank].value("threads", 0);
        ASSERT_EQ(threads, 2);

        // check bytes
        const unsigned long int bytes =
            profilingJSON[mpiRank].value("bytes", 0UL);
        ASSERT_EQ(bytes, 6536);

        const auto transportType =
            profilingJSON[mpiRank]["transport_0"].value("type", "0");
        ASSERT_EQ(transportType, "File_POSIX");
    }
}

//******************************************************************************
// main
//******************************************************************************

int main(int argc, char **argv)
{
#ifdef ADIOS2_HAVE_MPI
    MPI_Init(nullptr, nullptr);
#endif

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

#ifdef ADIOS2_HAVE_MPI
    MPI_Finalize();
#endif

    return result;
}