1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // Implementation of the pointer-to-implementation wrapper for the data-parallel
17 // kernel abstraction. KernelBase just delegates to the internal
18 // platform-specific implementation instance.
19 
20 #include "tensorflow/stream_executor/kernel.h"
21 
22 #include "absl/strings/string_view.h"
23 #include "absl/strings/strip.h"
24 #include "tensorflow/stream_executor/lib/demangle.h"
25 #include "tensorflow/stream_executor/platform.h"
26 #include "tensorflow/stream_executor/platform/logging.h"
27 #include "tensorflow/stream_executor/platform/port.h"
28 #include "tensorflow/stream_executor/stream_executor.h"
29 
30 namespace stream_executor {
31 
registers_per_thread(int * registers_per_thread) const32 bool KernelMetadata::registers_per_thread(int *registers_per_thread) const {
33   if (has_registers_per_thread_) {
34     *registers_per_thread = registers_per_thread_;
35     return true;
36   }
37 
38   return false;
39 }
40 
set_registers_per_thread(int registers_per_thread)41 void KernelMetadata::set_registers_per_thread(int registers_per_thread) {
42   registers_per_thread_ = registers_per_thread;
43   has_registers_per_thread_ = true;
44 }
45 
shared_memory_bytes(int * shared_memory_bytes) const46 bool KernelMetadata::shared_memory_bytes(int *shared_memory_bytes) const {
47   if (has_shared_memory_bytes_) {
48     *shared_memory_bytes = shared_memory_bytes_;
49     return true;
50   }
51 
52   return false;
53 }
54 
set_shared_memory_bytes(int shared_memory_bytes)55 void KernelMetadata::set_shared_memory_bytes(int shared_memory_bytes) {
56   shared_memory_bytes_ = shared_memory_bytes;
57   has_shared_memory_bytes_ = true;
58 }
59 
KernelBase(KernelBase && from)60 KernelBase::KernelBase(KernelBase &&from)
61     : parent_(from.parent_),
62       implementation_(std::move(from.implementation_)),
63       name_(std::move(from.name_)),
64       demangled_name_(std::move(from.demangled_name_)),
65       metadata_(from.metadata_) {
66   from.parent_ = nullptr;
67 }
68 
KernelBase(StreamExecutor * parent)69 KernelBase::KernelBase(StreamExecutor *parent)
70     : parent_(parent),
71       implementation_(parent->implementation()->CreateKernelImplementation()) {}
72 
KernelBase(StreamExecutor * parent,internal::KernelInterface * implementation)73 KernelBase::KernelBase(StreamExecutor *parent,
74                        internal::KernelInterface *implementation)
75     : parent_(parent), implementation_(implementation) {}
76 
~KernelBase()77 KernelBase::~KernelBase() {
78   if (parent_) {
79     parent_->UnloadKernel(this);
80   }
81 }
82 
Arity() const83 unsigned KernelBase::Arity() const { return implementation_->Arity(); }
84 
SetPreferredCacheConfig(KernelCacheConfig config)85 void KernelBase::SetPreferredCacheConfig(KernelCacheConfig config) {
86   return implementation_->SetPreferredCacheConfig(config);
87 }
88 
GetPreferredCacheConfig() const89 KernelCacheConfig KernelBase::GetPreferredCacheConfig() const {
90   return implementation_->GetPreferredCacheConfig();
91 }
92 
set_name(absl::string_view name)93 void KernelBase::set_name(absl::string_view name) {
94   name_ = std::string(name);
95 
96   // CUDA splitter prefixes stub functions with __device_stub_.
97   demangled_name_ =
98       port::Demangle(absl::StripPrefix(name, "__device_stub_").data());
99 }
100 
101 }  // namespace stream_executor
102