Debian Codegraph @

stdlib/templates/floatarraylabeled.template.mli

raw symbols SCIP
1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* Nicolas Ojeda Bar, LexiFi *)
7 (* *)
8 (* Copyright 2018 Institut National de Recherche en Informatique et *)
9 (* en Automatique. *)
10 (* *)
11 (* All rights reserved. This file is distributed under the terms of *)
12 (* the GNU Lesser General Public License version 2.1, with the *)
13 (* special exception on linking described in the file LICENSE. *)
14 (* *)
15 (**************************************************************************)
16
17 type t = floatarray
18 (** The type of float arrays with packed representation.
19 @since 4.08
20 *)
21
22 val length : t -> int
23 (** Return the length (number of elements) of the given floatarray. *)
24
25 val get : t -> int -> float
26 (** [get a n] returns the element number [n] of floatarray [a].
27 @raise Invalid_argument if [n] is outside the range 0 to
28 [(length a - 1)]. *)
29
30 val set : t -> int -> float -> unit
31 (** [set a n x] modifies floatarray [a] in place, replacing element
32 number [n] with [x].
33 @raise Invalid_argument if [n] is outside the range 0 to
34 [(length a - 1)]. *)
35
36 val make : int -> float -> t
37 (** [make n x] returns a fresh floatarray of length [n], initialized with [x].
38 @raise Invalid_argument if [n < 0] or [n > Sys.max_floatarray_length]. *)
39
40 val create : int -> t
41 (** [create n] returns a fresh floatarray of length [n],
42 with uninitialized data.
43 @raise Invalid_argument if [n < 0] or [n > Sys.max_floatarray_length]. *)
44
45 val init : int -> f:(int -> float) -> t
46 (** [init n ~f] returns a fresh floatarray of length [n],
47 with element number [i] initialized to the result of [f i].
48 In other terms, [init n ~f] tabulates the results of [f]
49 applied to the integers [0] to [n-1].
50 @raise Invalid_argument if [n < 0] or [n > Sys.max_floatarray_length]. *)
51
52 val make_matrix : dimx:int -> dimy:int -> float -> t array
53 (** [make_matrix ~dimx ~dimy e] returns a two-dimensional array
54 (an array of arrays) with first dimension [dimx] and
55 second dimension [dimy], where all elements are initialized with [e].
56
57 @raise Invalid_argument if [dimx] or [dimy] is negative or
58 greater than {!Sys.max_floatarray_length}.
59
60 @since 5.2 *)
61
62 val init_matrix : dimx:int -> dimy:int -> f:(int -> int -> float) -> t array
63 (** [init_matrix ~dimx ~dimy ~f] returns a two-dimensional array
64 (an array of arrays)
65 with first dimension [dimx] and second dimension [dimy],
66 where the element at index ([x,y]) is initialized with [f x y].
67
68 @raise Invalid_argument if [dimx] or [dimy] is negative or
69 greater than {!Sys.max_floatarray_length}.
70
71 @since 5.2 *)
72
73 val append : t -> t -> t
74 (** [append v1 v2] returns a fresh floatarray containing the
75 concatenation of the floatarrays [v1] and [v2].
76 @raise Invalid_argument if
77 [length v1 + length v2 > Sys.max_floatarray_length]. *)
78
79 val concat : t list -> t
80 (** Same as {!append}, but concatenates a list of floatarrays. *)
81
82 val sub : t -> pos:int -> len:int -> t
83 (** [sub a ~pos ~len] returns a fresh floatarray of length [len],
84 containing the elements number [pos] to [pos + len - 1]
85 of floatarray [a].
86 @raise Invalid_argument if [pos] and [len] do not
87 designate a valid subarray of [a]; that is, if
88 [pos < 0], or [len < 0], or [pos + len > length a]. *)
89
90 val copy : t -> t
91 (** [copy a] returns a copy of [a], that is, a fresh floatarray
92 containing the same elements as [a]. *)
93
94 val fill : t -> pos:int -> len:int -> float -> unit
95 (** [fill a ~pos ~len x] modifies the floatarray [a] in place,
96 storing [x] in elements number [pos] to [pos + len - 1].
97 @raise Invalid_argument if [pos] and [len] do not
98 designate a valid subarray of [a]. *)
99
100 val blit : src:t -> src_pos:int -> dst:t -> dst_pos:int -> len:int -> unit
101 (** [blit ~src ~src_pos ~dst ~dst_pos ~len] copies [len] elements
102 from floatarray [src], starting at element number [src_pos],
103 to floatarray [dst], starting at element number [dst_pos].
104 It works correctly even if
105 [src] and [dst] are the same floatarray, and the source and
106 destination chunks overlap.
107 @raise Invalid_argument if [src_pos] and [len] do not
108 designate a valid subarray of [src], or if [dst_pos] and [len] do not
109 designate a valid subarray of [dst]. *)
110
111 val to_list : t -> float list
112 (** [to_list a] returns the list of all the elements of [a]. *)
113
114 val of_list : float list -> t
115 (** [of_list l] returns a fresh floatarray containing the elements
116 of [l].
117 @raise Invalid_argument if the length of [l] is greater than
118 [Sys.max_floatarray_length].*)
119
120 (** {1:comparison Comparison} *)
121
122 val equal : eq:(float -> float -> bool) -> t -> t -> bool
123 (** [equal eq a b] is [true] if and only if [a] and [b] have the
124 same length [n] and for all [i] in \[[0];[n-1]\], [eq a.(i) b.(i)]
125 is [true].
126
127 @since 5.4 *)
128
129 val compare : cmp:(float -> float -> int) -> t -> t -> int
130 (** [compare cmp a b] compares [a] and [b] according to the shortlex order,
131 that is, shorter arrays are smaller and equal-sized arrays are compared
132 in lexicographic order using [cmp] to compare elements.
133
134 @since 5.4 *)
135
136 (** {1 Iterators} *)
137
138 val iter : f:(float -> unit) -> t -> unit
139 (** [iter ~f a] applies function [f] in turn to all
140 the elements of [a]. It is equivalent to
141 [f a.(0); f a.(1); ...; f a.(length a - 1); ()]. *)
142
143 val iteri : f:(int -> float -> unit) -> t -> unit
144 (** Same as {!iter}, but the
145 function is applied with the index of the element as first argument,
146 and the element itself as second argument. *)
147
148 val map : f:(float -> float) -> t -> t
149 (** [map ~f a] applies function [f] to all the elements of [a],
150 and builds a floatarray with the results returned by [f]. *)
151
152 val map_inplace : f:(float -> float) -> t -> unit
153 (** [map_inplace f a] applies function [f] to all elements of [a],
154 and updates their values in place.
155 @since 5.1 *)
156
157 val mapi : f:(int -> float -> float) -> t -> t
158 (** Same as {!map}, but the
159 function is applied to the index of the element as first argument,
160 and the element itself as second argument. *)
161
162 val mapi_inplace : f:(int -> float -> float) -> t -> unit
163 (** Same as {!map_inplace}, but the function is applied to the index of the
164 element as first argument, and the element itself as second argument.
165 @since 5.1 *)
166
167 val fold_left : f:('acc -> float -> 'acc) -> init:'acc -> t -> 'acc
168 (** [fold_left ~f x ~init] computes
169 [f (... (f (f x init.(0)) init.(1)) ...) init.(n-1)],
170 where [n] is the length of the floatarray [init]. *)
171
172 val fold_right : f:(float -> 'acc -> 'acc) -> t -> init:'acc -> 'acc
173 (** [fold_right f a init] computes
174 [f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...))],
175 where [n] is the length of the floatarray [a]. *)
176
177 (** {1 Iterators on two arrays} *)
178
179 val iter2 : f:(float -> float -> unit) -> t -> t -> unit
180 (** [Array.iter2 ~f a b] applies function [f] to all the elements of [a]
181 and [b].
182 @raise Invalid_argument if the floatarrays are not the same size. *)
183
184 val map2 : f:(float -> float -> float) -> t -> t -> t
185 (** [map2 ~f a b] applies function [f] to all the elements of [a]
186 and [b], and builds a floatarray with the results returned by [f]:
187 [[| f a.(0) b.(0); ...; f a.(length a - 1) b.(length b - 1)|]].
188 @raise Invalid_argument if the floatarrays are not the same size. *)
189
190 (** {1 Array scanning} *)
191
192 val for_all : f:(float -> bool) -> t -> bool
193 (** [for_all ~f [|a1; ...; an|]] checks if all elements of the floatarray
194 satisfy the predicate [f]. That is, it returns
195 [(f a1) && (f a2) && ... && (f an)]. *)
196
197 val exists : f:(float -> bool) -> t -> bool
198 (** [exists f [|a1; ...; an|]] checks if at least one element of
199 the floatarray satisfies the predicate [f]. That is, it returns
200 [(f a1) || (f a2) || ... || (f an)]. *)
201
202 val mem : float -> set:t -> bool
203 (** [mem a ~set] is true if and only if there is an element of [set] that is
204 structurally equal to [a], i.e. there is an [x] in [set] such
205 that [compare a x = 0]. *)
206
207 val mem_ieee : float -> set:t -> bool
208 (** Same as {!mem}, but uses IEEE equality instead of structural equality. *)
209
210 (** {1 Array searching} *)
211
212 val find_opt : f:(float -> bool) -> t -> float option
213 (* [find_opt ~f a] returns the first element of the array [a] that satisfies
214 the predicate [f]. Returns [None] if there is no value that satisfies [f]
215 in the array [a].
216 @since 5.1 *)
217
218 val find_index : f:(float-> bool) -> t -> int option
219 (** [find_index ~f a] returns [Some i], where [i] is the index of the first
220 element of the array [a] that satisfies [f x], if there is such an
221 element.
222
223 It returns [None] if there is no such element.
224 @since 5.1 *)
225
226 val find_map : f:(float -> 'a option) -> t -> 'a option
227 (* [find_map ~f a] applies [f] to the elements of [a] in order, and returns
228 the first result of the form [Some v], or [None] if none exist.
229 @since 5.1 *)
230
231 val find_mapi : f:(int -> float -> 'a option) -> t -> 'a option
232 (** Same as [find_map], but the predicate is applied to the index of
233 the element as first argument (counting from 0), and the element
234 itself as second argument.
235
236 @since 5.1 *)
237
238 (** {1:sorting_and_shuffling Sorting and shuffling} *)
239
240 val sort : cmp:(float -> float -> int) -> t -> unit
241 (** Sort a floatarray in increasing order according to a comparison
242 function. The comparison function must return 0 if its arguments
243 compare as equal, a positive integer if the first is greater,
244 and a negative integer if the first is smaller (see below for a
245 complete specification). For example, {!Stdlib.compare} is
246 a suitable comparison function. After calling [sort], the
247 array is sorted in place in increasing order.
248 [sort] is guaranteed to run in constant heap space
249 and (at most) logarithmic stack space.
250
251 The current implementation uses Heap Sort. It runs in constant
252 stack space.
253
254 Specification of the comparison function:
255 Let [a] be the floatarray and [cmp] the comparison function. The following
256 must be true for all [x], [y], [z] in [a] :
257 - [cmp x y] > 0 if and only if [cmp y x] < 0
258 - if [cmp x y] >= 0 and [cmp y z] >= 0 then [cmp x z] >= 0
259
260 When [sort] returns, [a] contains the same elements as before,
261 reordered in such a way that for all i and j valid indices of [a] :
262 - [cmp a.(i) a.(j)] >= 0 if i >= j
263 *)
264
265 val stable_sort : cmp:(float -> float -> int) -> t -> unit
266 (** Same as {!sort}, but the sorting algorithm is stable (i.e.
267 elements that compare equal are kept in their original order) and
268 not guaranteed to run in constant heap space.
269
270 The current implementation uses Merge Sort. It uses a temporary
271 floatarray of length [n/2], where [n] is the length of the floatarray.
272 It is usually faster than the current implementation of {!sort}. *)
273
274 val fast_sort : cmp:(float -> float -> int) -> t -> unit
275 (** Same as {!sort} or {!stable_sort}, whichever is faster
276 on typical input. *)
277
278 val shuffle :
279 rand: (* thwart tools/sync_stdlib_docs *) (int -> int) -> t -> unit
280 (** [shuffle ~rand a] randomly permutes [a]'s elements using [rand]
281 for randomness. The distribution of permutations is uniform.
282
283 [rand] must be such that a call to [rand n] returns a uniformly
284 distributed random number in the range \[[0];[n-1]\].
285 {!Random.int} can be used for this (do not forget to
286 {{!Random.self_init}initialize} the generator).
287
288 @since 5.2 *)
289
290 (** {1 Float arrays and Sequences} *)
291
292 val to_seq : t -> float Seq.t
293 (** Iterate on the floatarray, in increasing order. Modifications of the
294 floatarray during iteration will be reflected in the sequence. *)
295
296 val to_seqi : t -> (int * float) Seq.t
297 (** Iterate on the floatarray, in increasing order, yielding indices along
298 elements. Modifications of the floatarray during iteration will be
299 reflected in the sequence. *)
300
301 val of_seq : float Seq.t -> t
302 (** Create an array from the generator. *)
303
304
305 val map_to_array : f:(float -> 'a) -> t -> 'a array
306 (** [map_to_array ~f a] applies function [f] to all the elements of [a],
307 and builds an array with the results returned by [f]:
308 [[| f a.(0); f a.(1); ...; f a.(length a - 1) |]]. *)
309
310 val map_from_array : f:('a -> float) -> 'a array -> t
311 (** [map_from_array ~f a] applies function [f] to all the elements of [a],
312 and builds a floatarray with the results returned by [f]. *)
313
314 (** {1:floatarray_concurrency Arrays and concurrency safety}
315
316 Care must be taken when concurrently accessing float arrays from multiple
317 domains: accessing an array will never crash a program, but unsynchronized
318 accesses might yield surprising (non-sequentially-consistent) results.
319
320 {2:floatarray_atomicity Atomicity}
321
322 Every float array operation that accesses more than one array element is
323 not atomic. This includes iteration, scanning, sorting, splitting and
324 combining arrays.
325
326 For example, consider the following program:
327 {[let size = 100_000_000
328 let a = Float.ArrayLabels.make size 1.
329 let update a f () =
330 Float.ArrayLabels.iteri ~f:(fun i x -> Float.Array.set a i (f x)) a
331 let d1 = Domain.spawn (update a (fun x -> x +. 1.))
332 let d2 = Domain.spawn (update a (fun x -> 2. *. x +. 1.))
333 let () = Domain.join d1; Domain.join d2
334 ]}
335
336 After executing this code, each field of the float array [a] is either
337 [2.], [3.], [4.] or [5.]. If atomicity is required, then the user must
338 implement their own synchronization (for example, using {!Mutex.t}).
339
340 {2:floatarray_data_race Data races}
341
342 If two domains only access disjoint parts of the array, then the
343 observed behaviour is the equivalent to some sequential interleaving of
344 the operations from the two domains.
345
346 A data race is said to occur when two domains access the same array
347 element without synchronization and at least one of the accesses is a
348 write. In the absence of data races, the observed behaviour is equivalent
349 to some sequential interleaving of the operations from different domains.
350
351 Whenever possible, data races should be avoided by using synchronization
352 to mediate the accesses to the array elements.
353
354 Indeed, in the presence of data races, programs will not crash but the
355 observed behaviour may not be equivalent to any sequential interleaving of
356 operations from different domains. Nevertheless, even in the presence of
357 data races, a read operation will return the value of some prior write to
358 that location with a few exceptions.
359
360
361 {2:floatarray_datarace_tearing Tearing }
362
363 Float arrays have two supplementary caveats in the presence of data races.
364
365 First, the blit operation might copy an array byte-by-byte. Data races
366 between such a blit operation and another operation might produce
367 surprising values due to tearing: partial writes interleaved with other
368 operations can create float values that would not exist with a sequential
369 execution.
370
371 For instance, at the end of
372 {[let zeros = Float.Array.make size 0.
373 let max_floats = Float.Array.make size Float.max_float
374 let res = Float.Array.copy zeros
375 let d1 = Domain.spawn (fun () -> Float.Array.blit zeros 0 res 0 size)
376 let d2 = Domain.spawn (fun () -> Float.Array.blit max_floats 0 res 0 size)
377 let () = Domain.join d1; Domain.join d2
378 ]}
379
380 the [res] float array might contain values that are neither [0.]
381 nor [max_float].
382
383 Second, on 32-bit architectures, getting or setting a field involves two
384 separate memory accesses. In the presence of data races, the user may
385 observe tearing on any operation.
386 *)
387
388 (**/**)
389
390 (** {1 Undocumented functions} *)
391
392 (* These functions are for system use only. Do not call directly. *)
393 external unsafe_get : t -> int -> float = "%floatarray_unsafe_get"
394 external unsafe_set : t -> int -> float -> unit = "%floatarray_unsafe_set"