[StreamExecutor] getOrDie and dieIfError utils

Reviewers: jlebar

Subscribers: jprice, parallel_libs-commits

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

llvm-svn: 280312
This commit is contained in:
Jason Henline
2016-08-31 23:30:41 +00:00
parent 8dd4dad08b
commit c1e2b83d09
4 changed files with 52 additions and 37 deletions

View File

@@ -23,27 +23,6 @@
#include "streamexecutor/StreamExecutor.h"
/// [Example saxpy host helper functions]
// Example handler for streamexecutor::Expected return values.
template <typename T> T getOrDie(streamexecutor::Expected<T> &&E) {
if (!E) {
std::fprintf(stderr, "Error extracting an expected value: %s.\n",
streamexecutor::consumeAndGetMessage(E.takeError()).c_str());
std::exit(EXIT_FAILURE);
}
return std::move(*E);
}
// Example handler for streamexecutor::Error return values.
void check(streamexecutor::Error &&E) {
if (E) {
std::fprintf(stderr, "Error encountered: %s.\n",
streamexecutor::consumeAndGetMessage(std::move(E)).c_str());
std::exit(EXIT_FAILURE);
}
}
/// [Example saxpy host helper functions]
/// [Example saxpy compiler-generated]
// Code in this namespace is generated by the compiler (e.g. clang).
//
@@ -148,7 +127,7 @@ int main() {
.thenLaunch(ArraySize, 1, *Kernel, A, X, Y)
.thenCopyD2H<float>(X, HostX);
// Wait for the stream to complete.
check(Stream->blockHostUntilDone());
se::dieIfError(Stream->blockHostUntilDone());
// Process output data in HostX.
std::vector<float> ExpectedX = {4, 47, 90, 133};
@@ -157,7 +136,7 @@ int main() {
}
// Free device memory.
check(Device->freeDeviceMemory(X));
check(Device->freeDeviceMemory(Y));
se::dieIfError(Device->freeDeviceMemory(X));
se::dieIfError(Device->freeDeviceMemory(Y));
/// [Example saxpy host main]
}