workspace_pool.h 1.75 KB
Newer Older
1
/**
Minjie Wang's avatar
Minjie Wang committed
2
 *  Copyright (c) 2017 by Contributors
3
4
 * @file workspace_pool.h
 * @brief Workspace pool utility.
Minjie Wang's avatar
Minjie Wang committed
5
 */
6
7
#ifndef DGL_RUNTIME_WORKSPACE_POOL_H_
#define DGL_RUNTIME_WORKSPACE_POOL_H_
Minjie Wang's avatar
Minjie Wang committed
8
9

#include <dgl/runtime/device_api.h>
10

11
#include <memory>
12
#include <vector>
Minjie Wang's avatar
Minjie Wang committed
13

14
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
15
namespace runtime {
16
/**
17
 * @brief A workspace pool to manage
Minjie Wang's avatar
Minjie Wang committed
18
19
20
21
22
23
24
25
26
27
28
 *
 *  \note We have the following assumption about backend temporal
 *   workspace allocation, and will optimize for such assumption,
 *   some of these assumptions can be enforced by the compiler.
 *
 *  - Only a few allocation will happen, and space will be released after use.
 *  - The release order is usually in reverse order of allocate
 *  - Repeative pattern of same allocations over different runs.
 */
class WorkspacePool {
 public:
29
  /**
30
31
32
   * @brief Create pool with specific device type and device.
   * @param device_type The device type.
   * @param device The device API.
Minjie Wang's avatar
Minjie Wang committed
33
   */
34
  WorkspacePool(DGLDeviceType device_type, std::shared_ptr<DeviceAPI> device);
35
  /** @brief destructor */
Minjie Wang's avatar
Minjie Wang committed
36
  ~WorkspacePool();
37
  /**
38
39
40
   * @brief Allocate temporal workspace.
   * @param ctx The context of allocation.
   * @param size The size to be allocated.
Minjie Wang's avatar
Minjie Wang committed
41
   */
42
  void* AllocWorkspace(DGLContext ctx, size_t size);
43
  /**
44
   * @brief Free temporal workspace in backend execution.
Minjie Wang's avatar
Minjie Wang committed
45
   *
46
47
   * @param ctx The context of allocation.
   * @param ptr The pointer to be freed.
Minjie Wang's avatar
Minjie Wang committed
48
   */
49
  void FreeWorkspace(DGLContext ctx, void* ptr);
Minjie Wang's avatar
Minjie Wang committed
50
51
52

 private:
  class Pool;
53
  /** @brief pool of device local array */
Minjie Wang's avatar
Minjie Wang committed
54
  std::vector<Pool*> array_;
55
  /** @brief device type this pool support */
56
  DGLDeviceType device_type_;
57
  /** @brief The device API */
Minjie Wang's avatar
Minjie Wang committed
58
59
60
61
  std::shared_ptr<DeviceAPI> device_;
};

}  // namespace runtime
62
}  // namespace dgl
63
#endif  // DGL_RUNTIME_WORKSPACE_POOL_H_