async_transferer.h 1.46 KB
Newer Older
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
61
62
63
64
65
66
67
/*!
 *  Copyright (c) 2020 by Contributors
 * \file array/async_transferer.h
 * \brief The AsyncTransferer class for copying the data to/from the GPU on a
 * separate stream. 
 */


#ifndef DGL_DATALOADING_ASYNC_TRANSFERER_H_
#define DGL_DATALOADING_ASYNC_TRANSFERER_H_

#include <dgl/runtime/c_runtime_api.h>
#include <dgl/runtime/ndarray.h>
#include <dgl/runtime/object.h>
#include <unordered_map>
#include <memory>

namespace dgl {
namespace dataloading {

class AsyncTransferer : public runtime::Object {
 public:
  using TransferId = int;

  explicit AsyncTransferer(
      DGLContext ctx);
  ~AsyncTransferer();

  // disable copying
  AsyncTransferer(
      const AsyncTransferer&) = delete;
  AsyncTransferer& operator=(
      const AsyncTransferer&) = delete;

  TransferId StartTransfer(
      runtime::NDArray data,
      DGLContext ctx);

  runtime::NDArray Wait(
      TransferId id);

  static constexpr const char* _type_key = "ndarray.AsyncTransferer";
  DGL_DECLARE_OBJECT_TYPE_INFO(AsyncTransferer, Object);

 private:
  struct Event;
  struct Transfer {
    std::unique_ptr<Event> event;
    bool recorded;
    runtime::NDArray src;
    runtime::NDArray dst;
  };

  DGLContext ctx_;
  TransferId next_id_;
  std::unordered_map<TransferId, Transfer> transfers_;
  DGLStreamHandle stream_;

  TransferId GenerateId();
};

DGL_DEFINE_OBJECT_REF(AsyncTransfererRef, AsyncTransferer);

}  // namespace dataloading
}  // namespace dgl

#endif  // DGL_DATALOADING_ASYNC_TRANSFERER_H_