diff --git a/sycl/include/dpct/blas_utils.hpp b/sycl/include/dpct/blas_utils.hpp index 4a58aad3..60afdfb2 100644 --- a/sycl/include/dpct/blas_utils.hpp +++ b/sycl/include/dpct/blas_utils.hpp @@ -303,14 +303,14 @@ public: _saved_queue_ptr = q_ptr; } static inline sycl::queue &get_saved_queue() noexcept { + if (!_saved_queue_ptr) _saved_queue_ptr = &::dpct::cs::get_default_queue(); return *_saved_queue_ptr; } private: ::dpct::cs::queue_ptr _queue_ptr = &::dpct::cs::get_default_queue(); math_mode _mm = math_mode::mm_default; - static inline ::dpct::cs::queue_ptr _saved_queue_ptr = - &::dpct::cs::get_default_queue(); + static inline ::dpct::cs::queue_ptr _saved_queue_ptr = nullptr; }; using descriptor_ptr = descriptor *; diff --git a/sycl/include/dpct/detail/memory_detail.hpp b/sycl/include/dpct/detail/memory_detail.hpp index e0bad438..1c4c5d9e 100644 --- a/sycl/include/dpct/detail/memory_detail.hpp +++ b/sycl/include/dpct/detail/memory_detail.hpp @@ -822,14 +822,11 @@ public: /// Constructor with range device_memory(const sycl::range &range_in) : _size(range_in.size() * sizeof(T)), _range(range_in), _reference(false), - _host_ptr(nullptr), _device_ptrs(get_ptrs_size(), nullptr) { + _host_ptr(nullptr) { static_assert((Memory == global) || (Memory == constant) || (Memory == shared), "device memory region should be global, constant or shared"); - // Make sure that singleton class mem_mgr and dev_mgr will destruct later - // than this. - detail::mem_mgr::instance(); - dev_mgr::instance(); + // Strata: the pointer table, mem_mgr and dev_mgr are made at the first get_ptr_impl() (sycl/tools/fixups.py) } /// Constructor with range @@ -927,6 +924,11 @@ private: } value_t *get_ptr_impl(sycl::queue &q) { + if (_device_ptrs.empty()) { + detail::mem_mgr::instance(); + dev_mgr::instance(); + _device_ptrs.assign(get_ptrs_size(), nullptr); + } #ifdef DPCT_USM_LEVEL_NONE auto &ptr = _device_ptrs.front(); #else @@ -953,7 +955,7 @@ private: bool _reference; value_t *_host_ptr; std::vector _device_ptrs; - sycl::queue _q; + std::optional _q; // Strata: a default-built sycl::queue picks a device (sycl/tools/fixups.py) }; template class device_memory : public device_memory { diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index 5a4d9f0a..2a4db4d1 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -1723,6 +1723,15 @@ int main(int argc, char **argv) try { // pipe or a file is block-buffered, so a program that dies loses every line it had already printed - which // turns "it crashed at step 7" into "it crashed somewhere", and the difference is a debugging session. std::setvbuf(stdout, nullptr, _IONBF, 0); + // `strata --version` / `-V` (also `strata generate --version`): the version and nothing else, before any GPU is touched, as the CUDA + // and HIP engines have answered it. `strata update` runs it as the engine's smoke test, and the Intel package build runs it with + // an empty environment. + for (int i = 1; i < argc && i < 3; ++i) { + if (std::strcmp(argv[i], "--version") == 0 || std::strcmp(argv[i], "-V") == 0) { + std::printf("strata %s\n", STRATA_VERSION); + return 0; + } + } #if (defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906)) && !defined(_WIN32) // AMD, a file-backed arena (STRATA_ARENA_MMAP): ROCclr copies a pageable source of 1 MiB or more by locking its // pages in place (a GPU userptr), and keeps them - so every expert the VRAM fill copied from the mapped diff --git a/sycl/tools/fixups.py b/sycl/tools/fixups.py index 053d2810..b26e2bc9 100644 --- a/sycl/tools/fixups.py +++ b/sycl/tools/fixups.py @@ -340,3 +340,51 @@ edit("src/kernels/cuda/native_router.dp.cpp", lambda s: s.replace( edit("include/strata/core/mtp.hpp", lambda s: s.replace( " dpct::queue_ptr cs_ = &dpct::get_in_order_queue();\n", " dpct::queue_ptr cs_ = nullptr; // load() creates it; not the device's queue, which ~MtpDrafter would destroy\n")) + +# `strata --version` must print before any GPU is touched (the engine smoke test of `strata update` runs it, and a user outside the +# `render` group has no usable device). Two things in dpct's helper headers looked up the SYCL devices before main: +# 1. blas::descriptor's `static inline queue_ptr _saved_queue_ptr = &get_default_queue();` (an inline variable): the default is +# looked up in get_saved_queue() instead. +def lazy_saved_queue(s): + s = s.replace(" static inline ::dpct::cs::queue_ptr _saved_queue_ptr =\n &::dpct::cs::get_default_queue();", + " static inline ::dpct::cs::queue_ptr _saved_queue_ptr = nullptr;") + return s.replace(" static inline sycl::queue &get_saved_queue() noexcept {\n return *_saved_queue_ptr;", + " static inline sycl::queue &get_saved_queue() noexcept {\n" + " if (!_saved_queue_ptr) _saved_queue_ptr = &::dpct::cs::get_default_queue();\n" + " return *_saved_queue_ptr;") +edit("include/dpct/blas_utils.hpp", lazy_saved_queue) + +# 2. dpct::device_memory (global_memory / constant_memory) sized its per-device pointer table, and so built mem_mgr and dev_mgr, in its +# constructor. The engine's namespace-scope tables (iq4nl_values, c_codes) are heap objects built before main. Both now happen at the +# first get_ptr_impl(); the destruction order the original comment protected does not arise for objects that are never freed. +def lazy_device_memory(s): + s = s.replace(" _host_ptr(nullptr), _device_ptrs(get_ptrs_size(), nullptr) {\n", " _host_ptr(nullptr) {\n") + s = s.replace(" // Make sure that singleton class mem_mgr and dev_mgr will destruct later\n // than this.\n" + " detail::mem_mgr::instance();\n dev_mgr::instance();\n }\n", + " // Strata: the pointer table, mem_mgr and dev_mgr are made at the first get_ptr_impl() (sycl/tools/fixups.py)\n }\n") + s = s.replace(" std::vector _device_ptrs;\n sycl::queue _q;\n};", # a default-built sycl::queue picks a device + " std::vector _device_ptrs;\n std::optional _q; // Strata: a default-built sycl::queue picks a device (sycl/tools/fixups.py)\n};") + return s.replace(" value_t *get_ptr_impl(sycl::queue &q) {\n#ifdef DPCT_USM_LEVEL_NONE\n", + " value_t *get_ptr_impl(sycl::queue &q) {\n" + " if (_device_ptrs.empty()) {\n detail::mem_mgr::instance();\n dev_mgr::instance();\n" + " _device_ptrs.assign(get_ptrs_size(), nullptr);\n }\n#ifdef DPCT_USM_LEVEL_NONE\n") +edit("include/dpct/detail/memory_detail.hpp", lazy_device_memory) + +# 3. the migrated generate.cpp has no `--version` (src/program/generate.cpp answers it before any GPU is touched): the engine printed its +# usage and exited 2. Same lines as the CUDA tree, at the top of main(). +def version_flag(s): + if '"--version"' in s: + return s + return s.replace(" std::setvbuf(stdout, nullptr, _IONBF, 0);\n#if (defined(STRATA_USE_HIP)", + " std::setvbuf(stdout, nullptr, _IONBF, 0);\n" + " // `strata --version` / `-V` (also `strata generate --version`): the version and nothing else, before any GPU is touched, as the CUDA\n" + " // and HIP engines have answered it. `strata update` runs it as the engine's smoke test, and the Intel package build runs it with\n" + " // an empty environment.\n" + " for (int i = 1; i < argc && i < 3; ++i) {\n" + " if (std::strcmp(argv[i], \"--version\") == 0 || std::strcmp(argv[i], \"-V\") == 0) {\n" + " std::printf(\"strata %s\\n\", STRATA_VERSION);\n" + " return 0;\n" + " }\n" + " }\n" + "#if (defined(STRATA_USE_HIP)", 1) +edit("src/program/generate.cpp", version_flag)