summaryrefslogtreecommitdiff
path: root/examples/threads.c
blob: 4fccd1a21b77fb2d1dbd12220f5532453b4aa36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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;
}