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/impl/Logger.h"
17
18#include <llvm/ADT/Twine.h>
19#include <llvm/Support/SourceMgr.h>
20
21#include <atomic>
22#include <filesystem>
23#include <string>
24
25template <typename T>
26inline void saveToFile(llvm::StringRef Filepath, T &&Data) {
27 std::error_code EC;
28 llvm::raw_fd_ostream Out(Filepath, EC);
29 if (EC)
30 proteus::reportFatalError("Cannot open file" + Filepath);
31 Out << Data;
32 Out.close();
33}
34
35// Write to temp file first, then rename so readers never see a partial file.
36// Note: rename() atomicity is not guaranteed on NFS; concurrent readers may
37// occasionally see missing files, causing redundant compilation.
38template <typename T>
39void saveToFileAtomic(llvm::StringRef Filepath, T &&Data) {
40 static std::atomic<uint64_t> TempCounter{0};
41 std::string TempPath =
42 Filepath.str() + ".tmp." +
43 std::to_string(TempCounter.fetch_add(1, std::memory_order_relaxed));
44
45 std::error_code EC;
46 llvm::raw_fd_ostream Out(TempPath, EC);
47 if (EC)
48 proteus::reportFatalError("Cannot open file " + TempPath);
49 Out << Data;
50 Out.close();
51
52 std::filesystem::rename(TempPath, Filepath.str());
53}
54
55inline std::string getDistributedRank() {
56 // Try commonly used environment variables to get the rank in distributed
57 // runs.
58 const char *Id = nullptr;
59
60 // MPICH, Intel MPI, MVAPICH.
61 if (!Id)
62 Id = std::getenv("PMI_RANK");
63 if (!Id)
64 Id = std::getenv("MPI_RANK");
65
66 // Open MPI.
67 if (!Id)
68 Id = std::getenv("OMPI_COMM_WORLD_RANK");
69
70 // SLURM (if using srun).
71 if (!Id)
72 Id = std::getenv("SLURM_PROCID");
73
74 // PBS/Torque.
75 if (!Id)
76 Id = std::getenv("PBS_TASKNUM");
77
78 if (Id) {
79 return std::string(Id);
80 }
81
82 // Fallback for non-distributed execution.
83 return "0";
84}
85
86#if PROTEUS_ENABLE_HIP
88#endif
89
90#if PROTEUS_ENABLE_CUDA
92#endif
93
94#endif
void saveToFileAtomic(llvm::StringRef Filepath, T &&Data)
Definition Utils.h:39
std::string getDistributedRank()
Definition Utils.h:55
void saveToFile(llvm::StringRef Filepath, T &&Data)
Definition Utils.h:26
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14