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.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <string.h>
  16. #define LITE_CAPI_CHECK(_expr) \
  17. do { \
  18. int _ret = (_expr); \
  19. if (_ret) { \
  20. fprintf(stderr, "error msg: %s", LITE_get_last_error()); \
  21. return -1; \
  22. } \
  23. } while (0)
  24. int basic_c_interface(const char* mode_path) {
  25. //! create and load the network
  26. LiteNetwork c_network;
  27. LITE_CAPI_CHECK(
  28. LITE_make_network(&c_network, *default_config(), *default_network_io()));
  29. LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, mode_path));
  30. //! set input data to input tensor
  31. LiteTensor c_input_tensor;
  32. LITE_CAPI_CHECK(
  33. 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(LITE_get_tensor_total_size_in_byte(c_input_tensor,
  37. &length_in_byte));
  38. LITE_CAPI_CHECK(LITE_get_tensor_memory(c_input_tensor, &dst_ptr));
  39. //! copy or forward data to network
  40. 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(LITE_get_io_tensor(c_network, output_name, LITE_IO,
  50. &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(c_output_tensor,
  55. &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. for (size_t i = 0; i < out_length; i++) {
  61. float data = ((float*)(output_ptr))[i];
  62. sum += data;
  63. if (max < data)
  64. max = data;
  65. }
  66. printf("max=%e, sum=%e\n", max, sum);
  67. return 0;
  68. }
  69. int main(int argc, char** argv) {
  70. if (argc < 2) {
  71. printf("usage: lite_c_examples <model file> , just test C interface "
  72. "build.\n");
  73. return -1;
  74. }
  75. return basic_c_interface(argv[1]);
  76. }
  77. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台