Skip to content
Snippets Groups Projects
Commit f565364d authored by William F Godoy's avatar William F Godoy
Browse files

Need to fix linear index for multidimensions

parent 62110a0a
No related branches found
No related tags found
1 merge request!294Bp1read : Initial Reading Capabilities and latest API
......@@ -16,7 +16,7 @@
<!-- XXKb, XXMb, or XXXGb supported, default=16Kb
(applications might choose an optimal value) -->
<!--<parameter key="InitialBufferSize" value="16Kb"/> -->
<parameter key="InitialBufferSize" value="16Kb"/>
<!-- XXKb, XXMb, or XXXGb supported, default=Unlimited (until
fails), maximum at each time step
......@@ -41,5 +41,17 @@
<parameter key="ProfileUnits" value="Microseconds"/>
</transport>
<transport type="WAN">
<!-- POSIX, stdio (C FILE*), fstream (C++) -->
<!-- <parameter key="Library" value="GRPC"/> -->
<parameter key="Library" value="GRPC"/>
<parameter key="IPAddress" value="127.0.0.1"/>
<!-- For read/write, Microseconds (default), Milliseconds, Seconds,
Minutes, Hours. open/close always in Microseconds -->
<parameter key="ProfileUnits" value="Microseconds"/>
</transport>
</io>
</adios-config>
......@@ -8,6 +8,11 @@ if(ADIOS2_HAVE_MPI)
target_include_directories(hello_bpReader PRIVATE ${MPI_C_INCLUDE_PATH})
target_link_libraries(hello_bpReader ${MPI_C_LIBRARIES})
add_executable(hello_bpReaderHeatMap helloBPReaderHeatMap.cpp)
target_include_directories(hello_bpReaderHeatMap PRIVATE ${MPI_C_INCLUDE_PATH})
target_link_libraries(hello_bpReaderHeatMap ${MPI_C_LIBRARIES})
target_link_libraries(hello_bpReaderHeatMap adios2)
# add_executable(hello_bpReader_c helloBPReader.c)
# target_include_directories(hello_bpReader_c PRIVATE ${MPI_C_INCLUDE_PATH})
# target_link_libraries(hello_bpReader_c ${MPI_C_LIBRARIES})
......@@ -33,6 +38,7 @@ else()
endif()
target_link_libraries(hello_bpReader adios2)
#target_link_libraries(hello_bpReader_c adios2)
#if(ADIOS2_HAVE_Fortran)
......
......@@ -61,9 +61,11 @@ int main(int argc, char *argv[])
bpIO.InquireVariable<float>("bpFloats");
adios2::Variable<int> *bpInts = bpIO.InquireVariable<int>("bpInts");
if (bpFloats != nullptr) // means not found
if (bpFloats != nullptr) // means found
{
// myFloats.data is pre-allocated
bpReader.GetSync<float>(*bpFloats, myFloats.data());
std::cout << "MyFloats: \n";
for (const auto number : myFloats)
{
std::cout << number << " ";
......@@ -73,6 +75,7 @@ int main(int argc, char *argv[])
if (bpInts != nullptr) // means not found
{
// myInts.data is pre-allocated
bpReader.GetSync<int>(*bpInts, myInts.data());
std::cout << "MyInts: \n";
......
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* helloBPReaderHeatMap.cpp : Writes a regular heat map in a regular 2D mesh,
* values grow from 0 in increments of 1
*
* temperature[gNx, gNy]
* where: gNx = MPI_size_x * Nx and gNy = MPI_size_y * Ny
*
* 0 1 2 ... gNy-1
* gNy gNy+1 gNy+2 ... 2*gNy-1
* ...
* ...
* (gNx-1)*gNy ... gNx*gNy-1
*
*
* Created on: Nov 1, 2017
* Author: William F Godoy godoywf@ornl.gov
*/
#include <algorithm>
#include <ios> //std::ios_base::failure
#include <iostream> //std::cout
#include <mpi.h>
#include <stdexcept> //std::invalid_argument std::exception
#include <vector>
#include <adios2.h>
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/** Application variable dimensions */
constexpr std::size_t Nx = 10;
constexpr std::size_t Ny = 10;
const adios2::Dims count{Nx, Ny};
const adios2::Dims start{rank * Nx, rank * Ny};
const adios2::Dims shape{size * Nx, size * Ny};
// populate local temperature values
std::vector<unsigned int> temperatures(Nx * Ny);
for (unsigned int i = 0; i < Nx; ++i)
{
const unsigned int iGlobal = start[0] + i;
for (unsigned int j = 0; j < Ny; ++j)
{
const unsigned int jGlobal = start[1] + j;
const unsigned int value = iGlobal * shape[1] + jGlobal;
temperatures[i * Ny + j] = value;
}
}
try
{
/** ADIOS class factory of IO class objects, Debug is ON by default */
adios2::ADIOS adios(MPI_COMM_WORLD);
// ************************** WRITE
/*** IO class object: settings and factory of Settings: Variables,
* Parameters, Transports, and Execution: Engines */
adios2::IO &putHeatMap = adios.DeclareIO("HeatMapWriter");
adios2::Variable<unsigned int> &outTemperature =
putHeatMap.DefineVariable<unsigned int>(
"temperature", shape, start, count, adios2::ConstantDims);
/** Will create HeatMap.bp */
adios2::Engine &bpWriter =
putHeatMap.Open("HeatMap.bp", adios2::Mode::Write);
bpWriter.PutSync(outTemperature, temperatures.data());
bpWriter.Close();
// ************************** READ
if (rank == 0)
{
adios2::IO &getHeatMap = adios.DeclareIO("HeatMapReader");
adios2::Engine &bpReader =
getHeatMap.Open("HeatMap.bp", adios2::Mode::Read);
// this just discovers in the metadata file that the variable exists
adios2::Variable<unsigned int> *inTemperature =
getHeatMap.InquireVariable<unsigned int>("temperature");
// inTemperature->SetSelection({{,0})
// now read the variable
if (inTemperature != nullptr)
{
std::vector<unsigned int> inTemperatures(Nx * Ny);
bpReader.GetSync(*inTemperature, inTemperatures.data());
for (auto i = 0; i < inTemperatures.size(); ++i)
{
std::cout << inTemperatures[i] << " ";
if (i % Nx == 0 && i > 0)
{
std::cout << "\n";
}
}
std::cout << "\n";
}
bpReader.Close();
}
}
catch (std::invalid_argument &e)
{
std::cout << "Invalid argument exception, STOPPING PROGRAM from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::ios_base::failure &e)
{
std::cout << "IO System base failure exception, STOPPING PROGRAM "
"from rank "
<< rank << "\n";
std::cout << e.what() << "\n";
}
catch (std::exception &e)
{
std::cout << "Exception, STOPPING PROGRAM from rank " << rank << "\n";
std::cout << e.what() << "\n";
}
MPI_Finalize();
return 0;
}
......@@ -39,7 +39,7 @@ int main(int argc, char *argv[])
/*** IO class object: settings and factory of Settings: Variables,
* Parameters, Transports, and Execution: Engines */
adios2::IO &bpIO = adios.DeclareIO("BPFile_N2N");
// bpIO.SetParameters({{"Threads", "2"}});
bpIO.SetParameters({{"Threads", "2"}});
/** global array: name, { shape (total dimensions) }, { start
* (local) },
......
......@@ -32,5 +32,5 @@ ioArray = bpIO.DefineVariable(
# ADIOS Engine
bpFileWriter = bpIO.Open("npArray.bp", adios2.OpenModeWrite)
# doesn't work: bpFileWriter = bpIO.Open("npArray.bp", adios2.OpenModeWrite, newcomm)
bpFileWriter.Write(ioArray, myArray)
bpFileWriter.PutSync(ioArray, myArray)
bpFileWriter.Close()
......@@ -31,7 +31,11 @@ void BPFileReader::PerformGets()
m_BP3Deserializer.PerformGetsVariablesSubFileInfo(m_IO);
}
void BPFileReader::Close(const int /*transportIndex*/) {}
void BPFileReader::Close(const int transportIndex)
{
m_SubFileManager.CloseFiles();
m_FileManager.CloseFiles();
}
// PRIVATE
void BPFileReader::Init()
......@@ -154,8 +158,6 @@ void BPFileReader::ReadVariables(
} // end step
} // end subfile
} // end variable
m_SubFileManager.CloseFiles();
}
} // end namespace adios2
......@@ -59,6 +59,30 @@ size_t NextExponentialSize(const size_t requiredSize, const size_t currentSize,
return nextExponentialSize;
}
Box<Dims> StartEndBox(const Dims &start, const Dims &count) noexcept
{
Box<Dims> box;
box.first = start;
box.second.reserve(start.size());
std::transform(start.begin(), start.end(), count.begin(),
std::back_inserter(box.second), std::plus<size_t>());
return box;
}
Box<Dims> StartCountBox(const Dims &start, const Dims &end) noexcept
{
Box<Dims> box;
box.first = start;
box.second.reserve(start.size());
std::transform(end.begin(), end.end(), start.begin(),
std::back_inserter(box.second), std::minus<size_t>());
return box;
}
Box<Dims> IntersectionBox(const Box<Dims> &box1, const Box<Dims> &box2) noexcept
{
Box<Dims> intersectionBox;
......@@ -66,11 +90,8 @@ Box<Dims> IntersectionBox(const Box<Dims> &box1, const Box<Dims> &box2) noexcept
for (size_t d = 0; d < dimensionsSize; ++d)
{
// start, count to (start, end)
const Box<size_t> line1{box1.first[d], box1.first[d] + box1.second[d]};
const Box<size_t> line2{box2.first[d], box2.first[d] + box2.second[d]};
// Don't intercept
if (line2.first >= line1.second || line2.second <= line1.first)
if (box2.first[d] >= box1.second[d] || box2.second[d] <= box1.first[d])
{
return intersectionBox;
}
......@@ -82,27 +103,24 @@ Box<Dims> IntersectionBox(const Box<Dims> &box1, const Box<Dims> &box2) noexcept
for (size_t d = 0; d < dimensionsSize; ++d)
{
const Box<size_t> line1{box1.first[d], box1.first[d] + box1.second[d]};
const Box<size_t> line2{box2.first[d], box2.first[d] + box2.second[d]};
// start
if (line1.first < line2.first)
if (box1.first[d] < box2.first[d])
{
intersectionBox.first.push_back(line2.first);
intersectionBox.first.push_back(box2.first[d]);
}
else
{
intersectionBox.first.push_back(line1.first);
intersectionBox.first.push_back(box1.first[d]);
}
// end
if (line1.second > line2.second)
// end, must be inclusive
if (box1.second[d] > box2.second[d])
{
intersectionBox.second.push_back(line2.second);
intersectionBox.second.push_back(box2.second[d] - 1);
}
else
{
intersectionBox.second.push_back(line1.second);
intersectionBox.second.push_back(box1.second[d] - 1);
}
}
......@@ -110,7 +128,7 @@ Box<Dims> IntersectionBox(const Box<Dims> &box1, const Box<Dims> &box2) noexcept
}
size_t LinearIndex(const Box<Dims> &localBox, const Dims &point,
const bool isRowMajor, const bool isZeroIndex)
const bool isRowMajor, const bool isZeroIndex) noexcept
{
auto lf_RowZero = [](const Dims &count,
const Dims &normalizedPoint) -> size_t {
......@@ -146,8 +164,11 @@ size_t LinearIndex(const Box<Dims> &localBox, const Dims &point,
return linearIndex;
};
const Dims &start = localBox.first;
const Dims &count = localBox.second;
const Box<Dims> localBoxStartCount =
StartCountBox(localBox.first, localBox.second);
const Dims &start = localBoxStartCount.first;
const Dims &count = localBoxStartCount.second;
if (count.size() == 1)
{
......@@ -156,6 +177,7 @@ size_t LinearIndex(const Box<Dims> &localBox, const Dims &point,
// normalize the point
Dims normalizedPoint;
normalizedPoint.reserve(point.size());
std::transform(point.begin(), point.end(), start.begin(),
std::back_inserter(normalizedPoint), std::minus<size_t>());
......
......@@ -100,9 +100,20 @@ size_t NextExponentialSize(const size_t requiredSize, const size_t currentSize,
const float growthFactor) noexcept;
/**
* Returns the intersection box { start, end } from box1 and box2
* @param box1
* @param box2
* Converts a start, count box into a [start, end[ box where end = start + count
* @param start
* @param count
* @return [start, end[ box
*/
Box<Dims> StartEndBox(const Dims &start, const Dims &count) noexcept;
Box<Dims> StartCountBox(const Dims &start, const Dims &end) noexcept;
/**
* Returns the intersection box { start, end } where end is inclusive from box1
* and box2
* @param box1 {start, end} input (end is exclusive)
* @param box2 {start, end} input (end is exclusive)
* @return empty if not interception, otherwise intersection box
*/
Box<Dims> IntersectionBox(const Box<Dims> &box1,
......@@ -117,7 +128,7 @@ Box<Dims> IntersectionBox(const Box<Dims> &box1,
* @return linear index for contiguous memory
*/
size_t LinearIndex(const Box<Dims> &localBox, const Dims &point,
const bool isRowMajor, const bool isZeroIndex);
const bool isRowMajor, const bool isZeroIndex) noexcept;
} // end namespace adios2
......
......@@ -169,8 +169,7 @@ size_t BytesFactor(const std::string units, const bool debugMode)
return factor;
}
std::string OpenModeToString(const Mode openMode,
const bool oneLetter) noexcept
std::string OpenModeToString(const Mode openMode, const bool oneLetter) noexcept
{
std::string openModeString;
......@@ -210,4 +209,4 @@ std::string OpenModeToString(const Mode openMode,
return openModeString;
}
} // end namespace adios
} // end namespace adios2
......@@ -138,8 +138,9 @@ BP3Deserializer::GetSubFileInfo(const Variable<T> &variable) const
const size_t stepEnd =
stepStart + variable.m_StepCount; // inclusive or exclusive?
// selection, start and count
const Box<Dims> selection{variable.m_Start, variable.m_Count};
// selection = [start, end[
const Box<Dims> selectionBox =
StartEndBox(variable.m_Start, variable.m_Count);
for (size_t step = stepStart; step < stepEnd; ++step)
{
......@@ -157,12 +158,12 @@ BP3Deserializer::GetSubFileInfo(const Variable<T> &variable) const
const Characteristics<T> blockCharacteristics =
ReadElementIndexCharacteristics<T>(buffer, blockPosition);
const Box<Dims> blockDimensions{blockCharacteristics.Start,
blockCharacteristics.Count};
const Box<Dims> blockBox = StartEndBox(blockCharacteristics.Start,
blockCharacteristics.Count);
// check if they intersect
SubFileInfo info;
info.IntersectionBox = IntersectionBox(selection, blockDimensions);
info.IntersectionBox = IntersectionBox(selectionBox, blockBox);
if (info.IntersectionBox.first.empty() ||
info.IntersectionBox.second.empty())
......@@ -171,16 +172,15 @@ BP3Deserializer::GetSubFileInfo(const Variable<T> &variable) const
}
// if they intersect get info Seeks (first: start, second: end)
// TODO: map to sizeof(T)?
info.Seeks.first =
blockCharacteristics.Statistics.PayloadOffset +
LinearIndex(blockDimensions, info.IntersectionBox.first,
m_IsRowMajor, m_IsZeroIndex) *
sizeof(T);
info.Seeks.first = blockCharacteristics.Statistics.PayloadOffset +
LinearIndex(blockBox, info.IntersectionBox.first,
m_IsRowMajor, m_IsZeroIndex) *
sizeof(T);
info.Seeks.second =
blockCharacteristics.Statistics.PayloadOffset +
LinearIndex(blockDimensions, info.IntersectionBox.second,
m_IsRowMajor, m_IsZeroIndex) *
LinearIndex(blockBox, info.IntersectionBox.second, m_IsRowMajor,
m_IsZeroIndex) *
sizeof(T);
const size_t fileIndex = static_cast<const size_t>(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment