Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ProcessManager.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 * IP, Inti Pelupessy, Netherlands eScience Center, i.pelupessy@esciencecenter.nl
6 *
7 * Copyright (c) 2021, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13#ifndef ROOT_ROOFIT_MultiProcess_ProcessManager
14#define ROOT_ROOFIT_MultiProcess_ProcessManager
15
16#include <sys/types.h> // pid_t
17#include <array>
18#include <csignal> // sig_atomic_t and for sigterm handling on child processes (in ProcessManager.cxx)
19#include <vector>
20
21// forward declaration
22class Queue;
23
24namespace RooFit {
25namespace MultiProcess {
26
28 friend Queue;
29
30public:
31 explicit ProcessManager(std::size_t N_workers);
33
34 bool is_initialized() const;
35
36 void terminate() noexcept;
38
39 bool is_master() const;
40 bool is_queue() const;
41 bool is_worker() const;
42 std::size_t worker_id() const;
43 std::size_t N_workers() const;
44
46
49 /// Read end of the self-pipe that the SIGTERM handler writes to (or -1 on
50 /// the master process, which installs no handler); used by Channel::wait.
52
53 // Interprocess channel file descriptors, created with socketpair() before
54 // forking. The Messenger claims the ends belonging to the current process
55 // and takes over their ownership; unclaimed descriptors are closed when
56 // this ProcessManager is destroyed.
57 int claim_mq_fd();
58 int claim_qw_fd(std::size_t worker_ix);
59 int claim_mw_fd(std::size_t worker_ix);
60
61 // for debugging/testing:
62 pid_t get_queue_pid() const { return queue_pid_; }
63 std::vector<pid_t> get_worker_pids() { return worker_pids_; }
64
65private:
66 void initialize_processes(bool cpu_pinning = true);
67 void shutdown_processes();
68 void create_channel_fds();
70 void close_channel_fds();
71
72 bool is_master_ = false;
73 bool is_queue_ = false;
74 bool is_worker_ = false;
75 std::size_t worker_id_;
76 std::size_t N_workers_;
77
78 // master must wait for workers after completion, for which it needs their PIDs
79 std::vector<pid_t> worker_pids_;
81
82 bool initialized_ = false;
83
84 // socketpair ends for the interprocess channels; in each array, index 0 is
85 // the end used by the process listed first in the member name (m: master,
86 // q: queue, w: worker), index 1 the other end
87 std::array<int, 2> mq_fds_{{-1, -1}};
88 std::vector<std::array<int, 2>> qw_fds_;
89 std::vector<std::array<int, 2>> mw_fds_;
90
94};
95
96} // namespace MultiProcess
97} // namespace RooFit
98
99#endif // ROOT_ROOFIT_MultiProcess_ProcessManager
Fork processes for queue and workers.
std::vector< pid_t > get_worker_pids()
void initialize_processes(bool cpu_pinning=true)
Fork processes and activate CPU pinning.
std::vector< std::array< int, 2 > > qw_fds_
void close_channel_fds()
Close all channel ends still owned by this ProcessManager (i.e.
static void handle_sigterm(int signum)
We need this to tell the children to die, because we can't talk to them anymore during JobManager des...
int claim_mw_fd(std::size_t worker_ix)
Hand over the master-worker channel end for the current process type.
void close_unused_channel_fds()
Close the channel ends that do not belong to the current process type.
int claim_mq_fd()
Hand over the master-queue channel end for the current process type.
std::vector< std::array< int, 2 > > mw_fds_
void identify_processes() const
Print to stdout which type of process we are on and what its PID is (for debugging)
static int sigterm_wake_fd()
Read end of the self-pipe that the SIGTERM handler writes to (or -1 on the master process,...
void terminate() noexcept
Shutdown forked processes if on master and if this process manager is initialized.
void shutdown_processes()
Shutdown forked processes if on master.
static volatile sig_atomic_t sigterm_received_
int claim_qw_fd(std::size_t worker_ix)
Hand over the queue-worker channel end for the current process type.
void create_channel_fds()
Create the socketpairs that connect the processes.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73