Skip to content

Commit

Permalink
move RecordCoord string conversion into Core
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Nov 1, 2021
1 parent 344b9ec commit 0f6714b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 89 deletions.
25 changes: 25 additions & 0 deletions include/llama/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <boost/core/demangle.hpp>
#include <iostream>
#include <string>
#include <type_traits>

namespace llama
Expand Down Expand Up @@ -654,4 +655,28 @@ namespace llama
using MergedRecordDims = typename decltype(internal::mergeRecordDimsImpl(
boost::mp11::mp_identity<RecordDimA>{},
boost::mp11::mp_identity<RecordDimB>{}))::type;

/// Returns the tags interspersed by '.' represented by the given record coord in the given record dimension.
template<typename RecordDim, std::size_t... Coords>
auto recordCoordTags(RecordCoord<Coords...>) -> std::string
{
using Tags = GetTags<RecordDim, RecordCoord<Coords...>>;

std::string r;
boost::mp11::mp_for_each<Tags>(
[&](auto tag)
{
using Tag = decltype(tag);
if(!r.empty())
r += '.';
if constexpr(isRecordCoord<Tag>)
{
static_assert(Tag::size == 1);
r += std::to_string(Tag::front); // handle array indices
}
else
r += structName(tag);
});
return r;
}
} // namespace llama
78 changes: 12 additions & 66 deletions include/llama/DumpMapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,6 @@ namespace llama
return {Coords...};
}

template<typename Tag>
auto tagToString(Tag tag)
{
return structName(tag);
}

// handle array indices
template<std::size_t N>
auto tagToString(RecordCoord<N>)
{
return std::to_string(N);
}

template<typename RecordDim, std::size_t... CoordsBefore, std::size_t CoordCurrent, std::size_t... CoordsAfter>
void collectTagsAsStrings(
std::vector<std::string>& v,
RecordCoord<CoordsBefore...> /*before*/,
RecordCoord<CoordCurrent, CoordsAfter...> /*after*/)
{
using Tag = GetTag<RecordDim, RecordCoord<CoordsBefore..., CoordCurrent>>;
v.push_back(tagToString(Tag{}));
if constexpr(sizeof...(CoordsAfter) > 0)
collectTagsAsStrings<RecordDim>(
v,
RecordCoord<CoordsBefore..., CoordCurrent>{},
RecordCoord<CoordsAfter...>{});
}

template<typename RecordDim, std::size_t... Coords>
auto tagsAsStrings(RecordCoord<Coords...>) -> std::vector<std::string>
{
std::vector<std::string> v;
collectTagsAsStrings<RecordDim>(v, RecordCoord<>{}, RecordCoord<Coords...>{});
return v;
}

inline auto color(const std::vector<std::size_t>& recordCoord) -> std::size_t
{
auto c = boost::hash<std::vector<std::size_t>>{}(recordCoord) &0xFFFFFF;
Expand All @@ -86,24 +50,12 @@ namespace llama
}
}

inline auto formatDDTags(const std::vector<std::string>& tags)
{
std::string s;
for(const auto& tag : tags)
{
if(!s.empty())
s += ".";
s += tag;
}
return s;
}

template<std::size_t Dim>
struct FieldBox
{
ArrayIndex<Dim> arrayIndex;
std::vector<std::size_t> recordCoord;
std::vector<std::string> recordTags;
std::string recordTags;
NrAndOffset nrAndOffset;
std::size_t size;
};
Expand All @@ -122,7 +74,7 @@ namespace llama
infos.push_back(
{ai,
internal::toVec(rc),
internal::tagsAsStrings<RecordDim>(rc),
recordCoordTags<RecordDim>(rc),
mapping.blobNrAndOffset(ai, rc),
sizeof(GetType<RecordDim, decltype(rc)>)});
});
Expand All @@ -149,6 +101,12 @@ namespace llama
}
return boxes;
}

inline auto cssClass(std::string tags)
{
std::replace(begin(tags), end(tags), '.', '_');
return tags;
};
} // namespace internal

/// Returns an SVG image visualizing the memory layout created by the given mapping. The created memory blocks are
Expand Down Expand Up @@ -240,7 +198,7 @@ namespace llama
x + width / 2,
y + byteSizeInPixel * 3 / 4,
internal::formatArrayIndex(info.arrayIndex),
internal::formatDDTags(info.recordTags));
info.recordTags);
if(cropBoxes)
svg += R"(</svg>
)";
Expand Down Expand Up @@ -273,18 +231,6 @@ namespace llama
[](const auto& a, const auto& b) { return a.nrAndOffset == b.nrAndOffset; }),
end(infos));

auto cssClass = [](const std::vector<std::string>& tags)
{
std::string s;
for(const auto& tag : tags)
{
if(!s.empty())
s += "_";
s += tag;
}
return s;
};

std::string html;
html += fmt::format(
R"(<!DOCTYPE html>
Expand Down Expand Up @@ -325,7 +271,7 @@ namespace llama
background-color: #{:X};
}}
)",
cssClass(internal::tagsAsStrings<RecordDim>(rc)),
internal::cssClass(recordCoordTags<RecordDim>(rc)),
byteSizeInPixel * size,
internal::color(internal::toVec(rc)));
});
Expand Down Expand Up @@ -355,9 +301,9 @@ namespace llama
}
html += fmt::format(
R"(<div class="box {0}" title="{1} {2}">{1} {2}</div>)",
cssClass(info.recordTags),
internal::cssClass(info.recordTags),
internal::formatArrayIndex(info.arrayIndex),
internal::formatDDTags(info.recordTags));
info.recordTags);
}
html += R"(</body>
</html>)";
Expand Down
25 changes: 2 additions & 23 deletions include/llama/mapping/Trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,12 @@
#include "Common.hpp"

#include <atomic>
#include <boost/core/demangle.hpp>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

namespace llama::mapping
{
namespace internal
{
template<typename RecordDim, std::size_t... Coords>
auto coordName(RecordCoord<Coords...>) -> std::string
{
using Tags = GetTags<RecordDim, RecordCoord<Coords...>>;

std::string r;
boost::mp11::mp_for_each<Tags>(
[&](auto tag)
{
if(!r.empty())
r += '.';
r += structName(tag);
});
return r;
}
} // namespace internal

/// Forwards all calls to the inner mapping. Traces all accesses made through this mapping and prints a summary on
/// destruction.
/// \tparam Mapping The type of the inner mapping.
Expand All @@ -48,7 +27,7 @@ namespace llama::mapping
: mapping(mapping)
, printOnDestruction(printOnDestruction)
{
forEachLeafCoord<RecordDim>([&](auto rc) { fieldHits[internal::coordName<RecordDim>(rc)] = 0; });
forEachLeafCoord<RecordDim>([&](auto rc) { fieldHits[recordCoordTags<RecordDim>(rc)] = 0; });
}

Trace(const Trace&) = delete;
Expand Down Expand Up @@ -78,7 +57,7 @@ namespace llama::mapping
LLAMA_FN_HOST_ACC_INLINE auto blobNrAndOffset(ArrayIndex ai, RecordCoord<RecordCoords...> rc = {}) const
-> NrAndOffset
{
const static auto name = internal::coordName<RecordDim>(RecordCoord<RecordCoords...>{});
const static auto name = recordCoordTags<RecordDim>(RecordCoord<RecordCoords...>{});
fieldHits.at(name)++;

LLAMA_FORCE_INLINE_RECURSIVE return mapping.blobNrAndOffset(ai, rc);
Expand Down

0 comments on commit 0f6714b

Please sign in to comment.