device_api.h 4.31 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 21 22
enum DeviceAttrKind : int {
  kExist = 0,
  kMaxThreadsPerBlock = 1,
  kWarpSize = 2
};
23 24 25 26

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

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

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

33 34 35 36
/*!
 * \brief TVM Runtime Device API, abstracts the device
 *  specific interface for memory management.
 */
37 38 39 40 41
class DeviceAPI {
 public:
  /*! \brief virtual destructor */
  virtual ~DeviceAPI() {}
  /*!
42 43
   * \brief Set the environment device id to ctx
   * \param ctx The context to be set.
44
   */
45
  virtual void SetDevice(TVMContext ctx) = 0;
46 47
  /*!
   * \brief Get attribute of specified device.
48
   * \param ctx The device context
49 50
   * \param kind The result kind
   * \param rv The return value.
51
   * \sa DeviceAttrKind
52
   */
53
  virtual void GetAttr(TVMContext ctx, DeviceAttrKind kind, TVMRetValue* rv) = 0;
54
  /*!
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   * \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.
71
   * \param from_offset The byte offeset in the from.
72
   * \param to The target array.
73
   * \param to_offset The byte offset in the to.
74 75 76
   * \param size The size of the memory
   * \param ctx_from The source context
   * \param ctx_to The target context
77
   * \param stream Optional stream object.
78 79
   */
  virtual void CopyDataFromTo(const void* from,
80
                              size_t from_offset,
81
                              void* to,
82
                              size_t to_offset,
83 84 85 86 87 88 89 90 91 92
                              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;
93
  /*!
94 95 96 97 98 99
   * \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) {}
  /*!
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
   * \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.
   */
  virtual void* AllocWorkspace(TVMContext ctx, size_t size);
  /*!
   * \brief Free temporal workspace in backend execution.
   *
   * \param ctx The context of allocation.
   * \param ptr The pointer to be freed.
   */
  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.
   */
  static DeviceAPI* Get(TVMContext ctx, bool allow_missing = false);
128
};
129

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