Cutlass CUDA Templates for Linear Algebra Subroutines and Solvers

scalar_or_pointer.h
Go to the documentation of this file.
1
2 /***************************************************************************************************
3 * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification, are permitted
6 * provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice, this list of
10 * conditions and the following disclaimer in the documentation and/or other materials
11 * provided with the distribution.
12 * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
13 * to endorse or promote products derived from this software without specific prior written
14 * permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 **************************************************************************************************/
29 #pragma once
30
31 #include "cutlass/cutlass.h"
32
33 namespace cutlass {
34
36
37 namespace detail {
38
41 template <typename Scalar_>
42 class ScalarOrPointer {
43 public:
45 typedef Scalar_ Scalar;
46
47 private:
48 //
49 // Data members
50 //
51
53 Scalar scalar;
54
56 Scalar const *ptr;
57
58 public:
59
60 //
61 // Methods
62 //
63
65 CUTLASS_HOST_DEVICE
66 ScalarOrPointer(): scalar(0), ptr(nullptr) {}
67
69 CUTLASS_HOST_DEVICE
70 ScalarOrPointer(Scalar const &val): scalar(val), ptr(nullptr) {}
71
73 CUTLASS_HOST_DEVICE
74 ScalarOrPointer(Scalar const *ptr_): scalar(0), ptr(ptr_) {}
75
77 CUTLASS_HOST_DEVICE
78 bool is_pointer() const {
79 return bool(ptr);
80 }
81
83 CUTLASS_HOST_DEVICE
84 Scalar const *get_ptr() const {
85 return ptr;
86 }
87
89 CUTLASS_HOST_DEVICE
90 Scalar get_scalar() const {
91 return scalar;
92 }
93
95 CUTLASS_HOST_DEVICE
96 ScalarOrPointer &operator=(Scalar const &scalar_) {
97 scalar = scalar_;
98 ptr = nullptr;
99 return *this;
100 }
101
103 CUTLASS_HOST_DEVICE
104 ScalarOrPointer &operator=(Scalar const *ptr_) {
105 ptr = ptr_;
106 return *this;
107 }
108
110 CUTLASS_HOST_DEVICE
111 Scalar get() const {
112 if (ptr) {
113 return *ptr;
114 }
115 return scalar;
116 }
117
119 CUTLASS_HOST_DEVICE
120 operator Scalar() const {
121 return get();
122 }
123 };
124
125 } // namespace detail
126
128
129 } // namespace cutlass
cutlass
Definition: convert.h:33
cutlass::detail::ScalarOrPointer::get_scalar
CUTLASS_HOST_DEVICE Scalar get_scalar() const
Gets the pointer value.
Definition: scalar_or_pointer.h:90
cutlass::detail::ScalarOrPointer::ScalarOrPointer
CUTLASS_HOST_DEVICE ScalarOrPointer(Scalar const &val)
Object behaves as a scalar.
Definition: scalar_or_pointer.h:70
cutlass::detail::ScalarOrPointer::Scalar
Scalar_ Scalar
Underlying scalar type.
Definition: scalar_or_pointer.h:45
cutlass::detail::ScalarOrPointer::operator=
CUTLASS_HOST_DEVICE ScalarOrPointer & operator=(Scalar const *ptr_)
Assigns to a pointer value.
Definition: scalar_or_pointer.h:104
nullptr
#define nullptr
nullptr
Definition: platform.h:144
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:46
cutlass::detail::ScalarOrPointer::ScalarOrPointer
CUTLASS_HOST_DEVICE ScalarOrPointer(Scalar const *ptr_)
Object behaves as a scalar.
Definition: scalar_or_pointer.h:74
cutlass::detail::ScalarOrPointer::is_pointer
CUTLASS_HOST_DEVICE bool is_pointer() const
Returns true if is pointer.
Definition: scalar_or_pointer.h:78
cutlass::detail::ScalarOrPointer::ScalarOrPointer
CUTLASS_HOST_DEVICE ScalarOrPointer()
Default ctor.
Definition: scalar_or_pointer.h:66
cutlass::detail::ScalarOrPointer::get_ptr
CUTLASS_HOST_DEVICE Scalar const * get_ptr() const
Gets the pointer value.
Definition: scalar_or_pointer.h:84
cutlass.h
Basic include for CUTLASS macros.
cutlass::detail::ScalarOrPointer::operator=
CUTLASS_HOST_DEVICE ScalarOrPointer & operator=(Scalar const &scalar_)
Assigns to a scalar and sets pointer to nullptr.
Definition: scalar_or_pointer.h:96
cutlass::detail::ScalarOrPointer
Definition: scalar_or_pointer.h:42