# Writing bigarray to file

**URL:** <https://discuss.ocaml.org/t/writing-bigarray-to-file/9036>\
**Category:** Community\
**Created:** [December 19, 2021, 9:00am UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036 "2021-12-19T09:00:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craff](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craff/32/3281_2.png) [@craff](https://discuss.ocaml.org/u/craff)\
**Post date:** [December 19, 2021, 9:00am UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/1 "2021-12-19T09:00:46Z")

</div>

Dear camlers

There is no way to write a bigarray to a file (except in C or element by element) ? In my case, the file is a socket hence map\_file is not an option.

Cheers,  
Christophe

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [December 19, 2021, 9:30am UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/2 "2021-12-19T09:30:53Z")

</div>

Indeed, this operation is not available in the standard library. If you are dealing with a “char” bigarray, there are third-party libraries that will do what you want, eg [`ocaml-bigstring`](https://github.com/c-cube/ocaml-bigstring):

> <https://github.com/c-cube/ocaml-bigstring/blob/6cd528ed4bce75500fb7d73d6789093e9720522a/src/bigstring_unix.mli#L15>

If you are dealing with a general bigarray, then you will need to work a bit harder by writing some C code yourself.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![XVilka](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xvilka/32/5334_2.png) [@XVilka](https://discuss.ocaml.org/u/XVilka)\
**Post date:** [December 20, 2021, 10:35am UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/3 "2021-12-20T10:35:42Z")

</div>

[GitHub - inhabitedtype/bigstringaf: Bigstring intrinsics and fast blits based on memcpy/memmove](https://github.com/inhabitedtype/bigstringaf) is the maintained alternative to the `ocaml-bigstring`. I use it in production and it works great, can recommend.

---

<div class="post-metadata">

**Author:** ![c-cube](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/c-cube/32/1727_2.png) [@c-cube](https://discuss.ocaml.org/u/c-cube)\
**Post date:** [December 20, 2021, 2:44pm UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/4 "2021-12-20T14:44:22Z")

</div>

I second bigstringaf, it’s better than bigstring 🙂

---

<div class="post-metadata">

**Author:** ![lukstafi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lukstafi/32/3442_2.png) [@lukstafi](https://discuss.ocaml.org/u/lukstafi)\
**Post date:** [July 6, 2023, 3:19pm UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/5 "2023-07-06T15:19:26Z")

</div>

Maybe [`npy`](https://github.com/LaurentMazare/npy-ocaml) is the answer for float bigarrays.

---

<div class="post-metadata">

**Author:** ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)\
**Post date:** [July 7, 2023, 1:14am UTC](https://discuss.ocaml.org/t/writing-bigarray-to-file/9036/6 "2023-07-07T01:14:08Z")

</div>

You could still try marshaling the bigarray to file then send that file over the socket.

Mmapping a bigarray to file is several orders of magnitude faster than marshalling w/ Marshal, in my experience.  
Here is a code snippet:

```ocaml
  assert(n = BA1.dim g.arr);
  let fd = Unix.(openfile fn [O_RDWR; O_CREAT; O_TRUNC] 0o600) in
  (* create mmapped bigarray *)
  let dst =
    BA.array1_of_genarray
      (Unix.map_file fd BA.Float32 BA.c_layout true [|n|]) in
  (* copy existing bigarray to the (new) one mapped to file *)
  BA1.blit g.arr dst;
  Unix.close fd;

```
