mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-18 08:36:43 -05:00
13 lines
No EOL
30 KiB
HTML
13 lines
No EOL
30 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Array (ocaml.Stdlib.Float.Array)</title><link rel="stylesheet" href="../../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../../../index.html">ocaml</a> » <a href="../../index.html">Stdlib</a> » <a href="../index.html">Float</a> » Array</nav><header class="odoc-preamble"><h1>Module <code><span>Float.Array</span></code></h1><p>Float arrays with packed representation.</p></header><nav class="odoc-toc"><ul><li><a href="#iterators">Iterators</a></li><li><a href="#iterators-on-two-arrays">Iterators on two arrays</a></li><li><a href="#array-scanning">Array scanning</a></li><li><a href="#sorting">Sorting</a></li><li><a href="#float-arrays-and-sequences">Float arrays and Sequences</a></li><li><a href="#floatarray_concurrency">Arrays and concurrency safety</a><ul><li><a href="#floatarray_atomicity">Atomicity</a></li><li><a href="#floatarray_data_race">Data races</a></li><li><a href="#floatarray_datarace_tearing">Tearing</a></li></ul></li></ul></nav><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span><span> = floatarray</span></code></div><div class="spec-doc"><p>The type of float arrays with packed representation.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.08.0</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-length"><a href="#val-length" class="anchor"></a><code><span><span class="keyword">val</span> length : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> int</span></code></div><div class="spec-doc"><p>Return the length (number of elements) of the given floatarray.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get"><a href="#val-get" class="anchor"></a><code><span><span class="keyword">val</span> get : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> float</span></code></div><div class="spec-doc"><p><code>get a n</code> returns the element number <code>n</code> of floatarray <code>a</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n</code> is outside the range 0 to <code>(length a - 1)</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set"><a href="#val-set" class="anchor"></a><code><span><span class="keyword">val</span> set : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>set a n x</code> modifies floatarray <code>a</code> in place, replacing element number <code>n</code> with <code>x</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n</code> is outside the range 0 to <code>(length a - 1)</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-make"><a href="#val-make" class="anchor"></a><code><span><span class="keyword">val</span> make : <span>int <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>make n x</code> returns a fresh floatarray of length <code>n</code>, initialized with <code>x</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n < 0</code> or <code>n > Sys.max_floatarray_length</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span>int <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>create n</code> returns a fresh floatarray of length <code>n</code>, with uninitialized data.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n < 0</code> or <code>n > Sys.max_floatarray_length</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-init"><a href="#val-init" class="anchor"></a><code><span><span class="keyword">val</span> init : <span>int <span class="arrow">-></span></span> <span><span>(<span>int <span class="arrow">-></span></span> float)</span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>init n f</code> returns a fresh floatarray of length <code>n</code>, with element number <code>i</code> initialized to the result of <code>f i</code>. In other terms, <code>init n f</code> tabulates the results of <code>f</code> applied to the integers <code>0</code> to <code>n-1</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>n < 0</code> or <code>n > Sys.max_floatarray_length</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-append"><a href="#val-append" class="anchor"></a><code><span><span class="keyword">val</span> append : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>append v1 v2</code> returns a fresh floatarray containing the concatenation of the floatarrays <code>v1</code> and <code>v2</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>length v1 + length v2 > Sys.max_floatarray_length</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-concat"><a href="#val-concat" class="anchor"></a><code><span><span class="keyword">val</span> concat : <span><span><a href="#type-t">t</a> list</span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same as <a href="#val-append"><code>append</code></a>, but concatenates a list of floatarrays.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-sub"><a href="#val-sub" class="anchor"></a><code><span><span class="keyword">val</span> sub : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>sub a pos len</code> returns a fresh floatarray of length <code>len</code>, containing the elements number <code>pos</code> to <code>pos + len - 1</code> of floatarray <code>a</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>pos</code> and <code>len</code> do not designate a valid subarray of <code>a</code>; that is, if <code>pos < 0</code>, or <code>len < 0</code>, or <code>pos + len > length a</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-copy"><a href="#val-copy" class="anchor"></a><code><span><span class="keyword">val</span> copy : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>copy a</code> returns a copy of <code>a</code>, that is, a fresh floatarray containing the same elements as <code>a</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fill"><a href="#val-fill" class="anchor"></a><code><span><span class="keyword">val</span> fill : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>fill a pos len x</code> modifies the floatarray <code>a</code> in place, storing <code>x</code> in elements number <code>pos</code> to <code>pos + len - 1</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>pos</code> and <code>len</code> do not designate a valid subarray of <code>a</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-blit"><a href="#val-blit" class="anchor"></a><code><span><span class="keyword">val</span> blit : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> <span>int <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>blit src src_pos dst dst_pos len</code> copies <code>len</code> elements from floatarray <code>src</code>, starting at element number <code>src_pos</code>, to floatarray <code>dst</code>, starting at element number <code>dst_pos</code>. It works correctly even if <code>src</code> and <code>dst</code> are the same floatarray, and the source and destination chunks overlap.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>src_pos</code> and <code>len</code> do not designate a valid subarray of <code>src</code>, or if <code>dst_pos</code> and <code>len</code> do not designate a valid subarray of <code>dst</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_list"><a href="#val-to_list" class="anchor"></a><code><span><span class="keyword">val</span> to_list : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>float list</span></span></code></div><div class="spec-doc"><p><code>to_list a</code> returns the list of all the elements of <code>a</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_list"><a href="#val-of_list" class="anchor"></a><code><span><span class="keyword">val</span> of_list : <span><span>float list</span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>of_list l</code> returns a fresh floatarray containing the elements of <code>l</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if the length of <code>l</code> is greater than <code>Sys.max_floatarray_length</code>.</p></li></ul></div></div><h3 id="iterators"><a href="#iterators" class="anchor"></a>Iterators</h3><div class="odoc-spec"><div class="spec value anchored" id="val-iter"><a href="#val-iter" class="anchor"></a><code><span><span class="keyword">val</span> iter : <span><span>(<span>float <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>iter f a</code> applies function <code>f</code> in turn to all the elements of <code>a</code>. It is equivalent to <code>f a.(0); f a.(1); ...; f a.(length a - 1); ()</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-iteri"><a href="#val-iteri" class="anchor"></a><code><span><span class="keyword">val</span> iteri : <span><span>(<span>int <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-iter"><code>iter</code></a>, but the function is applied with the index of the element as first argument, and the element itself as second argument.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map"><a href="#val-map" class="anchor"></a><code><span><span class="keyword">val</span> map : <span><span>(<span>float <span class="arrow">-></span></span> float)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>map f a</code> applies function <code>f</code> to all the elements of <code>a</code>, and builds a floatarray with the results returned by <code>f</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-mapi"><a href="#val-mapi" class="anchor"></a><code><span><span class="keyword">val</span> mapi : <span><span>(<span>int <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> float)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same as <a href="#val-map"><code>map</code></a>, but the function is applied to the index of the element as first argument, and the element itself as second argument.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fold_left"><a href="#val-fold_left" class="anchor"></a><code><span><span class="keyword">val</span> fold_left : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>fold_left f x init</code> computes <code>f (... (f (f x init.(0)) init.(1)) ...) init.(n-1)</code>, where <code>n</code> is the length of the floatarray <code>init</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fold_right"><a href="#val-fold_right" class="anchor"></a><code><span><span class="keyword">val</span> fold_right : <span><span>(<span>float <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <span class="arrow">-></span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>fold_right f a init</code> computes <code>f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...))</code>, where <code>n</code> is the length of the floatarray <code>a</code>.</p></div></div><h3 id="iterators-on-two-arrays"><a href="#iterators-on-two-arrays" class="anchor"></a>Iterators on two arrays</h3><div class="odoc-spec"><div class="spec value anchored" id="val-iter2"><a href="#val-iter2" class="anchor"></a><code><span><span class="keyword">val</span> iter2 : <span><span>(<span>float <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>Array.iter2 f a b</code> applies function <code>f</code> to all the elements of <code>a</code> and <code>b</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if the floatarrays are not the same size.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map2"><a href="#val-map2" class="anchor"></a><code><span><span class="keyword">val</span> map2 : <span><span>(<span>float <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> float)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>map2 f a b</code> applies function <code>f</code> to all the elements of <code>a</code> and <code>b</code>, and builds a floatarray with the results returned by <code>f</code>: <code>[| f a.(0) b.(0); ...; f a.(length a - 1) b.(length b - 1)|]</code>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if the floatarrays are not the same size.</p></li></ul></div></div><h3 id="array-scanning"><a href="#array-scanning" class="anchor"></a>Array scanning</h3><div class="odoc-spec"><div class="spec value anchored" id="val-for_all"><a href="#val-for_all" class="anchor"></a><code><span><span class="keyword">val</span> for_all : <span><span>(<span>float <span class="arrow">-></span></span> bool)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p><code>for_all f [|a1; ...; an|]</code> checks if all elements of the floatarray satisfy the predicate <code>f</code>. That is, it returns <code>(f a1) && (f a2) && ... && (f an)</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-exists"><a href="#val-exists" class="anchor"></a><code><span><span class="keyword">val</span> exists : <span><span>(<span>float <span class="arrow">-></span></span> bool)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p><code>exists f [|a1; ...; an|]</code> checks if at least one element of the floatarray satisfies the predicate <code>f</code>. That is, it returns <code>(f a1) || (f a2) || ... || (f an)</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-mem"><a href="#val-mem" class="anchor"></a><code><span><span class="keyword">val</span> mem : <span>float <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p><code>mem a set</code> is true if and only if there is an element of <code>set</code> that is structurally equal to <code>a</code>, i.e. there is an <code>x</code> in <code>set</code> such that <code>compare a x = 0</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-mem_ieee"><a href="#val-mem_ieee" class="anchor"></a><code><span><span class="keyword">val</span> mem_ieee : <span>float <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> bool</span></code></div><div class="spec-doc"><p>Same as <a href="#val-mem"><code>mem</code></a>, but uses IEEE equality instead of structural equality.</p></div></div><h3 id="sorting"><a href="#sorting" class="anchor"></a>Sorting</h3><div class="odoc-spec"><div class="spec value anchored" id="val-sort"><a href="#val-sort" class="anchor"></a><code><span><span class="keyword">val</span> sort : <span><span>(<span>float <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> int)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Sort a floatarray in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see below for a complete specification). For example, <a href="../../index.html#val-compare"><code>Stdlib.compare</code></a> is a suitable comparison function. After calling <code>sort</code>, the array is sorted in place in increasing order. <code>sort</code> is guaranteed to run in constant heap space and (at most) logarithmic stack space.</p><p>The current implementation uses Heap Sort. It runs in constant stack space.</p><p>Specification of the comparison function: Let <code>a</code> be the floatarray and <code>cmp</code> the comparison function. The following must be true for all <code>x</code>, <code>y</code>, <code>z</code> in <code>a</code> :</p><ul><li><code>cmp x y</code> > 0 if and only if <code>cmp y x</code> < 0</li><li>if <code>cmp x y</code> >= 0 and <code>cmp y z</code> >= 0 then <code>cmp x z</code> >= 0</li></ul><p>When <code>sort</code> returns, <code>a</code> contains the same elements as before, reordered in such a way that for all i and j valid indices of <code>a</code> :</p><ul><li><code>cmp a.(i) a.(j)</code> >= 0 if and only if i >= j</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-stable_sort"><a href="#val-stable_sort" class="anchor"></a><code><span><span class="keyword">val</span> stable_sort : <span><span>(<span>float <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> int)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-sort"><code>sort</code></a>, but the sorting algorithm is stable (i.e. elements that compare equal are kept in their original order) and not guaranteed to run in constant heap space.</p><p>The current implementation uses Merge Sort. It uses a temporary floatarray of length <code>n/2</code>, where <code>n</code> is the length of the floatarray. It is usually faster than the current implementation of <a href="#val-sort"><code>sort</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-fast_sort"><a href="#val-fast_sort" class="anchor"></a><code><span><span class="keyword">val</span> fast_sort : <span><span>(<span>float <span class="arrow">-></span></span> <span>float <span class="arrow">-></span></span> int)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-sort"><code>sort</code></a> or <a href="#val-stable_sort"><code>stable_sort</code></a>, whichever is faster on typical input.</p></div></div><h3 id="float-arrays-and-sequences"><a href="#float-arrays-and-sequences" class="anchor"></a>Float arrays and Sequences</h3><div class="odoc-spec"><div class="spec value anchored" id="val-to_seq"><a href="#val-to_seq" class="anchor"></a><code><span><span class="keyword">val</span> to_seq : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span>float <a href="../../Seq/index.html#type-t">Seq.t</a></span></span></code></div><div class="spec-doc"><p>Iterate on the floatarray, in increasing order. Modifications of the floatarray during iteration will be reflected in the sequence.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_seqi"><a href="#val-to_seqi" class="anchor"></a><code><span><span class="keyword">val</span> to_seqi : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span>(int * float)</span> <a href="../../Seq/index.html#type-t">Seq.t</a></span></span></code></div><div class="spec-doc"><p>Iterate on the floatarray, in increasing order, yielding indices along elements. Modifications of the floatarray during iteration will be reflected in the sequence.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_seq"><a href="#val-of_seq" class="anchor"></a><code><span><span class="keyword">val</span> of_seq : <span><span>float <a href="../../Seq/index.html#type-t">Seq.t</a></span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Create an array from the generator.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map_to_array"><a href="#val-map_to_array" class="anchor"></a><code><span><span class="keyword">val</span> map_to_array : <span><span>(<span>float <span class="arrow">-></span></span> <span class="type-var">'a</span>)</span> <span class="arrow">-></span></span> <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> array</span></span></code></div><div class="spec-doc"><p><code>map_to_array f a</code> applies function <code>f</code> to all the elements of <code>a</code>, and builds an array with the results returned by <code>f</code>: <code>[| f a.(0); f a.(1); ...; f a.(length a - 1) |]</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-map_from_array"><a href="#val-map_from_array" class="anchor"></a><code><span><span class="keyword">val</span> map_from_array : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">-></span></span> float)</span> <span class="arrow">-></span></span> <span><span><span class="type-var">'a</span> array</span> <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>map_from_array f a</code> applies function <code>f</code> to all the elements of <code>a</code>, and builds a floatarray with the results returned by <code>f</code>.</p></div></div><h2 id="floatarray_concurrency"><a href="#floatarray_concurrency" class="anchor"></a>Arrays and concurrency safety</h2><p>Care must be taken when concurrently accessing float arrays from multiple domains: accessing an array will never crash a program, but unsynchronized accesses might yield surprising (non-sequentially-consistent) results.</p><h3 id="floatarray_atomicity"><a href="#floatarray_atomicity" class="anchor"></a>Atomicity</h3><p>Every float array operation that accesses more than one array element is not atomic. This includes iteration, scanning, sorting, splitting and combining arrays.</p><p>For example, consider the following program:</p><pre class="language-ocaml"><code>let size = 100_000_000
|
||
let a = Float.Array.make size 1.
|
||
let update a f () =
|
||
Float.Array.iteri (fun i x -> Float.Array.set a i (f x)) a
|
||
let d1 = Domain.spawn (update a (fun x -> x +. 1.))
|
||
let d2 = Domain.spawn (update a (fun x -> 2. *. x +. 1.))
|
||
let () = Domain.join d1; Domain.join d2</code></pre><p>After executing this code, each field of the float array <code>a</code> is either <code>2.</code>, <code>3.</code>, <code>4.</code> or <code>5.</code>. If atomicity is required, then the user must implement their own synchronization (for example, using <a href="../../Mutex/index.html#type-t"><code>Mutex.t</code></a>).</p><h3 id="floatarray_data_race"><a href="#floatarray_data_race" class="anchor"></a>Data races</h3><p>If two domains only access disjoint parts of the array, then the observed behaviour is the equivalent to some sequential interleaving of the operations from the two domains.</p><p>A data race is said to occur when two domains access the same array element without synchronization and at least one of the accesses is a write. In the absence of data races, the observed behaviour is equivalent to some sequential interleaving of the operations from different domains.</p><p>Whenever possible, data races should be avoided by using synchronization to mediate the accesses to the array elements.</p><p>Indeed, in the presence of data races, programs will not crash but the observed behaviour may not be equivalent to any sequential interleaving of operations from different domains. Nevertheless, even in the presence of data races, a read operation will return the value of some prior write to that location with a few exceptions.</p><h3 id="floatarray_datarace_tearing"><a href="#floatarray_datarace_tearing" class="anchor"></a>Tearing</h3><p>Float arrays have two supplementary caveats in the presence of data races.</p><p>First, the blit operation might copy an array byte-by-byte. Data races between such a blit operation and another operation might produce surprising values due to tearing: partial writes interleaved with other operations can create float values that would not exist with a sequential execution.</p><p>For instance, at the end of</p><pre class="language-ocaml"><code>let zeros = Float.Array.make size 0.
|
||
let max_floats = Float.Array.make size Float.max_float
|
||
let res = Float.Array.copy zeros
|
||
let d1 = Domain.spawn (fun () -> Float.Array.blit zeros 0 res 0 size)
|
||
let d2 = Domain.spawn (fun () -> Float.Array.blit max_floats 0 res 0 size)
|
||
let () = Domain.join d1; Domain.join d2</code></pre><p>the <code>res</code> float array might contain values that are neither <code>0.</code> nor <code>max_float</code>.</p><p>Second, on 32-bit architectures, getting or setting a field involves two separate memory accesses. In the presence of data races, the user may observe tearing on any operation.</p></div></body></html> |