Version française
Home     About     Download     Resources     Contact us    
Browse thread
Bigarray.c_layout
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Florent Monnier <fmonnier@l...>
Subject: Bigarray.c_layout
Hi,
My problem is with the Bigarray.c_layout 

All the items are ordered row by row in OCaml with this layout,
and the library I use through OCaml use the datas ordered line by line.

As a temporary work-around, I've made the function 'reorder' below.
But this way is very too slow.

So what solution would you advice to me ?
- creating an other Bigarray.c_layout : "Bigarray.c_layout_by_line" ?
(and then propose it as a patch to be included in a futur OCaml version)
- or replacing my big arrays of 3 dimensions by big arrays of 1 dimension
and creating a function which converts the index i j k by the unique index ?
- something else ?


-- 
best regards


=========================
let reorder_c_to_ml ~arr =
  let width  = Array3.dim1 arr
  and height = Array3.dim2 arr
  and cells  = Array3.dim3 arr
  and kind   = Array3.kind arr
  in
  assert(cells=3);
  let tmp = Array1.create kind c_layout (width * height * cells)
  and k = ref 0
  in
  for i = 0 to pred(width) do
    for j = 0 to pred(height) do
      tmp.{!k} <- arr.{i,j,0}; incr k;
      tmp.{!k} <- arr.{i,j,1}; incr k;
      tmp.{!k} <- arr.{i,j,2}; incr k;
    done;
  done;
  k := 0;
  for j = 0 to pred(height) do
    for i = 0 to pred(width) do
      arr.{i,j,0} <- tmp.{!k}; incr k;
      arr.{i,j,1} <- tmp.{!k}; incr k;
      arr.{i,j,2} <- tmp.{!k}; incr k;
    done;
  done;
  (arr)
;;

let reorder_ml_to_c ~arr =
  let width  = Array3.dim1 arr
  and height = Array3.dim2 arr
  and cells  = Array3.dim3 arr
  and kind   = Array3.kind arr
  in
  assert(cells=3);
  let tmp = Array1.create kind c_layout (width * height * cells)
  and k = ref 0
  in
  for j = 0 to pred(height) do
    for i = 0 to pred(width) do
      tmp.{!k} <- arr.{i,j,0}; incr k;
      tmp.{!k} <- arr.{i,j,1}; incr k;
      tmp.{!k} <- arr.{i,j,2}; incr k;
    done;
  done;
  k := 0;
  for i = 0 to pred(width) do
    for j = 0 to pred(height) do
      arr.{i,j,0} <- tmp.{!k}; incr k;
      arr.{i,j,1} <- tmp.{!k}; incr k;
      arr.{i,j,2} <- tmp.{!k}; incr k;
    done;
  done;
  (arr)
;;

type dir = ML_to_C | C_to_ML

let reorder ~arr ?(dir=ML_to_C) () =
  match dir with
  | ML_to_C -> reorder_ml_to_c ~arr
  | C_to_ML -> reorder_c_to_ml ~arr
;;