2019-08-28 17:20:44 +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/helpers.h>
|
|
|
|
#include <ops/declarable/helpers/hamming.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-08-28 17:20:44 +02:00
|
|
|
|
|
|
|
namespace nd4j {
|
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
2019-09-01 18:57:39 +02:00
|
|
|
|
|
|
|
static Nd4jLong hamming_distance(unsigned long long x, unsigned long long y) {
|
|
|
|
Nd4jLong dist = 0;
|
|
|
|
|
|
|
|
for (unsigned long long val = x ^ y; val > 0; val /= 2) {
|
|
|
|
if (val & 1)
|
|
|
|
dist++;
|
|
|
|
}
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-28 17:20:44 +02:00
|
|
|
template <typename X, typename Z>
|
|
|
|
static void _hamming(NDArray &x, NDArray &y, NDArray &z) {
|
|
|
|
auto xEws = x.ews();
|
|
|
|
auto yEws = y.ews();
|
|
|
|
|
|
|
|
auto xBuffer = x.bufferAsT<X>();
|
|
|
|
auto yBuffer = y.bufferAsT<X>();
|
|
|
|
|
|
|
|
Nd4jLong distance = 0;
|
2019-09-01 18:33:23 +02:00
|
|
|
auto lengthOf = x.lengthOf();
|
2019-11-13 15:15:18 +01:00
|
|
|
int maxThreads = nd4j::math::nd4j_min<int>(256, omp_get_max_threads());
|
2019-09-01 18:33:23 +02:00
|
|
|
Nd4jLong intermediate[256];
|
|
|
|
|
|
|
|
// nullify temp values
|
|
|
|
for (int e = 0; e < maxThreads; e++)
|
|
|
|
intermediate[e] = 0;
|
2019-08-28 17:20:44 +02:00
|
|
|
|
|
|
|
if (xEws == 1 && yEws == 1 && x.ordering() == y.ordering()) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto _x = static_cast<unsigned long long>(xBuffer[e]);
|
|
|
|
auto _y = static_cast<unsigned long long>(yBuffer[e]);
|
2019-08-28 17:20:44 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
intermediate[thread_id] += hamming_distance(_x, _y);
|
|
|
|
}
|
|
|
|
};
|
2019-08-28 17:20:44 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
maxThreads = samediff::Threads::parallel_for(func, 0, lengthOf);
|
2019-08-28 17:20:44 +02:00
|
|
|
} else if (xEws > 1 && yEws > 1 && x.ordering() == y.ordering()) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto _x = static_cast<unsigned long long>(xBuffer[e * xEws]);
|
|
|
|
auto _y = static_cast<unsigned long long>(yBuffer[e * yEws]);
|
2019-08-28 17:20:44 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
intermediate[thread_id] += hamming_distance(_x, _y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
maxThreads = samediff::Threads::parallel_for(func, 0, lengthOf);
|
2019-08-28 17:20:44 +02:00
|
|
|
} else {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto e = start; e < stop; e++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
auto _x = static_cast<unsigned long long>(x.e<Nd4jLong>(e));
|
|
|
|
auto _y = static_cast<unsigned long long>(y.e<Nd4jLong>(e));
|
|
|
|
|
|
|
|
intermediate[thread_id] += hamming_distance(_x, _y);
|
|
|
|
}
|
|
|
|
};
|
2019-08-28 17:20:44 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
maxThreads = samediff::Threads::parallel_for(func, 0, lengthOf);
|
2019-08-28 17:20:44 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 18:33:23 +02:00
|
|
|
// accumulate intermediate variables into output array
|
|
|
|
for (int e = 0; e < maxThreads; e++)
|
|
|
|
distance += intermediate[e];
|
|
|
|
|
2019-08-28 17:20:44 +02:00
|
|
|
z.p(0, distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hamming(LaunchContext *context, NDArray &x, NDArray &y, NDArray &output) {
|
|
|
|
BUILD_DOUBLE_SELECTOR(x.dataType(), output.dataType(), _hamming, (x, y, output), INTEGER_TYPES, INDEXING_TYPES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|