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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 megcoreMalloc(megcoreDeviceHandle_t handle,
  75. void **devPtr, size_t sizeInBytes);
  76. megcoreStatus_t megcoreFree(megcoreDeviceHandle_t handle,
  77. void *devPtr);
  78. /**
  79. * \brief Layer 2: computing handle
  80. */
  81. struct megcoreComputingContext;
  82. typedef struct megcoreComputingContext *megcoreComputingHandle_t;
  83. megcoreStatus_t megcoreCreateComputingHandle(
  84. megcoreComputingHandle_t *compHandle,
  85. megcoreDeviceHandle_t devHandle,
  86. unsigned int flags = 0);
  87. megcoreStatus_t megcoreCreateComputingHandleWithCPUDispatcher(
  88. megcoreComputingHandle_t *compHandle,
  89. megcoreDeviceHandle_t devHandle,
  90. const std::shared_ptr<MegcoreCPUDispatcher>& dispatcher,
  91. unsigned int flags = 0);
  92. megcoreStatus_t megcoreDestroyComputingHandle(
  93. megcoreComputingHandle_t handle);
  94. megcoreStatus_t megcoreGetDeviceHandle(
  95. megcoreComputingHandle_t compHandle,
  96. megcoreDeviceHandle_t *devHandle);
  97. megcoreStatus_t megcoreGetComputingFlags(
  98. megcoreComputingHandle_t handle,
  99. unsigned int *flags);
  100. MegcoreCPUDispatcher* megcoreGetCPUDispatcher(megcoreComputingHandle_t handle);
  101. megcoreStatus_t megcoreMemcpy(
  102. megcoreComputingHandle_t handle,
  103. void *dst, const void *src, size_t sizeInBytes,
  104. megcoreMemcpyKind_t kind);
  105. megcoreStatus_t megcoreMemset(
  106. megcoreComputingHandle_t handle,
  107. void *dst, int value, size_t sizeInBytes);
  108. megcoreStatus_t megcoreSynchronize(megcoreComputingHandle_t handle);
  109. /**
  110. * \brief Miscellaneous
  111. */
  112. const char *megcoreGetErrorName(megcoreStatus_t status);
  113. #include "megdnn/internal/visibility_epilogue.h"
  114. // vim: syntax=cpp.doxygen

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

Contributors (1)