source: git/ntl/doc/tour-gmp.html @ 26e030

spielwiese
Last change on this file since 26e030 was 26e030, checked in by Hans Schönemann <hannes@…>, 14 years ago
*hannes: update to 5.5.1 git-svn-id: file:///usr/local/Singular/svn/trunk@11949 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.7 KB
Line 
1<html>
2<head>
3<title>
4A Tour of NTL: Using NTL with GMP  </title>
5</head>
6
7<body bgcolor="#fff9e6">
8
9<center>
10<a href="tour-impl.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
11 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
12<a href="tour-gf2x.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
13</center>
14
15<h1> 
16<p align=center>
17A Tour of NTL: Using NTL with GMP
18</p>
19</h1>
20
21<p> <hr> <p>
22
23GMP is the GNU Multi-Precision library.
24You can get more information about it, as well as the latest version
25from <a href="http://gmplib.org">here.</a>
26
27<p>
28
29Briefly, GMP is a library for long integer arithmetic.
30It has hand-crafted assembly routines for a wide variety
31of architectures.
32For basic operations, like integer multiplication, it can be
33two to three (and sometimes bit more) times faster than NTL's
34traditional long integer package.
35The speedup is most dramatic on x86 machines.
36
37<p>
38You can choose one of three different ways of implementing
39long integer arithmetic in NTL:
40<p>
41<ol>
42<li>
43One can use the traditional NTL long integer arithmtic package,
44and avoid dealing with GMP entirely.
45
46<p>
47<li>
48One can use traditional NTL long integer arithmtic package,
49but with GMP as a <i>supplemental</i> long integer package.
50This gives you many (though not all) of the performance benefits of GMP,
51but while still maintaining complete backward compatability.
52
53<p>
54<li>
55One can use GMP as the primary long integer package.
56This gives you essentially all of the performance benefits
57of GMP, but there are some minor backward incompatabilities
58<a href="#compat">(see below)</a>.
59
60</ol>
61
62<p>
63
64<p>
65The use of GMP as the primary long integer package
66is the preferred method of using GMP.
67The use of GMP as a supplemental long integer package is intended
68primarily for backward compatability.
69
70<p>
71Building NTL with GMP (either as a supplemental or primary
72long integer package) takes a few extra minutes work,
73and you certainly do not need to use NTL with GMP if you don't want to.
74As far as I know, GMP is only available on Unix systems
75and on Windows systems using Cygwin tools.
76
77<p>
78Even if you do not use GMP as the primary long integer package,
79you should still read the <a href="#compat">section below
80on backward compatabilty</a>
81so that you can write portable code and avoid deprecated constructs.
82
83<p>
84<h2>
85Downloading and building GMP
86</h2>
87<p>
88
89Download GMP from <a href="http://gmplib.org">here.</a>
90You will get a file <tt>gmp-XXX.tar.gz</tt>.
91<p>
92Now do the following:
93<pre>
94   % gunzip gmp-XXX.tar.gz
95   % tar xf gmp-XXX.tar
96   % cd gmp-XXX
97   % ./configure --prefix=$HOME/sw
98   % make
99   % make check
100   % make install
101</pre>
102This will build, test, and install GMP in <tt>$HOME/sw</tt>.
103Of course, change $HOME/sw to whatever you want (the default is
104<tt>/usr/local</tt>).
105You will find the GMP header files in <tt>$HOME/sw/include</tt> 
106and the compiled binaries in <tt>$HOME/sw/lib</tt>.
107
108<p>
109You can also supply the option
110<tt>--disable-shared</tt> to the <tt>configure</tt> script,
111if you only want static libraries.
112However, if you ultimately want to build NTL as a shared
113library, then you must also buld GMP as a shared library.
114
115<p>
116You must ensure that NTL and GMP have the same
117<a href="tour-unix.html#abi">ABI</a>.
118Usually, GMP's configure script will automatically
119choose a 64-bit ABI if available.
120
121
122<p>
123<h2>
124Building and using NTL with GMP
125</h2>
126<p>
127
128When building NTL with GMP, you have to tell NTL that you want to
129use GMP as the primary long integer package,
130and where the include files and library are.
131The easiest way to do this is by passing the argument
132<tt>NTL_GMP_LIP=on</tt> to the NTL configuration script
133when you are <a href="tour-unix.html">installing NTL</a>.
134Assuming you installed GMP in <tt>$HOME/sw</tt> as above,
135and you also want to install NTL in <tt>$HOME/sw</tt>,
136you execute:
137<pre>
138   % ./configure PREFIX=$HOME/sw NTL_GMP_LIP=on  GMP_PREFIX=$HOME/sw
139</pre>
140You can write this more simply as
141<pre>
142   % ./configure DEF_PREFIX=$HOME/sw NTL_GMP_LIP=on
143</pre>
144Here, <tt>DEF_PREFIX</tt> is a variable that is used
145to specify the location of all software,
146and it defaults to <tt>/usr/local</tt>.
147
148
149<p>
150If you installed GMP in <tt>/usr/local</tt> (or some other
151standard system directory where your compiler will look by default)
152then simply
153<pre>
154   % ./configure PREFIX=$HOME/sw NTL_GMP_LIP=on
155</pre>
156does the job.
157Moreover, if NTL is also to be installed in <tt>/usr/local</tt>,
158then
159<pre>
160   % ./configure NTL_GMP_LIP=on
161</pre>
162does the job.
163
164<p>
165If instead you want to use GMP as a supplemental long integer package,
166you should pass the argument <tt>NTL_GMP_HACK=on</tt> to the configure script,
167instead of <tt>NTL_GMP_LIP=on</tt>.
168One still has to specify where to find GMP using the <tt>GMP_PREFIX</tt>
169variable in the configuration script.
170
171<p>
172Instead of passing arguments to the configure script,
173you can also just edit the <tt>config.h</tt> and <tt>makefile</tt> by hand.
174The documentation in these files should be self-explanatory.
175
176
177<p>
178When compiling programs that use NTL with GMP,
179you need to link with the GMP library.
180If GMP is installed as above in
181<tt>$HOME/sw</tt>, rather than in a standard system directory,
182 this just means adding
183<tt>-L$HOME/sw/lib -lgmp</tt> to the compilation command.
184If you installed GMP in a standard system directory,
185then just <tt>-lgmp</tt> does the job.
186Note that <tt>-lgmp</tt> must come <i>after</i> <tt>-lntl</tt>
187on the command line.
188Finally, if NTL and GF2X are installed as
189shared libraries, then you don't even need <tt>-lgmp</tt>.
190
191
192<p>
193NTL has been tested and works correctly with GMP versions 3.1,  3.1.1,
194and 4.1.4 (among others)
195as the primary long integer package.
196It is not possible to use versions of GMP prior to 3.1 as the
197primary long integer package.
198
199<p>
200
201NTL has been tested and works correctly
202with versions 2.0.2, 3.0.1, 3.1, 3.1.1, and 4.1.4
203(among otheers) of GMP as a supplemental
204long integer package.
205It is not recommended to use versions of GMP prior to 3.1.1,
206as these are generally more buggy and less efficient.
207
208<p>
209When using NTL with GMP as either primary or supplemental
210long integer package, as a user of NTL, you do not need to
211know or understand anything about the the GMP library.
212So while there is detailed documentation available about how
213to use GMP, you do not have to read it.
214The programming interface to the long integer package
215completely hides implementation details.
216
217
218
219
220<p>
221<h2>
222Some implementation details
223</h2>
224<p>
225
226When using GMP as the primary long integer package,
227the code used by NTL is essentially a layer of <tt>C</tt> routines
228that call the low level <tt>mpn</tt> routines in the GMP package.
229These NTL wrapper routines provide essentially the same
230functionality of the higher level <tt>mpz</tt> routines in GMP,
231but while presenting an interface to the rest of NTL that is almost identical
232to that of the
233traditional NTL long integer package.
234There are, however, some very minor backward incompatabilities
235<a href="#compat">(see below)</a>.
236
237<p>
238When using GMP as a supplemental long integer package,
239the code employs
240a "quick and dirty", yet fairly effective hack.
241This quick and dirty
242approach converts "on the fly"
243between the classic LIP and GMP representations.
244This makes the use of GMP <i>completely</i> invisible to higher layer software.
245Of course, there is a penalty: converting between representations takes
246time.
247For operations like addition, conversion would take longer
248than performing the operation, and so it is not done.
249However, for computationally expensive
250operations like multiplication, the "overhead" is not so bad,
251at least for numbers that are not too small.
252To multiply two 256-bit numbers on a Pentium-II, the extra time
253required for the data conversions is just 35% of the time to
254do the multiplication in GMP, i.e., the "overhead" is 35%.
255For 512-bit numbers, the corresponding opportunity cost is about 14%,
256and for 1024-bit numbers, it is less than 10%.
257For smaller numbers, the opportunity cost is greater, but
258never much worse than about 50%.
259
260<p>
261<h2>
262<a name="compat">
263Backward compatbility
264</a>
265</h2>
266<p>
267
268With version 5.0 of NTL, some aspects of the programming interface
269are 'deprecated' so as to allow the use of another long integer package,
270such as GMP, as the primary long integer package.
271
272<p>
273Prior to version 5.0, the macro <tt>NTL_NBITS</tt> was defined,
274along with the macro <tt>NTL_RADIX</tt> defined to be
275<tt>(1L &lt;&lt; NTL_NBITS)</tt>.
276While these macros are still available when using NTL's traditional
277long integer package (i.e., when <tt>NTL_GMP_LIP</tt> is not set),
278they are not available when using the GMP as the primary long integer
279package (i.e., when <tt>NTL_GMP_LIP</tt> is set).
280Furthermore, when writing portable programs, one should avoid these macros.
281
282<p>
283
284
285Also, the static function <tt>long ZZ::digit(const ZZ &amp;);</tt>
286is defined when using traditional long integer arithmetic,
287but is not available when using GMP as the primary long integer package,
288and in any case, its use should be avoided when writing portable programs.
289
290
291<p>
292Instead of the above macros, one should use the followng macros:
293
294<pre>
295   NTL_ZZ_NBITS -- number of bits in a zzigit;
296                   a ZZ is represented as a sequence of zzigits.
297
298   NTL_SP_NBITS -- max number of bits in a "single-precision" number
299
300   NTL_WSP_NBITS -- max number of bits in a "wide single-precision" number
301</pre>
302<p>
303The following relations hold:
304<pre>
305   NTL_SP_NBITS &lt;= NTL_WSP_NBITS &lt;= NTL_ZZ_NBITS
306   26 &lt;= NTL_SP_NBITS &lt;= min(NTL_BITS_PER_LONG-2, NTL_DOUBLE_PRECISION-3)
307   NTL_WSP_NBITS &lt;= NTL_BITS_PER_LONG-2
308</pre>
309
310<p>
311
312Note that <tt>NTL_ZZ_NBITS</tt> may be less than, equal to, or greater than
313<tt>NTL_BITS_PER_LONG</tt>  -- no particular relationship
314should be assumed to hold.
315In particular, expressions like <tt>(1L &lt;&lt; NTL_ZZ_BITS)</tt>
316might overflow.
317
318<p>
319"single-precision" numbers are meant to be used in conjunction with the
320single-precision modular arithmetic routines.
321
322<p>
323"wide single-precision" numbers are meant to be used in conjunction
324with the <tt>ZZ</tt> arithmetic routines for optimal efficiency.
325
326<p>
327Note that when using traditional long integer arithmetic, we have
328<pre>
329    NTL_ZZ_NBITS = NTL_SP_NBITS = NTL_WSP_NBITS = NTL_NBITS.
330</pre>
331
332<p>
333The following auxilliary macros are also defined:
334
335<pre>
336NTL_FRADIX -- double-precision value of <tt>2^NTL_ZZ_NBITS</tt>
337NTL_SP_BOUND -- (1L &lt;&lt; NTL_SP_NBITS)
338NTL_WSP_BOUND -- (1L &lt;&lt; NTL_WSP_NBITS)
339</pre>
340
341<p>
342
343Note that for a <tt>ZZ</tt> <tt>n</tt>,
344<tt>n.size()</tt> returns the number of "zzigits" of <tt>n</tt>.
345This is supported with either traditional or GMP integer arithemtic.
346Note, however, that some old codes might write <tt>n.size() &lt;= 1</tt>
347as a way to test if <tt>NumBits(n) &lt;= NTL_NBITS</tt>.
348This is no longer the right thing to do, if one wants portable code
349that works with either traditional or GMP long integer arithmetic.
350First, one has to decide whether one wants to test if
351<tt>NumBits(n)</tt> is bounded by <tt>NTL_ZZ_NBITS</tt>,
352<tt>NTL_SP_NBITS</tt>, or <tt>NTL_WSP_NBITS</tt>.
353In the first case, <tt>n.size() &lt;= 1</tt> is still
354the right way to test this.
355In the second case, write this as <tt>n.SinglePrecision()</tt>.
356In the third case, write this as <tt>n.WideSinglePrecision()</tt>.
357The routines <tt>SinglePrecision</tt> and <tt>WideSinglePrecision</tt>
358are new to NTL version 5.0.
359
360<p>
361
362Most "high level" applications that use NTL should not be affected
363by these changes to NTL's programming interface, and if they are,
364changing the programs should be quite easy.
365
366
367<p> <hr> <p>
368
369
370<p>
371
372<center>
373<a href="tour-impl.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
374 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
375<a href="tour-gf2x.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
376</center>
377
378
379
380</body>
381</html>
Note: See TracBrowser for help on using the repository browser.