summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2018-07-20 13:09:20 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2018-07-20 13:09:20 +0000
commit43aba257b9a0b97fbfb1acf2a00f3df3e5b05973 (patch)
treede348b965691e4593f8a5e14e3678ba9047a67fb /examples
parent75758f1b4b350e120b792f06e9cda9e1cbd9a0bd (diff)
downloadmpfr-43aba257b9a0b97fbfb1acf2a00f3df3e5b05973.tar.gz
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
Diffstat (limited to 'examples')
-rw-r--r--examples/threads.c90
1 files changed, 90 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include <mpfr.h>
+
+#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;
+}