oneapi-src / oneAPI-samples

Samples for Intel® oneAPI Toolkits
https://oneapi-src.github.io/oneAPI-samples/
MIT License
915 stars 678 forks source link

catch exception in main function #2292

Open wenju-he opened 4 months ago

wenju-he commented 4 months ago

Many samples don't catch SYCL exceptions, e.g. https://github.com/oneapi-src/oneAPI-samples/blob/9fa0a0f79902fed87190a21ba64e37510ee3bc96/DirectProgramming/C%2B%2BSYCL/ParallelPatterns/histogram/src/main.cpp#L106-L121 For instance, when an OpenCL backend API clCreateKernel fails with error INVALID_PROGRAM_EXECUTABLE, current backtrace isn't intuitive for users:

Exception Code: 0xE06D7363
0x00007FFF7966F19C, C:\WINDOWS\System32\KERNELBASE.dll(0x00007FFF79630000) + 0x3F19C byte(s), RaiseException() + 0x6C byte(s)
0x00007FFF4DAF3859, C:\WINDOWS\SYSTEM32\MSVCP140D.dll(0x00007FFF4DAF0000) + 0x3859 byte(s), ?__ExceptionPtrRethrow@@YAXPEBX@Z() + 0x1B9 byte(s)
0x00007FFF4A90ED1B, \bin\sycl8d.dll(0x00007FFF4A900000) + 0xED1B byte(s), ?make_event@level_zero@oneapi@ext@_V1@sycl@@YA?AVevent@45@AEBVcontext@45@_K_N@Z() + 0x8EB byte(s)
0x00007FFF4AB74021, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x274021 byte(s), ?flush@stream_impl@detail@_V1@sycl@@QEAAXXZ() + 0x4E941 byte(s)
0x00007FFF5B8C1060, C:\WINDOWS\SYSTEM32\VCRUNTIME140D.dll(0x00007FFF5B8C0000) + 0x1060 byte(s)
0x00007FFF5B8C7CAA, C:\WINDOWS\SYSTEM32\VCRUNTIME140D.dll(0x00007FFF5B8C0000) + 0x7CAA byte(s), _is_exception_typeof() + 0x3EDA byte(s)
0x00007FFF7BB13C06, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFF7BA70000) + 0xA3C06 byte(s), RtlCaptureContext2() + 0x4A6 byte(s)
0x00007FFF4AB73C11, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x273C11 byte(s), ?flush@stream_impl@detail@_V1@sycl@@QEAAXXZ() + 0x4E531 byte(s)
0x00007FFF4AB724D5, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x2724D5 byte(s), ?flush@stream_impl@detail@_V1@sycl@@QEAAXXZ() + 0x4CDF5 byte(s)
0x00007FFF4ABE6B10, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x2E6B10 byte(s), ?finalize@handler@_V1@sycl@@AEAA?AVevent@23@XZ() + 0x3A60 byte(s)
0x00007FFF4AB0F6DE, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x20F6DE byte(s), __sycl_unregister_lib() + 0x3331E byte(s)
0x00007FFF4AB0F377, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x20F377 byte(s), __sycl_unregister_lib() + 0x32FB7 byte(s)
0x00007FFF4AB0F011, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x20F011 byte(s), __sycl_unregister_lib() + 0x32C51 byte(s)
0x00007FFF4AC31EF7, \bin\sycl8d.dll(0x00007FFF4A900000) + 0x331EF7 byte(s), ?submit_impl@queue@_V1@sycl@@AEAA?AVevent@23@V?$function@$$A6AXAEAVhandler@_V1@sycl@@@Z@std@@AEBUcode_location@detail@23@@Z() + 0x37 byte(s)
0x00007FF6DE2227A3, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0x827A3 byte(s), sycl::_V1::queue::submit<`lambda at \hetero\dpcpp\parallel_backend_sycl.h:232:46'>() + 0x83 byte(s), \include\sycl\queue.hpp, line 358 + 0x2F byte(s)
0x00007FF6DE2225F2, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0x825F2 byte(s), oneapi::dpl::__par_backend_hetero::__parallel_for_submitter<oneapi::dpl::__par_backend_hetero::__internal::__optional_kernel_name<> >::operator()<oneapi::dpl::execution::__dpl::device_policy<oneapi::dpl::execution::__dpl::DefaultKernelName> &,oneapi::dpl::internal::custom_brick<oneapi::dpl::__internal::__pstl_less,const long long,1>,int,oneapi::dpl::__ranges::zip_view<oneapi::dpl::__ranges::all_view<unsigned long long,1024,2014,1>,oneapi::dpl::__ranges::guard_view<oneapi::dpl() + 0x102 byte(s), oneapi/dpl/pstl\hetero\..\hetero/dpcpp/parallel_backend_sycl.h, line 232 + 0x6C byte(s)
0x00007FF6DE2210DA, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0x810DA byte(s), oneapi::dpl::__par_backend_hetero::__parallel_for<oneapi::dpl::execution::__dpl::device_policy<oneapi::dpl::execution::__dpl::DefaultKernelName> &,oneapi::dpl::internal::custom_brick<oneapi::dpl::__internal::__pstl_less,const long long,1>,int,0,oneapi::dpl::__ranges::zip_view<oneapi::dpl::__ranges::all_view<unsigned long long,1024,2014,1>,oneapi::dpl::__ranges::guard_view<oneapi::dpl::counting_iterator<int> >,oneapi::dpl::__ranges::all_view<unsigned long long,1026,2014,1> > &() + 0x6A byte(s), oneapi/dpl/pstl\hetero\..\hetero/dpcpp/parallel_backend_sycl.h, line 255 + 0x3C byte(s)
0x00007FF6DE22052E, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0x8052E byte(s), oneapi::dpl::internal::upper_bound_impl<oneapi::dpl::execution::__dpl::device_policy<oneapi::dpl::execution::__dpl::DefaultKernelName> &,oneapi::dpl::__internal::sycl_iterator<1026,unsigned long long,sycl::_V1::detail::aligned_allocator<unsigned long long> >,oneapi::dpl::counting_iterator<int>,oneapi::dpl::__internal::sycl_iterator<1026,unsigned long long,sycl::_V1::detail::aligned_allocator<unsigned long long> >,oneapi::dpl::__internal::__pstl_less>() + 0x2EE byte(s), oneapi/dpl/internal/binary_search_impl.h, line 161 + 0x31 byte(s)
0x00007FF6DE1AF4B7, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xF4B7 byte(s), oneapi::dpl::upper_bound<oneapi::dpl::execution::__dpl::device_policy<oneapi::dpl::execution::__dpl::DefaultKernelName> &,oneapi::dpl::__internal::sycl_iterator<1026,unsigned long long,sycl::_V1::detail::aligned_allocator<unsigned long long> >,oneapi::dpl::counting_iterator<int>,oneapi::dpl::__internal::sycl_iterator<1026,unsigned long long,sycl::_V1::detail::aligned_allocator<unsigned long long> > >() + 0xF7 byte(s), oneapi/dpl/internal/binary_search_impl.h, line 228 + 0x82 byte(s)
0x00007FF6DE1ABDA9, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xBDA9 byte(s), dense_histogram() + 0x499 byte(s), \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\src\main.cpp, line 41 + 0x18D byte(s)
0x00007FF6DE1AD226, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xD226 byte(s), main() + 0x106 byte(s), \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\src\main.cpp, line 118 + 0x9 byte(s)
0x00007FF6DE25D509, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xBD509 byte(s), invoke_main() + 0x39 byte(s), \vctools\crt\vcstartup\src\startup\exe_common.inl, line 79 + 0x0 byte(s)
0x00007FF6DE25D3EE, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xBD3EE byte(s), __scrt_common_main_seh() + 0x12E byte(s), \vctools\crt\vcstartup\src\startup\exe_common.inl, line 288 + 0x5 byte(s)
0x00007FF6DE25D2AE, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xBD2AE byte(s), __scrt_common_main() + 0xE byte(s), \vctools\crt\vcstartup\src\startup\exe_common.inl, line 331 + 0x0 byte(s)
0x00007FF6DE25D59E, \oneAPI-samples\DirectProgramming\C++SYCL\ParallelPatterns\histogram\histogram.run(0x00007FF6DE1A0000) + 0xBD59E byte(s), mainCRTStartup() + 0xE byte(s), \vctools\crt\vcstartup\src\startup\exe_main.cpp, line 17 + 0x0 byte(s)
0x00007FFF7A7F4CB0, C:\WINDOWS\System32\KERNEL32.DLL(0x00007FFF7A7E0000) + 0x14CB0 byte(s), BaseThreadInitThunk() + 0x10 byte(s)
0x00007FFF7BAEE8AB, C:\WINDOWS\SYSTEM32\ntdll.dll(0x00007FFF7BA70000) + 0x7E8AB byte(s), RtlUserThreadStart() + 0x2B byte(s)

If the exception is caught and reported in the main function like below change:

diff --git a/DirectProgramming/C++SYCL/ParallelPatterns/histogram/src/main.cpp b/DirectProgramming/C++SYCL/ParallelPatterns/histogram/src/main.cpp
index b671d9a5..6371f477 100644
--- a/DirectProgramming/C++SYCL/ParallelPatterns/histogram/src/main.cpp
+++ b/DirectProgramming/C++SYCL/ParallelPatterns/histogram/src/main.cpp
@@ -115,7 +115,12 @@ int main(void) {
   // which shows the difference between sparse and dense algorithm output
   for (int i = 0; i < N; i++)
     if (input[i] == 4) input[i] = rand() % 3;
-  dense_histogram(input);
-  sparse_histogram(input);
+  try {
+    dense_histogram(input);
+    sparse_histogram(input);
+  } catch (std::exception &e) {
+    std::cout << "Exception caught: " << e.what() << "\n";
+    return -1;
+  }
   return 0;
 }

, we'll see following error which is easier to understand:

Exception caught: Native API failed. Native API returns: -45 (PI_ERROR_INVALID_PROGRAM_EXECUTABLE)