|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#ifndef COMMON_HIPSPARSELT_UTILS_HPP |
| 24 | +#define COMMON_HIPSPARSELT_UTILS_HPP |
| 25 | + |
| 26 | +#include <hipsparselt/hipsparselt.h> |
| 27 | + |
| 28 | +#include <cstdlib> |
| 29 | +#include <iostream> |
| 30 | + |
| 31 | +/// \brief Converts a \p hipsparseStatus_t to its correspondent string. |
| 32 | +inline const char* hipsparseStatusToString(hipsparseStatus_t status) |
| 33 | +{ |
| 34 | + switch(status) |
| 35 | + { |
| 36 | + case HIPSPARSE_STATUS_SUCCESS: |
| 37 | + return "Success"; |
| 38 | + |
| 39 | + case HIPSPARSE_STATUS_NOT_INITIALIZED: |
| 40 | + return "hipSPARSELt was not initialized"; |
| 41 | + |
| 42 | + case HIPSPARSE_STATUS_ALLOC_FAILED: |
| 43 | + return "Resource allocation failed"; |
| 44 | + |
| 45 | + case HIPSPARSE_STATUS_INVALID_VALUE: |
| 46 | + return "Invalid value"; |
| 47 | + |
| 48 | + case HIPSPARSE_STATUS_ARCH_MISMATCH: |
| 49 | + return "Device architecture not supported"; |
| 50 | + |
| 51 | + case HIPSPARSE_STATUS_MAPPING_ERROR: |
| 52 | + return "Access to GPU memory space failed"; |
| 53 | + |
| 54 | + case HIPSPARSE_STATUS_EXECUTION_FAILED: |
| 55 | + return "GPU program failed to execute"; |
| 56 | + |
| 57 | + case HIPSPARSE_STATUS_INTERNAL_ERROR: |
| 58 | + return "An internal hipSPARSELt operation failed"; |
| 59 | + |
| 60 | + case HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED: |
| 61 | + return "Matrix type not supported"; |
| 62 | + |
| 63 | + case HIPSPARSE_STATUS_ZERO_PIVOT: |
| 64 | + return "Zero pivot was computed"; |
| 65 | + |
| 66 | + case HIPSPARSE_STATUS_NOT_SUPPORTED: |
| 67 | + return "Operation is not supported"; |
| 68 | + |
| 69 | +#if !defined(CUDART_VERSION) || (defined(CUDART_VERSION) && CUDART_VERSION >= 11003) |
| 70 | + case HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES: |
| 71 | + return "Resources are insufficient"; |
| 72 | +#endif |
| 73 | + |
| 74 | + // We do use default because we are not in control of these enumeration values. |
| 75 | + // Ideally this function is something hipSPARSELt would provide |
| 76 | + default: |
| 77 | + return "<unknown hipsparseStatus_t value>"; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// \brief Checks if the provided status code is \p HIPSPARSE_STATUS_SUCCESS and if not, prints an error message to the |
| 82 | +/// standard error output and terminates the program with an error code. |
| 83 | +#define HIPSPARSELT_CHECK(condition) \ |
| 84 | +{ \ |
| 85 | + const hipsparseStatus_t status = condition; \ |
| 86 | + if(status != HIPSPARSE_STATUS_SUCCESS) \ |
| 87 | + { \ |
| 88 | + std::cerr << "hipSPARSELt error encountered: \"" \ |
| 89 | + << hipsparseStatusToString(status) \ |
| 90 | + << "\" at " << __FILE__ << ':' << __LINE__ \ |
| 91 | + << std::endl; \ |
| 92 | + std::exit(EXIT_FAILURE); \ |
| 93 | + } \ |
| 94 | +} |
| 95 | + |
| 96 | +#endif |
0 commit comments