|
| 1 | +/*! |
| 2 | + * Copyright (c) 2016 by Contributors |
| 3 | + * \file c_api.h |
| 4 | + * \brief C API of NNVM symbolic construction and pass. |
| 5 | + * Enables construction and transformation of Graph |
| 6 | + * in any other host languages. |
| 7 | + */ |
| 8 | +#ifndef NNVM_C_API_H_ |
| 9 | +#define NNVM_C_API_H_ |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +#define NNVM_EXTERN_C extern "C" |
| 13 | +#endif |
| 14 | + |
| 15 | +/*! \brief NNVM_DLL prefix for windows */ |
| 16 | +#ifdef _WIN32 |
| 17 | +#ifdef NNVM_EXPORTS |
| 18 | +#define NNVM_DLL NNVM_EXTERN_C __declspec(dllexport) |
| 19 | +#else |
| 20 | +#define NNVM_DLL NNVM_EXTERN_C __declspec(dllimport) |
| 21 | +#endif |
| 22 | +#else |
| 23 | +#define NNVM_DLL NNVM_EXTERN_C |
| 24 | +#endif |
| 25 | + |
| 26 | +/*! \brief manually define unsigned int */ |
| 27 | +typedef unsigned int nn_uint; |
| 28 | + |
| 29 | +/*! \brief handle to a function that takes param and creates symbol */ |
| 30 | +typedef void *AtomicSymbolCreator; |
| 31 | +/*! \brief handle to a symbol that can be bind as operator */ |
| 32 | +typedef void *SymbolHandle; |
| 33 | +/*! \brief handle to a AtomicSymbol */ |
| 34 | +typedef void *AtomicSymbolHandle; |
| 35 | + |
| 36 | +/*! |
| 37 | + * \brief return str message of the last error |
| 38 | + * all function in this file will return 0 when success |
| 39 | + * and -1 when an error occured, |
| 40 | + * NNGetLastError can be called to retrieve the error |
| 41 | + * |
| 42 | + * this function is threadsafe and can be called by different thread |
| 43 | + * \return error info |
| 44 | + */ |
| 45 | +NNVM_DLL const char *NNGetLastError(); |
| 46 | + |
| 47 | +/*! |
| 48 | + * \brief list all the available AtomicSymbolEntry |
| 49 | + * \param out_size the size of returned array |
| 50 | + * \param out_array the output AtomicSymbolCreator array |
| 51 | + * \return 0 when success, -1 when failure happens |
| 52 | + */ |
| 53 | +NNVM_DLL int NNSymbolListAtomicSymbolCreators(nn_uint *out_size, |
| 54 | + AtomicSymbolCreator **out_array); |
| 55 | +/*! |
| 56 | + * \brief Get the detailed information about atomic symbol. |
| 57 | + * \param creator the AtomicSymbolCreator. |
| 58 | + * \param name The returned name of the creator. |
| 59 | + * \param description The returned description of the symbol. |
| 60 | + * \param num_doc_args Number of arguments that contain documents. |
| 61 | + * \param arg_names Name of the arguments of doc args |
| 62 | + * \param arg_type_infos Type informations about the arguments. |
| 63 | + * \param arg_descriptions Description information about the arguments. |
| 64 | + * \param return_type Return type of the function, if any. |
| 65 | + * \return 0 when success, -1 when failure happens |
| 66 | + */ |
| 67 | +NNVM_DLL int NNSymbolGetAtomicSymbolInfo(AtomicSymbolCreator creator, |
| 68 | + const char **name, |
| 69 | + const char **description, |
| 70 | + nn_uint *num_doc_args, |
| 71 | + const char ***arg_names, |
| 72 | + const char ***arg_type_infos, |
| 73 | + const char ***arg_descriptions, |
| 74 | + const char **return_type = NULL); |
| 75 | +/*! |
| 76 | + * \brief Create an AtomicSymbol functor. |
| 77 | + * \param creator the AtomicSymbolCreator |
| 78 | + * \param num_param the number of parameters |
| 79 | + * \param keys the keys to the params |
| 80 | + * \param vals the vals of the params |
| 81 | + * \param out pointer to the created symbol handle |
| 82 | + * \return 0 when success, -1 when failure happens |
| 83 | + */ |
| 84 | +NNVM_DLL int NNSymbolCreateAtomicSymbol(AtomicSymbolCreator creator, |
| 85 | + nn_uint num_param, |
| 86 | + const char **keys, |
| 87 | + const char **vals, |
| 88 | + SymbolHandle *out); |
| 89 | +/*! |
| 90 | + * \brief Create a Variable Symbol. |
| 91 | + * \param name name of the variable |
| 92 | + * \param out pointer to the created symbol handle |
| 93 | + * \return 0 when success, -1 when failure happens |
| 94 | + */ |
| 95 | +NNVM_DLL int NNSymbolCreateVariable(const char *name, SymbolHandle *out); |
| 96 | +/*! |
| 97 | + * \brief Create a Symbol by grouping list of symbols together |
| 98 | + * \param num_symbols number of symbols to be grouped |
| 99 | + * \param symbols array of symbol handles |
| 100 | + * \param out pointer to the created symbol handle |
| 101 | + * \return 0 when success, -1 when failure happens |
| 102 | + */ |
| 103 | +NNVM_DLL int NNSymbolCreateGroup(nn_uint num_symbols, |
| 104 | + SymbolHandle *symbols, |
| 105 | + SymbolHandle *out); |
| 106 | +/*! |
| 107 | + * \brief Free the symbol handle. |
| 108 | + * \param symbol the symbol |
| 109 | + * \return 0 when success, -1 when failure happens |
| 110 | + */ |
| 111 | +NNVM_DLL int NNSymbolFree(SymbolHandle symbol); |
| 112 | +/*! |
| 113 | + * \brief Copy the symbol to another handle |
| 114 | + * \param symbol the source symbol |
| 115 | + * \param out used to hold the result of copy |
| 116 | + * \return 0 when success, -1 when failure happens |
| 117 | + */ |
| 118 | +NNVM_DLL int NNSymbolCopy(SymbolHandle symbol, SymbolHandle *out); |
| 119 | +/*! |
| 120 | + * \brief Print the content of symbol, used for debug. |
| 121 | + * \param symbol the symbol |
| 122 | + * \param out_str pointer to hold the output string of the printing. |
| 123 | + * \return 0 when success, -1 when failure happens |
| 124 | + */ |
| 125 | +NNVM_DLL int NNSymbolPrint(SymbolHandle symbol, const char **out_str); |
| 126 | + |
| 127 | +/*! |
| 128 | + * \brief Set string attribute from symbol. |
| 129 | + * NOTE: Setting attribute to a symbol can affect the semantics(mutable/immutable) of symbolic graph. |
| 130 | + * |
| 131 | + * Safe recommendaton: use immutable graph |
| 132 | + * - Only allow set attributes during creation of new symbol as optional parameter |
| 133 | + * |
| 134 | + * Mutable graph (be careful about the semantics): |
| 135 | + * - Allow set attr at any point. |
| 136 | + * - Mutating an attribute of some common node of two graphs can cause confusion from user. |
| 137 | + * |
| 138 | + * \param symbol the source symbol |
| 139 | + * \param num_param Number of parameters to set. |
| 140 | + * \param keys The keys of the attribute |
| 141 | + * \param values The value to be set |
| 142 | + * \return 0 when success, -1 when failure happens |
| 143 | + */ |
| 144 | +NNVM_DLL int NNSymbolSetAttrs(SymbolHandle symbol, |
| 145 | + nn_uint num_param, |
| 146 | + const char** keys, |
| 147 | + const char** values); |
| 148 | +/*! |
| 149 | + * \brief Get all attributes from symbol, including all descendents. |
| 150 | + * \param symbol the source symbol |
| 151 | + * \param recursive_option 0 for recursive, 1 for shallow. |
| 152 | + * \param out_size The number of output attributes |
| 153 | + * \param out 2*out_size strings representing key value pairs. |
| 154 | + * \return 0 when success, -1 when failure happens |
| 155 | + */ |
| 156 | +NNVM_DLL int NNSymbolListAttrs(SymbolHandle symbol, |
| 157 | + int recursive_option, |
| 158 | + nn_uint *out_size, |
| 159 | + const char*** out); |
| 160 | +/*! |
| 161 | + * \brief List arguments in the symbol. |
| 162 | + * \param symbol the symbol |
| 163 | + * \param out_size output size |
| 164 | + * \param out_str_array pointer to hold the output string array |
| 165 | + * \return 0 when success, -1 when failure happens |
| 166 | + */ |
| 167 | +NNVM_DLL int NNSymbolListArguments(SymbolHandle symbol, |
| 168 | + nn_uint *out_size, |
| 169 | + const char ***out_str_array); |
| 170 | +/*! |
| 171 | + * \brief List returns in the symbol. |
| 172 | + * \param symbol the symbol |
| 173 | + * \param out_size output size |
| 174 | + * \param out_str_array pointer to hold the output string array |
| 175 | + * \return 0 when success, -1 when failure happens |
| 176 | + */ |
| 177 | +NNVM_DLL int NNSymbolListOutputs(SymbolHandle symbol, |
| 178 | + nn_uint *out_size, |
| 179 | + const char ***out_str_array); |
| 180 | +/*! |
| 181 | + * \brief Get a symbol that contains all the internals. |
| 182 | + * \param symbol The symbol |
| 183 | + * \param out The output symbol whose outputs are all the internals. |
| 184 | + * \return 0 when success, -1 when failure happens |
| 185 | + */ |
| 186 | +NNVM_DLL int NNSymbolGetInternals(SymbolHandle symbol, |
| 187 | + SymbolHandle *out); |
| 188 | +/*! |
| 189 | + * \brief Get index-th outputs of the symbol. |
| 190 | + * \param symbol The symbol |
| 191 | + * \param index the Index of the output. |
| 192 | + * \param out The output symbol whose outputs are the index-th symbol. |
| 193 | + * \return 0 when success, -1 when failure happens |
| 194 | + */ |
| 195 | +NNVM_DLL int NNSymbolGetOutput(SymbolHandle symbol, |
| 196 | + nn_uint index, |
| 197 | + SymbolHandle *out); |
| 198 | + |
| 199 | +/*! |
| 200 | + * \brief Compose the symbol on other symbols. |
| 201 | + * |
| 202 | + * This function will change the sym hanlde. |
| 203 | + * To achieve function apply behavior, copy the symbol first |
| 204 | + * before apply. |
| 205 | + * |
| 206 | + * \param sym the symbol to apply |
| 207 | + * \param name the name of symbol |
| 208 | + * \param num_args number of arguments |
| 209 | + * \param keys the key of keyword args (optional) |
| 210 | + * \param args arguments to sym |
| 211 | + * \return 0 when success, -1 when failure happens |
| 212 | + */ |
| 213 | +NNVM_DLL int NNSymbolCompose(SymbolHandle sym, |
| 214 | + const char* name, |
| 215 | + nn_uint num_args, |
| 216 | + const char** keys, |
| 217 | + SymbolHandle* args); |
| 218 | + |
| 219 | +#endif // NNVM_C_API_H_ |
0 commit comments