From 43aba257b9a0b97fbfb1acf2a00f3df3e5b05973 Mon Sep 17 00:00:00 2001 From: vlefevre Date: Fri, 20 Jul 2018 13:09:20 +0000 Subject: Added examples/threads.c (and updated Makefile.am): multithreading test to detect scaling issues with MPFR. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@12943 280ebfd0-de03-0410-8827-d642c229c3f4 --- examples/threads.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/threads.c (limited to 'examples') diff --git a/examples/threads.c b/examples/threads.c new file mode 100644 index 000000000..4fccd1a21 --- /dev/null +++ b/examples/threads.c @@ -0,0 +1,90 @@ +/* Multithreading test to detect scaling issues with MPFR. + +Define: + * the function F; + * the precision PREC; + * the value V as an expression that will have the type double + (it may depend on the thread number i). + +Example: + gcc threads.c -lmpfr -lgmp -lpthread -DF=mpfr_sin -DPREC=200 -DV=100 + +Copyright 2018 Free Software Foundation, Inc. +Contributed by the AriC and Caramba projects, INRIA. + +This file is part of the GNU MPFR Library. + +The GNU MPFR Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +The GNU MPFR Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see +http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., +51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include + +#include + +#define MAX_THREADS 256 + +static int m; + +static void *start_routine (void *arg) +{ + mpfr_t x, y; + int i = *(int *) arg, j; + + (void) i; /* avoid a warning if i is not used by V */ + + mpfr_inits2 (PREC, x, y, (mpfr_ptr) 0); + mpfr_set_d (x, (V), MPFR_RNDN); + + for (j = 0; j < m; j++) + F (y, x, MPFR_RNDN); + + mpfr_clears (x, y, (mpfr_ptr) 0); + pthread_exit (NULL); +} + +int main (int argc, char *argv[]) +{ + int i, n; + pthread_t tid[MAX_THREADS]; + + if (argc != 3 || + (m = atoi (argv[1]), m < 1) || + (n = atoi (argv[2]), n < 1 || n > MAX_THREADS)) + { + fprintf (stderr, "Usage: %s <#iterations> <#threads>\n", argv[0]); + exit (1); + } + + printf ("%d iteration(s), %d thread(s).\n", m, n); + + for (i = 0; i < n; i++) + if (pthread_create (&tid[i], NULL, start_routine, &i) != 0) + { + fprintf (stderr, "%s: failed to create thread %d\n", argv[0], i); + exit (1); + } + + for (i = 0; i < n; i++) + if (pthread_join (tid[i], NULL) != 0) + { + fprintf (stderr, "%s: failed to join thread %d\n", argv[0], i); + exit (1); + } + + return 0; +} -- cgit v1.2.1