From 8f4567a13902fcc095c443f73643d2f8a3b9e952 Mon Sep 17 00:00:00 2001 From: Tim Shen <timshen@google.com> Date: Thu, 27 Dec 2018 11:49:45 -0800 Subject: [PATCH] Refactor stream executor BUILD files. PiperOrigin-RevId: 227048559 --- tensorflow/core/platform/stream_executor.h | 6 +- .../core/platform/stream_executor_no_cuda.h | 6 +- tensorflow/stream_executor/cuda/cuda_blas.cc | 6 +- tensorflow/stream_executor/cuda/cuda_fft.cc | 6 +- tensorflow/stream_executor/cuda/cuda_rng.cc | 8 +-- tensorflow/stream_executor/lib/initialize.h | 51 +-------------- .../{ => platform/default}/dso_loader.cc | 28 +++++---- .../{ => platform/default}/dso_loader.h | 2 +- .../platform/default/initialize.h | 62 +++++++++++++++++++ .../stream_executor/platform/default/mutex.h | 3 +- .../stream_executor/platform/dso_loader.h | 31 ++++++++++ .../stream_executor/platform/initialize.h | 27 ++++++++ tensorflow/stream_executor/platform/mutex.h | 3 +- .../stream_executor/platform/platform.h | 36 +++++++++++ 14 files changed, 183 insertions(+), 92 deletions(-) rename tensorflow/stream_executor/{ => platform/default}/dso_loader.cc (93%) rename tensorflow/stream_executor/{ => platform/default}/dso_loader.h (100%) create mode 100644 tensorflow/stream_executor/platform/default/initialize.h create mode 100644 tensorflow/stream_executor/platform/dso_loader.h create mode 100644 tensorflow/stream_executor/platform/initialize.h create mode 100644 tensorflow/stream_executor/platform/platform.h diff --git a/tensorflow/core/platform/stream_executor.h b/tensorflow/core/platform/stream_executor.h index 0a590b3d40c..42822859f6e 100644 --- a/tensorflow/core/platform/stream_executor.h +++ b/tensorflow/core/platform/stream_executor.h @@ -18,11 +18,6 @@ limitations under the License. #include "tensorflow/core/platform/platform.h" -#if defined(PLATFORM_GOOGLE) -#include "tensorflow/stream_executor/platform/google/dso_loader.h" -#else -#include "tensorflow/stream_executor/dso_loader.h" -#endif #include "tensorflow/stream_executor/cuda/cuda_platform_id.h" #include "tensorflow/stream_executor/device_memory.h" #include "tensorflow/stream_executor/dnn.h" @@ -31,6 +26,7 @@ limitations under the License. #include "tensorflow/stream_executor/lib/status.h" #include "tensorflow/stream_executor/multi_platform_manager.h" #include "tensorflow/stream_executor/platform.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/scratch_allocator.h" #include "tensorflow/stream_executor/stream.h" #include "tensorflow/stream_executor/stream_executor.h" diff --git a/tensorflow/core/platform/stream_executor_no_cuda.h b/tensorflow/core/platform/stream_executor_no_cuda.h index 50a5e732c0e..123035cc8a6 100644 --- a/tensorflow/core/platform/stream_executor_no_cuda.h +++ b/tensorflow/core/platform/stream_executor_no_cuda.h @@ -18,11 +18,6 @@ limitations under the License. #include "tensorflow/core/platform/platform.h" -#if defined(PLATFORM_GOOGLE) -#include "tensorflow/stream_executor/platform/google/dso_loader.h" -#else -#include "tensorflow/stream_executor/dso_loader.h" -#endif #include "tensorflow/stream_executor/cuda/cuda_platform_id.h" #include "tensorflow/stream_executor/device_memory.h" #include "tensorflow/stream_executor/dnn.h" @@ -31,6 +26,7 @@ limitations under the License. #include "tensorflow/stream_executor/lib/status.h" #include "tensorflow/stream_executor/multi_platform_manager.h" #include "tensorflow/stream_executor/platform.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/scratch_allocator.h" #include "tensorflow/stream_executor/stream.h" #include "tensorflow/stream_executor/stream_executor.h" diff --git a/tensorflow/stream_executor/cuda/cuda_blas.cc b/tensorflow/stream_executor/cuda/cuda_blas.cc index 957f6c98da5..7325476ef16 100644 --- a/tensorflow/stream_executor/cuda/cuda_blas.cc +++ b/tensorflow/stream_executor/cuda/cuda_blas.cc @@ -58,16 +58,12 @@ limitations under the License. #include "tensorflow/stream_executor/cuda/cuda_stream.h" #include "tensorflow/stream_executor/cuda/cuda_timer.h" #include "tensorflow/stream_executor/device_memory.h" - -#ifndef PLATFORM_GOOGLE -#include "tensorflow/stream_executor/dso_loader.h" -#endif - #include "tensorflow/stream_executor/lib/env.h" #include "tensorflow/stream_executor/lib/initialize.h" #include "tensorflow/stream_executor/lib/status.h" #include "tensorflow/stream_executor/lib/status_macros.h" #include "tensorflow/stream_executor/lib/stringprintf.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/platform/logging.h" #include "tensorflow/stream_executor/platform/port.h" #include "tensorflow/stream_executor/plugin_registry.h" diff --git a/tensorflow/stream_executor/cuda/cuda_fft.cc b/tensorflow/stream_executor/cuda/cuda_fft.cc index acac7d63688..776719bc35c 100644 --- a/tensorflow/stream_executor/cuda/cuda_fft.cc +++ b/tensorflow/stream_executor/cuda/cuda_fft.cc @@ -23,14 +23,10 @@ limitations under the License. #include "tensorflow/stream_executor/cuda/cuda_platform_id.h" #include "tensorflow/stream_executor/cuda/cuda_stream.h" #include "tensorflow/stream_executor/device_memory.h" - -#ifndef PLATFORM_GOOGLE -#include "tensorflow/stream_executor/dso_loader.h" -#endif - #include "tensorflow/stream_executor/lib/env.h" #include "tensorflow/stream_executor/lib/initialize.h" #include "tensorflow/stream_executor/lib/status.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/platform/logging.h" #include "tensorflow/stream_executor/platform/port.h" #include "tensorflow/stream_executor/plugin_registry.h" diff --git a/tensorflow/stream_executor/cuda/cuda_rng.cc b/tensorflow/stream_executor/cuda/cuda_rng.cc index 7f920719321..022ee17ff72 100644 --- a/tensorflow/stream_executor/cuda/cuda_rng.cc +++ b/tensorflow/stream_executor/cuda/cuda_rng.cc @@ -21,17 +21,15 @@ limitations under the License. #include "tensorflow/stream_executor/cuda/cuda_platform_id.h" #include "tensorflow/stream_executor/cuda/cuda_stream.h" #include "tensorflow/stream_executor/device_memory.h" - -#ifndef PLATFORM_GOOGLE -#include "tensorflow/stream_executor/dso_loader.h" -#endif - #include "tensorflow/stream_executor/lib/env.h" #include "tensorflow/stream_executor/lib/initialize.h" #include "tensorflow/stream_executor/lib/status.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/platform/logging.h" #include "tensorflow/stream_executor/rng.h" +// clang-format off #include "cuda/include/curand.h" +// clang-format on // Formats curandStatus_t to output prettified values into a log stream. std::ostream &operator<<(std::ostream &in, const curandStatus_t &status) { diff --git a/tensorflow/stream_executor/lib/initialize.h b/tensorflow/stream_executor/lib/initialize.h index 688b0214694..cd0b9dad19b 100644 --- a/tensorflow/stream_executor/lib/initialize.h +++ b/tensorflow/stream_executor/lib/initialize.h @@ -16,55 +16,6 @@ limitations under the License. #ifndef TENSORFLOW_STREAM_EXECUTOR_LIB_INITIALIZE_H_ #define TENSORFLOW_STREAM_EXECUTOR_LIB_INITIALIZE_H_ -#include "tensorflow/stream_executor/platform/port.h" - -#if defined(PLATFORM_GOOGLE) -#include "tensorflow/stream_executor/platform/google/initialize.h" -#else - -#undef REGISTER_MODULE_INITIALIZER -#undef DECLARE_MODULE_INITIALIZER -#undef REGISTER_MODULE_INITIALIZER_SEQUENCE - -namespace stream_executor { -namespace port { - -class Initializer { - public: - typedef void (*InitializerFunc)(); - explicit Initializer(InitializerFunc func) { func(); } - - struct Dependency { - Dependency(const char *n, Initializer *i) : name(n), initializer(i) {} - const char *const name; - Initializer *const initializer; - }; - - struct DependencyRegisterer { - DependencyRegisterer(const char *type, const char *name, - Initializer *initializer, - const Dependency &dependency); - }; -}; - -} // namespace port -} // namespace stream_executor - -#define REGISTER_INITIALIZER(type, name, body) \ - static void google_init_##type##_##name() { body; } \ - ::stream_executor::port::Initializer google_initializer_##type##_##name( \ - google_init_##type##_##name) - -#define REGISTER_MODULE_INITIALIZER(name, body) \ - REGISTER_INITIALIZER(module, name, body) - -#define DECLARE_INITIALIZER(type, name) \ - extern ::stream_executor::port::Initializer google_initializer_##type##_##name - -#define DECLARE_MODULE_INITIALIZER(name) DECLARE_INITIALIZER(module, name) - -#define REGISTER_MODULE_INITIALIZER_SEQUENCE(name1, name2) - -#endif // !defined(PLATFORM_GOOGLE) +#include "tensorflow/stream_executor/platform/initialize.h" #endif // TENSORFLOW_STREAM_EXECUTOR_LIB_INITIALIZE_H_ diff --git a/tensorflow/stream_executor/dso_loader.cc b/tensorflow/stream_executor/platform/default/dso_loader.cc similarity index 93% rename from tensorflow/stream_executor/dso_loader.cc rename to tensorflow/stream_executor/platform/default/dso_loader.cc index 6dda5d63155..681f005f170 100644 --- a/tensorflow/stream_executor/dso_loader.cc +++ b/tensorflow/stream_executor/platform/default/dso_loader.cc @@ -16,8 +16,6 @@ limitations under the License. // TODO(jhen): Replace hardcoded, platform specific path strings in GetXXXPath() // with a function in e.g. cuda.h. -#include "tensorflow/stream_executor/dso_loader.h" - #include <limits.h> #include <stdlib.h> #include <initializer_list> @@ -30,6 +28,7 @@ limitations under the License. #include "tensorflow/stream_executor/lib/path.h" #include "tensorflow/stream_executor/lib/str_util.h" #include "tensorflow/stream_executor/lib/stringprintf.h" +#include "tensorflow/stream_executor/platform/dso_loader.h" #include "tensorflow/stream_executor/platform/logging.h" #include "tensorflow/stream_executor/platform/port.h" @@ -89,10 +88,13 @@ string GetCudnnVersion() { return TF_CUDNN_VERSION; } #if defined(__APPLE__) // On Mac OS X, CUDA sometimes installs libcuda.dylib instead of // libcuda.1.dylib. - return status.ok() ? status : GetDsoHandle( - FindDsoPath(port::Env::Default()->FormatLibraryFileName("cuda", ""), - GetCudaDriverLibraryPath()), - dso_handle); + return status.ok() + ? status + : GetDsoHandle( + FindDsoPath( + port::Env::Default()->FormatLibraryFileName("cuda", ""), + GetCudaDriverLibraryPath()), + dso_handle); #else return status; #endif @@ -144,7 +146,7 @@ static mutex& GetRpathMutex() { << ". LD_LIBRARY_PATH: " << (ld_library_path != nullptr ? ld_library_path : "") #endif - ; + ; return port::Status(port::error::FAILED_PRECONDITION, absl::StrCat("could not dlopen DSO: ", path, "; dlerror: ", s.error_message())); @@ -218,27 +220,27 @@ static std::vector<string>* CreatePrimordialRpaths() { /* static */ string DsoLoader::GetCudaLibraryDirPath() { #if defined(__APPLE__) - return "external/local_config_cuda/cuda/lib"; + return "third_party/gpus/cuda/lib"; #else - return "external/local_config_cuda/cuda/lib64"; + return "third_party/gpus/cuda/lib64"; #endif } /* static */ string DsoLoader::GetCudaDriverLibraryPath() { #if defined(__APPLE__) - return "external/local_config_cuda/cuda/driver/lib"; + return "third_party/gpus/cuda/driver/lib"; #elif defined(PLATFORM_WINDOWS) return ""; #else - return "external/local_config_cuda/cuda/driver/lib64"; + return "third_party/gpus/cuda/driver/lib64"; #endif } /* static */ string DsoLoader::GetCudaCuptiLibraryPath() { #if defined(__APPLE__) - return "external/local_config_cuda/cuda/extras/CUPTI/lib"; + return "third_party/gpus/cuda/extras/CUPTI/lib"; #else - return "external/local_config_cuda/cuda/extras/CUPTI/lib64"; + return "third_party/gpus/cuda/extras/CUPTI/lib64"; #endif } diff --git a/tensorflow/stream_executor/dso_loader.h b/tensorflow/stream_executor/platform/default/dso_loader.h similarity index 100% rename from tensorflow/stream_executor/dso_loader.h rename to tensorflow/stream_executor/platform/default/dso_loader.h index f063b68d605..806f65b24cd 100644 --- a/tensorflow/stream_executor/dso_loader.h +++ b/tensorflow/stream_executor/platform/default/dso_loader.h @@ -19,8 +19,8 @@ limitations under the License. #ifndef TENSORFLOW_STREAM_EXECUTOR_DSO_LOADER_H_ #define TENSORFLOW_STREAM_EXECUTOR_DSO_LOADER_H_ -#include "tensorflow/stream_executor/platform/port.h" #include <vector> +#include "tensorflow/stream_executor/platform/port.h" #include "absl/strings/string_view.h" #include "tensorflow/stream_executor/lib/status.h" diff --git a/tensorflow/stream_executor/platform/default/initialize.h b/tensorflow/stream_executor/platform/default/initialize.h new file mode 100644 index 00000000000..2d27c85336e --- /dev/null +++ b/tensorflow/stream_executor/platform/default/initialize.h @@ -0,0 +1,62 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_INITIALIZE_H_ +#define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_INITIALIZE_H_ + +#undef REGISTER_MODULE_INITIALIZER +#undef DECLARE_MODULE_INITIALIZER +#undef REGISTER_MODULE_INITIALIZER_SEQUENCE + +namespace stream_executor { +namespace port { + +class Initializer { + public: + typedef void (*InitializerFunc)(); + explicit Initializer(InitializerFunc func) { func(); } + + struct Dependency { + Dependency(const char *n, Initializer *i) : name(n), initializer(i) {} + const char *const name; + Initializer *const initializer; + }; + + struct DependencyRegisterer { + DependencyRegisterer(const char *type, const char *name, + Initializer *initializer, + const Dependency &dependency); + }; +}; + +} // namespace port +} // namespace stream_executor + +#define REGISTER_INITIALIZER(type, name, body) \ + static void google_init_##type##_##name() { body; } \ + ::stream_executor::port::Initializer google_initializer_##type##_##name( \ + google_init_##type##_##name) + +#define REGISTER_MODULE_INITIALIZER(name, body) \ + REGISTER_INITIALIZER(module, name, body) + +#define DECLARE_INITIALIZER(type, name) \ + extern ::stream_executor::port::Initializer google_initializer_##type##_##name + +#define DECLARE_MODULE_INITIALIZER(name) DECLARE_INITIALIZER(module, name) + +#define REGISTER_MODULE_INITIALIZER_SEQUENCE(name1, name2) + +#endif // TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_INITIALIZE_H_ diff --git a/tensorflow/stream_executor/platform/default/mutex.h b/tensorflow/stream_executor/platform/default/mutex.h index c9f5a7c609e..2f8f0636ba7 100644 --- a/tensorflow/stream_executor/platform/default/mutex.h +++ b/tensorflow/stream_executor/platform/default/mutex.h @@ -16,7 +16,8 @@ limitations under the License. #ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_MUTEX_H_ #define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DEFAULT_MUTEX_H_ -#include "tensorflow/stream_executor/platform/mutex.h" +#include "tensorflow/core/platform/mutex.h" +#include "tensorflow/core/platform/types.h" namespace stream_executor { diff --git a/tensorflow/stream_executor/platform/dso_loader.h b/tensorflow/stream_executor/platform/dso_loader.h new file mode 100644 index 00000000000..1dd56684b19 --- /dev/null +++ b/tensorflow/stream_executor/platform/dso_loader.h @@ -0,0 +1,31 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DSO_LOADER_H_ +#define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DSO_LOADER_H_ + +#include "tensorflow/stream_executor/platform/platform.h" + +// Include appropriate platform-dependent implementations +#if defined(PLATFORM_GOOGLE) +#include "tensorflow/stream_executor/platform/google/dso_loader.h" +#elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \ + defined(PLATFORM_GOOGLE_ANDROID) +#include "tensorflow/stream_executor/platform/default/dso_loader.h" +#else +#error Define the appropriate PLATFORM_<foo> macro for this platform +#endif + +#endif // TENSORFLOW_STREAM_EXECUTOR_PLATFORM_DSO_LOADER_H_ diff --git a/tensorflow/stream_executor/platform/initialize.h b/tensorflow/stream_executor/platform/initialize.h new file mode 100644 index 00000000000..fb13132afff --- /dev/null +++ b/tensorflow/stream_executor/platform/initialize.h @@ -0,0 +1,27 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_INITIALIZE_H_ +#define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_INITIALIZE_H_ + +#include "tensorflow/stream_executor/platform/platform.h" + +#if defined(PLATFORM_GOOGLE) +#include "tensorflow/stream_executor/platform/google/initialize.h" +#else +#include "tensorflow/stream_executor/platform/default/initialize.h" +#endif + +#endif // TENSORFLOW_STREAM_EXECUTOR_PLATFORM_INITIALIZE_H_ diff --git a/tensorflow/stream_executor/platform/mutex.h b/tensorflow/stream_executor/platform/mutex.h index 28828951de5..fa6c8c017c3 100644 --- a/tensorflow/stream_executor/platform/mutex.h +++ b/tensorflow/stream_executor/platform/mutex.h @@ -16,8 +16,7 @@ limitations under the License. #ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_MUTEX_H_ #define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_MUTEX_H_ -#include "tensorflow/core/platform/mutex.h" -#include "tensorflow/core/platform/types.h" +#include "tensorflow/stream_executor/platform/platform.h" #if defined(PLATFORM_GOOGLE) #include "tensorflow/stream_executor/platform/google/mutex.h" diff --git a/tensorflow/stream_executor/platform/platform.h b/tensorflow/stream_executor/platform/platform.h new file mode 100644 index 00000000000..5bf0e120d39 --- /dev/null +++ b/tensorflow/stream_executor/platform/platform.h @@ -0,0 +1,36 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_STREAM_EXECUTOR_PLATFORM_PLATFORM_H_ +#define TENSORFLOW_STREAM_EXECUTOR_PLATFORM_PLATFORM_H_ + +#if !defined(PLATFORM_POSIX) && !defined(PLATFORM_GOOGLE) && \ + !defined(PLATFORM_POSIX_ANDROID) && !defined(PLATFORM_GOOGLE_ANDROID) + +// Choose which platform we are on. +#if defined(ANDROID) || defined(__ANDROID__) +#define PLATFORM_POSIX_ANDROID + +#elif defined(__APPLE__) +#define PLATFORM_POSIX + +#else +// If no platform specified, use: +#define PLATFORM_POSIX + +#endif +#endif + +#endif // TENSORFLOW_STREAM_EXECUTOR_PLATFORM_PLATFORM_H_