Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1//===-- Utils.h -- Utilities header --===//
2//
3// Part of the Proteus Project, under the Apache License v2.0 with LLVM
4// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef PROTEUS_UTILS_H
12#define PROTEUS_UTILS_H
13
14#include "proteus/Error.h"
15#include "proteus/Logger.hpp"
17
18#include <llvm/ADT/Twine.h>
19#include <llvm/Support/SourceMgr.h>
20
21#include <string>
22
23template <typename T>
24inline void saveToFile(llvm::StringRef Filepath, T &&Data) {
25 std::error_code EC;
26 llvm::raw_fd_ostream Out(Filepath, EC);
27 if (EC)
28 proteus::reportFatalError("Cannot open file" + Filepath);
29 Out << Data;
30 Out.close();
31}
32
33inline std::string getDistributedRank() {
34 // Try commonly used environment variables to get the rank in distributed
35 // runs.
36 const char *Id = nullptr;
37
38 // MPICH, Intel MPI, MVAPICH.
39 if (!Id)
40 Id = std::getenv("PMI_RANK");
41 if (!Id)
42 Id = std::getenv("MPI_RANK");
43
44 // Open MPI.
45 if (!Id)
46 Id = std::getenv("OMPI_COMM_WORLD_RANK");
47
48 // SLURM (if using srun).
49 if (!Id)
50 Id = std::getenv("SLURM_PROCID");
51
52 // PBS/Torque.
53 if (!Id)
54 Id = std::getenv("PBS_TASKNUM");
55
56 if (Id) {
57 return std::string(Id);
58 }
59
60 // Fallback for non-distributed execution.
61 return "0";
62}
63
64#if PROTEUS_ENABLE_HIP
65#include "proteus/UtilsHIP.h"
66#endif
67
68#if PROTEUS_ENABLE_CUDA
69#include "proteus/UtilsCUDA.h"
70#endif
71
72#endif
std::string getDistributedRank()
Definition Utils.h:33
void saveToFile(llvm::StringRef Filepath, T &&Data)
Definition Utils.h:24
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:113