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

#include <dgl/runtime/device_api.h>
#include <vector>
11
#include <memory>
Minjie Wang's avatar
Minjie Wang committed
12

13
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
namespace runtime {
/*!
 * \brief A workspace pool to manage
 *
 *  \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:
  /*!
   * \brief Create pool with specific device type and device.
   * \param device_type The device type.
   * \param device The device API.
   */
33
  WorkspacePool(DGLDeviceType device_type, std::shared_ptr<DeviceAPI> device);
Minjie Wang's avatar
Minjie Wang committed
34
35
36
37
38
39
40
  /*! \brief destructor */
  ~WorkspacePool();
  /*!
   * \brief Allocate temporal workspace.
   * \param ctx The context of allocation.
   * \param size The size to be allocated.
   */
41
  void* AllocWorkspace(DGLContext ctx, size_t size);
Minjie Wang's avatar
Minjie Wang committed
42
43
44
45
46
47
  /*!
   * \brief Free temporal workspace in backend execution.
   *
   * \param ctx The context of allocation.
   * \param ptr The pointer to be freed.
   */
48
  void FreeWorkspace(DGLContext ctx, void* ptr);
Minjie Wang's avatar
Minjie Wang committed
49
50
51
52
53
54

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

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