1
0

common.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include "common.hpp"
  12. int get_current_device_id() {
  13. return dpct::dev_mgr::instance().current_device_id();
  14. }
  15. void* ggml_sycl_host_malloc(size_t size) try {
  16. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  17. return nullptr;
  18. }
  19. void* ptr = nullptr;
  20. // allow to use dpct::get_in_order_queue() for host malloc
  21. dpct::err0 err = CHECK_TRY_ERROR(
  22. ptr = (void*)sycl::malloc_host(size, dpct::get_in_order_queue()));
  23. if (err != 0) {
  24. // clear the error
  25. fprintf(
  26. stderr,
  27. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  28. size / 1024.0 / 1024.0,
  29. "syclGetErrorString is not supported");
  30. return nullptr;
  31. }
  32. return ptr;
  33. } catch (sycl::exception const& exc) {
  34. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  35. << ", line:" << __LINE__ << std::endl;
  36. std::exit(1);
  37. }
  38. void ggml_sycl_host_free(void* ptr) try {
  39. // allow to use dpct::get_in_order_queue() for host malloc
  40. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  41. } catch (sycl::exception const& exc) {
  42. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  43. << ", line:" << __LINE__ << std::endl;
  44. std::exit(1);
  45. }