! * \brief memory allocation plan held by a variable * * A MemAllocPlan is a view (i.e. with offset and layout) for some Chunk; Memory * sharing between vars is implemented by sharing a Chunk of their mem plans. */
| 32 | * sharing between vars is implemented by sharing a Chunk of their mem plans. |
| 33 | */ |
| 34 | class MemAllocPlan final : public json::Serializable, public NonCopyableObj { |
| 35 | public: |
| 36 | /*! |
| 37 | * \brief identifier for allocated memory |
| 38 | * |
| 39 | * Each Chunk object corresponds to an allocated memory chunk. Memory |
| 40 | * forwarding and force updating are implemented by sharing Chunk |
| 41 | * objects between vars. |
| 42 | * |
| 43 | * If mem_alloc_status is not invalid, the memory region for this chunk |
| 44 | * is owner_var->dev_tensor().storage(). |
| 45 | */ |
| 46 | class Chunk : public NonCopyableObj { |
| 47 | friend class MemAllocPlan; |
| 48 | friend class VarDevMemDefragmenter; |
| 49 | |
| 50 | std::atomic_size_t m_refcnt; |
| 51 | size_t m_size; |
| 52 | |
| 53 | public: |
| 54 | /*! |
| 55 | * \brief memory allocation status for this chunk |
| 56 | * |
| 57 | * Allocation status can either be INVALID, FROM_OWNER_VAR, or an |
| 58 | * offset in a static allocation buffer. This status is compactly |
| 59 | * represented by an integer value. No error check is performed in |
| 60 | * the accessors. |
| 61 | * |
| 62 | * Note that for static_offset, it is set in |
| 63 | * SeqMemOptimizer::plan_chunk_allocation() and accessed in |
| 64 | * VarNodeMemManager::make_static_var_tensor_from_alloc_plan() |
| 65 | */ |
| 66 | class MemAllocStatus { |
| 67 | static constexpr size_t INVALID = 0, FROM_OWNER_VAR = 1, OFFSET = 2; |
| 68 | size_t m_val = INVALID; |
| 69 | |
| 70 | public: |
| 71 | //! whether memory is not allocated yet |
| 72 | bool is_invalid() const { return m_val == INVALID; } |
| 73 | |
| 74 | //! whether memory comes from owner_var->dev_tensor() |
| 75 | bool is_from_owner_var() const { return m_val == FROM_OWNER_VAR; } |
| 76 | |
| 77 | //! whether memory is statically allocated |
| 78 | bool is_static_offset() const { return m_val >= OFFSET; } |
| 79 | |
| 80 | size_t static_offset() const { return m_val - OFFSET; } |
| 81 | |
| 82 | void set_invalid() { m_val = INVALID; } |
| 83 | |
| 84 | void set_from_owner_var() { m_val = FROM_OWNER_VAR; } |
| 85 | |
| 86 | void set_static_offset(size_t offset) { m_val = offset + OFFSET; } |
| 87 | }; |
| 88 | |
| 89 | //! var that first creates this chunk |
| 90 | VarNode* const owner_var; |
| 91 |