-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathArrayInfo.hpp
More file actions
144 lines (104 loc) · 4.19 KB
/
Copy pathArrayInfo.hpp
File metadata and controls
144 lines (104 loc) · 4.19 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#include <common/defines.hpp>
#include <af/device.h>
#include <af/dim4.hpp>
#include <cstddef>
#include <vector>
af::dim4 calcStrides(const af::dim4& parentDim);
af::dim4 getOutDims(const af::dim4& ldims, const af::dim4& rdims,
bool batchMode);
/// Array Arrayementation Info class
// This class is the base class to all Array objects. The purpose of this class
// was to have a way to retrieve basic information of an Array object without
// specifying what type the object is at compile time.
class ArrayInfo {
private:
// The devId variable stores information about the deviceId as well as the
// backend. The 8 LSBs (0-7) are used to store the device ID. The 09th LSB
// is set to 1 if backend is CPU The 10th LSB is set to 1 if backend is CUDA
// The 11th LSB is set to 1 if backend is OpenCL The 12th LSB is set to 1
// for oneAPI
// This information can be retrieved directly from an af_array by doing
// int* devId = reinterpret_cast<int*>(a); // a is an af_array
// af_backend backendID = *devId >> 8; // Returns 1, 2, 4 for CPU,
// CUDA or OpenCL respectively int deviceID = *devId & 0xff; //
// Returns devices ID between 0-255
// This is possible by doing a static_assert on devId
//
// This can be changed in the future if the need arises for more devices as
// this implementation is internal. Make sure to change the bit shift ops
// when such a change is being made
unsigned devId;
af_dtype type;
af::dim4 dim_size;
dim_t offset;
af::dim4 dim_strides;
bool is_sparse;
public:
ArrayInfo(unsigned id, af::dim4 size, dim_t offset_, af::dim4 stride,
af_dtype af_type);
ArrayInfo(unsigned id, af::dim4 size, dim_t offset_, af::dim4 stride,
af_dtype af_type, bool sparse);
ArrayInfo() = default;
ArrayInfo(const ArrayInfo& other) = default;
ArrayInfo(ArrayInfo&& other) = default;
ArrayInfo& operator=(ArrayInfo other) noexcept {
swap(other);
return *this;
}
void swap(ArrayInfo& other) noexcept {
using std::swap;
swap(devId, other.devId);
swap(type, other.type);
swap(dim_size, other.dim_size);
swap(offset, other.offset);
swap(dim_strides, other.dim_strides);
swap(is_sparse, other.is_sparse);
}
const af_dtype& getType() const { return type; }
dim_t getOffset() const { return offset; }
const af::dim4& strides() const { return dim_strides; }
dim_t elements() const { return dim_size.elements(); }
dim_t ndims() const { return dim_size.ndims(); }
const af::dim4& dims() const { return dim_size; }
size_t total() const { return offset + dim_strides[3] * dim_size[3]; }
unsigned getDevId() const;
void setId(int id) const;
void setId(int id);
af_backend getBackendId() const;
void resetInfo(const af::dim4& dims) {
dim_size = dims;
dim_strides = calcStrides(dims);
offset = 0;
}
void resetDims(const af::dim4& dims) { dim_size = dims; }
void modDims(const af::dim4& newDims);
void modStrides(const af::dim4& newStrides);
bool isEmpty() const;
bool isScalar() const;
bool isRow() const;
bool isColumn() const;
bool isVector() const;
bool isComplex() const;
bool isReal() const;
bool isDouble() const;
bool isSingle() const;
bool isHalf() const;
bool isRealFloating() const;
bool isFloating() const;
bool isInteger() const;
bool isBool() const;
bool isLinear() const;
bool isSparse() const;
};
af::dim4 toDims(const std::vector<af_seq>& seqs, const af::dim4& parentDims);
af::dim4 toOffset(const std::vector<af_seq>& seqs, const af::dim4& parentDims);
af::dim4 toStride(const std::vector<af_seq>& seqs, const af::dim4& parentDims);