2022-07-31 21:43:26 +08:00
|
|
|
#pragma once
|
2022-09-01 20:30:12 +08:00
|
|
|
#include "utils/exception.h"
|
2022-07-31 21:43:26 +08:00
|
|
|
#include <cassert>
|
2022-08-08 15:52:07 +08:00
|
|
|
#include <functional>
|
2022-07-31 21:43:26 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-08-07 21:12:17 +08:00
|
|
|
namespace infini {
|
2022-07-31 21:43:26 +08:00
|
|
|
using std::list;
|
|
|
|
using std::map;
|
2022-08-15 15:08:56 +08:00
|
|
|
using std::optional;
|
2022-07-31 21:43:26 +08:00
|
|
|
using std::pair;
|
|
|
|
using std::set;
|
|
|
|
using std::string;
|
|
|
|
using std::tie;
|
|
|
|
using std::to_string;
|
|
|
|
using std::tuple;
|
|
|
|
using std::unordered_map;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
// Aliases
|
|
|
|
using dtype = float;
|
2022-08-15 15:08:56 +08:00
|
|
|
using HashType = uint64_t; // compatible with std::hash
|
2022-07-31 21:43:26 +08:00
|
|
|
|
2022-08-06 15:58:40 +08:00
|
|
|
// Metaprogramming utilities
|
2022-08-05 12:50:34 +08:00
|
|
|
#define _CAT(A, B) A##B
|
|
|
|
#define _SELECT(NAME, NUM) _CAT(NAME##_, NUM)
|
|
|
|
#define _GET_COUNT(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, COUNT, ...) COUNT
|
|
|
|
#define _VA_SIZE(...) _GET_COUNT(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
|
|
|
|
#define _VA_SELECT(NAME, ...) _SELECT(NAME, _VA_SIZE(__VA_ARGS__))(__VA_ARGS__)
|
|
|
|
|
2022-08-06 15:58:40 +08:00
|
|
|
// Assert: conditions should have no side effect
|
2023-03-01 14:15:02 +08:00
|
|
|
#define _IT_ASSERT_2(condition, info) \
|
2023-09-14 14:19:45 +08:00
|
|
|
static_cast<bool>(condition) \
|
|
|
|
? void(0) \
|
|
|
|
: throw ::infini::Exception( \
|
|
|
|
std::string("[") + __FILE__ + ":" + std::to_string(__LINE__) + \
|
|
|
|
"] Assertion failed (" + #condition + "): " + info)
|
|
|
|
#define _IT_ASSERT_1(condition) _IT_ASSERT_2(condition, "")
|
2022-08-05 12:50:34 +08:00
|
|
|
#define IT_ASSERT(...) _VA_SELECT(_IT_ASSERT, __VA_ARGS__)
|
2022-08-23 16:55:59 +08:00
|
|
|
|
|
|
|
#define IT_TODO_HALT() _IT_ASSERT_2(false, "Unimplemented")
|
|
|
|
#define IT_TODO_HALT_MSG(msg) _IT_ASSERT_2(false, msg)
|
2023-03-01 14:15:02 +08:00
|
|
|
#define IT_ASSERT_TODO(condition) _IT_ASSERT_2(condition, "Unimplemented")
|
2022-08-07 21:12:17 +08:00
|
|
|
#define IT_TODO_SKIP() puts("Unimplemented " __FILE__ ":" __LINE__)
|
2022-08-05 12:50:34 +08:00
|
|
|
|
2022-08-06 15:58:40 +08:00
|
|
|
// Other utilities
|
|
|
|
|
|
|
|
// std::to_underlying is avaiable since C++23
|
|
|
|
template <typename T> auto enum_to_underlying(T e) {
|
|
|
|
return static_cast<std::underlying_type_t<T>>(e);
|
|
|
|
}
|
|
|
|
|
2022-08-17 14:16:01 +08:00
|
|
|
template <typename T> std::string vecToString(const std::vector<T> &vec) {
|
|
|
|
std::string ret;
|
|
|
|
ret.append("[");
|
|
|
|
for (auto d : vec) {
|
|
|
|
ret.append(std::to_string(d));
|
2022-08-25 11:29:16 +08:00
|
|
|
ret.append(",");
|
2022-08-17 14:16:01 +08:00
|
|
|
}
|
2022-08-25 11:29:16 +08:00
|
|
|
if (!vec.empty())
|
|
|
|
ret.pop_back();
|
2022-08-17 14:16:01 +08:00
|
|
|
ret.append("]");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-29 21:37:07 +08:00
|
|
|
double timeit(
|
|
|
|
const std::function<void()> &func,
|
2023-10-30 15:04:16 +08:00
|
|
|
const std::function<void(void)> &sync = []() {}, int warmupRounds = 10,
|
|
|
|
int timingRounds = 10);
|
2022-08-07 21:12:17 +08:00
|
|
|
|
|
|
|
} // namespace infini
|