File: mem_arena.h

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (60 lines) | stat: -rw-r--r-- 1,900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
#pragma once
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <vector>

namespace torch {
namespace jit {
namespace tensorexpr {

class KernelScopedObject;

// An arena that manages all the underlying kernel-scoped objects.
class KernelArena {
 public:
  static KernelArena* GetCurrentKernelArena();
  static void SetCurrentKernelArena(KernelArena* new_arena);
  TORCH_API KernelArena() {}
  TORCH_API ~KernelArena();

 private:
  KernelArena(const KernelArena&) = delete;
  KernelArena& operator=(const KernelArena&) = delete;
  friend class KernelScopedObject;
  std::vector<KernelScopedObject*> kernel_objects_; // owned
};

// A RAII convenience wrapper on top of a kernel.
// It either creates or takes an existing Kernel and sets it as the current
// Kernel. When this object is destroyed, the previous Kernel is set as current,
// and the created kernel is freed. If the kernel was passed, it stays alive.
class KernelScope {
 public:
  TORCH_API KernelScope();
  TORCH_API explicit KernelScope(KernelArena* arena_);
  TORCH_API ~KernelScope();

 private:
  KernelScope(const KernelScope&) = delete;
  KernelScope& operator=(const KernelScope&) = delete;
  KernelArena* old_kernel_arena_ =
      nullptr; // previous arena, will be restored in destructor
  bool owning_ = false; // determines whether the arena will be freed along with
                        // the scope object
};

// The base object managed by the Kernel.
// The object must be created through "new", and when the Kernel is destroyed,
// All its registered objects are destroyed through "delete".
class TORCH_API KernelScopedObject {
 public:
  KernelScopedObject();
  virtual ~KernelScopedObject() = default;

 private:
  KernelScopedObject(const KernelScopedObject&) = delete;
  KernelScopedObject& operator=(const KernelScopedObject&) = delete;
};

} // namespace tensorexpr
} // namespace jit
} // namespace torch