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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-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. #pragma once
  12. #include <cstddef>
  13. #include <memory>
  14. #include "megcore_cdefs.h"
  15. #include "megdnn/thin/function.h"
  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, size_t parallelism) = 0;
  40. /*!
  41. * \brief synchronize the calling thread with the computing thread
  42. */
  43. virtual void sync() = 0;
  44. /*!
  45. * \brief the computing thread number.
  46. */
  47. virtual size_t nr_threads() = 0;
  48. };
  49. } // namespace megcore
  50. using MegcoreCPUDispatcher = megcore::CPUDispatcher;
  51. /**
  52. * \brief Layer 1: device handle
  53. */
  54. struct megcoreDeviceContext;
  55. typedef struct megcoreDeviceContext* megcoreDeviceHandle_t;
  56. megcoreStatus_t megcoreCreateDeviceHandle(
  57. megcoreDeviceHandle_t* handle, megcorePlatform_t platform, int deviceID = -1,
  58. unsigned int flags = 0);
  59. megcoreStatus_t megcoreDestroyDeviceHandle(megcoreDeviceHandle_t handle);
  60. megcoreStatus_t megcoreGetPlatform(
  61. megcoreDeviceHandle_t handle, megcorePlatform_t* platform);
  62. megcoreStatus_t megcoreGetDeviceID(megcoreDeviceHandle_t handle, int* deviceID);
  63. megcoreStatus_t megcoreGetMemAlignment(
  64. megcoreDeviceHandle_t handle, size_t* memAlignmentInBytes);
  65. megcoreStatus_t megcoreGetDeviceFlags(
  66. megcoreDeviceHandle_t handle, unsigned int* flags);
  67. megcoreStatus_t megcoreActivate(megcoreDeviceHandle_t handle);
  68. megcoreStatus_t megcoreDeactivate(megcoreDeviceHandle_t handle);
  69. megcoreStatus_t megcoreMalloc(
  70. megcoreDeviceHandle_t handle, void** devPtr, size_t sizeInBytes);
  71. megcoreStatus_t megcoreFree(megcoreDeviceHandle_t handle, void* devPtr);
  72. /**
  73. * \brief Layer 2: computing handle
  74. */
  75. struct megcoreComputingContext;
  76. typedef struct megcoreComputingContext* megcoreComputingHandle_t;
  77. megcoreStatus_t megcoreCreateComputingHandle(
  78. megcoreComputingHandle_t* compHandle, megcoreDeviceHandle_t devHandle,
  79. unsigned int flags = 0);
  80. megcoreStatus_t megcoreCreateComputingHandleWithCPUDispatcher(
  81. megcoreComputingHandle_t* compHandle, megcoreDeviceHandle_t devHandle,
  82. const std::shared_ptr<MegcoreCPUDispatcher>& dispatcher,
  83. unsigned int flags = 0);
  84. megcoreStatus_t megcoreDestroyComputingHandle(megcoreComputingHandle_t handle);
  85. megcoreStatus_t megcoreGetDeviceHandle(
  86. megcoreComputingHandle_t compHandle, megcoreDeviceHandle_t* devHandle);
  87. megcoreStatus_t megcoreGetComputingFlags(
  88. megcoreComputingHandle_t handle, unsigned int* flags);
  89. MegcoreCPUDispatcher* megcoreGetCPUDispatcher(megcoreComputingHandle_t handle);
  90. megcoreStatus_t megcoreMemcpy(
  91. megcoreComputingHandle_t handle, void* dst, const void* src, size_t sizeInBytes,
  92. megcoreMemcpyKind_t kind);
  93. megcoreStatus_t megcoreMemset(
  94. megcoreComputingHandle_t handle, void* dst, int value, size_t sizeInBytes);
  95. megcoreStatus_t megcoreSynchronize(megcoreComputingHandle_t handle);
  96. /**
  97. * \brief Miscellaneous
  98. */
  99. const char* megcoreGetErrorName(megcoreStatus_t status);
  100. #include "megdnn/internal/visibility_epilogue.h"
  101. // vim: syntax=cpp.doxygen

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