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.

megcore.h 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * \file dnn/include/megcore.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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. #pragma once
  12. #include "megdnn/thin/function.h"
  13. #include "megcore_cdefs.h"
  14. #include <cstddef>
  15. #include <memory>
  16. #include "megdnn/internal/visibility_prologue.h"
  17. namespace megcore {
  18. /*!
  19. * \brief a callback to dispatch computing task on desired CPU thread
  20. *
  21. * This is analogous to cuda streams. The default dispatcher on CPU executes in
  22. * the caller thread immediately.
  23. */
  24. class CPUDispatcher {
  25. public:
  26. using Task = megdnn::thin_function<void()>;
  27. using MultiThreadingTask = megdnn::thin_function<void(size_t, size_t)>;
  28. virtual ~CPUDispatcher() noexcept;
  29. /*!
  30. * \brief dispatch a task on the computing thread
  31. * \param task the task that would be moved away
  32. */
  33. virtual void dispatch(Task&& task) = 0;
  34. /*!
  35. * \brief dispatch a multithreading task on the computing thread
  36. * \param task the task would be moved away
  37. * \param parallelism the parallelism of the task.
  38. */
  39. virtual void dispatch(MultiThreadingTask&& task,
  40. size_t parallelism) = 0;
  41. /*!
  42. * \brief synchronize the calling thread with the computing thread
  43. */
  44. virtual void sync() = 0;
  45. /*!
  46. * \brief the computing thread number.
  47. */
  48. virtual size_t nr_threads() = 0;
  49. };
  50. } // namespace megcore
  51. using MegcoreCPUDispatcher = megcore::CPUDispatcher;
  52. /**
  53. * \brief Layer 1: device handle
  54. */
  55. struct megcoreDeviceContext;
  56. typedef struct megcoreDeviceContext *megcoreDeviceHandle_t;
  57. megcoreStatus_t megcoreCreateDeviceHandle(
  58. megcoreDeviceHandle_t *handle,
  59. megcorePlatform_t platform,
  60. int deviceID = -1,
  61. unsigned int flags = 0);
  62. megcoreStatus_t megcoreDestroyDeviceHandle(
  63. megcoreDeviceHandle_t handle);
  64. megcoreStatus_t megcoreGetPlatform(megcoreDeviceHandle_t handle,
  65. megcorePlatform_t *platform);
  66. megcoreStatus_t megcoreGetDeviceID(megcoreDeviceHandle_t handle,
  67. int *deviceID);
  68. megcoreStatus_t megcoreGetMemAlignment(megcoreDeviceHandle_t handle,
  69. size_t *memAlignmentInBytes);
  70. megcoreStatus_t megcoreGetDeviceFlags(
  71. megcoreDeviceHandle_t handle,
  72. unsigned int *flags);
  73. megcoreStatus_t megcoreActivate(megcoreDeviceHandle_t handle);
  74. megcoreStatus_t megcoreDeactivate(megcoreDeviceHandle_t handle);
  75. megcoreStatus_t megcoreMalloc(megcoreDeviceHandle_t handle,
  76. void **devPtr, size_t sizeInBytes);
  77. megcoreStatus_t megcoreFree(megcoreDeviceHandle_t handle,
  78. void *devPtr);
  79. /**
  80. * \brief Layer 2: computing handle
  81. */
  82. struct megcoreComputingContext;
  83. typedef struct megcoreComputingContext *megcoreComputingHandle_t;
  84. megcoreStatus_t megcoreCreateComputingHandle(
  85. megcoreComputingHandle_t *compHandle,
  86. megcoreDeviceHandle_t devHandle,
  87. unsigned int flags = 0);
  88. megcoreStatus_t megcoreCreateComputingHandleWithCPUDispatcher(
  89. megcoreComputingHandle_t *compHandle,
  90. megcoreDeviceHandle_t devHandle,
  91. const std::shared_ptr<MegcoreCPUDispatcher>& dispatcher,
  92. unsigned int flags = 0);
  93. megcoreStatus_t megcoreDestroyComputingHandle(
  94. megcoreComputingHandle_t handle);
  95. megcoreStatus_t megcoreGetDeviceHandle(
  96. megcoreComputingHandle_t compHandle,
  97. megcoreDeviceHandle_t *devHandle);
  98. megcoreStatus_t megcoreGetComputingFlags(
  99. megcoreComputingHandle_t handle,
  100. unsigned int *flags);
  101. MegcoreCPUDispatcher* megcoreGetCPUDispatcher(megcoreComputingHandle_t handle);
  102. megcoreStatus_t megcoreMemcpy(
  103. megcoreComputingHandle_t handle,
  104. void *dst, const void *src, size_t sizeInBytes,
  105. megcoreMemcpyKind_t kind);
  106. megcoreStatus_t megcoreMemset(
  107. megcoreComputingHandle_t handle,
  108. void *dst, int value, size_t sizeInBytes);
  109. megcoreStatus_t megcoreSynchronize(megcoreComputingHandle_t handle);
  110. /**
  111. * \brief Miscellaneous
  112. */
  113. const char *megcoreGetErrorName(megcoreStatus_t status);
  114. #include "megdnn/internal/visibility_epilogue.h"
  115. // vim: syntax=cpp.doxygen

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