-
Notifications
You must be signed in to change notification settings - Fork 555
Expand file tree
/
Copy pathcopy.cpp
More file actions
160 lines (142 loc) · 5.79 KB
/
Copy pathcopy.cpp
File metadata and controls
160 lines (142 loc) · 5.79 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************
* 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
********************************************************/
#include <Array.hpp>
#include <common/ArrayInfo.hpp>
#include <common/complex.hpp>
#include <common/half.hpp>
#include <copy.hpp>
#include <err_cpu.hpp>
#include <kernel/copy.hpp>
#include <platform.hpp>
#include <queue.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>
#include <cstdio>
#include <cstring>
using arrayfire::common::half; // NOLINT(misc-unused-using-decls) bug in
// clang-tidy
using arrayfire::common::is_complex;
namespace arrayfire {
namespace cpu {
template<typename T>
void copyData(T *to, const Array<T> &from) {
if (from.elements() == 0) { return; }
from.eval();
// Ensure all operations on 'from' are complete before copying data to host.
getQueue().sync();
if (from.isLinear()) {
// FIXME: Check for errors / exceptions
memcpy(to, from.get(), from.elements() * sizeof(T));
} else {
dim4 ostrides = calcStrides(from.dims());
kernel::stridedCopy<T>(to, ostrides, from.get(), from.dims(),
from.strides(), from.ndims() - 1);
}
}
template<typename T>
Array<T> copyArray(const Array<T> &A) {
Array<T> out = createEmptyArray<T>(A.dims());
if (A.elements() > 0) { getQueue().enqueue(kernel::copy<T, T>, out, A); }
return out;
}
template<typename inType, typename outType>
void copyArray(Array<outType> &out, Array<inType> const &in) {
static_assert(
!(is_complex<inType>::value && !is_complex<outType>::value),
"Cannot copy from complex Array<T> to a non complex Array<T>");
getQueue().enqueue(kernel::copy<outType, inType>, out, in);
}
#define INSTANTIATE(T) \
template void copyData<T>(T * data, const Array<T> &from); \
template Array<T> copyArray<T>(const Array<T> &A);
INSTANTIATE(float)
INSTANTIATE(double)
INSTANTIATE(cfloat)
INSTANTIATE(cdouble)
INSTANTIATE(int)
INSTANTIATE(uint)
INSTANTIATE(schar)
INSTANTIATE(uchar)
INSTANTIATE(char)
INSTANTIATE(intl)
INSTANTIATE(uintl)
INSTANTIATE(short)
INSTANTIATE(ushort)
INSTANTIATE(half)
#define INSTANTIATE_COPY_ARRAY(SRC_T) \
template void copyArray<SRC_T, float>(Array<float> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, double>(Array<double> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, cfloat>(Array<cfloat> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, cdouble>(Array<cdouble> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, int>(Array<int> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, uint>(Array<uint> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, intl>(Array<intl> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, uintl>(Array<uintl> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, short>(Array<short> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, ushort>(Array<ushort> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, schar>(Array<schar> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, uchar>(Array<uchar> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, char>(Array<char> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, half>(Array<half> & dst, \
Array<SRC_T> const &src);
INSTANTIATE_COPY_ARRAY(float)
INSTANTIATE_COPY_ARRAY(double)
INSTANTIATE_COPY_ARRAY(int)
INSTANTIATE_COPY_ARRAY(uint)
INSTANTIATE_COPY_ARRAY(intl)
INSTANTIATE_COPY_ARRAY(uintl)
INSTANTIATE_COPY_ARRAY(schar)
INSTANTIATE_COPY_ARRAY(uchar)
INSTANTIATE_COPY_ARRAY(char)
INSTANTIATE_COPY_ARRAY(ushort)
INSTANTIATE_COPY_ARRAY(short)
INSTANTIATE_COPY_ARRAY(half)
#define INSTANTIATE_COPY_ARRAY_COMPLEX(SRC_T) \
template void copyArray<SRC_T, cfloat>(Array<cfloat> & dst, \
Array<SRC_T> const &src); \
template void copyArray<SRC_T, cdouble>(Array<cdouble> & dst, \
Array<SRC_T> const &src);
INSTANTIATE_COPY_ARRAY_COMPLEX(cfloat)
INSTANTIATE_COPY_ARRAY_COMPLEX(cdouble)
template<typename T>
T getScalar(const Array<T> &in) {
in.eval();
getQueue().sync();
return in.get()[0];
}
#define INSTANTIATE_GETSCALAR(T) template T getScalar(const Array<T> &in);
INSTANTIATE_GETSCALAR(float)
INSTANTIATE_GETSCALAR(double)
INSTANTIATE_GETSCALAR(cfloat)
INSTANTIATE_GETSCALAR(cdouble)
INSTANTIATE_GETSCALAR(int)
INSTANTIATE_GETSCALAR(uint)
INSTANTIATE_GETSCALAR(schar)
INSTANTIATE_GETSCALAR(uchar)
INSTANTIATE_GETSCALAR(char)
INSTANTIATE_GETSCALAR(intl)
INSTANTIATE_GETSCALAR(uintl)
INSTANTIATE_GETSCALAR(short)
INSTANTIATE_GETSCALAR(ushort)
INSTANTIATE_GETSCALAR(half)
} // namespace cpu
} // namespace arrayfire