[Parallel_libs-commits] [parallel-libs] r280312 - [StreamExecutor] getOrDie and dieIfError utils

Jason Henline via Parallel_libs-commits parallel_libs-commits at lists.llvm.org
Wed Aug 31 16:30:41 PDT 2016


Author: jhen
Date: Wed Aug 31 18:30:41 2016
New Revision: 280312

URL: http://llvm.org/viewvc/llvm-project?rev=280312&view=rev
Log:
[StreamExecutor] getOrDie and dieIfError utils

Reviewers: jlebar

Subscribers: jprice, parallel_libs-commits

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

Modified:
    parallel-libs/trunk/streamexecutor/examples/Example.cpp
    parallel-libs/trunk/streamexecutor/include/streamexecutor/StreamExecutor.h
    parallel-libs/trunk/streamexecutor/include/streamexecutor/Utils/Error.h
    parallel-libs/trunk/streamexecutor/lib/Utils/Error.cpp

Modified: parallel-libs/trunk/streamexecutor/examples/Example.cpp
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/examples/Example.cpp?rev=280312&r1=280311&r2=280312&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/examples/Example.cpp (original)
+++ parallel-libs/trunk/streamexecutor/examples/Example.cpp Wed Aug 31 18:30:41 2016
@@ -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]
 }

Modified: parallel-libs/trunk/streamexecutor/include/streamexecutor/StreamExecutor.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/include/streamexecutor/StreamExecutor.h?rev=280312&r1=280311&r2=280312&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/include/streamexecutor/StreamExecutor.h (original)
+++ parallel-libs/trunk/streamexecutor/include/streamexecutor/StreamExecutor.h Wed Aug 31 18:30:41 2016
@@ -23,15 +23,12 @@
 ///
 /// \snippet examples/Example.cpp Example saxpy host main
 ///
-/// In the example, a couple of handler functions are used to handle error
-/// return values in the StreamExecutor API:
-///
-/// \snippet examples/Example.cpp Example saxpy host helper functions
-///
-/// These are just example handler functions. A real application will likely
-/// want to define similar handlers of its own that log errors in an
-/// application-specific way, convert errors to the application's own
-/// error-handling framework, or try to recover from errors as appropriate.
+/// In the example, a couple of handler functions, \c getOrDie and \c
+/// dieIfError, are used to handle error return values in the StreamExecutor
+/// API. These functions are provided by StreamExecutor for quick-and-dirty
+/// error handling, but real applications will likely want to define their own
+/// versions of these handlers so that errors are handled more gracefully than
+/// just exiting the program.
 ///
 /// The example also references some symbols from a compiler-generated
 /// namespace:

Modified: parallel-libs/trunk/streamexecutor/include/streamexecutor/Utils/Error.h
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/include/streamexecutor/Utils/Error.h?rev=280312&r1=280311&r2=280312&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/include/streamexecutor/Utils/Error.h (original)
+++ parallel-libs/trunk/streamexecutor/include/streamexecutor/Utils/Error.h Wed Aug 31 18:30:41 2016
@@ -65,6 +65,8 @@
 /// There is also a function consumeError that consumes an error value without
 /// fetching the error message. This is useful when we want to ignore an error.
 ///
+/// The dieIfError function is also provided for quick-and-dirty error handling.
+///
 ///
 /// \section expected The Expected Class
 ///
@@ -137,6 +139,8 @@
 /// }
 /// \endcode
 ///
+/// The getOrDie function is also available for quick-and-dirty error handling.
+///
 ///
 /// \section llvm Relation to llvm::Error and llvm::Expected
 ///
@@ -159,6 +163,8 @@
 #ifndef STREAMEXECUTOR_UTILS_ERROR_H
 #define STREAMEXECUTOR_UTILS_ERROR_H
 
+#include <cstdio>
+#include <cstdlib>
 #include <memory>
 #include <string>
 
@@ -171,14 +177,39 @@ using llvm::Error;
 using llvm::Expected;
 using llvm::Twine;
 
-// Makes an Error object from an error message.
+/// Makes an Error object from an error message.
 Error make_error(Twine Message);
 
-// Consumes the input error and returns its error message.
-//
-// Assumes the input was created by the make_error function above.
+/// Consumes the input error and returns its error message.
+///
+/// Assumes the input was created by the make_error function above.
 std::string consumeAndGetMessage(Error &&E);
 
+/// Extracts the T value from an Expected<T> or prints an error message to
+/// stderr and exits the program with code EXIT_FAILURE if the Expected<T> is an
+/// error.
+///
+/// This function and the dieIfError function are provided for applications that
+/// are OK with aborting the program if an error occurs, and which don't have
+/// any special error logging needs. Applications with different error handling
+/// needs will likely want to declare their own functions with similar
+/// signatures but which log error messages in a different way or attempt to
+/// recover from errors instead of aborting the program.
+template <typename T> T getOrDie(Expected<T> &&E) {
+  if (!E) {
+    std::fprintf(stderr, "Error extracting an expected value: %s.\n",
+                 consumeAndGetMessage(E.takeError()).c_str());
+    std::exit(EXIT_FAILURE);
+  }
+  return std::move(*E);
+}
+
+/// Prints an error message to stderr and exits the program with code
+/// EXIT_FAILURE if the input is an error.
+///
+/// \sa getOrDie
+void dieIfError(Error &&E);
+
 } // namespace streamexecutor
 
 #endif // STREAMEXECUTOR_UTILS_ERROR_H

Modified: parallel-libs/trunk/streamexecutor/lib/Utils/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/parallel-libs/trunk/streamexecutor/lib/Utils/Error.cpp?rev=280312&r1=280311&r2=280312&view=diff
==============================================================================
--- parallel-libs/trunk/streamexecutor/lib/Utils/Error.cpp (original)
+++ parallel-libs/trunk/streamexecutor/lib/Utils/Error.cpp Wed Aug 31 18:30:41 2016
@@ -60,4 +60,12 @@ std::string consumeAndGetMessage(Error &
   return Message;
 }
 
+void dieIfError(Error &&E) {
+  if (E) {
+    std::fprintf(stderr, "Error encountered: %s.\n",
+                 streamexecutor::consumeAndGetMessage(std::move(E)).c_str());
+    std::exit(EXIT_FAILURE);
+  }
+}
+
 } // namespace streamexecutor




More information about the Parallel_libs-commits mailing list