/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this ATS program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0001.221.234,6041065  0% 0% 0% 100%
5,000,00011.9311.944,6041065  1% 0% 0% 100%
50,000,000120.31120.374,6041065  0% 0% 0% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read thread-ring benchmark to see what this program should do.

 notes

ATS/Anairiats version 0.2.9

 thread-ring ATS program source code

(*
** The Computer Language Benchmarks Game
** http://benchmarksgame.alioth.debian.org/
**
** contributed by Hongwei Xi
**
** compilation command:
**   atscc -O3 -fomit-frame-pointer thread-ring.dats -o thread-ring -lpthread
*)

%{^

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <limits.h>

#define NTHREAD (503)

static pthread_mutex_t mutex0 ;
static pthread_mutex_t mutexarr[NTHREAD] ;
typedef struct { char _[PTHREAD_STACK_MIN] ; } threadstack ;
static threadstack threadstackarr[NTHREAD] ;

static inline
ats_void_type lock_acquire (ats_int_type i) {
  if (i == 0) {
    pthread_mutex_lock (&mutex0) ;
  } else {
    pthread_mutex_lock (&mutexarr[i-1]) ;
  }
  return ;
}

static inline
ats_void_type lock_release (ats_int_type i) {
  if (i == 0) {
    pthread_mutex_unlock (&mutex0) ;
  } else {
    pthread_mutex_unlock (&mutexarr[i-1]) ;
  }
  return ;
}

%}

#define NTHREAD (503)

%{^

static ats_int_type the_answer = 0 ;

static inline
ats_int_type the_answer_get () { return the_answer ; }

static inline
ats_void_type the_answer_set (ats_int_type n) { the_answer = n ; return ; }

/* ****** ****** */

static ats_int_type the_token = 0 ;

static inline
ats_int_type the_token_get () { return the_token ; }

static inline
ats_void_type the_token_set (ats_int_type n) { the_token = n ; return ; }

%}

absview ticket_v

extern fun the_answer_get (pf: !ticket_v | (*nil*)): int = "the_answer_get"
extern fun the_answer_set (pf: !ticket_v | n: int): void = "the_answer_set"

extern fun the_token_get (pf: !ticket_v | (*nil*)): int = "the_token_get"
extern fun the_token_set (pf: !ticket_v | n: int): void = "the_token_set"

(* ****** ****** *)

extern fun lock_acquire (i: natLte NTHREAD): (ticket_v | void)
  = "lock_acquire"

extern fun lock_release (pf: ticket_v | i: natLte NTHREAD): void
  = "lock_release"

(* ****** ****** *)

extern fun fthread
  {i:int | 1 <= i; i <= NTHREAD} (i: int i): void = "fthread"

implement fthread (i) = let
(*
  val () = (prerr "fthread: i = "; prerr i ; prerr_newline ())
*)
  val (pf | ()) = lock_acquire (i)
  val n = the_token_get (pf | (*nil*))
(*
  val () = (prerr "fthread: n = "; prerr n ; prerr_newline ())
*)
  val i1 = i + 1
  val i1 = (if i1 <= NTHREAD then i1 else 1): natLte NTHREAD
in
  if n > 0 then let
    val () = the_token_set (pf | n - 1); val () = lock_release (pf | i1)
  in
    fthread (i)
  end else begin
    the_answer_set (pf | i); lock_release (pf | 0)
  end // end of [if]
end // end of [fthread]

(* ****** ****** *)

extern fun initialization (n: Nat): (ticket_v | void) = "initialization"

(* ****** ****** *)

implement main (argc, argv) = let
  val () = assert (argc >= 2)
  val n = int1_of_string (argv.[1])
  val () = assert (n >= 0)
  val (pf | ()) = initialization (n)
  val () = lock_release (pf | 1)
  val (pf | ()) = lock_acquire (0)
  val ans = the_answer_get (pf | (*nil*))
  prval () = __leak (pf) where {
    extern prfun __leak (pf: ticket_v): void
  }
