Register global function Parameters ---------- func_name : str or function The function name f : function, optional The function to be registered. override: boolean optional Whether override existing entry. Returns ------- fregister : funct
(func_name, f=None, override=False)
| 145 | |
| 146 | |
| 147 | def register_func(func_name, f=None, override=False): |
| 148 | """Register global function |
| 149 | |
| 150 | Parameters |
| 151 | ---------- |
| 152 | func_name : str or function |
| 153 | The function name |
| 154 | |
| 155 | f : function, optional |
| 156 | The function to be registered. |
| 157 | |
| 158 | override: boolean optional |
| 159 | Whether override existing entry. |
| 160 | |
| 161 | Returns |
| 162 | ------- |
| 163 | fregister : function |
| 164 | Register function if f is not specified. |
| 165 | |
| 166 | Examples |
| 167 | -------- |
| 168 | The following code registers my_packed_func as global function. |
| 169 | Note that we simply get it back from global function table to invoke |
| 170 | it from python side. However, we can also invoke the same function |
| 171 | from C++ backend, or in the compiled DGL code. |
| 172 | |
| 173 | .. code-block:: python |
| 174 | |
| 175 | targs = (10, 10.0, "hello") |
| 176 | @dgl.register_func |
| 177 | def my_packed_func(*args): |
| 178 | assert(tuple(args) == targs) |
| 179 | return 10 |
| 180 | # Get it out from global function table |
| 181 | f = dgl.get_global_func("my_packed_func") |
| 182 | assert isinstance(f, dgl.nd.Function) |
| 183 | y = f(*targs) |
| 184 | assert y == 10 |
| 185 | """ |
| 186 | if callable(func_name): |
| 187 | f = func_name |
| 188 | func_name = f.__name__ |
| 189 | |
| 190 | if not isinstance(func_name, str): |
| 191 | raise ValueError("expect string function name") |
| 192 | |
| 193 | ioverride = ctypes.c_int(override) |
| 194 | |
| 195 | def register(myf): |
| 196 | """internal register function""" |
| 197 | if not isinstance(myf, Function): |
| 198 | myf = convert_to_dgl_func(myf) |
| 199 | check_call( |
| 200 | _LIB.DGLFuncRegisterGlobal(c_str(func_name), myf.handle, ioverride) |
| 201 | ) |
| 202 | return myf |
| 203 | |
| 204 | if f: |
nothing calls this directly
no test coverage detected