Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
RuntimeLoader.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* RuntimeLoader.cc (C) 2000-2025 */
9/* */
10/* Management of the accelerator runtime loading. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/accelerator/internal/RuntimeLoader.h"
15
16#include "arccore/base/PlatformUtils.h"
17#include "arccore/base/FatalErrorException.h"
18#include "arccore/base/internal/IDynamicLibraryLoader.h"
19
22#include "arccore/common/internal/MemoryUtilsInternal.h"
23#include "arccore/common/accelerator/AcceleratorRuntimeInitialisationInfo.h"
24#include "arccore/common/accelerator/internal/RegisterRuntimeInfo.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane::Accelerator::Impl
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35/*!
36 * \brief Detects and loads the accelerator runtime management library.
37 *
38 * This method must only be called once.
39 *
40 * If not null, \a default_runtime_name will be used if
41 * init_info.acceleratorRuntime() is null.
42 *
43 * In return, \a has_accelerator is true if an accelerator runtime
44 * (Cuda, Hip or Sycl) has been loaded.
45 *
46 * \retval 0 if everything is OK
47 */
48int RuntimeLoader::
49loadRuntime(AcceleratorRuntimeInitialisationInfo& init_info,
50 const String& default_runtime_name,
51 const String& library_path,
52 bool& has_accelerator)
53{
54 has_accelerator = false;
55 //AcceleratorRuntimeInitialisationInfo& init_info = si->m_accelerator_init_info;
56 if (!init_info.isUsingAcceleratorRuntime())
57 return 0;
58 String runtime_name = init_info.acceleratorRuntime();
59 if (runtime_name == "sequential")
60 return 0;
61 if (runtime_name.empty())
62 runtime_name = default_runtime_name;
63 if (runtime_name.empty())
64 return 0;
65 init_info.setAcceleratorRuntime(runtime_name);
66 try {
67 // For now, only 'cuda', 'hip' and 'sycl' runtimes are allowed
68 if (runtime_name != "cuda" && runtime_name != "hip" && runtime_name != "sycl")
69 ARCCORE_FATAL("Invalid accelerator runtime '{0}'. Only 'cuda', 'hip' or 'sycl' is allowed", runtime_name);
70
71 // To automatically register an accelerator runtime named \a NAME,
72 // you must call the method 'arcaneRegisterAcceleratorRuntime${NAME}' which is found
73 // in the dynamic library 'arcane_${NAME}'.
74
75 typedef void (*ArcaneAutoDetectAcceleratorFunctor)(Accelerator::RegisterRuntimeInfo&);
76
77 IDynamicLibraryLoader* dll_loader = IDynamicLibraryLoader::getDefault();
78
79 String os_dir(library_path);
80 String dll_name = "arccore_accelerator_" + runtime_name + "_runtime";
81 String symbol_name = "arcaneRegisterAcceleratorRuntime" + runtime_name;
82 IDynamicLibrary* dl = dll_loader->open(os_dir, dll_name);
83 if (!dl)
84 ARCCORE_FATAL("Can not found dynamic library '{0}' for using accelerator runtime", dll_name);
85
86 bool is_found = false;
87 void* functor_addr = dl->getSymbolAddress(symbol_name, &is_found);
88 if (!is_found || !functor_addr)
89 ARCCORE_FATAL("Can not find symbol '{0}' in library '{1}'", symbol_name, dll_name);
90
91 auto my_functor = reinterpret_cast<ArcaneAutoDetectAcceleratorFunctor>(functor_addr);
92 Accelerator::RegisterRuntimeInfo runtime_info;
93
94 String verbose_str = Platform::getEnvironmentVariable("ARCANE_DEBUG_ACCELERATOR");
95 if (!verbose_str.null())
96 runtime_info.setVerbose(true);
97
98 (*my_functor)(runtime_info);
99 has_accelerator = true;
100
101 // Allows overriding the data allocator choice
102 String data_allocator_str = Platform::getEnvironmentVariable("ARCANE_DEFAULT_DATA_MEMORY_RESOURCE");
103 if (!data_allocator_str.null()) {
106 MemoryUtils::setDefaultDataMemoryResource(v);
107 }
108 }
109 catch (const Exception& ex) {
110 return ExceptionUtils::print(ex, nullptr);
111 }
112 catch (const std::exception& ex) {
113 return ExceptionUtils::print(ex, nullptr);
114 }
115 catch (...) {
116 return ExceptionUtils::print(nullptr);
117 }
118 return 0;
119}
120
121/*---------------------------------------------------------------------------*/
122/*---------------------------------------------------------------------------*/
123
124} // namespace Arcane::Accelerator::Impl
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Utility functions for exception handling.
Memory management utility functions.
eMemoryResource getMemoryResourceFromName(const String &name)
Returns the memory resource by its name.
String getEnvironmentVariable(const String &name)
Environment variable named name.
eMemoryResource
List of available memory resources.
@ Unknown
Unknown or uninitialized value.