MCPcopy Create free account
hub / github.com/apache/singa / traverse_unary

Function traverse_unary

src/core/tensor/tensor_math_cpp.h:141–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

139
140template <typename DType>
141void traverse_unary(const Tensor &in, Tensor *out,
142 std::function<DType(DType)> func) {
143 DType *outPtr = static_cast<DType *>(out->block()->mutable_data());
144 const DType *inPtr = static_cast<const DType *>(in.block()->data());
145 /*
146 vector<int> traversal_info = generate_traversal_info(in);
147 vector<int> shape_multipliers = generate_shape_multipliers(in);
148
149 for (size_t i = 0; i < in.Size(); i++) {
150 outPtr[i] = func(inPtr[traversal_info[in.shape().size()]]);
151 traverse_next(in, shape_multipliers, traversal_info, i + 1);
152 }
153 */
154 CHECK(in.shape() == out->shape());
155 if (in.stride() == out->stride()) {
156 for (size_t i = 0; i < in.Size(); i++) outPtr[i] = func(inPtr[i]);
157 } else {
158 // LOG(INFO) << "not equal stride";
159 size_t in_offset = 0, out_offset = 0;
160 vector<int> in_idx(in.nDim(), 0), out_idx(out->nDim(), 0);
161 for (size_t i = 0; i < Product(in.shape()); i++) {
162 outPtr[out_offset] = func(inPtr[in_offset]);
163 out_offset =
164 next_offset(out_offset, out->shape(), out->stride(), &out_idx);
165 in_offset = next_offset(in_offset, in.shape(), in.stride(), &in_idx);
166 }
167 }
168}
169
170template <typename DType>
171void traverse_binary(const Tensor &in1, const Tensor &in2, Tensor *out,

Callers

nothing calls this directly

Calls 8

ProductFunction · 0.85
next_offsetFunction · 0.85
mutable_dataMethod · 0.80
shapeMethod · 0.80
nDimMethod · 0.80
blockMethod · 0.45
dataMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected