caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Multiplication of matrix in C and OCaml
@ 2007-02-07 23:42 Frédéric Gava
  2007-02-08  2:14 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 33+ messages in thread
From: Frédéric Gava @ 2007-02-07 23:42 UTC (permalink / raw)
  To: caml-list

Dear All,

I compared multiplication of matrix in C and OCaml and I was a little 
surprise to see that the following C code (using -O2) is 8 time faster 
than the OCaml one (even with -unsafe).

Anybody have an idea to optimize my OCaml code or know why is there this 
"big" difference ?

many thanks

Frédéric Gava

ps: the difference is the same even when I use a non-polymorphic 
multiplication

*************** C Code ******************

typedef struct Complexe {double re; double img;} complexe;

__inline__ void* add(void *x, void *y)
{
complexe a=((complexe*) x)[0];
complexe b=((complexe*) y)[0];
return (void *) &((complexe){a.re+b.re, a.img+b.img});
}

__inline__ void* mult(void *x, void *y)
{
complexe a=((complexe*) x)[0];
complexe b=((complexe*) y)[0];
return (void *) &((complexe){a.re*b.re-a.img*b.img, a.re*b.img+a.img*b.re});
}

void multiply_generic(int n, void *zero, void* (*add)(void *a, void *b), 
void* (*mult)(void *a, void *b), void *a, void *b, void *c)
{
  register int i,j,k;
  void *tmp;
  void *cc;

   for (i=0; i<n; i++)
    {
     for (j=0;j<n;j++)
      {
       tmp=zero;
       for (k=0;k<n;k++) tmp=add(tmp,(mult(b+(k*n+j),a+(i*n+k))));
       cc=c+(i*n+j);
       cc=tmp;
      }
   }
}

complexe random_complexe(void)
{
  complexe c = {c.re=(double)(rand()%1000), c.img=(double)(rand()%1000)};
  return c;
}


Call this with (and with random matrix).

complexe zero;
zero.re=(double)0;
zero.img=(double)0;
multiply_generic(i,&zero,add,mult,a,b,c);


********************* OCaml Code *******************************

let random_complex n = {Complex.re=Random.float n; im=Random.float n}

let matrixRandom n = Array.init (n*n) (fun _ -> random_complex 1000.)

let multiplication n zero add mul a b c =
   let tmp=ref zero in
   for i=0 to n-1 do
    for j=0 to n-1 do
     tmp:=zero;
     for k=0 to n-1 do
      tmp:= add (!tmp) (mul b.(k*n+j) a.(i*n+k))
     done;
     c.(i*n+j)<-(!tmp)
    done
   done


^ permalink raw reply	[flat|nested] 33+ messages in thread
* Re: [Caml-list] Multiplication of matrix in C and OCaml
@ 2007-02-09 14:24 Frederic GAVA
  0 siblings, 0 replies; 33+ messages in thread
From: Frederic GAVA @ 2007-02-09 14:24 UTC (permalink / raw)
  To: Brian Hurt, ls-ocaml-developer-2006; +Cc: caml-list

> For the C side, I recommend using clock() to time the runs. This also 
> factors out the cost of creating the random matricies. Basically, you 
> just do:
> 
> #include 
> 
> ...
> {
> clock_t start, stop;
> ...
> start = clock();
> /* stuff to time here */
> stop = clock();
> printf("The amount of time taken = %f seconds\n", 
> ((double)(stop-start))/((double) CLOCKS_PER_SEC));
> /* write result to file */
> }

Many parallel C libraries have also there own timer.
MPI=> MPI_Wtime
BSPlib => bsp_time
PVM=>...

It is very usefull because they do not take into account time to initialize the parallel machine...

Frédéric Gava


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

end of thread, other threads:[~2007-02-11 13:12 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-07 23:42 Multiplication of matrix in C and OCaml Frédéric Gava
2007-02-08  2:14 ` [Caml-list] " Jacques Garrigue
2007-02-08  9:27   ` Frédéric Gava
2007-02-08  9:38     ` Frédéric Gava
2007-02-08 12:08     ` Jacques Garrigue
2007-02-08  9:56   ` Frédéric Gava
2007-02-08 10:45     ` Xavier Leroy
2007-02-08 15:16       ` Frédéric Gava
2007-02-09  2:58         ` Jacques Garrigue
2007-02-09  9:06           ` ls-ocaml-developer-2006
2007-02-09 10:32             ` ls-ocaml-developer-2006
2007-02-09 14:22               ` skaller
2007-02-09 21:47                 ` ls-ocaml-developer-2006
2007-02-09 21:55                   ` Andrej Bauer
2007-02-09 22:36                     ` ls-ocaml-developer-2006
2007-02-09 23:53                       ` Jon Harrop
2007-02-10  1:41                         ` ls-ocaml-developer-2006
2007-02-10  2:24                           ` Jon Harrop
2007-02-10 14:41                             ` ls-ocaml-developer-2006
2007-02-10 14:52                               ` Jon Harrop
2007-02-10 15:51                                 ` ls-ocaml-developer-2006
2007-02-10 16:10                                   ` Xavier Leroy
2007-02-10 16:11                                   ` Jon Harrop
2007-02-10 14:55                               ` Mattias Engdegård
2007-02-11 13:13                             ` Christophe Raffalli
2007-02-10  1:10                     ` Brian Hurt
2007-02-10  1:16                       ` Robert Roessler
2007-02-09 23:56                 ` Jon Harrop
2007-02-09 12:05             ` Jon Harrop
2007-02-09 12:35               ` ls-ocaml-developer-2006
2007-02-09 13:50             ` Brian Hurt
2007-02-09 14:23               ` Gerd Stolpmann
2007-02-09 14:24 Frederic GAVA

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).