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
39} // end anonymous namespace
40
41namespace RooBatchCompute {
42
43/// Inspect hardware capabilities, and load the optimal library for RooFit computations.
45{
46 // Check if the library was not initialised already
47 static bool isInitialised = false;
48 if (isInitialised)
49 return 0;
50 isInitialised = true;
51
52 const std::string userChoice = gEnv->GetValue("RooFit.BatchCompute", "auto");
53#ifdef R__RF_ARCHITECTURE_SPECIFIC_LIBS
54
55 __builtin_cpu_init();
56 bool supported_avx512 = __builtin_cpu_supports("avx512cd") && __builtin_cpu_supports("avx512vl") &&
57 __builtin_cpu_supports("avx512bw") && __builtin_cpu_supports("avx512dq");
58
59 if (userChoice == "auto") {
60 if (supported_avx512) {
61 loadWithErrorChecking("libRooBatchCompute_AVX512");
62 } else if (__builtin_cpu_supports("avx2")) {
63 loadWithErrorChecking("libRooBatchCompute_AVX2");
64 } else if (__builtin_cpu_supports("avx")) {
65 loadWithErrorChecking("libRooBatchCompute_AVX");
66 } else if (__builtin_cpu_supports("sse4.1")) {
67 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
68 }
69 } else if (userChoice == "avx512") {
70 loadWithErrorChecking("libRooBatchCompute_AVX512");
71 } else if (userChoice == "avx2") {
72 loadWithErrorChecking("libRooBatchCompute_AVX2");
73 } else if (userChoice == "avx") {
74 loadWithErrorChecking("libRooBatchCompute_AVX");
75 } else if (userChoice == "sse") {
76 loadWithErrorChecking("libRooBatchCompute_SSE4.1");
77 } else if (userChoice != "generic") {
78 throw std::invalid_argument(
79 "Supported options for `RooFit.BatchCompute` are `auto`, `avx512`, `avx2`, `avx`, `sse`, `generic`.");
80 }
81#endif // R__RF_ARCHITECTURE_SPECIFIC_LIBS
82
83 if (RooBatchCompute::dispatchCPU == nullptr)
84 loadWithErrorChecking("libRooBatchCompute_GENERIC");
85
86 return 0;
87}
88
90{
91 // Check if the library was not initialised already
92 static bool isInitialised = false;
93 if (isInitialised)
94 return 0;
95 isInitialised = true;
96
97 return gSystem->Load("libRooBatchCompute_CUDA");
98}
99
100} // namespace RooBatchCompute
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
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:1857
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...
int initCPU()
Inspect hardware capabilities, and load the optimal library for RooFit computations.