CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers
tensor_norm.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  **************************************************************************************************/
25 #pragma once
26 
27 #include <cmath>
28 
29 #include "cutlass/cutlass.h"
30 #include "cutlass/complex.h"
31 #include "cutlass/tensor_ref.h"
32 
34 
35 namespace cutlass {
36 namespace reference {
37 namespace host {
38 
40 
42 template <
43  typename Element,
44  typename Layout,
45  typename ElementReduction
46 >
47  ElementReduction TensorNorm(
49  ElementReduction accumulator) {
50 
52  view.extent(),
53  [&](typename Layout::TensorCoord const & coord) {
54  Element element = Element(view.at(coord));
55  accumulator = cutlass::norm_accumulate(element, accumulator);
56  });
57  return std::sqrt(accumulator);
58 }
59 
61 template <
62  typename Element,
63  typename Layout
64 >
66 
67  return TensorNorm<Element, Layout, double>(view, 0);
68 }
69 
71 
72 } // namespace host
73 } // namespace reference
74 } // namespace cutlass
75 
Definition: aligned_buffer.h:35
ElementReduction TensorNorm(TensorView< Element, Layout > view, ElementReduction accumulator)
Computes the p=2 norm of the elements of a tensor with arbitrary reduction data type.
Definition: tensor_norm.h:47
Defines a structure containing strides, bounds, and a pointer to tensor data.
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_HOST_DEVICE R norm_accumulate(T const &x, R const &accumulator)
Norm-accumulate calculation.
Definition: complex.h:343
void TensorForEachLambda(Coord< Rank > extent, Func func)
Iterates over the index space of a tensor and calls a C++ lambda.
Definition: host/tensor_foreach.h:98
CUTLASS_HOST_DEVICE Reference at(TensorCoord const &coord) const
Returns a reference to the element at a given Coord.
Definition: tensor_ref.h:307
Basic include for CUTLASS.
CUTLASS_HOST_DEVICE complex< T > sqrt(complex< T > const &z)
Computes the square root of complex number z.
Definition: complex.h:393