#include <memory>
#include "oneapi/dnnl/dnnl_sycl.h"
#include "common/c_types_map.hpp"
#include "common/engine.hpp"
#include "common/stream.hpp"
#include "common/utils.hpp"
#include "xpu/sycl/stream_impl.hpp"
using dnnl::impl::engine_t;
using dnnl::impl::status_t;
using dnnl::impl::stream_t;
status_t dnnl_sycl_interop_stream_create(
stream_t **stream, engine_t *engine, void *queue) {
using namespace dnnl::impl;
bool args_ok = true && !utils::any_null(stream, engine, queue)
&& engine->runtime_kind() == runtime_kind::sycl;
if (!args_ok) return status::invalid_arguments;
auto &sycl_queue = *static_cast<::sycl::queue *>(queue);
unsigned flags;
CHECK(dnnl::impl::xpu::sycl::stream_impl_t::init_flags(&flags, sycl_queue));
std::unique_ptr<dnnl::impl::stream_impl_t> stream_impl(
new dnnl::impl::xpu::sycl::stream_impl_t(sycl_queue, flags));
if (!stream_impl) return status::out_of_memory;
CHECK(engine->create_stream(stream, stream_impl.get()));
stream_impl.release();
return status::success;
}
status_t dnnl_sycl_interop_stream_get_queue(stream_t *stream, void **queue) {
using namespace dnnl::impl;
bool args_ok = true && !utils::any_null(queue, stream)
&& stream->engine()->runtime_kind() == runtime_kind::sycl;
if (!args_ok) return status::invalid_arguments;
auto *sycl_stream_impl
= utils::downcast<dnnl::impl::xpu::sycl::stream_impl_t *>(
stream->impl());
auto &sycl_queue = *sycl_stream_impl->queue();
*queue = static_cast<void *>(&sycl_queue);
return status::success;
}