caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Bigarray.c_layout
@ 2006-04-14 14:04 Florent Monnier
       [not found] ` <20060414.193255.24118599.debian00@tiscali.fr>
  0 siblings, 1 reply; 5+ messages in thread
From: Florent Monnier @ 2006-04-14 14:04 UTC (permalink / raw)
  To: caml-list

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
;;


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Bigarray.c_layout
       [not found] ` <20060414.193255.24118599.debian00@tiscali.fr>
@ 2006-04-14 18:07   ` Florent Monnier
  2006-04-20  9:42     ` [Caml-list] Bigarray.c_layout Damien Doligez
  0 siblings, 1 reply; 5+ messages in thread
From: Florent Monnier @ 2006-04-14 18:07 UTC (permalink / raw)
  To: caml-list

> Why can't you just write arr.{j,i,k} instead of arr.{i,j,k} ?

No it is really the ordering of the data which changes, let's see an example:

In the library I use the elements are ordered like this in the 1dim C array:
01  02  03  04  05  06
07  08  09  10  11  12
13  14  15  16  17  18
19  20  21  22  23  24

but with the c_layout of big array the datas are ordered like this :
01  05  09  13  17  21
02  06  10  14  18  22
03  07  11  15  19  23
04  08  12  16  20  24


-- 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Re: Bigarray.c_layout
  2006-04-14 18:07   ` Bigarray.c_layout Florent Monnier
@ 2006-04-20  9:42     ` Damien Doligez
  2006-04-20 14:03       ` Florent Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Damien Doligez @ 2006-04-20  9:42 UTC (permalink / raw)
  To: caml users


On 2006-04-14, at 20:07, Florent Monnier wrote:

> In the library I use the elements are ordered like this in the 1dim  
> C array:
> 01  02  03  04  05  06
> 07  08  09  10  11  12
> 13  14  15  16  17  18
> 19  20  21  22  23  24
>
> but with the c_layout of big array the datas are ordered like this :
> 01  05  09  13  17  21
> 02  06  10  14  18  22
> 03  07  11  15  19  23
> 04  08  12  16  20  24

Looks like you need to use fortran_layout, and shift the indices by 1
to translate between 0-based and 1-based.

-- Damien


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Re: Bigarray.c_layout
  2006-04-20  9:42     ` [Caml-list] Bigarray.c_layout Damien Doligez
@ 2006-04-20 14:03       ` Florent Monnier
  2006-04-20 21:14         ` Remi Vanicat
  0 siblings, 1 reply; 5+ messages in thread
From: Florent Monnier @ 2006-04-20 14:03 UTC (permalink / raw)
  To: caml-list

> > In the library I use the elements are ordered like this in the 1dim
> > C array:
> > 01  02  03  04  05  06
> > 07  08  09  10  11  12
> > 13  14  15  16  17  18
> > 19  20  21  22  23  24
> >
> > but with the c_layout of big array the datas are ordered like this :
> > 01  05  09  13  17  21
> > 02  06  10  14  18  22
> > 03  07  11  15  19  23
> > 04  08  12  16  20  24
>
> Looks like you need to use fortran_layout, and shift the indices by 1
> to translate between 0-based and 1-based.

But it would be like the "reorder" function I use, isn't it ?
(which reorder function is in my first post:
http://caml.inria.fr/pub/ml-archives/caml-list/2006/04/64769096fc5b2f076911f60d1ce44c98.fr.html
)

The problem with reordering the datas is that it is far too slow.


-- 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Re: Bigarray.c_layout
  2006-04-20 14:03       ` Florent Monnier
@ 2006-04-20 21:14         ` Remi Vanicat
  0 siblings, 0 replies; 5+ messages in thread
From: Remi Vanicat @ 2006-04-20 21:14 UTC (permalink / raw)
  To: Florent Monnier; +Cc: caml-list

2006/4/20, Florent Monnier <fmonnier@linux-nantes.fr.eu.org>:
> > > In the library I use the elements are ordered like this in the 1dim
> > > C array:
> > > 01  02  03  04  05  06
> > > 07  08  09  10  11  12
> > > 13  14  15  16  17  18
> > > 19  20  21  22  23  24
> > >
> > > but with the c_layout of big array the datas are ordered like this :
> > > 01  05  09  13  17  21
> > > 02  06  10  14  18  22
> > > 03  07  11  15  19  23
> > > 04  08  12  16  20  24
> >
> > Looks like you need to use fortran_layout, and shift the indices by 1
> > to translate between 0-based and 1-based.
>
> But it would be like the "reorder" function I use, isn't it ?

Nope, a biggarray with a fortran_layout is a biggaray with ordering as
in your first example, when c_layout is with the ordering as in you
second example.

The other difference is that with fortran_layout, array are from 1 to
n, not from 0 to n-1.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-04-20 21:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-14 14:04 Bigarray.c_layout Florent Monnier
     [not found] ` <20060414.193255.24118599.debian00@tiscali.fr>
2006-04-14 18:07   ` Bigarray.c_layout Florent Monnier
2006-04-20  9:42     ` [Caml-list] Bigarray.c_layout Damien Doligez
2006-04-20 14:03       ` Florent Monnier
2006-04-20 21:14         ` Remi Vanicat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).