2019-08-30 09:12:40 +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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "../scalar_int.h"
|
|
|
|
#include <op_boilerplate.h>
|
|
|
|
#include <types/types.h>
|
|
|
|
#include <LoopKind.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-08-30 09:12:40 +02:00
|
|
|
|
|
|
|
#include "../legacy_ops.h"
|
|
|
|
|
|
|
|
using namespace simdOps;
|
|
|
|
|
|
|
|
namespace functions {
|
|
|
|
namespace scalar {
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
template<typename OpType>
|
|
|
|
void ScalarIntTransform<X>::transform(void *vx, Nd4jLong *xShapeInfo,
|
2019-09-11 19:12:09 +02:00
|
|
|
void *vextraParams,
|
|
|
|
void *vz, Nd4jLong *zShapeInfo,
|
|
|
|
void *vscalars,
|
|
|
|
int *dimension, int dimensionLength,
|
2019-08-30 09:12:40 +02:00
|
|
|
Nd4jLong *xTadShapeInfo, Nd4jLong *xTadOffsets,
|
2019-11-13 15:15:18 +01:00
|
|
|
Nd4jLong *zTadShapeInfo, Nd4jLong *zTadOffsets,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-08-30 09:12:40 +02:00
|
|
|
auto x = reinterpret_cast<X *>(vx);
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto scalars = reinterpret_cast<X *>(vscalars);
|
|
|
|
auto extraParams = reinterpret_cast<X *>(vextraParams);
|
|
|
|
|
|
|
|
if (zTadShapeInfo == nullptr) {
|
|
|
|
zTadShapeInfo = xTadShapeInfo;
|
|
|
|
zTadOffsets = xTadOffsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tad preparation
|
|
|
|
const int xTadEws = shape::elementWiseStride(xTadShapeInfo);
|
|
|
|
const int zTadEws = shape::elementWiseStride(zTadShapeInfo);
|
|
|
|
const int tadLength = shape::tadLength(xShapeInfo, dimension, dimensionLength);
|
|
|
|
const int numTads = shape::length(xShapeInfo) / tadLength;
|
|
|
|
|
|
|
|
nd4j::LoopKind::Kind kindOfLoop = nd4j::LoopKind::deduceKindOfLoopXZ(xTadShapeInfo, zTadShapeInfo);
|
|
|
|
|
|
|
|
if (kindOfLoop != nd4j::LoopKind::EWS1 && kindOfLoop != nd4j::LoopKind::EWSNONZERO) {
|
|
|
|
printf("ScalarIntTransform<X>::transform: super-bad loop visited. Shouldn't ever happen\n");
|
|
|
|
return;
|
|
|
|
}
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
int num_threads = nd4j::math::nd4j_min<int>(numTads, nd4j::Environment::getInstance()->maxThreads());
|
2019-08-30 09:12:40 +02:00
|
|
|
|
|
|
|
if (kindOfLoop == nd4j::LoopKind::EWS1) {
|
2019-11-13 15:15:18 +01:00
|
|
|
for (auto r = start; r < stop; r++) {
|
2019-08-30 09:12:40 +02:00
|
|
|
auto oZ = z + zTadOffsets[r];
|
|
|
|
auto oX = x + xTadOffsets[r];
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (int f = 0; f < tadLength; f++)
|
2019-08-30 09:12:40 +02:00
|
|
|
oZ[f] = OpType::op(oX[f], scalars[r], extraParams);
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
else {
|
|
|
|
for (auto r = start; r < stop; r++) {
|
2019-08-30 09:12:40 +02:00
|
|
|
auto oZ = z + zTadOffsets[r];
|
|
|
|
auto oX = x + xTadOffsets[r];
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (int f = 0; f < tadLength; f++)
|
2019-08-30 09:12:40 +02:00
|
|
|
oZ[f * zTadEws] = OpType::op(oX[f * xTadEws], scalars[r], extraParams);
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void ScalarIntTransform<X>::transform(int opNum,
|
|
|
|
void *x,
|
|
|
|
Nd4jLong *xShapeInfo,
|
|
|
|
void *extraParams,
|
|
|
|
void *z,
|
|
|
|
Nd4jLong *zShapeInfo,
|
|
|
|
void *scalars,
|
|
|
|
int *dimension,
|
|
|
|
int dimensionLength,
|
|
|
|
Nd4jLong *xTadShapeInfo,
|
|
|
|
Nd4jLong *xTadOffsets,
|
|
|
|
Nd4jLong *zTadShapeInfo,
|
2019-11-13 15:15:18 +01:00
|
|
|
Nd4jLong *zTadOffsets,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
|
|
|
|
|
|
|
DISPATCH_BY_OPNUM_T(transform, PARAMS(x, xShapeInfo, extraParams, z, zShapeInfo, scalars, dimension, dimensionLength, xTadShapeInfo, xTadOffsets, zTadShapeInfo, zTadOffsets, start, stop), SCALAR_INT_OPS);
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void ScalarIntTransform<X>::transform(const int opNum,
|
|
|
|
void *x,
|
|
|
|
Nd4jLong xEws,
|
|
|
|
void *z,
|
|
|
|
Nd4jLong zEws,
|
|
|
|
void *scalar,
|
|
|
|
void *extraParams,
|
2019-11-13 15:15:18 +01:00
|
|
|
const uint64_t n,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
|
|
|
DISPATCH_BY_OPNUM_T(transform, PARAMS(x, xEws, z, zEws, scalar, extraParams, n, start, stop), SCALAR_INT_OPS);
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
void ScalarIntTransform<X>::transform(const int opNum,
|
|
|
|
void *x,
|
|
|
|
Nd4jLong *xShapeInfo,
|
|
|
|
void *z,
|
|
|
|
Nd4jLong *zShapeInfo,
|
|
|
|
void *scalar,
|
2019-11-13 15:15:18 +01:00
|
|
|
void *extraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
|
|
|
DISPATCH_BY_OPNUM_T(transform, PARAMS(x, xShapeInfo, z, zShapeInfo, scalar, extraParams, start, stop), SCALAR_INT_OPS);
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
template<typename OpType>
|
|
|
|
void ScalarIntTransform<X>::transform(void *vx,
|
|
|
|
Nd4jLong *xShapeInfo,
|
|
|
|
void *vz,
|
|
|
|
Nd4jLong *zShapeInfo,
|
|
|
|
void *vscalar,
|
2019-11-13 15:15:18 +01:00
|
|
|
void *vextraParams,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-09-11 19:12:09 +02:00
|
|
|
|
2019-08-30 09:12:40 +02:00
|
|
|
auto x = reinterpret_cast<X *>(vx);
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto scalar = reinterpret_cast<X *>(vscalar)[0];
|
|
|
|
auto extraParams = reinterpret_cast<X *>(vextraParams);
|
|
|
|
|
|
|
|
auto xEws = shape::elementWiseStride(xShapeInfo);
|
|
|
|
auto zEws = shape::elementWiseStride(zShapeInfo);
|
|
|
|
auto len = shape::length(xShapeInfo);
|
|
|
|
|
|
|
|
nd4j::LoopKind::Kind kindOfLoop = nd4j::LoopKind::deduceKindOfLoopXZ(xShapeInfo, zShapeInfo);
|
|
|
|
|
|
|
|
if (kindOfLoop == nd4j::LoopKind::EWS1 || kindOfLoop == nd4j::LoopKind::EWSNONZERO) {
|
2019-11-13 15:15:18 +01:00
|
|
|
transform<OpType>(x, xEws, z, zEws, vscalar, extraParams, len, start, stop);
|
2019-08-30 09:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastX = nd4j::DataTypeUtils::castShapeInfo<uint>(xShapeInfo, xShapeInfoCast);
|
|
|
|
|
|
|
|
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], scalar, extraParams);
|
|
|
|
};
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint zShapeInfoCast[MAX_RANK];
|
|
|
|
const bool canCastZ = nd4j::DataTypeUtils::castShapeInfo<uint>(zShapeInfo, zShapeInfoCast);
|
|
|
|
|
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], scalar, extraParams);
|
|
|
|
};
|
2019-09-11 19:12:09 +02:00
|
|
|
}
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
template<typename OpType>
|
|
|
|
void ScalarIntTransform<X>::transform(void *vx,
|
|
|
|
Nd4jLong xEws,
|
|
|
|
void *vz,
|
|
|
|
Nd4jLong zEws,
|
|
|
|
void *vscalar,
|
|
|
|
void *vextraParams,
|
2019-11-13 15:15:18 +01:00
|
|
|
const uint64_t len,
|
|
|
|
const uint64_t start, const uint64_t stop) {
|
2019-08-30 09:12:40 +02:00
|
|
|
|
|
|
|
auto x = reinterpret_cast<X *>(vx);
|
|
|
|
auto z = reinterpret_cast<X *>(vz);
|
|
|
|
auto scalar = reinterpret_cast<X *>(vscalar)[0];
|
2019-09-11 19:12:09 +02:00
|
|
|
auto extraParams = reinterpret_cast<X *>(vextraParams);
|
2019-08-30 09:12:40 +02:00
|
|
|
|
2020-02-21 12:31:00 +01:00
|
|
|
if (scalar < (sizeof(X) * 8)) {
|
|
|
|
if (xEws == 1 && zEws == 1) {
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i] = OpType::op(x[i], scalar, extraParams);
|
|
|
|
} else {
|
|
|
|
for (auto i = start; i < stop; i++)
|
|
|
|
z[i * zEws] = OpType::op(x[i * xEws], scalar, extraParams);
|
|
|
|
}
|
2019-08-30 09:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BUILD_SINGLE_TEMPLATE(template class ND4J_EXPORT ScalarIntTransform, , INTEGER_TYPES);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|