2021-02-01 13:31:45 +01:00
|
|
|
/* ******************************************************************************
|
|
|
|
*
|
2019-06-06 14:21:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-02-01 13:31:45 +01:00
|
|
|
* See the NOTICE file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2019-06-06 14:21:15 +02:00
|
|
|
* 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 raver on 4/9/2018.
|
|
|
|
//
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <loops/indexreduce.h>
|
|
|
|
#include <system/op_boilerplate.h>
|
|
|
|
#include <helpers/Loops.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <types/types.h>
|
|
|
|
#include <helpers/ConstantTadHelper.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <loops/legacy_ops.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
using namespace simdOps;
|
|
|
|
|
|
|
|
namespace functions {
|
|
|
|
namespace indexreduce {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2019-08-27 09:37:10 +02:00
|
|
|
template <typename X, typename Y>
|
2020-05-09 07:06:14 +02:00
|
|
|
Nd4jLong IndexReduce<X,Y>::execScalar( const int opNum, const void *x, const Nd4jLong *xShapeInfo, void *extraParams) {
|
2019-08-27 09:37:10 +02:00
|
|
|
RETURNING_DISPATCH_BY_OPNUM_TT(execScalar, PARAMS(x, xShapeInfo, extraParams), INDEX_REDUCE_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2019-08-27 09:37:10 +02:00
|
|
|
template <typename X, typename Y>
|
|
|
|
void IndexReduce<X,Y>::exec(const int opNum,
|
2020-05-09 07:06:14 +02:00
|
|
|
const void *x, const Nd4jLong *xShapeInfo,
|
|
|
|
void *extraParams,
|
|
|
|
void *z, const Nd4jLong *zShapeInfo,
|
|
|
|
int *dimension, int dimensionLength,
|
|
|
|
const Nd4jLong *tadShapeInfo, const Nd4jLong *tadOffset) {
|
2019-11-13 15:15:18 +01:00
|
|
|
DISPATCH_BY_OPNUM_TT(exec, PARAMS(x, xShapeInfo, extraParams, z, zShapeInfo, dimension, dimensionLength, tadShapeInfo, tadOffset), INDEX_REDUCE_OPS);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2019-08-27 09:37:10 +02:00
|
|
|
template <typename X, typename Y>
|
2019-06-06 14:21:15 +02:00
|
|
|
template<typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
Nd4jLong IndexReduce<X, Y>::execScalar(const void *vx, const Nd4jLong *xShapeInfo, void *vextraParams) {
|
2019-06-15 13:34:34 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto extraParams = reinterpret_cast<X *>(vextraParams);
|
|
|
|
|
|
|
|
//T startingVal = OpType::startingValue(x);
|
|
|
|
auto startingIndex = OpType::startingIndexValue(x);
|
|
|
|
auto len = shape::length(xShapeInfo);
|
|
|
|
auto xEws = shape::elementWiseStride(xShapeInfo);
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::OmpLaunchHelper info(len);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
uint xShapeInfoCast[MAX_RANK];
|
2020-03-02 10:49:41 +01:00
|
|
|
bool canCastX = sd::DataTypeUtils::castShapeInfo(xShapeInfo, xShapeInfoCast);
|
2020-06-06 14:26:55 +02:00
|
|
|
int maxThreads = sd::math::nd4j_min<int>(64, sd::Environment::getInstance().maxThreads());
|
2019-11-13 15:15:18 +01:00
|
|
|
IndexValue<X> intermediatery[64];
|
|
|
|
for (int e = 0; e < maxThreads; e++)
|
|
|
|
intermediatery[e].index = -1;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-05-14 12:41:55 +02:00
|
|
|
if (xEws == 1 && shape::order(xShapeInfo) == 'c') {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
intermediatery[thread_id] = OpType::startingIndexValue(x);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
IndexValue<X> curr(x[i], i);
|
|
|
|
intermediatery[thread_id] = OpType::update(intermediatery[thread_id], curr, extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
maxThreads = samediff::Threads::parallel_for(func, 0, len, 1, maxThreads);
|
2019-11-13 15:15:18 +01:00
|
|
|
|
|
|
|
for (int e = 0; e < maxThreads; e++)
|
|
|
|
startingIndex = OpType::update(startingIndex, intermediatery[e], extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
} else {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
|
|
|
intermediatery[thread_id] = OpType::startingIndexValue(x);
|
|
|
|
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto i = start; i < stop; i++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto offset = shape::indexOffset(i, xShapeInfo, xShapeInfoCast, canCastX);
|
|
|
|
IndexValue<X> curr(x[offset], i);
|
|
|
|
intermediatery[thread_id] = OpType::update(intermediatery[thread_id], curr, extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2019-11-13 15:15:18 +01:00
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
maxThreads = samediff::Threads::parallel_for(func, 0, len, 1, maxThreads);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
for (int e = 0; e < maxThreads; e++)
|
|
|
|
startingIndex = OpType::update(startingIndex, intermediatery[e], extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
return startingIndex.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2019-08-27 09:37:10 +02:00
|
|
|
template <typename X, typename Z>
|
2019-06-06 14:21:15 +02:00
|
|
|
template<typename OpType>
|
2020-05-09 07:06:14 +02:00
|
|
|
void IndexReduce<X, Z>::exec(const void *vx, const Nd4jLong *xShapeInfo,
|
|
|
|
void *vextraParams,
|
|
|
|
void *vz, const Nd4jLong *zShapeInfo,
|
|
|
|
int *dimension, int dimensionLength,
|
|
|
|
const Nd4jLong *tadShapeInfo, const Nd4jLong *tadOffset) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
auto x = reinterpret_cast<const X *>(vx);
|
2019-08-27 09:37:10 +02:00
|
|
|
auto z = reinterpret_cast<Z *>(vz);
|
2019-06-06 14:21:15 +02:00
|
|
|
auto extraParams = reinterpret_cast<X *>(vextraParams);
|
|
|
|
|
2019-06-15 13:34:34 +02:00
|
|
|
const Nd4jLong zLen = shape::length(zShapeInfo);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
if(sd::ArrayOptions::arrayType(xShapeInfo) == sd::ArrayType::EMPTY) {
|
|
|
|
if(sd::ArrayOptions::arrayType(zShapeInfo) == sd::ArrayType::EMPTY)
|
2019-06-15 13:34:34 +02:00
|
|
|
return;
|
|
|
|
const auto indexValue = OpType::startingIndexValue(x);
|
2019-11-13 15:15:18 +01:00
|
|
|
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong i = 0; i < zLen; i++)
|
2019-11-13 15:15:18 +01:00
|
|
|
z[i] = (Z) indexValue.index;
|
|
|
|
|
2019-06-15 13:34:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
if(shape::isScalar(zShapeInfo)) {
|
2019-08-27 09:37:10 +02:00
|
|
|
z[0] = (Z) execScalar<OpType>(x,xShapeInfo,extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tadOnlyShapeInfo = tadShapeInfo;
|
2020-05-09 07:06:14 +02:00
|
|
|
auto tadOffsets = tadOffset;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if (tadOnlyShapeInfo == nullptr || tadOffsets == nullptr) {
|
|
|
|
if (dimensionLength < 1)
|
|
|
|
return;
|
|
|
|
|
2020-06-06 14:26:55 +02:00
|
|
|
auto tadPack = sd::ConstantTadHelper::getInstance().tadForDimensions(xShapeInfo, dimension, dimensionLength);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
tadOnlyShapeInfo = tadPack.primaryShapeInfo();
|
|
|
|
tadOffsets = tadPack.primaryOffsets();
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::IndexReductionLoops<X,Z>::template loopIndexReduce<OpType>(x, xShapeInfo, z, zShapeInfo, tadOnlyShapeInfo, tadOffsets, extraParams);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|