1 // Copyright (c) 2005,2010,2012  John Abbott
2 // This file is part of the CoCoALib suite of examples.
3 // You are free to use any part of this example in your own programs.
4 
5 #include "CoCoA/library.H"
6 
7 using namespace std;
8 
9 //----------------------------------------------------------------------
10 const string ShortDescription =
11   "Program comparing memory allocators for GMP values; compares speed.  \n";
12 
13 const string LongDescription =
14   "This example shows the various ways of specifying the memory manager to  \n"
15   "be used by the GMP library.  The choice of memory manager is indicated as\n"
16   "an argument to the GlobalManager constructor.  Here we illustrate four   \n"
17   "choices: the CoCoALib default, the system allocator, the specialized     \n"
18   "GMPAllocator (with and without explicit indication of the slice size).   \n"
19   "The program measures the speed of computation with these various choices.\n";
20 //----------------------------------------------------------------------
21 
22 namespace CoCoA
23 {
24 
25 // This function counts the number of iterations of the 3n+1 sequence
26 // are needed to reach 1 (the first time) starting from N.  We chose
27 // this example because it performs many operations on smallish values.
NumIters(BigInt N)28   long NumIters(BigInt N)
29   {
30     N = abs(N); // ignore sign of N
31     long iters = 0;
32     while (N > 1)
33     {
34       if (IsEven(N)) N /= 2;  // the defining relation for the sequence
35       else N = 3*N+1;         //
36       ++iters;
37     }
38     return iters;
39   }
40 
41 
computation()42   void computation()
43   {
44     // Assumes GlobalManager has already been created.
45 
46     const int Nmax = 123456;
47     long MaxIters = 0;
48     for (BigInt N(3); N <= Nmax; N += 2)
49     {
50       const long iters = NumIters(N);
51       if (iters > MaxIters)
52       {
53         MaxIters = iters;
54       }
55     }
56 //  cout << "The longest sequence had length " << MaxIters << endl;
57   }
58 
program()59   void program()
60   {
61     cout << ShortDescription << endl;
62 
63     {
64       // Computation using CoCoALib's default choice of mem mgr for GMP values...
65       GlobalManager CoCoAFoundations;
66       cout << "Using CoCoALib's default mem mgr for GMP ..." << endl;
67       const double t0 = CpuTime();
68       computation();
69       cout << "... time was " << CpuTime()-t0 << endl;
70     }
71 
72     {
73       // Computation using system allocator as mem mgr for GMP values...
74       GlobalManager CoCoAFoundations(UseSystemAllocatorForGMP);
75       cout << "Using standard system allocator mem mgr for GMP ..." << endl;
76       const double t0 = CpuTime();
77       computation();
78       cout << "... time was " << CpuTime()-t0 << endl;
79     }
80 
81     {
82       // Computation using GMPAllocator as mem mgr for GMP values...
83       GlobalManager CoCoAFoundations(UseGMPAllocator);
84       cout << "Using GMPAllocator as mem mgr for GMP ..." << endl;
85       const double t0 = CpuTime();
86       computation();
87       cout << "... time was " << CpuTime()-t0 << endl;
88     }
89 
90     {
91       // Computation using GMPAllocator(slice_size) as mem mgr for GMP values...
92       GlobalManager CoCoAFoundations(UseGMPAllocator(256));
93       cout << "Using GMPAllocator (with slice = 256 bytes) as mem mgr for GMP ..." << endl;
94       const double t0 = CpuTime();
95       computation();
96       cout << "... time was " << CpuTime()-t0 << endl;
97     }
98 
99   }
100 
101 } // end of namespace CoCoA
102 
103 //----------------------------------------------------------------------
104 // Use main() to handle any uncaught exceptions and warn the user about them.
main()105 int main()
106 {
107   try
108   {
109     CoCoA::program();
110     return 0;
111   }
112   catch (const CoCoA::ErrorInfo& err)
113   {
114     cerr << "***ERROR***  UNCAUGHT CoCoA error";
115     ANNOUNCE(cerr, err);
116   }
117   catch (const std::exception& exc)
118   {
119     cerr << "***ERROR***  UNCAUGHT std::exception: " << exc.what() << endl;
120   }
121   catch(...)
122   {
123     cerr << "***ERROR***  UNCAUGHT UNKNOWN EXCEPTION" << endl;
124   }
125 
126   CoCoA::BuildInfo::PrintAll(cerr);
127   return 1;
128 }
129 
130 //----------------------------------------------------------------------
131 // RCS header/log in the next few lines
132 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/examples/ex-GMPAllocator2.C,v 1.9 2015/11/04 10:14:09 abbott Exp $
133 // $Log: ex-GMPAllocator2.C,v $
134 // Revision 1.9  2015/11/04 10:14:09  abbott
135 // Summary: Made example faster
136 //
137 // Revision 1.8  2015/06/26 15:34:48  abbott
138 // Summary: Moved code into namespace CoCoA (see redmine 739)
139 // Author: JAA
140 //
141 // Revision 1.7  2012/11/30 15:21:19  abbott
142 // Improved a printed message.
143 //
144 // Revision 1.6  2012/11/30 15:16:07  abbott
145 // Replaced use of flush by a simple endl.
146 //
147 // Revision 1.5  2012/10/15 12:33:37  abbott
148 // Improved short/long descriptions.
149 // Removed a little cruft.
150 // Added some comments.
151 //
152 // Revision 1.4  2012/05/04 20:01:19  abbott
153 // Minor modifications to reduce execution time (usu. by reducing number of iterations).
154 //
155 // Revision 1.3  2011/08/23 12:04:04  bigatti
156 // -- updated after renaming ZZ --> BigInt
157 //
158 // Revision 1.2  2010/12/17 16:07:55  abbott
159 // Ensured that all i/o in examples is on standard C++ streams
160 // (rather than GlobalInput(), etc).
161 //
162 // Revision 1.1  2010/10/22 09:15:41  abbott
163 // New GMPAllocator example.
164 //
165 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
166 // Imported files
167 //
168 // Revision 1.5  2007/03/07 15:45:42  cocoa
169 // GMPAllocator must be created before GlobalManager, otherwise bus error.
170 //
171 // Revision 1.4  2007/03/03 14:15:45  bigatti
172 // -- "foundations" renamed into "GlobalManager"
173 //
174 // Revision 1.3  2007/02/12 15:29:07  bigatti
175 // -- added strings ShortDescription and LongDescription for indexing
176 //
177 // Revision 1.2  2007/02/10 18:44:04  cocoa
178 // Added "const" twice to each test and example.
179 // Eliminated dependency on io.H in several files.
180 // Improved BuildInfo, and added an example about how to use it.
181 // Some other minor cleaning.
182 //
183 // Revision 1.1.1.1  2006/05/30 11:39:36  cocoa
184 // Imported files
185 //
186 // Revision 1.1.1.1  2005/10/17 10:46:53  cocoa
187 // Imported files
188 //
189 // Revision 1.1.1.1  2005/05/03 15:47:30  cocoa
190 // Imported files
191 //
192 // Revision 1.1  2005/04/29 15:42:02  cocoa
193 // Improved documentation for GMPAllocator.
194 // Added example program for GMPAllocator.
195 // Added example program for simple ops on polynomials.
196 // Added two new ctors for (principal) ideals (from long, and from BigInt).
197 // Added (crude) printing for PPMonoids.
198 // Updated library.H (#included GMPAllocator.H).
199 //
200 // Revision 1.3  2005/04/27 16:14:56  cocoa
201 // Cleaned up example programs -- added "free use" permit.
202 // Changed a couple of ErrorInfo object names, and added
203 // ERR::NotTrueGCDDomain.
204 //
205 // Revision 1.2  2005/04/21 15:12:19  cocoa
206 // Revised NewPolyRing as Dag Arneson suggested (perhaps just an interim
207 // measure).
208 // Brought example programs up to date (new name for CoCoA error
209 // information objects).
210 //
211 // Revision 1.1.1.1  2005/01/27 15:12:13  cocoa
212 // Imported files
213 //
214 // Revision 1.3  2004/12/09 15:08:42  cocoa
215 // -- added log info
216 //
217