Index: parallel-libs/trunk/streamexecutor/include/streamexecutor/Platform.h =================================================================== --- parallel-libs/trunk/streamexecutor/include/streamexecutor/Platform.h +++ parallel-libs/trunk/streamexecutor/include/streamexecutor/Platform.h @@ -0,0 +1,42 @@ +//===-- Platform.h - The Platform class -------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// The Platform class which represents a platform such as CUDA or OpenCL. +/// +/// This is an abstract base class that will be overridden by each specific +/// platform. +/// +//===----------------------------------------------------------------------===// + +#ifndef STREAMEXECUTOR_PLATFORM_H +#define STREAMEXECUTOR_PLATFORM_H + +#include "streamexecutor/Utils/Error.h" + +namespace streamexecutor { + +class Device; + +class Platform { +public: + virtual ~Platform(); + + /// Gets the number of devices available for this platform. + virtual size_t getDeviceCount() const = 0; + + /// Gets a pointer to a Device with the given index for this platform. + /// + /// Ownership of the Device instance is NOT transferred to the caller. + virtual Expected getDevice(size_t DeviceIndex) = 0; +}; + +} // namespace streamexecutor + +#endif // STREAMEXECUTOR_PLATFORM_H Index: parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformManager.h =================================================================== --- parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformManager.h +++ parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformManager.h @@ -0,0 +1,53 @@ +//===-- PlatformManager.h - The PlatformManager class -----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// PlatformManager is the entry point into the StreamExecutor API. A user +/// begins be calling PlatformManager::getPlatformByName("cuda") where "cuda" +/// can be replaced by any supported platform name. This gives the user a +/// Platform object that can be used to create Device objects for that platform, +/// etc. +/// +//===----------------------------------------------------------------------===// + +#ifndef STREAMEXECUTOR_PLATFORMMANAGER_H +#define STREAMEXECUTOR_PLATFORMMANAGER_H + +#include + +#include "streamexecutor/Platform.h" +#include "streamexecutor/Utils/Error.h" + +namespace streamexecutor { + +/// A singleton that holds a reference to a Platform object for each +/// supported StreamExecutor platform. +class PlatformManager { +public: + /// Gets a reference to the Platform with the given name. + /// + /// The name parameter is not case-sensitive, so the following arguments are + /// all equivalent: "cuda", "CUDA", "Cuda", "cUdA". + /// + /// Returns an error if no platform is present for the name. + /// + /// Ownership of the platform is NOT transferred to the caller. + static Expected getPlatformByName(llvm::StringRef Name); + +private: + PlatformManager(); + PlatformManager(const PlatformManager &) = delete; + PlatformManager operator=(const PlatformManager &) = delete; + + std::map> PlatformsByName; +}; + +} // namespace streamexecutor + +#endif // STREAMEXECUTOR_PLATFORMMANAGER_H Index: parallel-libs/trunk/streamexecutor/lib/CMakeLists.txt =================================================================== --- parallel-libs/trunk/streamexecutor/lib/CMakeLists.txt +++ parallel-libs/trunk/streamexecutor/lib/CMakeLists.txt @@ -10,7 +10,9 @@ Kernel.cpp KernelSpec.cpp PackedKernelArgumentArray.cpp + Platform.cpp PlatformInterfaces.cpp + PlatformManager.cpp Stream.cpp) target_link_libraries(streamexecutor ${llvm_libs}) Index: parallel-libs/trunk/streamexecutor/lib/Platform.cpp =================================================================== --- parallel-libs/trunk/streamexecutor/lib/Platform.cpp +++ parallel-libs/trunk/streamexecutor/lib/Platform.cpp @@ -0,0 +1,21 @@ +//===-- Platform.cpp - Platform implementation ----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Implementation of Platform class internals. +/// +//===----------------------------------------------------------------------===// + +#include "streamexecutor/Platform.h" + +namespace streamexecutor { + +Platform::~Platform() = default; + +} // namespace streamexecutor Index: parallel-libs/trunk/streamexecutor/lib/PlatformManager.cpp =================================================================== --- parallel-libs/trunk/streamexecutor/lib/PlatformManager.cpp +++ parallel-libs/trunk/streamexecutor/lib/PlatformManager.cpp @@ -0,0 +1,36 @@ +//===-- PlatformManager.cpp - PlatformManager implementation --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Implementation of PlatformManager class internals. +/// +//===----------------------------------------------------------------------===// + +#include "streamexecutor/PlatformManager.h" + +namespace streamexecutor { + +PlatformManager::PlatformManager() { + // TODO(jhen): Register known platforms by name. + // We have a couple of options here: + // * Use build-system flags to set preprocessor macros that select the + // appropriate code to include here. + // * Use static initialization tricks to have platform libraries register + // themselves when they are loaded. +} + +Expected PlatformManager::getPlatformByName(llvm::StringRef Name) { + static PlatformManager Instance; + auto Iterator = Instance.PlatformsByName.find(Name.lower()); + if (Iterator != Instance.PlatformsByName.end()) + return Iterator->second.get(); + return make_error("no available platform with name " + Name); +} + +} // namespace streamexecutor