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 3.8 kB

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