[StreamExecutor] Add Platform and PlatformManager

Summary: Abstractions for a StreamExecutor platform

Reviewers: jlebar

Subscribers: jprice, parallel_libs-commits

Differential Revision: https://reviews.llvm.org/D23857

llvm-svn: 279779
This commit is contained in:
Jason Henline 2016-08-25 21:33:07 +00:00
parent 3ad8b43cc2
commit 20cf1eb161
5 changed files with 154 additions and 0 deletions

View File

@ -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<Device *> getDevice(size_t DeviceIndex) = 0;
};
} // namespace streamexecutor
#endif // STREAMEXECUTOR_PLATFORM_H

View File

@ -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 <map>
#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<Platform *> getPlatformByName(llvm::StringRef Name);
private:
PlatformManager();
PlatformManager(const PlatformManager &) = delete;
PlatformManager operator=(const PlatformManager &) = delete;
std::map<std::string, std::unique_ptr<Platform>> PlatformsByName;
};
} // namespace streamexecutor
#endif // STREAMEXECUTOR_PLATFORMMANAGER_H

View File

@ -10,7 +10,9 @@ add_library(
Kernel.cpp
KernelSpec.cpp
PackedKernelArgumentArray.cpp
Platform.cpp
PlatformInterfaces.cpp
PlatformManager.cpp
Stream.cpp)
target_link_libraries(streamexecutor ${llvm_libs})

View File

@ -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

View File

@ -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<Platform *> 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