1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 /*! 21 * \file c_api_error.h 22 * \brief Common fields of all C APIs 23 */ 24 #ifndef NNVM_C_API_C_API_COMMON_H_ 25 #define NNVM_C_API_C_API_COMMON_H_ 26 27 #include <dmlc/base.h> 28 #include <dmlc/logging.h> 29 #include <dmlc/thread_local.h> 30 #include <nnvm/c_api.h> 31 #include <nnvm/symbolic.h> 32 33 #include <string> 34 #include <unordered_map> 35 #include <utility> 36 #include <vector> 37 38 /*! \brief macro to guard beginning and end section of all functions */ 39 #define API_BEGIN() try { 40 /*! \brief every function starts with API_BEGIN(); 41 and finishes with API_END() or API_END_HANDLE_ERROR */ 42 #define API_END() \ 43 } \ 44 catch (dmlc::Error & _except_) { \ 45 return NNAPIHandleException(_except_); \ 46 } \ 47 return 0; // NOLINT(*) 48 /*! 49 * \brief every function starts with API_BEGIN(); 50 * and finishes with API_END() or API_END_HANDLE_ERROR 51 * The finally clause contains procedure to cleanup states when an error happens. 52 */ 53 #define API_END_HANDLE_ERROR(Finalize) \ 54 } \ 55 catch (dmlc::Error & _except_) { \ 56 Finalize; \ 57 return NNAPIHandleException(_except_); \ 58 } \ 59 return 0; // NOLINT(*) 60 61 /*! \brief entry to to easily hold returning information */ 62 struct NNAPIThreadLocalEntry { 63 /*! \brief result holder for returning string */ 64 std::string ret_str; 65 /*! \brief result holder for returning strings */ 66 std::vector<std::string> ret_vec_str; 67 /*! \brief result holder for returning string pointers */ 68 std::vector<const char*> ret_vec_charp; 69 /*! \brief result holder for returning handles */ 70 std::vector<void*> ret_handles; 71 /*! \brief argument holder to hold symbol */ 72 std::unordered_map<std::string, const nnvm::Symbol*> kwarg_symbol; 73 }; 74 75 /*! \brief Thread local store that can be used to hold return values. */ 76 typedef dmlc::ThreadLocalStore<NNAPIThreadLocalEntry> NNAPIThreadLocalStore; 77 78 /*! 79 * \brief handle exception throwed out 80 * \param e the exception 81 * \return the return value of API after exception is handled 82 */ NNAPIHandleException(const dmlc::Error & e)83inline int NNAPIHandleException(const dmlc::Error& e) { 84 NNAPISetLastError(e.what()); 85 return -1; 86 } 87 88 #endif // NNVM_C_API_C_API_COMMON_H_ 89