2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Created by remote on 2018-09-20.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/ops.h>
|
|
|
|
#include <loops/pairwise_transform.h>
|
|
|
|
#include <types/types.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <helpers/LoopKind.h>
|
|
|
|
#include <math/templatemath.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <helpers/shape.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
|
|
|
#include <helpers/OmpLaunchHelper.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
using namespace simdOps;
|
|
|
|
|
|
|
|
namespace functions {
|
|
|
|
namespace pairwise_transforms {
|
|
|
|
|
|
|
|
template <typename X, typename Y, typename Z>
|
2020-05-09 07:06:14 +02:00
|
|
|
void PairWiseTransform<X, Y, Z>::exec(const int opNum,
|
|
|
|
const void *x, Nd4jLong xEws,
|
|
|
|
const void *y, Nd4jLong yEws,
|
|
|
|
void *z, Nd4jLong zEws,
|
|
|
|
void *extraParams,
|
|
|
|
Nd4jLong n,
|
|
|
|
const uint64_t start,const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
DISPATCH_BY_OPNUM_TTT(exec, PARAMS(x,
|
|
|
|
xEws,
|
|
|
|
y,
|
|
|
|
yEws,
|
|
|
|
z,
|
|
|
|
zEws,
|
|
|
|
extraParams,
|
2019-11-13 15:15:18 +01:00
|
|
|
n, start, stop), PAIRWISE_TRANSFORM_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename X, typename Y, typename Z>
|
|
|
|
template <typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void PairWiseTransform<X, Y, Z>::exec(const void *vx, Nd4jLong xEws,
|
|
|
|
const void *vy, Nd4jLong yEws,
|
2019-06-06 14:21:15 +02:00
|
|
|
void *vz, Nd4jLong zEws,
|
|
|
|
void *vextraParams,
|
2019-11-13 15:15:18 +01:00
|
|
|
const Nd4jLong n,
|
|
|
|
const uint64_t start,
|
|
|
|
const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
|
|
|
auto y = reinterpret_cast<const Y *>(vy);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
|
|
|
auto extraParams = reinterpret_cast<Z *>(vextraParams);
|
|
|
|
|
|
|
|
if (xEws == 1 && yEws == 1 && zEws == 1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i] = OpType::op(x[i], y[i], extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i*zEws] = OpType::op(x[i*xEws], y[i*yEws], extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename X, typename Y, typename Z>
|
2020-05-09 07:06:14 +02:00
|
|
|
void PairWiseTransform<X, Y, Z>::exec(const int opNum,
|
|
|
|
const void *x, const Nd4jLong *xShapeInfo,
|
|
|
|
const void *y, const Nd4jLong *yShapeInfo,
|
|
|
|
void *z, const Nd4jLong *zShapeInfo,
|
|
|
|
void *extraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-06-06 14:21:15 +02:00
|
|
|
DISPATCH_BY_OPNUM_TTT(exec, PARAMS(x,
|
|
|
|
xShapeInfo,
|
|
|
|
y,
|
|
|
|
yShapeInfo,
|
|
|
|
z,
|
|
|
|
zShapeInfo,
|
2019-11-13 15:15:18 +01:00
|
|
|
extraParams, start, stop),
|
2019-06-06 14:21:15 +02:00
|
|
|
PAIRWISE_TRANSFORM_OPS);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename X, typename Y, typename Z>
|
|
|
|
template <typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void PairWiseTransform<X, Y, Z>::exec(const void *vx, const Nd4jLong* xShapeInfo,
|
|
|
|
const void *vy, const Nd4jLong* yShapeInfo,
|
|
|
|
void *vz, const Nd4jLong* zShapeInfo,
|
|
|
|
void *vextraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
|
|
|
auto y = reinterpret_cast<const Y *>(vy);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
|
|
|
auto extraParams = reinterpret_cast<Z *>(vextraParams);
|
|
|
|
|
|
|
|
auto n = shape::length(xShapeInfo);
|
|
|
|
auto xEws = shape::elementWiseStride(xShapeInfo);
|
|
|
|
auto yEws = shape::elementWiseStride(yShapeInfo);
|
|
|
|
auto zEws = shape::elementWiseStride(zShapeInfo);
|
|
|
|
|
|
|
|
|
|
|
|
if (shape::isScalar(yShapeInfo)) {
|
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
const bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for(auto i = start; i < stop; i++) {
|
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
z[offset] = OpType::op(x[offset], y[0], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-09-11 19:12:09 +02:00
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
const bool canCastZ = sd::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for(auto i = start; i < stop; i++) {
|
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
|
|
|
z[zOffset] = OpType::op(x[xOffset], y[0], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
const sd::LoopKind::Kind kindOfLoop = sd::LoopKind::deduceKindOfLoopXYZ(xShapeInfo, yShapeInfo, zShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
const bool sameShapesXY = shape::shapeEquals(xShapeInfo, yShapeInfo);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
if ((kindOfLoop == sd::LoopKind::EWS1 || kindOfLoop == sd::LoopKind::EWSNONZERO) && sameShapesXY) {
|
2019-11-13 15:15:18 +01:00
|
|
|
exec<OpType>(x, xEws, y, yEws, z, zEws, extraParams, n, start, stop);
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2020-03-02 10:49:41 +01:00
|
|
|
else if ((kindOfLoop == sd::LoopKind::EWS1 || kindOfLoop == sd::LoopKind::EWSNONZERO) && !sameShapesXY) { //not same shape
|
2019-11-13 15:15:18 +01:00
|
|
|
exec<OpType>(x, xEws, y, yEws, z, zEws, extraParams, shape::length(yShapeInfo), start, stop);
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if(shape::haveSameShapeAndStrides(xShapeInfo, yShapeInfo) && shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
z[offset] = OpType::op(x[offset], y[offset], extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(shape::haveSameShapeAndStrides(xShapeInfo, yShapeInfo)) {
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
bool canCastZ = sd::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
|
|
|
z[zOffset] = OpType::op(x[offset], y[offset], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else if(shape::haveSameShapeAndStrides(xShapeInfo, zShapeInfo)) {
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
bool canCastY = sd::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto yOffset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
|
|
|
z[offset] = OpType::op(x[offset], y[yOffset], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else if(shape::haveSameShapeAndStrides(yShapeInfo, zShapeInfo)) {
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
bool canCastY = sd::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto offset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
|
|
|
z[offset] = OpType::op(x[xOffset], y[offset], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
uint yShapeInfoCast[MAX_RANK];
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
|
|
|
bool canCastY = sd::DataTypeUtils::castShapeInfo(yShapeInfo, yShapeInfoCast);
|
|
|
|
bool canCastZ = sd::DataTypeUtils::castShapeInfo(zShapeInfo, zShapeInfoCast);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
|
|
|
for (auto i = start; i < stop; i++) {
|
|
|
|
auto xOffset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
auto yOffset = shape::indexOffset(i, yShapeInfo, yShapeInfoCast, canCastY);
|
|
|
|
auto zOffset = shape::indexOffset(i, zShapeInfo, zShapeInfoCast, canCastZ);
|
|
|
|
z[zOffset] = OpType::op(x[xOffset], y[yOffset], extraParams);
|
|
|
|
};
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|