|
| 1 | +/* Copyright 2024 Stanford University, NVIDIA Corporation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +#include <KokkosInit.h> |
| 16 | +namespace KokkosInterop |
| 17 | +{ |
| 18 | + std::string kokkos_backend_to_str(const KokkosInterop::K_BACKEND& dev) |
| 19 | + { |
| 20 | + std::string str; |
| 21 | + if (dev == KokkosInterop::K_BACKEND::K_CUDA) str="K::Cuda"; |
| 22 | + else if (dev == KokkosInterop::K_BACKEND::K_SERIAL) str="K::Serial"; |
| 23 | + else if (dev == KokkosInterop::K_BACKEND::K_OPENMP) str="K::OpenMP"; |
| 24 | + else if (dev == KokkosInterop::K_BACKEND::K_HIP) str="K::HIP"; |
| 25 | + |
| 26 | + return str; |
| 27 | + } |
| 28 | + |
| 29 | + // |
| 30 | + //------------------------------------------------------------------ |
| 31 | + // Input: A list of Kokkos backends to initialize. |
| 32 | + // Output: A list of Kokkos backends that were successfully initialized. |
| 33 | + // |
| 34 | + std::vector<KokkosInterop::K_BACKEND> |
| 35 | + kokkos_initialize(const std::vector<KokkosInterop::K_BACKEND>& K_Device) |
| 36 | + { |
| 37 | + std::vector<KokkosInterop::K_BACKEND> list_of_backends; |
| 38 | + |
| 39 | + // use Kokkos::Impl::{pre,post}_initialize to allow us to do our own |
| 40 | + // execution space initialization |
| 41 | + Kokkos::InitializationSettings kokkos_init_args; |
| 42 | + //os << "doing general pre-initialization" << endl; |
| 43 | + Kokkos::Impl::pre_initialize(kokkos_init_args); |
| 44 | + |
| 45 | + for(auto BE : K_Device) |
| 46 | + switch(BE) |
| 47 | + { |
| 48 | +#ifdef KOKKOS_ENABLE_SERIAL |
| 49 | + case K_SERIAL: |
| 50 | + { |
| 51 | + Kokkos::Serial::impl_initialize(kokkos_init_args); |
| 52 | + if (Kokkos::Serial::impl_is_initialized()) |
| 53 | + list_of_backends.push_back(K_SERIAL); |
| 54 | + break; |
| 55 | + }; |
| 56 | +#endif |
| 57 | +#ifdef KOKKOS_ENABLE_OPENMP |
| 58 | + case K_OPENMP: |
| 59 | + { |
| 60 | + KokkosOpenMPInitializer ompinit; |
| 61 | + if (ompinit.execute_on_processor()) |
| 62 | + list_of_backends.push_back(K_OPENMP); |
| 63 | + ompinit.wait_done(); |
| 64 | + break; |
| 65 | + }; |
| 66 | +#endif |
| 67 | +#ifdef KOKKOS_ENABLE_CUDA |
| 68 | + case K_CUDA: |
| 69 | + { |
| 70 | + KokkosCudaInitializer cudainit; |
| 71 | + if (cudainit.execute_on_processor()) |
| 72 | + list_of_backends.push_back(K_CUDA); |
| 73 | + cudainit.wait_done(); |
| 74 | + break; |
| 75 | + }; |
| 76 | +#endif |
| 77 | +#ifdef KOKKOS_ENABLE_HIP |
| 78 | + case K_HIP: |
| 79 | + { |
| 80 | + KokkosHIPInitializer hipinit; |
| 81 | + if (hipinit.execute_on_processor()) |
| 82 | + list_of_backends.push_back(K_HIP); |
| 83 | + hipinit.wait_done(); |
| 84 | + break; |
| 85 | + }; |
| 86 | +#endif |
| 87 | + }; |
| 88 | + // TODO: warn if Kokkos has other execution spaces enabled that we're not |
| 89 | + // willing/able to initialize? |
| 90 | + |
| 91 | + //os << "doing general post-initialization" << endl; |
| 92 | + Kokkos::Impl::post_initialize(kokkos_init_args); |
| 93 | + //global_initialized_kokkos_backends = list_of_backends; |
| 94 | + return list_of_backends; |
| 95 | + } |
| 96 | + // |
| 97 | + //------------------------------------------------------------------ |
| 98 | + // |
| 99 | + void kokkos_finalize() |
| 100 | + { |
| 101 | +#if KOKKOS_VERSION >= 40000 |
| 102 | + Kokkos::Impl::pre_finalize(); |
| 103 | +#endif |
| 104 | + |
| 105 | + // !!!! Does the Serial backend also need finalization? |
| 106 | + |
| 107 | +#ifdef KOKKOS_ENABLE_OPENMP |
| 108 | + if (Kokkos::OpenMP::impl_is_initialized()) |
| 109 | + { |
| 110 | + KokkosOpenMPFinalizer ompfinal; |
| 111 | + ompfinal.execute_on_processor(); |
| 112 | + ompfinal.wait_done(); |
| 113 | + } |
| 114 | +#endif |
| 115 | + |
| 116 | +#ifdef KOKKOS_ENABLE_CUDA |
| 117 | + if (Kokkos::Cuda::impl_is_initialized()) |
| 118 | + { |
| 119 | + KokkosCudaFinalizer cudafinal; |
| 120 | + cudafinal.execute_on_processor(); |
| 121 | + cudafinal.wait_done(); |
| 122 | + } |
| 123 | +#endif |
| 124 | + |
| 125 | +#ifdef KOKKOS_ENABLE_HIP |
| 126 | + if (Kokkos::HIP::impl_is_initialized()) |
| 127 | + { |
| 128 | + KokkosHIPFinalizer hipfinal; |
| 129 | + hipfinal.execute_on_processor(); |
| 130 | + hipfinal.wait_done(); |
| 131 | + } |
| 132 | +#endif |
| 133 | + |
| 134 | + // cerr << "doing general finalization" << endl; |
| 135 | +// #if KOKKOS_VERSION >= 40000 |
| 136 | +// Kokkos::Impl::post_finalize(); |
| 137 | +// #else |
| 138 | +// Kokkos::finalize(); |
| 139 | +// #endif |
| 140 | + } |
| 141 | +}; |
| 142 | + |
0 commit comments