-
Notifications
You must be signed in to change notification settings - Fork 6
/
nproc.mli
164 lines (137 loc) · 5.79 KB
/
nproc.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(** Process pools *)
(**
A process pool is a fixed set of processes that perform
arbitrary computations for a master process, in parallel
and without blocking the master.
Master and workers communicate by message-passing. The implementation
relies on fork, pipes, Marshal and {{:http://ocsigen.org/lwt/manual/}Lwt}.
*)
(*
Implementation status: works, but not used intensively yet.
Performance has not been seriously benchmarked nor optimized.
*)
type t
(** Type of a process pool *)
val create : int -> t * unit Lwt.t
(** Create a process pool.
[create nproc] returns [(ppool, lwt)] where
[ppool] is pool of [nproc] processes and [lwt] is a lightweight thread
that finishes when the pool is closed.
*)
val close : t -> unit Lwt.t
(** Close a process pool.
It waits for all submitted tasks to finish. *)
val submit : t -> f: ('a -> 'b) -> 'a -> 'b Lwt.t
(** Submit a task.
[submit ppool ~f x] passes [f] and [x] to one of the worker processes,
which computes [f x] and passes the result back to the master process,
i.e. to the calling process running the Lwt event loop.
The current implementation uses the Marshal module to serialize
and deserialize [f], its input and its output.
*)
val iter_stream :
?granularity: int ->
nproc: int ->
f: ('a -> 'b) ->
g: ('b -> unit) ->
'a Stream.t -> unit
(**
Iterate over a stream using a pool of
[nproc] worker processes running in parallel.
[iter_stream] runs the Lwt event loop internally. It is intended
for programs that do not use Lwt otherwise.
Function [f] runs in the worker processes. It is applied to elements
of the stream that it receives from the master process.
Function [g] is applied to the result of [f] in the master process.
The current implementation uses the Marshal module to serialize
and deserialize [f], its inputs (stream elements) and its outputs.
[f] is serialized as many times as there are elements in the stream.
If [f] relies on a large immutable data structure, we recommend
using the [env] option of [Full.iter_stream].
*)
val log_error : (string -> unit) ref
(** Function used by Nproc for printing error messages.
By default it writes a message to the [stderr] channel
and flushes its buffer. *)
val log_info : (string -> unit) ref
(** Function used by Nproc for printing informational messages.
By default it writes a message to the [stderr] channel
and flushes its buffer. *)
val string_of_exn : (exn -> string) ref
(** Function used by Nproc to convert exception into a string used
in error messages.
By default it is set to [Printexc.to_string].
Users might want to change it into a function that prints
a stack backtrace, e.g.
{v
Nproc.string_of_exn :=
(fun e -> Printexc.get_backtrace () ^ Printexc.to_string e)
v}
*)
(** Fuller interface allowing requests from a worker to the master
and environment data residing in the workers. *)
module Full :
sig
type ('serv_request, 'serv_response, 'env) t
(**
Type of a process pool.
The type parameters correspond to the following:
- ['serv_request]: type of the requests from worker to master,
- ['serv_response]: type of the responses to the requests,
- ['env]: type of the environment data passed just once to each
worker process.
*)
val create :
int ->
('serv_request -> 'serv_response Lwt.t) ->
'env ->
('serv_request, 'serv_response, 'env) t * unit Lwt.t
(** Create a process pool.
[create nproc service env] returns [(ppool, lwt)] where
[ppool] is pool of [nproc] processes and [lwt] is a
lightweight thread that finishes when the pool is closed.
[service] is a service which is run asynchronously by the
master process and can be called synchronously by the workers.
[env] is arbitrary environment data, typically large, that
is passed to the workers just once during their initialization.
*)
val close :
('serv_request, 'serv_response, 'env) t -> unit Lwt.t
(** Close a process pool.
It waits for all submitted tasks to finish. *)
val submit :
('serv_request, 'serv_response, 'env) t ->
f: (('serv_request -> 'serv_response) -> 'env -> 'a -> 'b) ->
'a -> 'b Lwt.t
(** Submit a task.
[submit ppool ~f x] passes [f] and [x] to one of the worker processes,
which computes [f service env x] and passes the result back
to the master process,
i.e. to the calling process running the Lwt event loop.
The current implementation uses the Marshal module to serialize
and deserialize [f], its input and its output.
*)
val iter_stream :
?granularity: int ->
nproc: int ->
serv: ('serv_request -> 'serv_response Lwt.t) ->
env: 'env ->
f: (('serv_request -> 'serv_response) -> 'env -> 'a -> 'b) ->
g: ('b -> unit) ->
'a Stream.t -> unit
(**
Iterate over a stream using a pool of
[nproc] worker processes running in parallel.
[iter_stream] runs the Lwt event loop internally. It is intended
for programs that do not use Lwt otherwise.
Function [f] runs in the worker processes. It is applied to elements
of the stream that it receives from the master process.
Function [g] is applied to the result of [f] in the master process.
The current implementation uses the Marshal module to serialize
and deserialize [f], its inputs (stream elements) and its outputs.
[f] is serialized as many times as there are elements in the stream.
If [f] relies on a large immutable data structure, it should be
putting into [env] in order to avoid costly and
repetitive serialization of that data.
*)
end