Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Xavier Leroy <Xavier.Leroy@inria.fr>
Cc: OCAML <caml-list@inria.fr>
Subject: Re: [Caml-list] native threads not parallel?
Date: Thu, 21 Jun 2001 15:43:16 +0200	[thread overview]
Message-ID: <20010621154316.A18064@miss.wu-wien.ac.at> (raw)
In-Reply-To: <20010615233325.B24915@miss.wu-wien.ac.at>; from mottl@miss.wu-wien.ac.at on Fri, Jun 15, 2001 at 23:33:26 +0200

On Fri, 15 Jun 2001, Markus Mottl wrote:
> I have just been told that things work smoothly with dual-processor
> machines under Linux, and it also turned out due to more experimentation
> that the behaviour of native threads under Solaris is comparable to
> a lottery: when running very long, it seems that multiple threads are
> used. Sometimes. But sometimes you can also get bus errors. It's really
> unpredictable. Better don't bother with native threads under Solaris...

I couldn't resist and have done another test now. First, I have made
sure that multiple threads work under C. As Xavier has pointed out, this
requires setting the concurrency level to more than the default of 1.
Works fine (= in parallel).

Then I have written a small test program that tries to do the same under
OCaml - but no success...

Here are the the timings achieved by calling C via OCaml:

  Running sequentially:
  Wall clock time: 6.598713

  Running in parallel:
  Wall clock time: 6.601169


Here are the the timings achieved by using C only (clearly runs faster
with threads):

  Running sequentially
  Wall clock time: 6.995337

  Running in parallel
  Wall clock time: 3.910411

The only thing I can imagine is that OCaml somehow changes
scheduling so that the threads cannot run in parallel anymore, even if
"enter_blocking_section" is used. Is this possible? Or am I just making
some stupid mistake?

Regards,
Markus Mottl

P.S.:

The files I used in the test:

The C-only version:
---------------------------------------------------------------------------
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>

#define NUM_THREADS 2

double gtimeofday () {
  struct timeval t;
  gettimeofday(&t, NULL);
  return (t.tv_sec + t.tv_usec/1000000.0);
}

void init() {
  thr_setconcurrency(sysconf(_SC_NPROCESSORS_ONLN));
}

void calc_seq() {
  int i;
  for(i=0; i<=50000000; i++);
}

void * calc_par(void *threadid) {
  int i;
  for(i=0; i<=50000000; i++);
  pthread_exit(NULL);
}

void seq () {
  double t1, t2;

  printf("Running sequentially\n");

  t1 = gtimeofday();
    calc_seq(); calc_seq();
  t2 = gtimeofday();

  printf("Wall clock time: %f\n", (t2 - t1));
}

void par () {
  double t1, t2;
  pthread_t tids[NUM_THREADS];
  int rc, t, status;

  printf("Running in parallel\n");

  t1 = gtimeofday();
    for (t=0; t<NUM_THREADS; t++) {
      rc = pthread_create(&tids[t], NULL, calc_par, (void *) t);

      if (rc) {
        printf("pthread_create failed: return code: %d\n", rc);
        exit(-1);
      }
    }

    for (t=0; t<NUM_THREADS; t++) {
      rc = pthread_join(tids[t], (void **) &status);

      if (rc) {
        printf("pthread_join failed: return code: %d\n", rc);
        exit(-1);
      }
    }
  t2 = gtimeofday();

  printf("Wall clock time: %f\n", (t2 - t1));
}

int main(int argc) {
  init ();
  seq (); printf ("\n"); par ();
  pthread_exit(NULL);
}
---------------------------------------------------------------------------

The C-implementation for OCaml:
---------------------------------------------------------------------------
#include <stdio.h>
#include <pthread.h>

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>

value test_init () {
  thr_setconcurrency(sysconf(_SC_NPROCESSORS_ONLN));
  return Val_unit;
}

value test_calc () {
  int i;

  enter_blocking_section();

    for (i=0; i<50000000; i++);

  leave_blocking_section();

  return Val_unit;
}
---------------------------------------------------------------------------

The OCaml-file to be used with the C-file right above:
---------------------------------------------------------------------------
open Printf
open Unix

external init : unit -> unit = "test_init" "noalloc"
external calc : unit -> unit = "test_calc" "noalloc"

let _ = init ()

let seq () =
  printf "Running sequentially:\n";

  let t1 = gettimeofday () in
    calc (); calc ();
  let t2 = gettimeofday () in

  printf "Wall clock time: %f\n" (t2 -. t1)

let par () =
  printf "Running in parallel:\n";

  let t1 = gettimeofday () in
    let tids = List.map (Thread.create calc) [(); ()] in
    List.iter Thread.join tids;
  let t2 = gettimeofday () in

  printf "Wall clock time: %f\n" (t2 -. t1)

let _ = seq (); print_newline (); par ()
---------------------------------------------------------------------------

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-06-21 13:43 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-15 16:49 Markus Mottl
2001-06-15 17:10 ` Xavier Leroy
2001-06-15 18:42   ` Markus Mottl
2001-06-15 21:33     ` Markus Mottl
2001-06-21 13:43       ` Markus Mottl [this message]
2003-02-20 23:10 Shivkumar Chandrasekaran
2003-02-21  0:15 ` Jacques Garrigue
2003-02-21  4:24   ` shivkumar chandrasekaran
2003-02-21 10:43     ` Markus Mottl
2003-02-21 15:11     ` Jacques Garrigue
2003-02-21 17:57       ` Markus Mottl
2003-02-24 17:39       ` Shivkumar Chandrasekaran
2003-02-21  0:32 ` Chris Uzdavinis
2003-02-21  1:56   ` james woodyatt
2003-02-21  4:43   ` shivkumar chandrasekaran

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010621154316.A18064@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=Xavier.Leroy@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox