[Parallel_libs-commits] [parallel-libs] r280401 - [StreamExecutor] Dev handles in platform interface
Jason Henline via Parallel_libs-commits
parallel_libs-commits at lists.llvm.org
Thu Sep 1 11:48:22 PDT 2016
Author: jhen
Date: Thu Sep 1 13:48:21 2016
New Revision: 280401
URL: http://llvm.org/viewvc/llvm-project?rev=280401&view=rev
Log:
[StreamExecutor] Dev handles in platform interface
Summary:
This is the first in a series of patches that will convert
GlobalDeviceMemory to own its device memory handle. The first step is to
remove GlobalDeviceMemoryBase from the PlatformInterface interfaces and
use raw handles there instead. This is useful because
GlobalDeviceMemoryBase is going to lose its importance in this process.
Reviewers: jlebar
Subscribers: jprice, parallel_libs-commits
Differential Revision: https://reviews.llvm.org/D24114
Added:
parallel-libs/trunk/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h
Modified:
parallel-libs/trunk/streamexecutor/include/streamexecutor/Device.h
parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformInterfaces.h
parallel-libs/trunk/streamexecutor/include/streamexecutor/Stream.h
parallel-libs/trunk/streamexecutor/lib/unittests/DeviceTest.cpp
parallel-libs/trunk/streamexecutor/lib/unittests/StreamTest.cpp
Modified: parallel-libs/trunk/streamexecutor/include/streamexecutor/Device.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/include/streamexecutor/Device.h?rev=280401&r1=280400&r2=280401&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/include/streamexecutor/Device.h (original)
+++ parallel-libs/trunk/streamexecutor/include/streamexecutor/Device.h Thu Sep 1 13:48:21 2016
@@ -56,16 +56,17 @@ public:
/// Allocates an array of ElementCount entries of type T in device memory.
template <typename T>
Expected<GlobalDeviceMemory<T>> allocateDeviceMemory(size_t ElementCount) {
- Expected<GlobalDeviceMemoryBase> MaybeBase =
+ Expected<void *> MaybeMemory =
PDevice->allocateDeviceMemory(ElementCount * sizeof(T));
- if (!MaybeBase)
- return MaybeBase.takeError();
- return GlobalDeviceMemory<T>(*MaybeBase);
+ if (!MaybeMemory)
+ return MaybeMemory.takeError();
+ return GlobalDeviceMemory<T>::makeFromElementCount(*MaybeMemory,
+ ElementCount);
}
/// Frees memory previously allocated with allocateDeviceMemory.
template <typename T> Error freeDeviceMemory(GlobalDeviceMemory<T> Memory) {
- return PDevice->freeDeviceMemory(Memory);
+ return PDevice->freeDeviceMemory(Memory.getHandle());
}
/// Allocates an array of ElementCount entries of type T in host memory.
@@ -140,7 +141,7 @@ public:
return make_error(
"copying too many elements, " + llvm::Twine(ElementCount) +
", to a host array of element count " + llvm::Twine(Dst.size()));
- return PDevice->synchronousCopyD2H(Src.getBaseMemory(),
+ return PDevice->synchronousCopyD2H(Src.getBaseMemory().getHandle(),
Src.getElementOffset() * sizeof(T),
Dst.data(), 0, ElementCount * sizeof(T));
}
@@ -194,9 +195,9 @@ public:
llvm::Twine(ElementCount) +
", to a device array of element count " +
llvm::Twine(Dst.getElementCount()));
- return PDevice->synchronousCopyH2D(Src.data(), 0, Dst.getBaseMemory(),
- Dst.getElementOffset() * sizeof(T),
- ElementCount * sizeof(T));
+ return PDevice->synchronousCopyH2D(
+ Src.data(), 0, Dst.getBaseMemory().getHandle(),
+ Dst.getElementOffset() * sizeof(T), ElementCount * sizeof(T));
}
template <typename T>
@@ -250,8 +251,8 @@ public:
", to a device array of element count " +
llvm::Twine(Dst.getElementCount()));
return PDevice->synchronousCopyD2D(
- Src.getBaseMemory(), Src.getElementOffset() * sizeof(T),
- Dst.getBaseMemory(), Dst.getElementOffset() * sizeof(T),
+ Src.getBaseMemory().getHandle(), Src.getElementOffset() * sizeof(T),
+ Dst.getBaseMemory().getHandle(), Dst.getElementOffset() * sizeof(T),
ElementCount * sizeof(T));
}
Modified: parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformInterfaces.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformInterfaces.h?rev=280401&r1=280400&r2=280401&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformInterfaces.h (original)
+++ parallel-libs/trunk/streamexecutor/include/streamexecutor/PlatformInterfaces.h Thu Sep 1 13:48:21 2016
@@ -94,8 +94,7 @@ public:
///
/// HostDst should have been allocated by allocateHostMemory or registered
/// with registerHostMemory.
- virtual Error copyD2H(PlatformStreamHandle *S,
- const GlobalDeviceMemoryBase &DeviceSrc,
+ virtual Error copyD2H(PlatformStreamHandle *S, const void *DeviceSrcHandle,
size_t SrcByteOffset, void *HostDst,
size_t DstByteOffset, size_t ByteCount) {
return make_error("copyD2H not implemented for platform " + getName());
@@ -106,15 +105,14 @@ public:
/// HostSrc should have been allocated by allocateHostMemory or registered
/// with registerHostMemory.
virtual Error copyH2D(PlatformStreamHandle *S, const void *HostSrc,
- size_t SrcByteOffset, GlobalDeviceMemoryBase DeviceDst,
+ size_t SrcByteOffset, const void *DeviceDstHandle,
size_t DstByteOffset, size_t ByteCount) {
return make_error("copyH2D not implemented for platform " + getName());
}
/// Copies data from one device location to another.
- virtual Error copyD2D(PlatformStreamHandle *S,
- const GlobalDeviceMemoryBase &DeviceSrc,
- size_t SrcByteOffset, GlobalDeviceMemoryBase DeviceDst,
+ virtual Error copyD2D(PlatformStreamHandle *S, const void *DeviceSrcHandle,
+ size_t SrcByteOffset, const void *DeviceDstHandle,
size_t DstByteOffset, size_t ByteCount) {
return make_error("copyD2D not implemented for platform " + getName());
}
@@ -127,14 +125,13 @@ public:
}
/// Allocates untyped device memory of a given size in bytes.
- virtual Expected<GlobalDeviceMemoryBase>
- allocateDeviceMemory(size_t ByteCount) {
+ virtual Expected<void *> allocateDeviceMemory(size_t ByteCount) {
return make_error("allocateDeviceMemory not implemented for platform " +
getName());
}
/// Frees device memory previously allocated by allocateDeviceMemory.
- virtual Error freeDeviceMemory(GlobalDeviceMemoryBase Memory) {
+ virtual Error freeDeviceMemory(const void *Handle) {
return make_error("freeDeviceMemory not implemented for platform " +
getName());
}
@@ -172,29 +169,29 @@ public:
/// Blocks the calling host thread until the copy is completed. Can operate on
/// any host memory, not just registered host memory or host memory allocated
/// by allocateHostMemory. Does not block any ongoing device calls.
- virtual Error synchronousCopyD2H(const GlobalDeviceMemoryBase &DeviceSrc,
+ virtual Error synchronousCopyD2H(const void *DeviceSrcHandle,
size_t SrcByteOffset, void *HostDst,
size_t DstByteOffset, size_t ByteCount) {
return make_error("synchronousCopyD2H not implemented for platform " +
getName());
}
- /// Similar to synchronousCopyD2H(const GlobalDeviceMemoryBase &, size_t, void
+ /// Similar to synchronousCopyD2H(const void *, size_t, void
/// *, size_t, size_t), but copies memory from host to device rather than
/// device to host.
virtual Error synchronousCopyH2D(const void *HostSrc, size_t SrcByteOffset,
- GlobalDeviceMemoryBase DeviceDst,
+ const void *DeviceDstHandle,
size_t DstByteOffset, size_t ByteCount) {
return make_error("synchronousCopyH2D not implemented for platform " +
getName());
}
- /// Similar to synchronousCopyD2H(const GlobalDeviceMemoryBase &, size_t, void
+ /// Similar to synchronousCopyD2H(const void *, size_t, void
/// *, size_t, size_t), but copies memory from one location in device memory
/// to another rather than from device to host.
- virtual Error synchronousCopyD2D(GlobalDeviceMemoryBase DeviceDst,
+ virtual Error synchronousCopyD2D(const void *DeviceDstHandle,
size_t DstByteOffset,
- const GlobalDeviceMemoryBase &DeviceSrc,
+ const void *DeviceSrcHandle,
size_t SrcByteOffset, size_t ByteCount) {
return make_error("synchronousCopyD2D not implemented for platform " +
getName());
Modified: parallel-libs/trunk/streamexecutor/include/streamexecutor/Stream.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/include/streamexecutor/Stream.h?rev=280401&r1=280400&r2=280401&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/include/streamexecutor/Stream.h (original)
+++ parallel-libs/trunk/streamexecutor/include/streamexecutor/Stream.h Thu Sep 1 13:48:21 2016
@@ -136,7 +136,8 @@ public:
setError("copying too many elements, " + llvm::Twine(ElementCount) +
", to a host array of element count " + llvm::Twine(Dst.size()));
else
- setError(PDevice->copyD2H(ThePlatformStream.get(), Src.getBaseMemory(),
+ setError(PDevice->copyD2H(ThePlatformStream.get(),
+ Src.getBaseMemory().getHandle(),
Src.getElementOffset() * sizeof(T), Dst.data(),
0, ElementCount * sizeof(T)));
return *this;
@@ -193,9 +194,10 @@ public:
", to a device array of element count " +
llvm::Twine(Dst.getElementCount()));
else
- setError(PDevice->copyH2D(
- ThePlatformStream.get(), Src.data(), 0, Dst.getBaseMemory(),
- Dst.getElementOffset() * sizeof(T), ElementCount * sizeof(T)));
+ setError(PDevice->copyH2D(ThePlatformStream.get(), Src.data(), 0,
+ Dst.getBaseMemory().getHandle(),
+ Dst.getElementOffset() * sizeof(T),
+ ElementCount * sizeof(T)));
return *this;
}
@@ -250,8 +252,8 @@ public:
llvm::Twine(Dst.getElementCount()));
else
setError(PDevice->copyD2D(
- ThePlatformStream.get(), Src.getBaseMemory(),
- Src.getElementOffset() * sizeof(T), Dst.getBaseMemory(),
+ ThePlatformStream.get(), Src.getBaseMemory().getHandle(),
+ Src.getElementOffset() * sizeof(T), Dst.getBaseMemory().getHandle(),
Dst.getElementOffset() * sizeof(T), ElementCount * sizeof(T)));
return *this;
}
Modified: parallel-libs/trunk/streamexecutor/lib/unittests/DeviceTest.cpp
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/lib/unittests/DeviceTest.cpp?rev=280401&r1=280400&r2=280401&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/lib/unittests/DeviceTest.cpp (original)
+++ parallel-libs/trunk/streamexecutor/lib/unittests/DeviceTest.cpp Thu Sep 1 13:48:21 2016
@@ -15,6 +15,7 @@
#include <cstdlib>
#include <cstring>
+#include "SimpleHostPlatformDevice.h"
#include "streamexecutor/Device.h"
#include "streamexecutor/PlatformInterfaces.h"
@@ -24,79 +25,6 @@ namespace {
namespace se = ::streamexecutor;
-class MockPlatformDevice : public se::PlatformDevice {
-public:
- ~MockPlatformDevice() override {}
-
- std::string getName() const override { return "MockPlatformDevice"; }
-
- se::Expected<std::unique_ptr<se::PlatformStreamHandle>>
- createStream() override {
- return se::make_error("not implemented");
- }
-
- se::Expected<se::GlobalDeviceMemoryBase>
- allocateDeviceMemory(size_t ByteCount) override {
- return se::GlobalDeviceMemoryBase(std::malloc(ByteCount));
- }
-
- se::Error freeDeviceMemory(se::GlobalDeviceMemoryBase Memory) override {
- std::free(const_cast<void *>(Memory.getHandle()));
- return se::Error::success();
- }
-
- se::Expected<void *> allocateHostMemory(size_t ByteCount) override {
- return std::malloc(ByteCount);
- }
-
- se::Error freeHostMemory(void *Memory) override {
- std::free(Memory);
- return se::Error::success();
- }
-
- se::Error registerHostMemory(void *, size_t) override {
- return se::Error::success();
- }
-
- se::Error unregisterHostMemory(void *) override {
- return se::Error::success();
- }
-
- se::Error synchronousCopyD2H(const se::GlobalDeviceMemoryBase &DeviceSrc,
- size_t SrcByteOffset, void *HostDst,
- size_t DstByteOffset,
- size_t ByteCount) override {
- std::memcpy(static_cast<char *>(HostDst) + DstByteOffset,
- static_cast<const char *>(DeviceSrc.getHandle()) +
- SrcByteOffset,
- ByteCount);
- return se::Error::success();
- }
-
- se::Error synchronousCopyH2D(const void *HostSrc, size_t SrcByteOffset,
- se::GlobalDeviceMemoryBase DeviceDst,
- size_t DstByteOffset,
- size_t ByteCount) override {
- std::memcpy(static_cast<char *>(const_cast<void *>(DeviceDst.getHandle())) +
- DstByteOffset,
- static_cast<const char *>(HostSrc) + SrcByteOffset, ByteCount);
- return se::Error::success();
- }
-
- se::Error synchronousCopyD2D(se::GlobalDeviceMemoryBase DeviceDst,
- size_t DstByteOffset,
- const se::GlobalDeviceMemoryBase &DeviceSrc,
- size_t SrcByteOffset,
- size_t ByteCount) override {
- std::memcpy(static_cast<char *>(const_cast<void *>(DeviceDst.getHandle())) +
- DstByteOffset,
- static_cast<const char *>(DeviceSrc.getHandle()) +
- SrcByteOffset,
- ByteCount);
- return se::Error::success();
- }
-};
-
/// Test fixture to hold objects used by tests.
class DeviceTest : public ::testing::Test {
public:
@@ -124,7 +52,7 @@ public:
int Host5[5];
int Host7[7];
- MockPlatformDevice PDevice;
+ SimpleHostPlatformDevice PDevice;
se::Device Device;
};
Added: parallel-libs/trunk/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h?rev=280401&view=auto
==============================================================================
--- parallel-libs/trunk/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h (added)
+++ parallel-libs/trunk/streamexecutor/lib/unittests/SimpleHostPlatformDevice.h Thu Sep 1 13:48:21 2016
@@ -0,0 +1,135 @@
+//===-- SimpleHostPlatformDevice.h - Host device for testing ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// The SimpleHostPlatformDevice class is a streamexecutor::PlatformDevice that
+/// is really just the host processor and memory. It is useful for testing
+/// because no extra device platform is required.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H
+#define STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H
+
+#include <cstdlib>
+#include <cstring>
+
+#include "streamexecutor/PlatformInterfaces.h"
+
+/// A streamexecutor::PlatformDevice that simply forwards all operations to the
+/// host platform.
+///
+/// The allocate and copy methods are simple wrappers for std::malloc and
+/// std::memcpy.
+class SimpleHostPlatformDevice : public streamexecutor::PlatformDevice {
+ std::string getName() const override { return "SimpleHostPlatformDevice"; }
+
+ streamexecutor::Expected<
+ std::unique_ptr<streamexecutor::PlatformStreamHandle>>
+ createStream() override {
+ return nullptr;
+ }
+
+ streamexecutor::Expected<void *>
+ allocateDeviceMemory(size_t ByteCount) override {
+ return std::malloc(ByteCount);
+ }
+
+ streamexecutor::Error freeDeviceMemory(const void *Handle) override {
+ std::free(const_cast<void *>(Handle));
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Expected<void *>
+ allocateHostMemory(size_t ByteCount) override {
+ return std::malloc(ByteCount);
+ }
+
+ streamexecutor::Error freeHostMemory(void *Memory) override {
+ std::free(const_cast<void *>(Memory));
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error registerHostMemory(void *Memory,
+ size_t ByteCount) override {
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error unregisterHostMemory(void *Memory) override {
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error copyD2H(streamexecutor::PlatformStreamHandle *S,
+ const void *DeviceHandleSrc,
+ size_t SrcByteOffset, void *HostDst,
+ size_t DstByteOffset,
+ size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(HostDst) + DstByteOffset,
+ static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
+ ByteCount);
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error copyH2D(streamexecutor::PlatformStreamHandle *S,
+ const void *HostSrc, size_t SrcByteOffset,
+ const void *DeviceHandleDst,
+ size_t DstByteOffset,
+ size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
+ DstByteOffset,
+ static_cast<const char *>(HostSrc) + SrcByteOffset, ByteCount);
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error
+ copyD2D(streamexecutor::PlatformStreamHandle *S, const void *DeviceHandleSrc,
+ size_t SrcByteOffset, const void *DeviceHandleDst,
+ size_t DstByteOffset, size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
+ DstByteOffset,
+ static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
+ ByteCount);
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error synchronousCopyD2H(const void *DeviceHandleSrc,
+ size_t SrcByteOffset, void *HostDst,
+ size_t DstByteOffset,
+ size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(HostDst) + DstByteOffset,
+ static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
+ ByteCount);
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error synchronousCopyH2D(const void *HostSrc,
+ size_t SrcByteOffset,
+ const void *DeviceHandleDst,
+ size_t DstByteOffset,
+ size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
+ DstByteOffset,
+ static_cast<const char *>(HostSrc) + SrcByteOffset, ByteCount);
+ return streamexecutor::Error::success();
+ }
+
+ streamexecutor::Error synchronousCopyD2D(const void *DeviceHandleSrc,
+ size_t SrcByteOffset,
+ const void *DeviceHandleDst,
+ size_t DstByteOffset,
+ size_t ByteCount) override {
+ std::memcpy(static_cast<char *>(const_cast<void *>(DeviceHandleDst)) +
+ DstByteOffset,
+ static_cast<const char *>(DeviceHandleSrc) + SrcByteOffset,
+ ByteCount);
+ return streamexecutor::Error::success();
+ }
+};
+
+#endif // STREAMEXECUTOR_LIB_UNITTESTS_SIMPLEHOSTPLATFORMDEVICE_H
Modified: parallel-libs/trunk/streamexecutor/lib/unittests/StreamTest.cpp
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/lib/unittests/StreamTest.cpp?rev=280401&r1=280400&r2=280401&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/lib/unittests/StreamTest.cpp (original)
+++ parallel-libs/trunk/streamexecutor/lib/unittests/StreamTest.cpp Thu Sep 1 13:48:21 2016
@@ -14,6 +14,7 @@
#include <cstring>
+#include "SimpleHostPlatformDevice.h"
#include "streamexecutor/Device.h"
#include "streamexecutor/Kernel.h"
#include "streamexecutor/KernelSpec.h"
@@ -26,52 +27,6 @@ namespace {
namespace se = ::streamexecutor;
-/// Mock PlatformDevice that performs asynchronous memcpy operations by
-/// ignoring the stream argument and calling std::memcpy on device memory
-/// handles.
-class MockPlatformDevice : public se::PlatformDevice {
-public:
- ~MockPlatformDevice() override {}
-
- std::string getName() const override { return "MockPlatformDevice"; }
-
- se::Expected<std::unique_ptr<se::PlatformStreamHandle>>
- createStream() override {
- return nullptr;
- }
-
- se::Error copyD2H(se::PlatformStreamHandle *S,
- const se::GlobalDeviceMemoryBase &DeviceSrc,
- size_t SrcByteOffset, void *HostDst, size_t DstByteOffset,
- size_t ByteCount) override {
- std::memcpy(HostDst, static_cast<const char *>(DeviceSrc.getHandle()) +
- SrcByteOffset,
- ByteCount);
- return se::Error::success();
- }
-
- se::Error copyH2D(se::PlatformStreamHandle *S, const void *HostSrc,
- size_t SrcByteOffset, se::GlobalDeviceMemoryBase DeviceDst,
- size_t DstByteOffset, size_t ByteCount) override {
- std::memcpy(static_cast<char *>(const_cast<void *>(DeviceDst.getHandle())) +
- DstByteOffset,
- HostSrc, ByteCount);
- return se::Error::success();
- }
-
- se::Error copyD2D(se::PlatformStreamHandle *S,
- const se::GlobalDeviceMemoryBase &DeviceSrc,
- size_t SrcByteOffset, se::GlobalDeviceMemoryBase DeviceDst,
- size_t DstByteOffset, size_t ByteCount) override {
- std::memcpy(static_cast<char *>(const_cast<void *>(DeviceDst.getHandle())) +
- DstByteOffset,
- static_cast<const char *>(DeviceSrc.getHandle()) +
- SrcByteOffset,
- ByteCount);
- return se::Error::success();
- }
-};
-
/// Test fixture to hold objects used by tests.
class StreamTest : public ::testing::Test {
public:
@@ -100,7 +55,7 @@ protected:
int Host5[5];
int Host7[7];
- MockPlatformDevice PDevice;
+ SimpleHostPlatformDevice PDevice;
se::Stream Stream;
};
More information about the Parallel_libs-commits
mailing list