kernel_registry.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright 2019-2020 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <runtime/cpu/cpu_ops_body.h>
  16. #include <runtime/k210/k210_ops_body.h>
  17. #include <runtime/kernel_registry.h>
  18. #include <runtime/neutral/neutral_ops_body.h>
  19. #include <runtime/span_reader.h>
  20. using namespace nncase;
  21. using namespace nncase::runtime;
  22. namespace nncase
  23. {
  24. namespace runtime
  25. {
  26. #define BEGINE_DEFINE_TARGET(target) \
  27. namespace target \
  28. {
  29. #define DEFINE_NEUTRAL_RUNTIME_OP(id, name, value) \
  30. kernel_call_result id(id##_options &, interpreter_t &, interpreter_step_t);
  31. #define DEFINE_RUNTIME_OP(target, id, name, value) \
  32. kernel_call_result id(id##_options &, interpreter_t &, interpreter_step_t);
  33. #define END_DEFINE_TARGET() }
  34. #include <runtime/runtime_op.def>
  35. #undef BEGINE_DEFINE_TARGET
  36. #undef DEFINE_NEUTRAL_RUNTIME_OP
  37. #undef DEFINE_RUNTIME_OP
  38. #undef END_DEFINE_TARGET
  39. }
  40. }
  41. kernel_call_result runtime::call_kernel(runtime_opcode opcode, xtl::span<const uint8_t> body, interpreter_t &interpreter, interpreter_step_t step)
  42. {
  43. span_reader reader(body);
  44. switch (opcode)
  45. {
  46. #define BEGINE_DEFINE_TARGET(...)
  47. #define DEFINE_NEUTRAL_RUNTIME_OP(id, name, value) \
  48. case rop_##id: \
  49. { \
  50. nncase::runtime::neutral::id##_options options; \
  51. options.deserialize(reader); \
  52. return nncase::runtime::neutral::id(options, interpreter, step); \
  53. }
  54. #define DEFINE_RUNTIME_OP(target, id, name, value) \
  55. case rop_##target##_##id: \
  56. { \
  57. nncase::runtime::target::id##_options options; \
  58. options.deserialize(reader); \
  59. return nncase::runtime::target::id(options, interpreter, step); \
  60. }
  61. #define END_DEFINE_TARGET()
  62. #include <runtime/runtime_op.def>
  63. #undef BEGINE_DEFINE_TARGET
  64. #undef DEFINE_NEUTRAL_RUNTIME_OP
  65. #undef DEFINE_RUNTIME_OP
  66. #undef END_DEFINE_TARGET
  67. default:
  68. return kcr_error;
  69. }
  70. }