-
Notifications
You must be signed in to change notification settings - Fork 0
/
kara-profile.c
151 lines (114 loc) · 3.65 KB
/
kara-profile.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*============================================================================
This file is part of FLINT.
FLINT 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 2 of the License, or
(at your option) any later version.
FLINT 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 FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
kara-profile.c
Comparative profiling for karatsuba multiplication
Copyright (C) 2007, William Hart and David Harvey
*****************************************************************************/
#include "profiler.h"
#include "flint.h"
#include "test-support.h"
#include "mpz_poly.h"
#include "fmpz_poly.h"
#include <stdio.h>
typedef struct
{
unsigned long length1, length2, bits1, bits2;
int which; // 0 for mpz_poly, 1 for fmpz_poly
} arg_t;
void target(void* y, unsigned long count)
{
arg_t* arg = (arg_t*) y;
mpz_t x;
mpz_init(x);
mpz_poly_t in1, in2, out;
mpz_poly_init(in1);
mpz_poly_init(in2);
mpz_poly_init(out);
fmpz_poly_t in1f, in2f, outf;
fmpz_poly_init2(in1f, arg->length1, (arg->bits1-1)/FLINT_BITS+1);
fmpz_poly_init2(in2f, arg->length2, (arg->bits2-1)/FLINT_BITS+1);
_fmpz_poly_stack_init(outf, arg->length1 + arg->length2 - 1,
in1f->limbs + in2f->limbs + 1);
unsigned long i;
for (i = 0; i < arg->length1; i++)
{
mpz_urandomb(x, randstate, arg->bits1);
mpz_poly_set_coeff(in1, i, x);
}
mpz_poly_to_fmpz_poly(in1f, in1);
unsigned long i;
for (i = 0; i < arg->length2; i++)
{
mpz_urandomb(x, randstate, arg->bits2);
mpz_poly_set_coeff(in2, i, x);
}
mpz_poly_to_fmpz_poly(in2f, in2);
start_clock(0);
if (arg->which)
{
unsigned long i;
for (i = 0; i < count; i++)
_fmpz_poly_mul_karatsuba(outf, in1f, in2f);
}
else
{
unsigned long i;
for (i = 0; i < count; i++)
mpz_poly_mul_karatsuba(out, in1, in2);
}
stop_clock(0);
_fmpz_poly_stack_clear(outf);
fmpz_poly_clear(in2f);
fmpz_poly_clear(in1f);
mpz_poly_clear(out);
mpz_poly_clear(in2);
mpz_poly_clear(in1);
mpz_clear(x);
}
/*
command line arguments are:
length1, length2, bits1, bits2
*/
int main(int argc, char* argv[])
{
if (argc != 5)
{
printf("expected four arguments (see source)\n");
return 0;
}
test_support_init();
arg_t arg;
arg.length1 = atoi(argv[1]);
arg.length2 = atoi(argv[2]);
arg.bits1 = atoi(argv[3]);
arg.bits2 = atoi(argv[4]);
double min_time, max_time;
unsigned long i;
for (i = 0; i < 3; i++)
{
arg.which = 0;
prof_repeat(&min_time, &max_time, target, &arg);
printf(" mpz_poly: min = %.3le, \tmax = %.3le\n", min_time, max_time);
fflush(stdout);
arg.which = 1;
prof_repeat(&min_time, &max_time, target, &arg);
printf("fmpz_poly: min = %.3le, \tmax = %.3le\n", min_time, max_time);
fflush(stdout);
}
test_support_cleanup();
return 0;
}
// end of file ****************************************************************