in
  print ans; print_newline ()
end // end of [main]

(* ****** ****** *)

%{$

ats_void_type initialization (ats_int_type n) {
  int i;
  pthread_t tid ;
  pthread_mutex_t *p_mutex ;
  pthread_attr_t thread_attr ;
  threadstack *p_threadstack ;

  the_token = n ;

  pthread_mutex_init (&mutex0, NULL) ;
  pthread_mutex_lock (&mutex0) ;

  pthread_attr_init (&thread_attr) ;

  i = 0 ;
  p_mutex = &mutexarr[0] ;
  p_threadstack = &threadstackarr[0] ;
  while (i < NTHREAD) {
    pthread_mutex_init (p_mutex, NULL); pthread_mutex_lock (p_mutex);
    pthread_attr_setstack (&thread_attr, p_threadstack, sizeof(threadstack)) ;
    pthread_create(&tid, &thread_attr, (void* (*)(void*))fthread, (void*)(intptr_t)(i+1)) ;
    i += 1 ; p_mutex += 1 ; p_threadstack += 1 ;
  }

  fprintf (stderr, "PTHREAD_STACK_MIN = %i\n", PTHREAD_STACK_MIN) ;
  return ;
} // end of [initialization]

%}

(* ****** ****** *)

(* end of [thread-ring] *)

 make, command-line, and program output logs

Wed, 23 Jan 2013 00:11:51 GMT

MAKE:
/usr/local/src/ats-lang-anairiats-0.2.9/bin/atscc  -pipe -Wall -O3 -fomit-frame-pointer -march=native -pthread threadring.dats -o threadring.ats_run 
/usr/local/src/ats-lang-anairiats-0.2.9/bin/atsopt --output threadring_dats.c --dynamic threadring.dats
In file included from threadring_dats.c:19:0:
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/basics.cats: In function ‘atspre_fprint_newline’:
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/basics.cats:271:11: warning: variable ‘n2’ set but not used [-Wunused-but-set-variable]
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/basics.cats:271:7: warning: variable ‘n1’ set but not used [-Wunused-but-set-variable]
threadring_dats.c: In function ‘mainats’:
threadring_dats.c:204:1: warning: label ‘__ats_lab_mainats’ defined but not used [-Wunused-label]
In file included from threadring_dats.c:29:0:
threadring_dats.c: At top level:
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/pointer.cats:52:14: warning: ‘atspre_null_ptr’ defined but not used [-Wunused-variable]
In file included from threadring_dats.c:30:0:
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/printf.cats:57:1: warning: ‘atspre_fprintf_err’ defined but not used [-Wunused-function]
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/printf.cats:69:1: warning: ‘atspre_fprintf_exn’ defined but not used [-Wunused-function]
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/printf.cats:87:1: warning: ‘atspre_printf_exn’ defined but not used [-Wunused-function]
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/printf.cats:105:1: warning: ‘atspre_prerrf_exn’ defined but not used [-Wunused-function]
In file included from threadring_dats.c:33:0:
/usr/local/src/ats-lang-anairiats-0.2.9/prelude/CATS/string.cats:351:14: warning: ‘atspre_stropt_none’ defined but not used [-Wunused-variable]
gcc -I/usr/local/src/ats-lang-anairiats-0.2.9/ -I/usr/local/src/ats-lang-anairiats-0.2.9/ccomp/runtime/ -L/usr/local/src/ats-lang-anairiats-0.2.9/ccomp/lib64/ /usr/local/src/ats-lang-anairiats-0.2.9/ccomp/runtime/ats_prelude.c -pipe -Wall -O3 -fomit-frame-pointer -march=native -pthread threadring_dats.c -o threadring.ats_run -lats 
rm threadring.dats
0.32s to complete and log all make actions

COMMAND LINE:
./threadring.ats_run 50000000

PROGRAM OUTPUT:
292

PTHREAD_STACK_MIN = 16384

Revised BSD license

  Home   Conclusions   License   Play