Skip to content

Commit

Permalink
make trace printing deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Dec 13, 2021
1 parent 4fcc0dd commit 48b468d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
36 changes: 25 additions & 11 deletions include/llama/mapping/Trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

#include "Common.hpp"

#include <array>
#include <atomic>
#include <iostream>
#include <string>
#include <unordered_map>

namespace llama::mapping
{
Expand All @@ -25,16 +24,29 @@ namespace llama::mapping
LLAMA_FN_HOST_ACC_INLINE
explicit Trace(Mapping mapping, bool printOnDestruction = true)
: mapping(mapping)
, fieldHits{}
, printOnDestruction(printOnDestruction)
{
forEachLeafCoord<RecordDim>([&](auto rc) { fieldHits[recordCoordTags<RecordDim>(rc)] = 0; });
}

Trace(const Trace&) = delete;
auto operator=(const Trace&) -> Trace& = delete;

Trace(Trace&&) noexcept = default;
auto operator=(Trace&&) noexcept -> Trace& = default;
Trace(Trace&& other) noexcept : mapping(std::move(other.mapping)), printOnDestruction(other.printOnDestruction)
{
for(std::size_t i = 0; i < fieldHits.size(); i++)
fieldHits[i] = other.fieldHits[i].load();
other.printOnDestruction = false;
}

auto operator=(Trace&& other) noexcept -> Trace&
{
mapping = std::move(other.mapping);
printOnDestruction = other.printOnDestruction;
for(std::size_t i = 0; i < fieldHits.size(); i++)
fieldHits[i] = other.fieldHits[i].load();
other.printOnDestruction = false;
}

~Trace()
{
Expand All @@ -57,21 +69,23 @@ namespace llama::mapping
LLAMA_FN_HOST_ACC_INLINE auto blobNrAndOffset(ArrayIndex ai, RecordCoord<RecordCoords...> rc = {}) const
-> NrAndOffset
{
const static auto name = recordCoordTags<RecordDim>(RecordCoord<RecordCoords...>{});
fieldHits.at(name)++;

fieldHits[flatRecordCoord<RecordDim, RecordCoord<RecordCoords...>>]++;
LLAMA_FORCE_INLINE_RECURSIVE return mapping.blobNrAndOffset(ai, rc);
}

void print() const
{
std::cout << "Trace mapping, number of accesses:\n";
for(const auto& [k, v] : fieldHits)
std::cout << '\t' << k << ":\t" << v << '\n';
forEachLeafCoord<RecordDim>(
[this](auto rc)
{
std::cout << '\t' << recordCoordTags<RecordDim>(rc) << ":\t"
<< fieldHits[flatRecordCoord<RecordDim, decltype(rc)>] << '\n';
});
}

Mapping mapping;
mutable std::unordered_map<std::string, std::atomic<std::size_t>> fieldHits;
mutable std::array<std::atomic<std::size_t>, flatFieldCount<RecordDim>> fieldHits;
bool printOnDestruction;
};
} // namespace llama::mapping
27 changes: 14 additions & 13 deletions tests/heatmap_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ TEST_CASE("Trace.nbody")
auto particles = llama::allocView(llama::mapping::Trace{mapping, false});
updateAndMove(particles);
auto& hits = particles.mapping().fieldHits;
CHECK(hits.at("Pos.X") == 10400);
CHECK(hits.at("Pos.Y") == 10400);
CHECK(hits.at("Pos.Z") == 10400);
CHECK(hits.at("Vel.X") == 400);
CHECK(hits.at("Vel.Y") == 400);
CHECK(hits.at("Vel.Z") == 400);
CHECK(hits.at("Mass") == 10300);
CHECK(hits.size() == 7);
CHECK(hits[0] == 10400);
CHECK(hits[1] == 10400);
CHECK(hits[2] == 10400);
CHECK(hits[3] == 400);
CHECK(hits[4] == 400);
CHECK(hits[5] == 400);
CHECK(hits[6] == 10300);
};
run(llama::mapping::AlignedAoS<llama::ArrayExtents<N>, ParticleHeatmap>{});
run(llama::mapping::SingleBlobSoA<llama::ArrayExtents<N>, ParticleHeatmap>{});
Expand All @@ -87,13 +88,13 @@ TEST_CASE("Trace.print_dtor")
CHECK(
buffer.str()
== "Trace mapping, number of accesses:\n"
"\tMass:\t10300\n"
"\tVel.Z:\t400\n"
"\tVel.Y:\t400\n"
"\tVel.X:\t400\n"
"\tPos.Z:\t10400\n"
"\tPos.X:\t10400\n"
"\tPos.Y:\t10400\n"
"\tPos.X:\t10400\n");
"\tPos.Z:\t10400\n"
"\tVel.X:\t400\n"
"\tVel.Y:\t400\n"
"\tVel.Z:\t400\n"
"\tMass:\t10300\n");
}

namespace
Expand Down

0 comments on commit 48b468d

Please sign in to comment.