COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
test.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <functional>
3#include <algorithm>
4#include <vector>
5#include <sstream>
6#include <sys/time.h> // for gettimeofday
7#include <tr1/tuple>
8#include "promote.h"
9#include "Semirings.h"
10#include "Deleter.h"
11#include <ext/numeric>
12
13using namespace std;
14#define TESTSIZE 100000000
15
16// accumulate with a function object or pointer
17template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
18_Tp fpaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
19 _BinaryOperation __binary_op)
20{
21 for (; __first != __last; ++__first)
22 __init = __binary_op(__init, *__first);
23 return __init;
24}
25
26// accumulate through a semiring
27template<typename SR, typename _InputIterator, typename _Tp>
28_Tp sraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
29{
30 for (; __first != __last; ++__first)
31 __init = SR::add(__init, *__first);
32 return __init;
33}
34
35
36// This is if you want something akin to covariant return types with CRTP
37template <class D, class B>
38class clonable: B
39{
40public:
41 D * clone () const
42 {
43 return static_cast <D*> (this-> do_clone ());
44 }
45private:
46 virtual clonable * do_clone () const
47 {
48 return new D (static_cast <const D&> (* this));
49 }
50};
51
52class B
53{
54public:
55 B * clone () const
56 {
57 return do_clone ();
58 }
59private:
60 virtual B * do_clone () const = 0;
61};
62
63
64template <class T, class DER>
65class Base
66{
67public:
69 {
70 cout << "Destructing base" << endl;
71 };
73 {
74 return static_cast<DER*>(this)->Square();
75 }
76 void Create(T obj)
77 {
78 cout << "Creating base with element " << obj << endl;
79 static_cast<DER*>(this)->CreateImpl(obj);
80 }
81 void Print()
82 {
83 static_cast<DER*>(this)->Print();
84 }
85};
86
87template <class T>
88class Derived : public Base < T, Derived<T> >
89{
90public:
92 {
93 cout << "Destructing derived" << endl;
94 };
95
96 void CreateImpl(T obj)
97 {
98 cout << "Creating derived with element " << obj << endl;
99 ele = obj;
100 }
101
103 {
104 Base< T, Derived<T> > mybase;
105 mybase.Create(ele*ele);
106 return mybase;
107 }
108 void Print()
109 {
110 cout << "Element is " << ele << endl;
111 }
112private:
113 T ele;
114};
115
116
117template <class T, int N>
118class Dummy
119{
120public:
122 {
123 cout << "Constructing" << endl;
124 vec = new vector<T>(N);
125 __gnu_cxx::iota(vec->begin(), vec->end(), 1);
126 }
127 Dummy(const Dummy<T,N> & rhs)
128 {
129 cout << "Copy constructing" << endl;
130 vec = new vector<T>(*rhs.vec);
131 }
133 {
134 cout << "Assigning" << endl;
135 if(this != &rhs)
136 {
137 if(vec != NULL) // unnecessary check, delete on NULL doesn't fail
138 delete vec;
139 if(rhs.vec != NULL)
140 vec = new vector<T>(*rhs.vec);
141 }
142 cout << "Assignment returning" << endl;
143 return *this;
144 }
145
146 template <typename SR>
147 void EleAdd(const Dummy<T,N> & rhs)
148 {
149 // No need to check size as it won't compile without N being the same
150 for(int i=0; i < vec->size(); i++)
151 {
152 (*vec)[i] = SR::add((*rhs.vec)[i], (*vec)[i]);
153 }
154 }
155
156 void Double()
157 {
158 typedef PlusTimesSRing<T,T> PT;
159
160 Dummy<T,N> duplicate(*this);
161 EleAdd< PT > (duplicate);
162 }
163
165 {
166 cout << "Destructing object with first element " << (*vec)[0] << endl;
167 delete vec;
168 }
169 void Reset()
170 {
171 *this = Dummy<T,N> ();
172 }
173 void Print()
174 {
175 for(int i=0; i<N; ++i)
176 cout << (*vec)[i] << " ";
177 cout << endl;
178 }
179
180 vector<T> * vec;
181};
182
183
184// A templated assignment operator
185class AClass {
186public:
187 template <typename T>
189 ostringstream oss;
190 oss << val;
191 m_value = oss.str();
192 return *this;
193 }
194 string const& str() const { return m_value; }
195private:
196 string m_value;
197};
198
199ostream& operator<<(ostream& os, AClass const& obj) {
200 os << obj.str();
201 return os;
202}
203
204
205template <typename T>
206T add_func (const T & a, const T & b)
207{
208 return a+b;
209}
210
211
212struct mystruct
213{
214 double first;
215 double second;
216 double third;
217};
218
220{
222 {
223 firsts = new double[size];
224 seconds = new double[size];
225 thirds = new double[size];
226 }
231 double * firsts;
232 double * seconds;
233 double * thirds;
234};
235
236
237
238int main()
239{
240 cout << sizeof(tr1::tuple<int, int, double>) << endl;
241
242 int * arrint = new int[5];
243 double * arrdouble = new double[10];
244 float * arrfloat = new float[5];
245
246 __gnu_cxx::iota(arrfloat, arrfloat+5,1);
247 transform(arrfloat, arrfloat+5,arrfloat, bind1st(divides<float>(), 1));
248 for(int i=0; i<5; ++i)
249 cout << arrfloat[i] << " ";
250 cout << endl;
251
252 DeleteAll(arrint, arrdouble, arrfloat);
253
254
255 int * A = new int[5];
256 __gnu_cxx::iota(A, A+5, 1);
257
258 transform(A, A+5, A, bind2nd(minus<int>(), 10));
259 for(int i=0; i<5; ++i)
260 cout << A[i] << " ";
261 cout << endl;
262
263 delete [] A;
264
265 Dummy<int, 7> BushJr;
266 Dummy<int, 7> BushSr = BushJr;
267 (*(BushJr.vec))[0] = 100;
268
269 BushSr.Print();
270 BushJr.Print();
271
272 BushJr.EleAdd< PlusTimesSRing<int, int> > (BushSr);
273 BushJr.Print();
274
275 BushJr.Double();
276 BushJr.Print();
277
278 BushJr.Reset();
279 BushJr.Print();
280
281 vector<int> v = *(BushJr.vec);
282 cout << v.size() << " elements, occupying "<< sizeof(v) << " bytes"<< endl;
283
285 mybase->Create(5);
286 Base< int, Derived<int> > squared = mybase->Square();
287 squared.Print();
288
289 Derived<int> & casted = static_cast< Derived<int> & > (squared);
290
291 int a = 5;
292 float b = 6.7;
293 typedef promote_trait<int,float>::T_promote T_promote;
294 T_promote result = a + b;
295 cout << result << endl;
296
297 vector<int>(3).swap(v);
298 cout << "size of v: " << v.size() <<" , capacity of v: " << v.capacity() << endl;
299
300 cout << "******* vector performance test *******" << endl;
301 int * source = new int[TESTSIZE];
302 __gnu_cxx::iota(source, source+TESTSIZE, 1);
303 vector<int> destination(TESTSIZE);
304 vector<int> inserted;
305
306 timeval tim;
307 gettimeofday(&tim, NULL);
308 double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
309
310 std::copy(source, source+TESTSIZE, destination.begin());
311
312 gettimeofday(&tim, NULL);
313 double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
314 printf("%.6lf seconds elapsed for std::copy\n", t2-t1);
315
316
317 gettimeofday(&tim, NULL);
318 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
319
320 inserted.insert(inserted.begin(), source, source+TESTSIZE);
321
322 gettimeofday(&tim, NULL);
323 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
324 printf("%.6lf seconds elapsed with insert\n", t2-t1);
325
326
327 gettimeofday(&tim, NULL);
328 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
329
330 memcpy(&destination[0], source, TESTSIZE*sizeof(int));
331
332 gettimeofday(&tim, NULL);
333 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
334 printf("%.6lf seconds elapsed with memcpy\n", t2-t1);
335
336
337 gettimeofday(&tim, NULL);
338 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
339
340 for (int i=0; i< TESTSIZE; ++i)
341 destination[i] = source[i];
342
343 gettimeofday(&tim, NULL);
344 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
345 printf("%.6lf seconds elapsed with loops\n", t2-t1);
346
347 delete [] source;
348
349 cout << "******* accumulate performance test *******" << endl;
350
351 gettimeofday(&tim, NULL);
352 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
353
354 int acc = fpaccumulate(destination.begin(), destination.end(), 0, plus<int>());
355
356 gettimeofday(&tim, NULL);
357 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
358 printf("%.6lf seconds elapsed with plus<int> function object, gave result: %d\n", t2-t1, acc);
359
360 gettimeofday(&tim, NULL);
361 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
362
363 typedef PlusTimesSRing<int, int> PT;
364 acc = sraccumulate< PT > (destination.begin(), destination.end(), 0);
365
366 gettimeofday(&tim, NULL);
367 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
368 printf("%.6lf seconds elapsed with SR::add static method, gave result: %d\n", t2-t1, acc);
369
370
371 gettimeofday(&tim, NULL);
372 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
373
374 acc = fpaccumulate(destination.begin(), destination.end(), 0, &add_func<int>);
375
376 gettimeofday(&tim, NULL);
377 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
378 printf("%.6lf seconds elapsed with function pointer, gave result: %d\n", t2-t1, acc);
379
380
381 cout << "******* array of structs test *******" << endl;
382
383 int * linear = new int[TESTSIZE];
384 int * random = new int[TESTSIZE];
385 __gnu_cxx::iota(linear, linear+TESTSIZE, 0);
386 __gnu_cxx::iota(random, random+TESTSIZE, 0);
387 random_shuffle(random, random+TESTSIZE);
388
389
390 StrOfArrays strofarrays (TESTSIZE);
391 mystruct * arrayofstrs = new mystruct[TESTSIZE];
392
393 for(int i=0; i<TESTSIZE; ++i)
394 {
395 arrayofstrs[i].first = 1;
396 arrayofstrs[i].second = 2;
397 arrayofstrs[i].third = 3;
398
399 strofarrays.firsts[i] = 1;
400 strofarrays.seconds[i] = 2;
401 strofarrays.thirds[i] = 3;
402 }
403
404 gettimeofday(&tim, NULL);
405 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
406 for(int i=0; i<TESTSIZE; ++i)
407 {
408 arrayofstrs[linear[i]].first += 1;
409 arrayofstrs[linear[i]].second += 2;
410 arrayofstrs[linear[i]].third += 3;
411 }
412 gettimeofday(&tim, NULL);
413 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
414 printf("%.6lf seconds elapsed for streaming array of structs\n", t2-t1);
415
416 gettimeofday(&tim, NULL);
417 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
418 for(int i=0; i<TESTSIZE; ++i)
419 {
420 strofarrays.firsts[linear[i]] += 1;
421 strofarrays.seconds[linear[i]] += 2;
422 strofarrays.thirds[linear[i]] += 3;
423 }
424 gettimeofday(&tim, NULL);
425 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
426 printf("%.6lf seconds elapsed for streaming struct of arrays\n", t2-t1);
427
428 gettimeofday(&tim, NULL);
429 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
430 for(int i=0; i<TESTSIZE; ++i)
431 {
432 arrayofstrs[random[i]].first += 1;
433 arrayofstrs[random[i]].second += 2;
434 arrayofstrs[random[i]].third += 3;
435 }
436 gettimeofday(&tim, NULL);
437 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
438 printf("%.6lf seconds elapsed for randomly accessing array of structs\n", t2-t1);
439
440 gettimeofday(&tim, NULL);
441 t1=tim.tv_sec+(tim.tv_usec/1000000.0);
442 for(int i=0; i<TESTSIZE; ++i)
443 {
444 strofarrays.firsts[random[i]] += 1;
445 strofarrays.seconds[random[i]] += 2;
446 strofarrays.thirds[random[i]] += 3;
447 }
448 gettimeofday(&tim, NULL);
449 t2=tim.tv_sec+(tim.tv_usec/1000000.0);
450 printf("%.6lf seconds elapsed for randomly accessing struct of arrays\n", t2-t1);
451
452 delete mybase;
453
454 return 0;
455}
void DeleteAll(A arr1)
Definition Deleter.h:7
AClass & operator=(T val)
Definition test.cpp:188
string const & str() const
Definition test.cpp:194
Definition test.cpp:53
B * clone() const
Definition test.cpp:55
Definition test.cpp:66
void Create(T obj)
Definition test.cpp:76
~Base()
Definition test.cpp:68
void Print()
Definition test.cpp:81
Base Square()
Definition test.cpp:72
void Print()
Definition test.cpp:108
void CreateImpl(T obj)
Definition test.cpp:96
Base< T, Derived< T > > Square()
Definition test.cpp:102
~Derived()
Definition test.cpp:91
void Reset()
Definition test.cpp:169
Dummy(const Dummy< T, N > &rhs)
Definition test.cpp:127
Dummy< T, N > & operator=(const Dummy< T, N > &rhs)
Definition test.cpp:132
vector< T > * vec
Definition test.cpp:180
void EleAdd(const Dummy< T, N > &rhs)
Definition test.cpp:147
void Double()
Definition test.cpp:156
Dummy()
Definition test.cpp:121
void Print()
Definition test.cpp:173
~Dummy()
Definition test.cpp:164
D * clone() const
Definition test.cpp:41
int size
Definition common.h:20
double A
double D
Definition options.h:15
StrOfArrays(int size)
Definition test.cpp:221
double * firsts
Definition test.cpp:231
double * thirds
Definition test.cpp:233
~StrOfArrays()
Definition test.cpp:227
double * seconds
Definition test.cpp:232
double third
Definition test.cpp:216
double first
Definition test.cpp:214
double second
Definition test.cpp:215
_Tp sraccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
Definition test.cpp:28
ostream & operator<<(ostream &os, AClass const &obj)
Definition test.cpp:199
T add_func(const T &a, const T &b)
Definition test.cpp:206
#define TESTSIZE
Definition test.cpp:14
_Tp fpaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
Definition test.cpp:18
int main()
Definition test.cpp:238