device_api.h 4.35 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2016 by Contributors
3
 * \file device_api.h
4
 * \brief Abstract device memory management API
5 6 7 8
 */
#ifndef TVM_RUNTIME_DEVICE_API_H_
#define TVM_RUNTIME_DEVICE_API_H_

9
#include <string>
10 11
#include "./packed_func.h"
#include "./c_runtime_api.h"
12 13 14

namespace tvm {
namespace runtime {
15 16 17
/*!
 * \brief the query type into GetAttr
 */
18 19 20
enum DeviceAttrKind : int {
  kExist = 0,
  kMaxThreadsPerBlock = 1,
21 22
  kWarpSize = 2,
  kComputeVersion = 3
23
};
24 25 26 27

/*! \brief Number of bytes each allocation must align to */
constexpr int kAllocAlignment = 64;

28 29 30
/*! \brief Number of bytes each allocation must align to in temporary allocation */
constexpr int kTempAllocaAlignment = 64;

31 32 33
/*! \brief Maximum size that can be allocated on stack */
constexpr int kMaxStackAlloca = 1024;

34 35 36 37
/*!
 * \brief TVM Runtime Device API, abstracts the device
 *  specific interface for memory management.
 */
38 39 40 41 42
class DeviceAPI {
 public:
  /*! \brief virtual destructor */
  virtual ~DeviceAPI() {}
  /*!
43 44
   * \brief Set the environment device id to ctx
   * \param ctx The context to be set.
45
   */
46
  virtual void SetDevice(TVMContext ctx) = 0;
47 48
  /*!
   * \brief Get attribute of specified device.
49
   * \param ctx The device context
50 51
   * \param kind The result kind
   * \param rv The return value.
52
   * \sa DeviceAttrKind
53
   */
54
  virtual void GetAttr(TVMContext ctx, DeviceAttrKind kind, TVMRetValue* rv) = 0;
55
  /*!
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   * \brief Allocate a data space on device.
   * \param ctx The device context to perform operation.
   * \param size The size of the memory
   * \param alignment The alignment of the memory.
   * \return The allocated device pointer
   */
  virtual void* AllocDataSpace(TVMContext ctx, size_t size, size_t alignment) = 0;
  /*!
   * \brief Free a data space on device.
   * \param ctx The device context to perform operation.
   * \param ptr The data space.
   */
  virtual void FreeDataSpace(TVMContext ctx, void* ptr) = 0;
  /*!
   * \brief copy data from one place to another
   * \param from The source array.
72
   * \param from_offset The byte offeset in the from.
73
   * \param to The target array.
74
   * \param to_offset The byte offset in the to.
75 76 77
   * \param size The size of the memory
   * \param ctx_from The source context
   * \param ctx_to The target context
78
   * \param stream Optional stream object.
79 80
   */
  virtual void CopyDataFromTo(const void* from,
81
                              size_t from_offset,
82
                              void* to,
83
                              size_t to_offset,
84 85 86 87 88 89 90 91 92 93
                              size_t size,
                              TVMContext ctx_from,
                              TVMContext ctx_to,
                              TVMStreamHandle stream) = 0;
  /*!
   * \brief Synchronize the stream
   * \param ctx The context to perform operation.
   * \param stream The stream to be sync.
   */
  virtual void StreamSync(TVMContext ctx, TVMStreamHandle stream) = 0;
94
  /*!
95 96 97 98 99 100
   * \brief Set the stream
   * \param ctx The context to set stream.
   * \param stream The stream to be set.
   */
  virtual void SetStream(TVMContext ctx, TVMStreamHandle stream) {}
  /*!
101 102 103 104 105 106 107 108 109 110 111 112 113
   * \brief Allocate temporal workspace for backend execution.
   *
   *  \note We have the following assumption about backend temporal
   *   workspace allocation, and backend will optimize for such assumption:
   *
   *  - Only a few allocation will happen, and space will be released after use.
   *  - The release order is usually in reverse order of allocate (stack style).
   *  - Repeative pattern of same allocations over different runs.
   *  - Workspace should not overlap between different threads(i.e. be threadlocal)
   *
   * \param ctx The context of allocation.
   * \param size The size to be allocated.
   */
114
  TVM_DLL virtual void* AllocWorkspace(TVMContext ctx, size_t size);
115 116 117 118 119 120
  /*!
   * \brief Free temporal workspace in backend execution.
   *
   * \param ctx The context of allocation.
   * \param ptr The pointer to be freed.
   */
121
  TVM_DLL virtual void FreeWorkspace(TVMContext ctx, void* ptr);
122
  /*!
123 124 125 126 127
   * \brief Get device API base don context.
   * \param ctx The context
   * \param allow_missing Whether allow missing
   * \return The corresponding device API.
   */
128
  TVM_DLL static DeviceAPI* Get(TVMContext ctx, bool allow_missing = false);
129
};
130

131 132
/*! \brief The device type bigger than this is RPC device */
constexpr int kRPCSessMask = 128;
133 134 135
}  // namespace runtime
}  // namespace tvm
#endif  // TVM_RUNTIME_DEVICE_API_H_