2019-07-16 17:48: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 <ops/declarable/helpers/histogram.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-07-16 17:48:40 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
template <typename X, typename Z>
|
2020-05-09 07:06:14 +02:00
|
|
|
static void histogram_(void const* xBuffer, Nd4jLong const* xShapeInfo, void *zBuffer, Nd4jLong const* zShapeInfo, Nd4jLong numBins, double min_val, double max_val) {
|
|
|
|
auto dx = reinterpret_cast<X const*>(xBuffer);
|
2019-07-16 17:48:40 +02:00
|
|
|
auto result = reinterpret_cast<Z*>(zBuffer);
|
|
|
|
|
|
|
|
int length = shape::length(xShapeInfo);
|
|
|
|
|
|
|
|
X binSize = (max_val - min_val) / (numBins);
|
|
|
|
|
2020-01-04 13:27:16 +01:00
|
|
|
// FIXME: this op should be parallelized
|
2019-07-16 17:48:40 +02:00
|
|
|
{
|
|
|
|
int *bins = new int[numBins];
|
|
|
|
std::memset(bins, 0, sizeof(int) * numBins);
|
|
|
|
|
|
|
|
PRAGMA_OMP_SIMD
|
2020-01-04 13:27:16 +01:00
|
|
|
for (int x = 0; x < length; x++) {
|
2019-07-16 17:48:40 +02:00
|
|
|
int idx = (int) ((dx[x] - min_val) / binSize);
|
|
|
|
if (idx < 0)
|
|
|
|
idx = 0;
|
|
|
|
else if (idx >= numBins)
|
|
|
|
idx = numBins - 1;
|
|
|
|
|
|
|
|
bins[idx]++;
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:27:16 +01:00
|
|
|
PRAGMA_OMP_SIMD
|
2020-02-26 19:12:19 +01:00
|
|
|
for (Nd4jLong x = 0; x < numBins; x++) {
|
2020-01-04 13:27:16 +01:00
|
|
|
result[x] += bins[x];
|
2019-07-16 17:48:40 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 13:27:16 +01:00
|
|
|
|
2019-07-16 17:48:40 +02:00
|
|
|
delete[] bins;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void histogramHelper(sd::LaunchContext *context, NDArray &input, NDArray &output) {
|
2019-07-16 17:48:40 +02:00
|
|
|
Nd4jLong numBins = output.lengthOf();
|
|
|
|
double min_val = input.reduceNumber(reduce::SameOps::Min).e<double>(0);
|
|
|
|
double max_val = input.reduceNumber(reduce::SameOps::Max).e<double>(0);
|
|
|
|
|
2020-05-09 07:06:14 +02:00
|
|
|
BUILD_DOUBLE_SELECTOR(input.dataType(), output.dataType(), histogram_, (input.buffer(), input.shapeInfo(), output.buffer(), output.shapeInfo(), numBins, min_val, max_val), LIBND4J_TYPES, INDEXING_TYPES);
|
2019-07-16 17:48:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|