host/tensor_compare.h
Go to the documentation of this file.
/***************************************************************************************************
* Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************************************/
/* \file
\brief Defines host-side elementwise operations on TensorView.
*/
#pragma once
// Standard Library includes
#include <utility>
// Cutlass includes
#include "cutlass/cutlass.h"
#include "cutlass/util/distribution.h"
//#include "cutlass/util/type_traits.h"
#include "tensor_foreach.h"
namespace cutlass {
namespace reference {
namespace host {
namespace detail {
template <
typename Element,
typename Layout>
struct TensorEqualsFunc {
//
// Data members
//
TensorView<Element, Layout> lhs;
TensorView<Element, Layout> rhs;
bool result;
TensorEqualsFunc(): result(true) { }
TensorEqualsFunc(
TensorView<Element, Layout> const &lhs_,
TensorView<Element, Layout> const &rhs_
) :
lhs(lhs_), rhs(rhs_), result(true) { }
void operator()(Coord<Layout::kRank> const &coord) {
Element lhs_ = lhs.at(coord);
Element rhs_ = rhs.at(coord);
if (lhs_ != rhs_) {
result = false;
}
}
operator bool() const {
return result;
}
};
} // namespace detail
template <
typename Element,
typename Layout>
bool TensorEquals(
TensorView<Element, Layout> const &lhs,
TensorView<Element, Layout> const &rhs) {
// Extents must be identical
if (lhs.extent() != rhs.extent()) {
return false;
}
detail::TensorEqualsFunc<Element, Layout> func(lhs, rhs);
TensorForEach(
lhs.extent(),
func
);
return bool(func);
}
template <
typename Element,
typename Layout>
bool TensorNotEquals(
TensorView<Element, Layout> const &lhs,
TensorView<Element, Layout> const &rhs) {
// Extents must be identical
if (lhs.extent() != rhs.extent()) {
return true;
}
detail::TensorEqualsFunc<Element, Layout> func(lhs, rhs);
TensorForEach(
lhs.extent(),
func
);
return !bool(func);
}
namespace detail {
template <
typename Element,
typename Layout>
struct TensorContainsFunc {
//
// Data members
//
TensorView<Element, Layout> view;
Element value;
bool contains;
Coord<Layout::kRank> location;
//
// Methods
//
TensorContainsFunc(): contains(false) { }
TensorContainsFunc(
TensorView<Element, Layout> const &view_,
Element value_
) :
view(view_), value(value_), contains(false) { }
void operator()(Coord<Layout::kRank> const &coord) {
if (view.at(coord) == value) {
if (!contains) {
location = coord;
}
contains = true;
}
}
operator bool() const {
return contains;
}
};
} // namespace detail
template <
typename Element,
typename Layout>
bool TensorContains(
TensorView<Element, Layout> const & view,
Element value) {
detail::TensorContainsFunc<Element, Layout> func(
view,
value
);
TensorForEach(
view.extent(),
func
);
return bool(func);
}
template <
typename Element,
typename Layout>
std::pair<bool, Coord<Layout::kRank> > TensorFind(
TensorView<Element, Layout> const & view,
Element value) {
detail::TensorContainsFunc<Element, Layout> func(
view,
value
);
TensorForEach(
view.extent(),
func
);
return std::make_pair(bool(func), func.location);
}
} // namespace host
} // namespace reference
} // namespace cutlass
cutlass::reference::host::detail::TensorContainsFunc::TensorContainsFunc
TensorContainsFunc()
Ctor.
Definition: host/tensor_compare.h:164
cutlass
Definition: aligned_buffer.h:35
cutlass::reference::host::detail::TensorContainsFunc
< Layout function
Definition: host/tensor_compare.h:148
cutlass::reference::host::detail::TensorContainsFunc::TensorContainsFunc
TensorContainsFunc(TensorView< Element, Layout > const &view_, Element value_)
Ctor.
Definition: host/tensor_compare.h:167
cutlass::TensorView::extent
CUTLASS_HOST_DEVICE TensorCoord const & extent() const
Returns the extent of the view (the size along each logical dimension).
Definition: tensor_view.h:167
cutlass::platform::make_pair
CUTLASS_HOST_DEVICE std::pair< T1, T2 > make_pair(T1 t, T2 u)
Definition: platform.h:232
cutlass::reference::host::detail::TensorContainsFunc::operator()
void operator()(Coord< Layout::kRank > const &coord)
Visits a coordinate.
Definition: host/tensor_compare.h:174
cutlass::reference::host::TensorEquals
bool TensorEquals(TensorView< Element, Layout > const &lhs, TensorView< Element, Layout > const &rhs)
Returns true if two tensor views are equal.
Definition: host/tensor_compare.h:97
cutlass::reference::host::detail::TensorContainsFunc::location
Coord< Layout::kRank > location
Definition: host/tensor_compare.h:157
cutlass::reference::host::detail::TensorContainsFunc::view
TensorView< Element, Layout > view
Definition: host/tensor_compare.h:154
cutlass::reference::host::detail::TensorEqualsFunc::lhs
TensorView< Element, Layout > lhs
Definition: host/tensor_compare.h:58
cutlass::reference::host::detail::TensorEqualsFunc::operator()
void operator()(Coord< Layout::kRank > const &coord)
Visits a coordinate.
Definition: host/tensor_compare.h:73
cutlass::reference::host::TensorContains
bool TensorContains(TensorView< Element, Layout > const &view, Element value)
Returns true if a value is present in a tensor.
Definition: host/tensor_compare.h:198
cutlass::TensorView< Element, Layout >
cutlass::reference::host::detail::TensorContainsFunc::contains
bool contains
Definition: host/tensor_compare.h:156
cutlass::reference::host::detail::TensorEqualsFunc::TensorEqualsFunc
TensorEqualsFunc()
Ctor.
Definition: host/tensor_compare.h:63
distribution.h
This header contains a class to parametrize a statistical distribution function.
cutlass::reference::host::TensorNotEquals
bool TensorNotEquals(TensorView< Element, Layout > const &lhs, TensorView< Element, Layout > const &rhs)
Returns true if two tensor views are NOT equal.
Definition: host/tensor_compare.h:122
cutlass::Coord< Layout::kRank >
cutlass::reference::host::detail::TensorContainsFunc::value
Element value
Definition: host/tensor_compare.h:155
cutlass::TensorRef::at
CUTLASS_HOST_DEVICE Reference at(TensorCoord const &coord) const
Returns a reference to the element at a given Coord.
Definition: tensor_ref.h:307
cutlass::reference::host::detail::TensorEqualsFunc::TensorEqualsFunc
TensorEqualsFunc(TensorView< Element, Layout > const &lhs_, TensorView< Element, Layout > const &rhs_)
Ctor.
Definition: host/tensor_compare.h:66
cutlass::reference::host::TensorForEach
void TensorForEach(Coord< Rank > extent, Func &func)
Iterates over the index space of a tensor.
Definition: host/tensor_foreach.h:87
cutlass::reference::host::detail::TensorEqualsFunc::result
bool result
Definition: host/tensor_compare.h:60
cutlass::reference::host::TensorFind
std::pair< bool, Coord< Layout::kRank > > TensorFind(TensorView< Element, Layout > const &view, Element value)
< Layout function
Definition: host/tensor_compare.h:223
cutlass.h
Basic include for CUTLASS.
cutlass::reference::host::detail::TensorEqualsFunc::rhs
TensorView< Element, Layout > rhs
Definition: host/tensor_compare.h:59
cutlass::reference::host::detail::TensorEqualsFunc
< Layout function
Definition: host/tensor_compare.h:52
tensor_foreach.h