You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * \file example/c_example/main.c
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "lite-c/global_c.h"
  12. #include "lite-c/network_c.h"
  13. #include "lite-c/tensor_c.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #define LITE_CAPI_CHECK(_expr) \
  18. do { \
  19. int _ret = (_expr); \
  20. if (_ret) { \
  21. fprintf(stderr, "error msg: %s", LITE_get_last_error()); \
  22. return -1; \
  23. } \
  24. } while (0)
  25. int basic_c_interface(const char* mode_path) {
  26. //! create and load the network
  27. LiteNetwork c_network;
  28. LITE_CAPI_CHECK(
  29. LITE_make_network(&c_network, *default_config(), *default_network_io()));
  30. LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, mode_path));
  31. //! set input data to input tensor
  32. LiteTensor c_input_tensor;
  33. LITE_CAPI_CHECK(LITE_get_io_tensor(c_network, "data", LITE_IO, &c_input_tensor));
  34. void* dst_ptr;
  35. size_t length_in_byte;
  36. LITE_CAPI_CHECK(
  37. LITE_get_tensor_total_size_in_byte(c_input_tensor, &length_in_byte));
  38. LITE_CAPI_CHECK(LITE_get_tensor_memory(c_input_tensor, &dst_ptr));
  39. //! copy or forward data to network
  40. LITE_memset(dst_ptr, 5, length_in_byte);
  41. //! forward
  42. LITE_CAPI_CHECK(LITE_forward(c_network));
  43. LITE_CAPI_CHECK(LITE_wait(c_network));
  44. //! get the output data or read tensor data
  45. const char* output_name;
  46. LiteTensor c_output_tensor;
  47. //! get the first output tensor name
  48. LITE_CAPI_CHECK(LITE_get_output_name(c_network, 0, &output_name));
  49. LITE_CAPI_CHECK(
  50. LITE_get_io_tensor(c_network, output_name, LITE_IO, &c_output_tensor));
  51. void* output_ptr;
  52. size_t length_output_in_byte;
  53. LITE_CAPI_CHECK(LITE_get_tensor_memory(c_output_tensor, &output_ptr));
  54. LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte(
  55. c_output_tensor, &length_output_in_byte));
  56. size_t out_length = length_output_in_byte / sizeof(float);
  57. printf("length=%zu\n", out_length);
  58. float max = -1.0f;
  59. float sum = 0.0f;
  60. int is_enable_ipc_debug = LITE_is_enable_ipc_debug_mode();
  61. float* copy_ptr = NULL;
  62. float* final_dst_ptr = (float*)output_ptr;
  63. if (is_enable_ipc_debug) {
  64. copy_ptr = (float*)(malloc(length_output_in_byte));
  65. LITE_CAPI_CHECK(LITE_copy_server_tensor_memory(
  66. output_ptr, copy_ptr, length_output_in_byte));
  67. final_dst_ptr = (float*)copy_ptr;
  68. }
  69. for (size_t i = 0; i < out_length; i++) {
  70. float data = final_dst_ptr[i];
  71. sum += data;
  72. if (max < data)
  73. max = data;
  74. }
  75. printf("max=%e, sum=%e\n", max, sum);
  76. LITE_destroy_network(c_network);
  77. if (is_enable_ipc_debug) {
  78. free(copy_ptr);
  79. }
  80. return 0;
  81. }
  82. int main(int argc, char** argv) {
  83. if (argc < 3) {
  84. printf("usage: lite_c_examples is_enable_fork_debug_model <model file> , just "
  85. "test C interface "
  86. "build.\n");
  87. return -1;
  88. }
  89. if (atoi(argv[1])) {
  90. LITE_enable_lite_ipc_debug();
  91. }
  92. return basic_c_interface(argv[2]);
  93. }
  94. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}