Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit bff75f5

Browse files
author
Sven Verdoolaege
committed
extract out shared part of mapping to block or thread identifiers
After the latest changes, it makes sense again to extract out the common parts of mapBlocksForward and mapThreadsBackward. The shared part is made private, however, since the user is expected to call mapBlocksForward and mapThreadsBackward only.
1 parent 51229f5 commit bff75f5

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

tc/core/polyhedral/cuda/mapped_scop.cc

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,51 @@ template <>
106106
const CudaDim& mappingSize<mapping::ThreadId>(const MappedScop* mscop) {
107107
return mscop->numThreads;
108108
}
109+
} // namespace
110+
111+
template <typename MappingTypeId>
112+
void MappedScop::mapRemaining(detail::ScheduleTree* tree, size_t nMapped) {
113+
size_t nToMap = mappingSize<MappingTypeId>(this).view.size();
114+
if (nMapped >= nToMap) {
115+
return;
116+
}
109117

110-
// Map the affine functions in "list" to the _new_ parameters
111-
// identified by ids[i] and limited by 0 <= ids[i] < extent[i]. The
112-
// parameters must not be present in the space of partial schedules in "list"
113-
// and extents must be non-zero. The mapping corresponds to inserting a filter
118+
std::unordered_set<MappingTypeId, typename MappingTypeId::Hash> ids;
119+
for (size_t i = nMapped; i < nToMap; ++i) {
120+
ids.insert(MappingTypeId::makeId(i));
121+
}
122+
auto root = scop_->scheduleRoot();
123+
auto domain = activeDomainPoints(root, tree);
124+
auto filter = makeFixRemainingZeroFilter(domain, ids);
125+
auto mapping = detail::ScheduleTree::makeMappingFilter(filter, ids);
126+
insertNodeAbove(root, tree, std::move(mapping));
127+
}
128+
129+
// Map the elements in "list" to successive blocks or thread identifiers,
130+
// with the first element mapped to identifier X. The extents are obtained
131+
// from the initial elements of numBlocks or numThreads. The identifiers
132+
// must not be present in the space of the partial schedules in "list" and
133+
// extents must be non-zero. The mapping corresponds to inserting a filter
114134
// node with condition 'list % extent = ids'.
115-
// The number of elements in "list" and "ids" needs to be the same,
116-
// but "extent" is allowed to have extra elements, in which case
117-
// only the initial elements are used.
118135
// The mapping is inserted above "tree".
119136
//
120-
// Returns a pointer to the updated node (below the inserted filter)
137+
// Return a pointer to the updated node (below the inserted filter)
121138
// for call chaining purposes.
122-
template <typename MappingIdType>
123-
detail::ScheduleTree* mapToParametersWithExtent(
124-
detail::ScheduleTree* root,
139+
template <typename MappingTypeId>
140+
detail::ScheduleTree* MappedScop::map(
125141
detail::ScheduleTree* tree,
126-
isl::union_pw_aff_list list,
127-
const std::vector<MappingIdType>& ids,
128-
const CudaDimView& extent) {
129-
CHECK_EQ(ids.size(), list.n());
130-
CHECK_LE(list.n(), extent.size()) << "dimension overflow";
142+
isl::union_pw_aff_list list) {
143+
size_t nToMap = list.n();
144+
const auto& extent = mappingSize<MappingTypeId>(this).view;
145+
CHECK_LE(nToMap, extent.size()) << "dimension overflow";
131146

147+
auto root = scop_->scheduleRoot();
132148
auto domain = activeDomainPoints(root, tree).universe();
133149
auto filter = domain;
134150

135-
std::unordered_set<MappingIdType, typename MappingIdType::Hash> idSet;
136-
for (size_t i = 0; i < ids.size(); ++i) {
137-
auto id = ids[i];
151+
std::unordered_set<MappingTypeId, typename MappingTypeId::Hash> idSet;
152+
for (size_t i = 0; i < nToMap; ++i) {
153+
auto id = MappingTypeId::makeId(i);
138154
auto upa = list.get(i);
139155
// Introduce the "mapping" parameter after checking it is not already
140156
// present in the schedule space.
@@ -151,26 +167,10 @@ detail::ScheduleTree* mapToParametersWithExtent(
151167
}
152168

153169
auto mapping = detail::ScheduleTree::makeMappingFilter(filter, idSet);
154-
return insertNodeAbove(root, tree, std::move(mapping))->child({0});
155-
}
156-
} // namespace
157-
158-
template <typename MappingTypeId>
159-
void MappedScop::mapRemaining(detail::ScheduleTree* tree, size_t nMapped) {
160-
size_t nToMap = mappingSize<MappingTypeId>(this).view.size();
161-
if (nMapped >= nToMap) {
162-
return;
163-
}
170+
tree = insertNodeAbove(root, tree, std::move(mapping))->child({0});
164171

165-
std::unordered_set<MappingTypeId, typename MappingTypeId::Hash> ids;
166-
for (size_t i = nMapped; i < nToMap; ++i) {
167-
ids.insert(MappingTypeId::makeId(i));
168-
}
169-
auto root = scop_->scheduleRoot();
170-
auto domain = activeDomainPoints(root, tree);
171-
auto filter = makeFixRemainingZeroFilter(domain, ids);
172-
auto mapping = detail::ScheduleTree::makeMappingFilter(filter, ids);
173-
insertNodeAbove(root, tree, std::move(mapping));
172+
mapRemaining<MappingTypeId>(tree, nToMap);
173+
return tree;
174174
}
175175

176176
detail::ScheduleTree* MappedScop::mapBlocksForward(
@@ -179,16 +179,9 @@ detail::ScheduleTree* MappedScop::mapBlocksForward(
179179
auto bandNode = band->elemAs<detail::ScheduleTreeElemBand>();
180180
CHECK(bandNode) << "expected a band, got " << *band;
181181

182-
auto root = scop_->scheduleRoot();
183-
std::vector<mapping::BlockId> mapped;
184-
for (size_t i = 0; i < nToMap; ++i) {
185-
mapped.emplace_back(mapping::BlockId::makeId(i));
186-
}
187182
auto list = bandNode->mupa_.get_union_pw_aff_list();
188183
list = list.drop(nToMap, list.n() - nToMap);
189-
band = mapToParametersWithExtent(root, band, list, mapped, numBlocks.view);
190-
mapRemaining<mapping::BlockId>(band, nToMap);
191-
return band;
184+
return map<mapping::BlockId>(band, list);
192185
}
193186

194187
// Uses as many blockSizes elements as outer coincident dimensions in the
@@ -375,16 +368,9 @@ detail::ScheduleTree* MappedScop::mapThreadsBackward(
375368
auto ctx = band->ctx_;
376369
insertNodeBelow(band, detail::ScheduleTree::makeThreadSpecificMarker(ctx));
377370

378-
auto root = scop_->scheduleRoot();
379-
std::vector<mapping::ThreadId> mapped;
380-
for (size_t i = 0; i < nToMap; ++i) {
381-
mapped.emplace_back(mapping::ThreadId::makeId(i));
382-
}
383371
auto list = bandNode->mupa_.get_union_pw_aff_list().reverse();
384372
list = list.drop(nToMap, list.n() - nToMap);
385-
band = mapToParametersWithExtent(root, band, list, mapped, numThreads.view);
386-
mapRemaining<mapping::ThreadId>(band, nToMap);
387-
return band;
373+
return map<mapping::ThreadId>(band, list);
388374
}
389375

390376
size_t MappedScop::mapToThreads(detail::ScheduleTree* band) {

tc/core/polyhedral/cuda/mapped_scop.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ class MappedScop {
134134
}
135135

136136
private:
137+
// Map the elements in "list" to successive blocks or thread identifiers,
138+
// with the first element mapped to identifier X.
139+
// Return a pointer to the updated node (below the inserted filter)
140+
// for call chaining purposes.
141+
template <typename MappingTypeId>
142+
detail::ScheduleTree* map(
143+
detail::ScheduleTree* tree,
144+
isl::union_pw_aff_list list);
137145
// Map "band" to block identifiers and then scale
138146
// the band members by "tileSizes".
139147
void mapToBlocksAndScaleBand(

0 commit comments

Comments
 (0)