FreeTensor
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_ARRAY_H
2#define FREE_TENSOR_ARRAY_H
3
4#include <cstdint>
5#include <vector>
6
7#include <driver/device.h>
8#include <tensor.h>
9
10namespace freetensor {
11
15struct ArrayCopy {
17 uint8_t *ptr_ = nullptr;
18 bool borrowed_ = false;
19
20 ArrayCopy(const Ref<Device> &device, uint8_t *ptr, bool borrowed)
21 : device_(device), ptr_(ptr), borrowed_(borrowed) {}
22};
23
44class Array {
45 // Identical data on different devices.
46 std::vector<ArrayCopy> ptrs_;
47
48 // Temorary copy of the data used by AccessType::InputMutable. The data may
49 // become different after the program runs.
50 std::optional<ArrayCopy> tempPtr_;
51
52 size_t size_ = 0, nElem_ = 0;
53 std::vector<size_t> shape_;
54 DataType dtype_;
55 bool dontDropBorrow_, moved_;
56
57 private:
72 Array(const std::vector<size_t> &shape, DataType dtype,
73 bool dontDropBorrow = false, bool moved = false);
74
75 public:
79 static Array moveFromRaw(void *ptr, const std::vector<size_t> &shape,
80 DataType dtype, const Ref<Device> &device);
81
88 static Array borrowFromRaw(void *ptr, const std::vector<size_t> &shape,
89 DataType dtype, const Ref<Device> &device,
90 bool dontDropBorrow, bool moved);
91
92 ~Array();
93
94 Array(Array &&);
95 Array(const Array &) = delete;
96
98 Array &operator=(const Array &) = delete;
99
100 size_t size() const { return size_; }
101 size_t nElem() const { return nElem_; }
102 const std::vector<size_t> &shape() const { return shape_; }
103 DataType dtype() const { return dtype_; }
104
105 bool dontDropBorrow() const { return dontDropBorrow_; }
106 void setDontDropBorrow(bool flag) { dontDropBorrow_ = flag; }
107
108 bool moved() const { return moved_; }
109 void setMoved(bool flag) { moved_ = flag; }
110
111 void *rawSharedTo(const Ref<Device> &device);
112 void *rawMovedTo(const Ref<Device> &device);
113 void *rawInitTo(const Ref<Device> &device);
114 void *rawTemporarilyCopiedTo(const Ref<Device> &device);
115
120 void makePrivateCopy();
121};
122
123} // namespace freetensor
124
125#endif // FREE_TENSOR_ARRAY_H
Definition: array.h:44
size_t size() const
Definition: array.h:100
void makePrivateCopy()
Definition: array.cc:353
void * rawInitTo(const Ref< Device > &device)
Definition: array.cc:305
void * rawTemporarilyCopiedTo(const Ref< Device > &device)
Definition: array.cc:338
Array & operator=(const Array &)=delete
void setDontDropBorrow(bool flag)
Definition: array.h:106
void setMoved(bool flag)
Definition: array.h:109
bool dontDropBorrow() const
Definition: array.h:105
size_t nElem() const
Definition: array.h:101
Array(const Array &)=delete
void * rawMovedTo(const Ref< Device > &device)
Definition: array.cc:255
DataType dtype() const
Definition: array.h:103
const std::vector< size_t > & shape() const
Definition: array.h:102
~Array()
Definition: array.cc:185
void * rawSharedTo(const Ref< Device > &device)
Definition: array.cc:227
bool moved() const
Definition: array.h:108
Array & operator=(Array &&)
Definition: array.cc:212
static Array borrowFromRaw(void *ptr, const std::vector< size_t > &shape, DataType dtype, const Ref< Device > &device, bool dontDropBorrow, bool moved)
Definition: array.cc:177
static Array moveFromRaw(void *ptr, const std::vector< size_t > &shape, DataType dtype, const Ref< Device > &device)
Definition: array.cc:170
Definition: data_type.h:106
Definition: ref.h:24
Definition: allocator.h:9
Definition: array.h:15
bool borrowed_
Definition: array.h:18
uint8_t * ptr_
Definition: array.h:17
ArrayCopy(const Ref< Device > &device, uint8_t *ptr, bool borrowed)
Definition: array.h:20
Ref< Device > device_
Definition: array.h:16