File: ecmfactor.c

package info (click to toggle)
gmp-ecm 7.0.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,724 kB
  • ctags: 4,790
  • sloc: asm: 36,431; ansic: 34,057; xml: 885; python: 799; sh: 698; makefile: 356
file content (119 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ecmfactor.c - example of use of the ECM library.

Copyright 2005-2016 Paul Zimmermann, Dave Newman, Pierrick Gaudry.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

This program 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 General Public License for
more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  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 <string.h>
#include <pthread.h>
#include <gmp.h> /* GMP header file */
#include <ecm.h> /* ecm header file */

/* thread structure */
typedef struct
{
  mpz_t n;        /* number to factor */
  double B1;      /* stage-1 bound */
  mpz_t f;        /* potential factor */
  int ret;        /* return value */
  ecm_params q;
} __tab_struct;
typedef __tab_struct tab_t[1];

void*
one_thread (void *args)
{
  tab_t *tab = (tab_t*) args;

  tab[0]->ret = ecm_factor (tab[0]->f, tab[0]->n, tab[0]->B1, tab[0]->q);
  return NULL;
}

int
main (int argc, char *argv[])
{
  mpz_t n;
  double B1;
  unsigned long nthreads = 1, i;
  tab_t *T;
  pthread_t *tid;

  if (argc >= 3 && strcmp (argv[1], "-t") == 0)
    {
      nthreads = strtoul (argv[2], NULL, 10);
      argc -= 2;
      argv += 2;
    }

  if (argc < 3)
    {
      fprintf (stderr, "Usage: ecmfactor [-t nnn] <number> <B1>\n");
      exit (1);
    }

  /* initialize tab_t for threads */
  T = malloc (nthreads * sizeof (tab_t));
  tid = malloc (nthreads * sizeof (pthread_t));

  mpz_init (n);

  /* read number on command line */
  if (mpz_set_str (n, argv[1], 10))
    {
      fprintf (stderr, "Invalid number: %s\n", argv[1]);
      exit (1);
    }

  B1 = atof (argv[2]);

  for (i = 0; i < nthreads ; i++)
    {
      mpz_init_set (T[i]->n, n);
      T[i]->B1 = B1;
      mpz_init (T[i]->f);
      ecm_init (T[i]->q);
    }

  printf ("Performing %lu curve(s) with B1=%1.0f\n", nthreads, B1);
  for (i = 0; i < nthreads; i++)
    pthread_create (&tid[i], NULL, one_thread, (void *) (T+i));
  for (i = 0; i < nthreads; i++)
    pthread_join (tid[i], NULL);

  for (i = 0; i < nthreads; i++)
    {
      gmp_printf ("thread %lu with sigma %d:%Zd ",
                  i, T[i]->q->param, T[i]->q->sigma);
      if (T[i]->ret > 0)
        gmp_printf ("found factor in step %u: %Zd\n", T[i]->ret, T[i]->f);
      else if (T[i]->ret == ECM_NO_FACTOR_FOUND)
        printf ("found no factor\n");
      else
        printf ("gave an error\n");
      mpz_clear (T[i]->n);
      mpz_clear (T[i]->f);
      ecm_clear (T[i]->q);
    }

  mpz_clear (n);

  free (T);
  free (tid);

  return 0;
}