4#include <unordered_map>
5#include <unordered_set>
19namespace Autoscheduler {
22struct GlobalAccessAccumulator;
24struct SharedAccessAccumulator;
26struct LocalAccessAccumulator;
33 static constexpr double bytes_per_transaction = 32;
40 static constexpr double bytes_per_transaction = 128;
47 static constexpr double bytes_per_transaction = 32;
60 return total_num_transactions;
63 void add_access_info(
double num_requests,
double num_transactions_per_request,
double num_bytes_used_per_request) {
66 double total_transactions = num_requests * num_transactions_per_request;
68 double total_bytes_used = num_requests * num_bytes_used_per_request;
71 <<
"\ntotal_bytes_used = " << total_bytes_used
72 <<
"\ntotal_bytes = " << total_bytes
73 <<
"\ntotal_transactions = " << total_transactions
74 <<
"\nnum_transactions_per_request = " << num_transactions_per_request
75 <<
"\nnum_requests = " << num_requests;
77 update_totals(total_transactions, total_bytes_used, total_bytes);
81 total_num_transactions += other.total_num_transactions;
82 total_num_bytes_used += other.total_num_bytes_used;
83 total_num_bytes += other.total_num_bytes;
87 if (total_num_bytes == 0) {
91 double result = total_num_bytes_used / total_num_bytes;
97 void update_totals(
double num_transactions,
double num_bytes_used,
double num_bytes) {
99 total_num_bytes_used += num_bytes_used;
100 total_num_bytes += num_bytes;
103 double total_num_transactions = 0;
104 double total_num_bytes_used = 0;
105 double total_num_bytes = 0;
117 explicit Strides(
const std::vector<int64_t> &storage_strides)
118 : storage_strides{storage_strides} {
129 bool valid(
size_t loop_index)
const {
130 return is_valid[loop_index];
135 internal_assert(index_strides[loop_index].size() == storage_strides.size());
138 for (
size_t i = 0; i < storage_strides.size(); ++i) {
139 result += (
int64_t)(point * index_strides[loop_index][i]) * storage_strides[i];
141 return std::abs(result);
144 void dump(
bool verbose =
false) {
149 for (
size_t i = 0; i < storage_strides.size(); ++i) {
151 aslog(2) <<
"stride " << i <<
": invalid\n";
154 aslog(2) <<
"storage_stride " << i <<
": " << storage_strides[i] <<
"\n";
157 for (
size_t i = 0; i < index_strides.size(); ++i) {
158 for (
size_t j = 0; j < index_strides[i].size(); ++j) {
159 aslog(2) <<
"index_stride " << i <<
", storage_stride " << j <<
": " << index_strides[i][j] <<
" ";
166 void add(
const std::vector<double> &strides,
bool e) {
167 index_strides.push_back(strides);
168 is_valid.push_back(e);
171 std::vector<int64_t> storage_strides;
172 std::vector<std::vector<double>> index_strides;
173 std::vector<bool> is_valid;
178 : bytes_per_access{bytes_per_access},
179 dimensions{dimensions},
184 void operator()(
int thread_id,
int x,
int y,
int z,
int active,
bool last_thread) {
190 aslog(2) <<
"thread_id: " << thread_id <<
" (" << x <<
", " << y <<
", " << z <<
")\n";
193 int thread_ids[3] = {x, y, z};
195 for (
size_t i = 0; i < dimensions; ++i) {
196 if (!strides.valid(i)) {
200 byte += bytes_per_access * strides.offset(i, thread_ids[i]);
204 aslog(2) <<
"byte accessed: " <<
byte <<
"\n";
209 aslog(2) <<
"sectors accessed: ";
211 for (
int i = 0; i < bytes_per_access; ++i) {
213 aslog(2) << sector <<
" ";
215 sectors_accessed[sector].insert(
byte + i);
223 int num_transactions_per_request = sectors_accessed.size() + unknown_sectors;
229 aslog(2) <<
"num_transactions_per_request = " << num_transactions_per_request <<
"\n";
232 int num_bytes_used_per_request = 0;
233 for (
const auto §or : sectors_accessed) {
234 num_bytes_used_per_request += sector.second.size();
237 num_bytes_used_per_request += unknown_sectors * bytes_per_access;
243 aslog(2) <<
"num_requests_per_block = " << num_requests <<
"\n";
248 num_transactions_per_request,
249 num_bytes_used_per_request);
253 int bytes_per_access;
257 int unknown_sectors = 0;
258 std::unordered_map<int64_t, std::unordered_set<int64_t>> sectors_accessed;
263 : bytes_per_access{bytes_per_access},
264 dimensions{dimensions},
269 void operator()(
int thread_id,
int x,
int y,
int z,
int active,
bool last_thread) {
275 aslog(2) <<
"thread_id: " << thread_id <<
" (" << x <<
", " << y <<
", " << z <<
")\n";
278 int thread_ids[3] = {x, y, z};
280 for (
size_t i = 0; i < dimensions; ++i) {
281 if (!strides.valid(i)) {
285 byte += bytes_per_access * strides.offset(i, thread_ids[i]);
289 aslog(2) <<
"bytes accessed: ";
290 for (
int i = 0; i < bytes_per_access; ++i) {
291 aslog(2) <<
byte + i <<
" ";
297 aslog(2) <<
"banks accessed: ";
299 for (
int i = 0; i < bytes_per_access; ++i) {
303 aslog(2) << bank <<
" ";
305 bytes_accessed.insert(
byte + i);
306 bank_to_words_accessed[bank].insert(word);
314 int num_transactions_per_request = 0;
315 for (
const auto &bank : bank_to_words_accessed) {
316 num_transactions_per_request = std::max(num_transactions_per_request, (
int)bank.size());
319 num_transactions_per_request += unknown_banks;
325 aslog(2) <<
"num_transactions_per_request = " << num_transactions_per_request <<
"\n";
328 int num_bytes_used_per_request = bytes_accessed.size();
330 num_bytes_used_per_request += unknown_banks * bytes_per_access;
336 aslog(2) <<
"num_requests_per_block = " << num_requests <<
"\n";
341 num_transactions_per_request,
342 num_bytes_used_per_request);
346 int bytes_per_access;
350 int unknown_banks = 0;
351 std::unordered_set<int64_t> bytes_accessed;
352 std::array<std::unordered_set<int64_t>, 32> bank_to_words_accessed;
357 : bytes_per_access{bytes_per_access},
361 void operator()(
int thread_id,
int x,
int y,
int z,
int active,
bool last_thread) {
369 aslog(2) <<
"thread_id: " << thread_id <<
" (" << x <<
", " << y <<
", " << z <<
")\n";
374 int num_bytes_used_per_request = thread_count * bytes_per_access;
376 int num_transactions_per_request = sectors_accessed;
382 aslog(2) <<
"num_transactions_per_request = " << num_transactions_per_request <<
"\n";
389 aslog(2) <<
"num_requests_per_block = " << num_requests <<
"\n";
394 num_transactions_per_request,
395 num_bytes_used_per_request);
399 int bytes_per_access;
401 int thread_count = 0;
402 std::unordered_map<int64_t, std::unordered_set<int64_t>> sectors_accessed;
#define internal_assert(c)
typename MemTraits< T >::Accumulator Accumulator
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
signed __INT64_TYPE__ int64_t
GlobalAccessAccumulator(int bytes_per_access, size_t dimensions, const Strides &strides, bool verbose)
void operator()(int thread_id, int x, int y, int z, int active, bool last_thread)
void add_access_info(int num_requests, GlobalMemInfo &global_mem_info, bool is_tail_warp) const
void add_access_info(int num_requests, LocalMemInfo &local_mem_info, bool is_tail_warp) const
LocalAccessAccumulator(int bytes_per_access, bool verbose)
void operator()(int thread_id, int x, int y, int z, int active, bool last_thread)
void add_access_info(double num_requests, double num_transactions_per_request, double num_bytes_used_per_request)
static constexpr double bytes_per_transaction
double efficiency() const
double num_transactions() const
void add(const MemInfo< T > &other)
void operator()(int thread_id, int x, int y, int z, int active, bool last_thread)
SharedAccessAccumulator(int bytes_per_access, size_t dimensions, const Strides &strides, bool verbose)
void add_access_info(int num_requests, SharedMemInfo &shared_mem_info, bool is_tail_warp) const
bool valid(size_t loop_index) const
void dump(bool verbose=false)
void add_valid(const std::vector< double > &strides)
Strides(const std::vector< int64_t > &storage_strides)
int64_t offset(size_t loop_index, int64_t point) const