atomic_add.h 2.8 KB
Newer Older
1
2
/*!
 * \file tl/op/atomic_add.h
3
 * \brief Atomic addition operations for concurrent memory updates
4
5
6
7
8
 */

#ifndef TVM_TL_OP_ATOMIC_ADD_H_
#define TVM_TL_OP_ATOMIC_ADD_H_

9
#include "operator.h"
10
11
12
13
14
15
16
#include "parallel.h"

namespace tvm {
namespace tl {

using namespace tir;

17
/// Node class for atomic addition operations
18
class AtomicAddNode : public TileOperatorNode {
19
public:
20
21
22
23
  Buffer src, dst; ///< Source and destination buffers
  Array<Range> src_range,
      dst_range;          ///< Access ranges for source and destination
  IntImm coalesced_width; ///< Width for memory coalescing optimization
24

25
  mutable ParallelOp par_op_; ///< Associated parallel operation
26
27
28
29
30
31
32
33
  static constexpr const char *_type_key = "tl.AtomicAdd";
  TVM_DECLARE_FINAL_OBJECT_INFO(AtomicAddNode, TileOperatorNode);

  Stmt Lower(const LowerArgs &T, arith::Analyzer *analyzer) const;
  LayoutMap InferLayout(const LayoutInferArgs &T, InferLevel level) const;

  static const Op &Get();
  TileOperator Clone() const;
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  static void RegisterReflection() {
    namespace refl = tvm::ffi::reflection;
    refl::ObjectDef<AtomicAddNode>()
        .def_ro("src", &AtomicAddNode::src)
        .def_ro("dst", &AtomicAddNode::dst)
        .def_ro("src_range", &AtomicAddNode::src_range)
        .def_ro("dst_range", &AtomicAddNode::dst_range)
        .def_ro("coalesced_width", &AtomicAddNode::coalesced_width);
  }

  bool SEqualReduce(const AtomicAddNode *other, SEqualReducer equal) const {
    return equal(src, other->src) && equal(dst, other->dst) &&
           equal(src_range, other->src_range) &&
           equal(dst_range, other->dst_range) &&
           equal(coalesced_width, other->coalesced_width);
  }

  void SHashReduce(SHashReducer hash_reduce) const {
    hash_reduce(src);
    hash_reduce(dst);
    hash_reduce(src_range);
    hash_reduce(dst_range);
    hash_reduce(coalesced_width);
  }

  static constexpr bool _type_has_method_sequal_reduce = true;
  static constexpr bool _type_has_method_shash_reduce = true;

63
protected:
64
  /// Create SIMT-style parallel loop structure
65
  For MakeSIMTLoop(arith::Analyzer *analyzer) const;
66
  /// Generate iteration variables for loop nest
67
  Array<IterVar> MakeIterVars() const;
68
  /// Generate buffer indices from iteration variables
69
  Array<PrimExpr> MakeIndices(const Array<IterVar> &ivs, int src_dst) const;
70
  /// Create boundary predicate for memory safety
71
72
  PrimExpr MakePredicate(arith::Analyzer *analyzer, const Array<IterVar> &ivs,
                         Array<PrimExpr> extents, int src_dst) const;
73
};
74

75
/// Wrapper class for atomic addition operations
76
77
78
79
80
class AtomicAdd : public TileOperator {
public:
  TVM_DEFINE_OBJECT_REF_METHODS(AtomicAdd, TileOperator, AtomicAddNode);
  TVM_DLL AtomicAdd(Array<PrimExpr> args, BufferMap vmap);
  static const Op &Get();
81
82
83
84
85
86
};

} // namespace tl
} // namespace tvm

#endif //  TVM_TL_OP_ATOMIC_ADD_H_