|
| 1 | +//===- KernelSchedule.h - Declare the class for ops schedule ----*- C++ -*-===// |
| 2 | +// |
| 3 | +// Compigra is under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file defines class for schedule functions. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef KERNEL_SCHEDULE_H |
| 14 | +#define KERNEL_SCHEDULE_H |
| 15 | + |
| 16 | +#include "compigra/Transforms/SatMapItDATE2023InputGen/PrintSatMapItDAG.h" |
| 17 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 18 | +using namespace mlir; |
| 19 | + |
| 20 | +#ifdef HAVE_GUROBI |
| 21 | +#include "gurobi_c++.h" |
| 22 | +#endif |
| 23 | + |
| 24 | +/// CGRA base scheduler class, where T is the struct that define the placement |
| 25 | +/// of the operation. |
| 26 | +namespace compigra { |
| 27 | + |
| 28 | +template <typename T> class CGRAKernelScheduler { |
| 29 | +public: |
| 30 | + CGRAKernelScheduler(unsigned maxReg, unsigned nRow, unsigned nCol) |
| 31 | + : maxReg(maxReg), nRow(nRow), nCol(nCol) {} |
| 32 | + |
| 33 | + virtual LogicalResult createSchedulerAndSolve() { return success(); }; |
| 34 | + |
| 35 | + /// Get the schedule result |
| 36 | + std::map<Operation *, T> getSolution() { return solution; } |
| 37 | + |
| 38 | + /// Get the schedule result for the operation |
| 39 | + T getSolution(Operation *op) { return solution[op]; } |
| 40 | + |
| 41 | +protected: |
| 42 | + // maximum number of registers in the PE; |
| 43 | + unsigned maxReg; |
| 44 | + |
| 45 | + // number of rows and columns in the CGRA |
| 46 | + unsigned nRow, nCol; |
| 47 | + |
| 48 | + std::map<Operation *, T> solution; |
| 49 | +}; |
| 50 | + |
| 51 | +struct ScheduleUnit { |
| 52 | + int time; |
| 53 | + int pe; |
| 54 | + int reg; |
| 55 | +}; |
| 56 | + |
| 57 | +/// OpenEdge kernel scheduler class |
| 58 | +class OpenEdgeKernelScheduler : public CGRAKernelScheduler<ScheduleUnit> { |
| 59 | +public: |
| 60 | + /// Initialize the region which contains the operations and the maximum number |
| 61 | + /// of PEs |
| 62 | + OpenEdgeKernelScheduler(Region ®ion, unsigned maxReg, unsigned grid) |
| 63 | + : CGRAKernelScheduler<ScheduleUnit>(maxReg, grid, grid), region(region) {} |
| 64 | + |
| 65 | + LogicalResult createSchedulerAndSolve() override; |
| 66 | + |
| 67 | +protected: |
| 68 | + Region ®ion; |
| 69 | + |
| 70 | +public: |
| 71 | +#ifdef HAVE_GUROBI |
| 72 | + |
| 73 | + void initObjectiveFunction(GRBModel &model, GRBVar &funcStartT, |
| 74 | + GRBVar &funcEndT, |
| 75 | + std::map<Operation *, GRBVar> &timeOpVar, |
| 76 | + std::map<Block *, GRBVar> &timeBlkEntry, |
| 77 | + std::map<Block *, GRBVar> &timeBlkExit); |
| 78 | + void initVariables(GRBModel &model, std::map<Block *, GRBVar> &timeBlkEntry, |
| 79 | + std::map<Block *, GRBVar> &timeBlkExit, |
| 80 | + std::map<Operation *, GRBVar> &timeOpVar, |
| 81 | + std::map<Operation *, GRBVar> &spaceOpVar); |
| 82 | + void initKnownSchedule(GRBModel &model, |
| 83 | + std::map<Operation *, GRBVar> &timeOpVar, |
| 84 | + std::map<Operation *, GRBVar> &spaceOpVar); |
| 85 | + void initOpTimeConstraints(GRBModel &model, |
| 86 | + std::map<Operation *, GRBVar> &timeOpVar, |
| 87 | + std::map<Block *, GRBVar> &timeBlkEntry, |
| 88 | + std::map<Block *, GRBVar> &timeBlkExit); |
| 89 | + void initOpSpaceConstraints(GRBModel &model, |
| 90 | + std::map<Operation *, GRBVar> &spaceOpVar); |
| 91 | + void initOpTimeSpaceConstraints(GRBModel &model, |
| 92 | + std::map<Operation *, GRBVar> &timeOpVar, |
| 93 | + std::map<Operation *, GRBVar> &spaceOpVar); |
| 94 | +#endif |
| 95 | + |
| 96 | +private: |
| 97 | + int getConnectedBlock(int block, std::string direction); |
| 98 | + void writeOpResult(Operation *op, int time, int pe, int reg) { |
| 99 | + ScheduleUnit unit = {time, pe, reg}; |
| 100 | + solution[op] = unit; |
| 101 | + } |
| 102 | + |
| 103 | +public: |
| 104 | + /// Assign the schedule results from SAT-MapIt printout to the operations |
| 105 | + void assignSchedule(mlir::Block::OpListType &ops, |
| 106 | + std::map<int, Instruction> instructions); |
| 107 | + |
| 108 | + // void printKownSchedule(); |
| 109 | + std::map<Operation *, Instruction> knownRes; |
| 110 | +}; |
| 111 | +} // namespace compigra |
| 112 | + |
| 113 | +#endif // KERNEL_SCHEDULE_H |
0 commit comments