Actual source code: syclcontext.sycl.cxx

  1: #include "sycldevice.hpp"
  2: #include <sycl/sycl.hpp>
  3: #include <chrono>

  5: namespace Petsc
  6: {

  8: namespace device
  9: {

 11: namespace sypm
 12: {

 14: namespace impl
 15: {

 17: class DeviceContext {
 18: public:
 19:   struct PetscDeviceContext_SYCL {
 20:     sycl::event event;
 21:     sycl::event begin; // timer-only
 22:     sycl::event end;   // timer-only
 23: #if PetscDefined(USE_DEBUG)
 24:     PetscBool timerInUse{};
 25: #endif
 26:     sycl::queue queue;

 28:     std::chrono::time_point<std::chrono::steady_clock> timeBegin{};
 29:   };

 31: private:
 32:   static bool initialized_;

 34:   static PetscErrorCode finalize_() noexcept
 35:   {
 36:     PetscFunctionBegin;
 37:     initialized_ = false;
 38:     PetscFunctionReturn(PETSC_SUCCESS);
 39:   }

 41:   static PetscErrorCode initialize_(PetscInt id, PetscDeviceContext dctx) noexcept
 42:   {
 43:     PetscFunctionBegin;
 44:     PetscCall(PetscDeviceCheckDeviceCount_Internal(id));
 45:     if (!initialized_) {
 46:       initialized_ = true;
 47:       PetscCall(PetscRegisterFinalize(finalize_));
 48:     }
 49:     PetscFunctionReturn(PETSC_SUCCESS);
 50:   }

 52: public:
 53:   const struct _DeviceContextOps ops = {destroy,    changeStreamType, setUp,   query,   waitForContext, synchronize, getBlasHandle, getSolverHandle, nullptr, getStreamHandle,
 54:                                         beginTimer, endTimer,         nullptr, nullptr, nullptr,        nullptr,     nullptr,       nullptr,         nullptr};

 56:   // default constructor
 57:   DeviceContext() noexcept = default;

 59:   // All of these functions MUST be static in order to be callable from C, otherwise they
 60:   // get the implicit 'this' pointer tacked on
 61:   static PetscErrorCode destroy(PetscDeviceContext dctx) noexcept
 62:   {
 63:     PetscFunctionBegin;
 64:     delete static_cast<PetscDeviceContext_SYCL *>(dctx->data);
 65:     dctx->data = nullptr;
 66:     PetscFunctionReturn(PETSC_SUCCESS);
 67:   }

 69:   static PetscErrorCode setUp(PetscDeviceContext dctx) noexcept
 70:   {
 71:     PetscDevice dev;
 72:     PetscInt    id;

 74:     PetscFunctionBegin;
 75: #if PetscDefined(USE_DEBUG)
 76:     static_cast<PetscDeviceContext_SYCL *>(dctx->data)->timerInUse = PETSC_FALSE;
 77: #endif
 78:     PetscCall(PetscDeviceContextGetDevice(dctx, &dev));
 79:     PetscCall(PetscDeviceGetDeviceId(dev, &id));
 80:     const sycl::device &syclDevice = (id == PETSC_SYCL_DEVICE_HOST) ? sycl::device(sycl::cpu_selector_v) : sycl::device::get_devices(sycl::info::device_type::gpu)[id];

 82:     static_cast<PetscDeviceContext_SYCL *>(dctx->data)->queue = sycl::queue(syclDevice, sycl::property::queue::in_order());
 83:     PetscFunctionReturn(PETSC_SUCCESS);
 84:   }

 86:   static PetscErrorCode query(PetscDeviceContext dctx, PetscBool *idle) noexcept
 87:   {
 88:     PetscFunctionBegin;
 89:     // available in future, https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/supported/sycl_ext_oneapi_queue_empty.asciidoc
 90:     // *idle = static_cast(dctx->data)->queue.empty() ? PETSC_TRUE : PETSC_FALSE;
 91:     *idle = PETSC_FALSE;
 92:     PetscFunctionReturn(PETSC_SUCCESS);
 93:   }

 95:   static PetscErrorCode synchronize(PetscDeviceContext dctx) noexcept
 96:   {
 97:     PetscBool  idle = PETSC_TRUE;
 98:     const auto dci  = static_cast<PetscDeviceContext_SYCL *>(dctx->data);

100:     PetscFunctionBegin;
101:     PetscCall(query(dctx, &idle));
102:     if (!idle) PetscCallCXX(dci->queue.wait());
103:     PetscFunctionReturn(PETSC_SUCCESS);
104:   }

106:   static PetscErrorCode getStreamHandle(PetscDeviceContext dctx, void **handle) noexcept
107:   {
108:     PetscFunctionBegin;
109:     *reinterpret_cast<sycl::queue **>(handle) = &(static_cast<PetscDeviceContext_SYCL *>(dctx->data)->queue);
110:     PetscFunctionReturn(PETSC_SUCCESS);
111:   }

113:   static PetscErrorCode beginTimer(PetscDeviceContext dctx) noexcept
114:   {
115:     const auto dci = static_cast<PetscDeviceContext_SYCL *>(dctx->data);

117:     PetscFunctionBegin;
118: #if PetscDefined(USE_DEBUG)
119:     PetscCheck(!dci->timerInUse, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Forgot to call PetscLogGpuTimeEnd()?");
120:     dci->timerInUse = PETSC_TRUE;
121: #endif
122:     // It is not a good approach to time SYCL kernels because the timer starts at the kernel launch time at host,
123:     // not at the start of execution time on device. SYCL provides this style of kernel timing:
124:     /*
125:       sycl::queue q(sycl::default_selector_v, sycl::property::queue::enable_profiling{});
126:       sycl::event e = q.submit([&](sycl::handler &h) {
127:         ...
128:       });
129:       e.wait();
130:       auto start_time = e.get_profiling_info<sycl::info::event_profiling::command_start>();
131:       auto end_time = e.get_profiling_info<sycl::info::event_profiling::command_end>();
132:       long long kernel_duration_ns = end_time - start_time;
133:     */
134:     // It requires 1) enable profiling at the queue's creation time, and 2) store the event returned by kernel launch.
135:     // But neither we have control of the input queue, nor does PetscDeviceContext support 2), so we just use a
136:     // host side timer.
137:     PetscCallCXX(dci->timeBegin = std::chrono::steady_clock::now());
138:     PetscFunctionReturn(PETSC_SUCCESS);
139:   }

141:   static PetscErrorCode endTimer(PetscDeviceContext dctx, PetscLogDouble *elapsed) noexcept
142:   {
143:     const auto                    dci = static_cast<PetscDeviceContext_SYCL *>(dctx->data);
144:     std::chrono::duration<double> duration;

146:     PetscFunctionBegin;
147: #if PetscDefined(USE_DEBUG)
148:     PetscCheck(dci->timerInUse, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Forgot to call PetscLogGpuTimeBegin()?");
149:     dci->timerInUse = PETSC_FALSE;
150: #endif
151:     PetscCallCXX(dci->queue.wait());
152:     PetscCallCXX(duration = std::chrono::steady_clock::now() - dci->timeBegin);
153:     PetscCallCXX(*elapsed = duration.count());
154:     PetscFunctionReturn(PETSC_SUCCESS);
155:   }

157:   static PetscErrorCode changeStreamType(PetscDeviceContext, PetscStreamType) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); }
158:   static PetscErrorCode waitForContext(PetscDeviceContext, PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); }
159:   static PetscErrorCode getBlasHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); }
160:   static PetscErrorCode getSolverHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); }
161: };

163: } // namespace impl

165: } // namespace sypm

167: } // namespace device

169: } // namespace Petsc

171: PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx)
172: {
173:   using namespace Petsc::device::sypm::impl;

175:   static const DeviceContext syclctx;

177:   PetscFunctionBegin;
178:   PetscCallCXX(dctx->data = new DeviceContext::PetscDeviceContext_SYCL());
179:   dctx->ops[0] = syclctx.ops;
180:   PetscFunctionReturn(PETSC_SUCCESS);
181: }