Logo ROOT  
Reference Guide
Initialisation.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Emmanouil Michalainas, CERN, December 2018
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include "RooBatchCompute.h"
14
15#include "TEnv.h"
16#include "TSystem.h"
17#include "TError.h"
18
19#include <string>
20#include <exception>
21#include <stdexcept>
22
23// First initialisation of the pointers. When implementations of the batch compute library
24// are loaded, they will overwrite the pointers.
27
28namespace {
29
30/// Dynamically load a library and throw exception in case of failure
31void loadWithErrorChecking(const std::string &libName)
32{
33 const auto returnValue = gSystem->Load(libName.c_str());
34 if (returnValue == -1 || returnValue == -2)
35 throw std::runtime_error("RooFit was unable to load its computation library " + libName);
36 else if (returnValue == 1) // Library should not have been loaded before we tried to do it.
37 throw std::logic_error("RooFit computation library " + libName +
38 " was loaded before RooFit initialisation began.");
39}
40
41} // end anonymous namespace
42
43namespace RooBatchCompute {
44
45/// Inspect hardware capabilities, and load the optimal library for RooFit computations.
46void init()
47{
48 // Check if the library was not initialised already
49 static bool isInitialised = false;
50 if (isInitialised)
51 return;
52 isInitialised = true;
53
54 const std::string userChoice = gEnv->GetValue("RooFit.BatchCompute", "auto");
55#ifdef R__RF_ARCHITECTURE_SPECIFIC_LIBS
56#ifdef R__HAS_CUDA
57 if(gSystem->Load("libRooBatchCompute_CUDA") != 0) {
59 }
60#endif // R__HAS_CUDA
61
62 __builtin_cpu_init();
63#if __GNUC__ > 5 || defined(__clang__)
64 bool supported_avx512 = __builtin_cpu_supports("avx512cd") && __builtin_cpu_supports("avx512vl") &&
65 __builtin_cpu_supports("avx512bw") && __builtin_cpu_supports("avx512dq");
66#else
67 bool supported_avx512 = false;
68#endif
69
70 if (userChoice == "auto") {
71 if (supported_avx512)
72 loadWithErrorChecking("libRooBatchCompute_AVX512");
73 else if (__builtin_cpu_supports("avx2"))
74 loadWithErrorChecking("libRooBatchCompute_AVX2");
75 else if (__builtin_cpu_supports("avx"))
76 loadWithErrorChecking("libRooBatchCompute_AVX");
77 else if (__builtin_cpu_supports("sse4.1"))
78 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
79 } else if (userChoice == "avx512")
80 loadWithErrorChecking("libRooBatchCompute_AVX512");
81 else if (userChoice == "avx2")
82 loadWithErrorChecking("libRooBatchCompute_AVX2");
83 else if (userChoice == "avx")
84 loadWithErrorChecking("libRooBatchCompute_AVX");
85 else if (userChoice == "sse")
86 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
87 else if (userChoice != "generic")
88 throw std::invalid_argument(
89 "Supported options for `RooFit.BatchCompute` are `auto`, `avx512`, `avx2`, `avx`, `sse`, `generic`.");
90#endif // R__RF_ARCHITECTURE_SPECIFIC_LIBS
91
92 if (RooBatchCompute::dispatchCPU == nullptr)
93 loadWithErrorChecking("libRooBatchCompute_GENERIC");
94}
95
96} // namespace RooBatchCompute
R__EXTERN TEnv * gEnv
Definition: TEnv.h:170
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
The interface which should be implemented to provide optimised computation functions for implementati...
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1858
Namespace for dispatching RooFit computations to various backends.
R__EXTERN RooBatchComputeInterface * dispatchCUDA
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
void init()
Inspect hardware capabilities, and load the optimal library for RooFit computations.