Use method from vespalib::hwaccelrated
#include "distance_function.h"
#include <vespa/eval/tensor/dense/typed_cells.h>
+#include <vespa/vespalib/hwaccelrated/iaccelrated.h>
namespace search::tensor {
@@ -13,18 +14,17 @@ namespace search::tensor {
template <typename FloatType>
class SquaredEuclideanDistance : public DistanceFunction {
public:
+ SquaredEuclideanDistance()
+ : _computer(vespalib::hwaccelrated::IAccelrated::getAccelrator())
+ {}
double calc(const vespalib::tensor::TypedCells& lhs, const vespalib::tensor::TypedCells& rhs) const override {
auto lhs_vector = lhs.typify<FloatType>();
auto rhs_vector = rhs.typify<FloatType>();
- double result = 0.0;
size_t sz = lhs_vector.size();
assert(sz == rhs_vector.size());
- for (size_t i = 0; i < sz; ++i) {
- double diff = lhs_vector[i] - rhs_vector[i];
- result += diff * diff;
- }
- return result;
+ return _computer->squaredEuclidianDistance(&lhs_vector[0], &rhs_vector[0], sz);
}
+ vespalib::hwaccelrated::IAccelrated::UP _computer;
};
template class SquaredEuclideanDistance<float>;
@@ -39,37 +39,18 @@ template class SquaredEuclideanDistance<double>;
template <typename FloatType, size_t VectorSizeBytes>
class HwAccelSquaredEuclideanDistance : public DistanceFunction {
public:
+ HwAccelSquaredEuclideanDistance()
+ : _computer(vespalib::hwaccelrated::IAccelrated::getAccelrator())
+ {}
double calc(const vespalib::tensor::TypedCells& lhs, const vespalib::tensor::TypedCells& rhs) const override {
- constexpr const size_t elems_per_vector = VectorSizeBytes / sizeof(FloatType);
- typedef FloatType VectorType __attribute__ ((vector_size (VectorSizeBytes), aligned(VectorSizeBytes)));
-
auto lhs_vector = lhs.typify<FloatType>();
auto rhs_vector = rhs.typify<FloatType>();
+
size_t sz = lhs_vector.size();
assert(sz == rhs_vector.size());
- // TODO: Handle remainder when tensor vector size is not a multiple of VectorSizeBytes.
- assert((sz % VectorSizeBytes) == 0);
-
- const auto* a = reinterpret_cast<const VectorType*>(lhs_vector.begin());
- const auto* b = reinterpret_cast<const VectorType*>(rhs_vector.begin());
-
- VectorType tmp_diff;
- VectorType tmp_squa;
- VectorType tmp_sum;
- memset(&tmp_sum, 0, sizeof(tmp_sum));
-
- const size_t num_ops = sz / elems_per_vector;
- for (size_t i = 0; i < num_ops; ++i) {
- tmp_diff = a[i] - b[i];
- tmp_squa = tmp_diff * tmp_diff;
- tmp_sum += tmp_squa;
- }
- double sum = 0;
- for (size_t i = 0; i < elems_per_vector; ++i) {
- sum += tmp_sum[i];
- }
- return sum;
+ return _computer->squaredEuclidianDistance(&lhs_vector[0], &rhs_vector[0], sz);
}
+ vespalib::hwaccelrated::IAccelrated::UP _computer;
};
template class HwAccelSquaredEuclideanDistance<float, 32>;
