Skip to content

Commit

Permalink
Mapspaces now accommodate min factors.
Browse files Browse the repository at this point in the history
  • Loading branch information
angshuman-parashar committed Oct 5, 2023
1 parent f1daa2e commit 6b41058
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/mapspaces/subspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class IndexFactorizationSpace
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors =
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>>(),
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors =
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>>(),
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors =
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>>());

unsigned long GetFactor(uint128_t nest_id, problem::Shape::FlattenedDimensionID dim, unsigned level);
Expand All @@ -80,6 +82,7 @@ class ResidualIndexFactorizationSpace
std::map<problem::Shape::FlattenedDimensionID, std::uint64_t> cofactors_order,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors,
std::vector<unsigned long int> remainders = {},
std::vector<unsigned long int> remainders_ix = {}
);
Expand Down
2 changes: 2 additions & 0 deletions include/util/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Factors
Factors(const unsigned long n, const int order, std::map<unsigned, unsigned long> given);

void PruneMax(std::map<unsigned, unsigned long>& max);
void PruneMin(std::map<unsigned, unsigned long>& min);

std::vector<unsigned long>& operator[](int index);

Expand Down Expand Up @@ -119,6 +120,7 @@ class ResidualFactors


void PruneMax();
void PruneMin();

std::vector<std::vector<unsigned long>> operator[](int index);

Expand Down
24 changes: 21 additions & 3 deletions src/mapspaces/ruby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void Ruby::InitIndexFactorizationSpace()
{
auto user_factors = constraints_.Factors();
auto user_max_factors = constraints_.MaxFactors();
auto user_min_factors = constraints_.MinFactors();
auto user_max_remainders = constraints_.MaxRemainders();

assert(user_factors.size() <= arch_props_.TilingLevels());
Expand All @@ -141,6 +142,7 @@ void Ruby::InitIndexFactorizationSpace()

std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors;
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors;
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors;
std::vector<bool> exhausted_um_loops(int(problem::GetShape()->NumFlattenedDimensions), false);

// Find user-specified fixed factors.
Expand Down Expand Up @@ -183,7 +185,23 @@ void Ruby::InitIndexFactorizationSpace()
}
}

//Find spatial levels and their fanouts
// Find user-specified min factors.
for (unsigned level = 0; level < arch_props_.TilingLevels(); level++)
{
auto it = user_min_factors.find(level);
if (it != user_min_factors.end())
{
// Some min factors exist for this level.
for (auto& factor : it->second)
{
auto& dimension = factor.first;
auto& min = factor.second;
minfactors[dimension][level] = min;
}
}
}

// Find spatial levels and their fanouts
std::vector<unsigned long int> remainders;
std::vector<unsigned long int> remainders_ix;
for (uint64_t level = arch_props_.TilingLevels(); level >= 1; level--)
Expand All @@ -198,8 +216,8 @@ void Ruby::InitIndexFactorizationSpace()
}
}

// We're now ready to initialize the object.
index_factorization_space_.Init(workload_, cofactors_order, prefactors, maxfactors, remainders, remainders_ix);
// We're now ready to initialize the object.
index_factorization_space_.Init(workload_, cofactors_order, prefactors, maxfactors, minfactors, remainders, remainders_ix);

