Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 }
37}
38
39bool &isInitialisedCpu()
40{
41 static bool isInitialised = false;
42 return isInitialised;
43}
44
45} // end anonymous namespace
46
47namespace RooBatchCompute {
48
49/// Inspect hardware capabilities, and load the optimal library for RooFit computations.
51{
52 // Check if the library was not initialised already
53 if (isInitialisedCpu())
54 return 0;
55
56 const std::string userChoice = getBatchComputeChoice();
57#ifdef R__RF_ARCHITECTURE_SPECIFIC_LIBS
58
60 bool supported_avx512 = __builtin_cpu_supports("avx512cd") && __builtin_cpu_supports("avx512vl") &&
61 __builtin_cpu_supports("avx512bw") && __builtin_cpu_supports("avx512dq");
62
63 if (userChoice == "auto") {
64 if (supported_avx512) {
65 loadWithErrorChecking("libRooBatchCompute_AVX512");
66 } else if (__builtin_cpu_supports("avx2")) {
67 loadWithErrorChecking("libRooBatchCompute_AVX2");
68 } else if (__builtin_cpu_supports("avx")) {
69 loadWithErrorChecking("libRooBatchCompute_AVX");
70 } else if (__builtin_cpu_supports("sse4.1")) {
71 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
72 }
73 } else if (userChoice == "avx512") {
74 loadWithErrorChecking("libRooBatchCompute_AVX512");
75 } else if (userChoice == "avx2") {
76 loadWithErrorChecking("libRooBatchCompute_AVX2");
77 } else if (userChoice == "avx") {
78 loadWithErrorChecking("libRooBatchCompute_AVX");
79 } else if (userChoice == "sse") {
80 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
81 } else if (userChoice != "generic") {
82 throw std::invalid_argument(
83 R"(Supported options for "RooFit.BatchCompute" are "auto", "avx512", "avx2", "avx", "sse", "generic".)");
84 }
85#endif // R__RF_ARCHITECTURE_SPECIFIC_LIBS
86
87 if (RooBatchCompute::dispatchCPU == nullptr)
88 loadWithErrorChecking("libRooBatchCompute_GENERIC");
89
90 isInitialisedCpu() = true;
91
92 return 0;
93}
94
96{
97 // Check if the library was not initialised already
98 static bool isInitialised = false;
99 if (isInitialised)
100 return 0;
101 isInitialised = true;
102
103 return gSystem->Load("libRooBatchCompute_CUDA");
104}
105
107{
108 return gEnv->GetValue("RooFit.BatchCompute", "generic");
109}
110
111void setBatchComputeChoice(std::string const &value)
112{
114 throw std::runtime_error("It is not possible to choose a different BatchCompute library after RooBatchCompute "
115 "was already initialised!");
116 }
117 gEnv->SetValue("RooFit.BatchCompute", value.c_str());
118}
119
120} // namespace RooBatchCompute
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
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 void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
Set the value of a resource or create a new resource.
Definition TEnv.cxx:736
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1870
Namespace for dispatching RooFit computations to various backends.
R__EXTERN RooBatchComputeInterface * dispatchCUDA
std::string getBatchComputeChoice()
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
void setBatchComputeChoice(std::string const &value)
int initCPU()
Inspect hardware capabilities, and load the optimal library for RooFit computations.