// Update the size of the mapspace.
size_[int(mapspace::Dimension::IndexFactorization)] = index_factorization_space_.Size();
Expand Down
10 changes: 9 additions & 1 deletion src/mapspaces/subspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ IndexFactorizationSpace::IndexFactorizationSpace() :
void IndexFactorizationSpace::Init(const problem::Workload &workload,
std::map<problem::Shape::FlattenedDimensionID, std::uint64_t> cofactors_order,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors)
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors)
{
// Sanity check on input pre-factors.
for (auto& prefactor_set: prefactors)
Expand Down Expand Up @@ -92,6 +93,9 @@ void IndexFactorizationSpace::Init(const problem::Workload &workload,
if (maxfactors.find(dim) != maxfactors.end())
dimension_factors_[idim].PruneMax(maxfactors[dim]);

if (minfactors.find(dim) != minfactors.end())
dimension_factors_[idim].PruneMin(minfactors[dim]);

counter_base[idim] = dimension_factors_[idim].size();
}

Expand Down Expand Up @@ -131,6 +135,7 @@ void ResidualIndexFactorizationSpace::Init(const problem::Workload &workload,
std::map<problem::Shape::FlattenedDimensionID, std::uint64_t> cofactors_order,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors,
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors,
std::vector<unsigned long int> remainders,
std::vector<unsigned long int> remainders_ix)
{
Expand Down Expand Up @@ -170,6 +175,9 @@ void ResidualIndexFactorizationSpace::Init(const problem::Workload &workload,
if (maxfactors.find(dim) != maxfactors.end())
dimension_factors_[idim].PruneMax();

if (minfactors.find(dim) != minfactors.end())
dimension_factors_[idim].PruneMin();

counter_base[idim] = dimension_factors_[idim].size();

}
Expand Down
20 changes: 19 additions & 1 deletion src/mapspaces/uber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void Uber::InitIndexFactorizationSpace()
{
auto user_factors = constraints_.Factors();
auto user_max_factors = constraints_.MaxFactors();
auto user_min_factors = constraints_.MinFactors();

assert(user_factors.size() <= arch_props_.TilingLevels());

Expand All @@ -140,6 +141,7 @@ void Uber::InitIndexFactorizationSpace()

std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> prefactors;
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> maxfactors;
std::map<problem::Shape::FlattenedDimensionID, std::map<unsigned, unsigned long>> minfactors;
std::vector<bool> exhausted_um_loops(int(problem::GetShape()->NumFlattenedDimensions), false);

// Find user-specified fixed factors.
Expand Down Expand Up @@ -182,8 +184,24 @@ void Uber::InitIndexFactorizationSpace()
}
}

// Find user-specified min factors.
for (unsigned level = 0; level < arch_props_.TilingLevels(); level++)
{
auto it = user_min_factors.find(level);
if (it != user_min_factors.end())
{
// Some min factors exist for this level.
for (auto& factor : it->second)
{
auto& dimension = factor.first;
auto& min = factor.second;
minfactors[dimension][level] = min;
}
}
}

// We're now ready to initialize the object.
index_factorization_space_.Init(workload_, cofactors_order, prefactors, maxfactors);
index_factorization_space_.Init(workload_, cofactors_order, prefactors, maxfactors, minfactors);

// Update the size of the mapspace.
size_[int(mapspace::Dimension::IndexFactorization)] = index_factorization_space_.Size();
Expand Down
46 changes: 46 additions & 0 deletions src/util/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,40 @@ void Factors::PruneMax(std::map<unsigned, unsigned long>& max)
}
}

void Factors::PruneMin(std::map<unsigned, unsigned long>& min)
{
// Prune the vector of cofactor sets by removing those sets that have factors
// outside user-specified min/max range. We should really have done this during
// MultiplicativeSplitRecursive. However, the "given" map complicates things
// because given factors may be scattered, and we'll need a map table to
// find the original rank from the "compressed" rank seen by
// MultiplicativeSplitRecursive. Doing it now is slower but cleaner and less
// bug-prone.

auto cofactors_it = cofactors_.begin();
while (cofactors_it != cofactors_.end())
{
bool illegal = false;
for (auto& min_factor : min)
{
auto index = min_factor.first;
auto min = min_factor.second;
assert(index <= cofactors_it->size());
auto value = cofactors_it->at(index);
if (value < min)
{
illegal = true;
break;
}
}

if (illegal)
cofactors_it = cofactors_.erase(cofactors_it);
else
cofactors_it++;
}
}

std::vector<unsigned long>& Factors::operator[](int index)
{
return cofactors_[index];
Expand Down Expand Up @@ -530,6 +564,18 @@ void ResidualFactors::PruneMax()

}

void ResidualFactors::PruneMin()
{
// Prune the vector of cofactor sets by removing those sets that have factors
// outside user-specified min/max range. We should really have done this during
// MultiplicativeSplitRecursive. However, the "given" map complicates things
// because given factors may be scattered, and we'll need a map table to
// find the original rank from the "compressed" rank seen by
// MultiplicativeSplitRecursive. Doing it now is slower but cleaner and less
// bug-prone.

}

ResidualFactors::ResidualFactors() : n_(0) {}

/***
Expand Down

0 comments on commit 6b41058

Please sign in to comment.