Stxxl  1.4.0
containers/bench_pqueue.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/bench_pqueue.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <limits>
00015 #include <stxxl/priority_queue>
00016 #include <stxxl/timer>
00017 
00018 
00019 #define RECORD_SIZE 20
00020 
00021 struct my_type
00022 {
00023     //typedef stxxl::int64 key_type;
00024     typedef int key_type;
00025 
00026     key_type key;
00027     char data[RECORD_SIZE - sizeof(key_type)];
00028     my_type() { }
00029     explicit my_type(key_type k) : key(k)
00030     {
00031         #ifdef STXXL_VALGRIND_AVOID_UNINITIALIZED_WRITE_ERRORS
00032         memset(data, 0, sizeof(data));
00033         #endif
00034     }
00035 };
00036 
00037 std::ostream & operator << (std::ostream & o, const my_type & obj)
00038 {
00039     o << obj.key;
00040     return o;
00041 }
00042 
00043 struct my_cmp : std::binary_function<my_type, my_type, bool> // greater
00044 {
00045     bool operator () (const my_type & a, const my_type & b) const
00046     {
00047         return a.key > b.key;
00048     }
00049     my_type min_value() const
00050     {
00051         return my_type((std::numeric_limits<my_type::key_type>::max)());
00052     }
00053 };
00054 
00055 int main()
00056 {
00057 /*
00058       unsigned BufferSize1_ = 32, // equalize procedure call overheads etc.
00059       unsigned N_ = 512, // bandwidth
00060       unsigned IntKMAX_ = 64, // maximal arity for internal mergers
00061       unsigned IntLevels_ = 4,
00062       unsigned BlockSize_ = (2*1024*1024),
00063       unsigned ExtKMAX_ = 64, // maximal arity for external mergers
00064       unsigned ExtLevels_ = 2,
00065  */
00066 // typedef priority_queue<priority_queue_config<my_type,my_cmp,
00067 //  32,512,64,3,(4*1024),0x7fffffff,1> > pq_type;
00068     const unsigned volume = 3 * 1024 * 1024; // in KiB
00069     const unsigned mem_for_queue = 256 * 1024 * 1024;
00070     const unsigned mem_for_pools = 512 * 1024 * 1024;
00071     typedef stxxl::PRIORITY_QUEUE_GENERATOR<my_type, my_cmp, mem_for_queue, volume / sizeof(my_type)> gen;
00072     typedef gen::result pq_type;
00073     typedef pq_type::block_type block_type;
00074 
00075     STXXL_MSG("Block size: " << block_type::raw_size);
00076     STXXL_MSG("AI: " << gen::AI);
00077     STXXL_MSG("X : " << gen::X);
00078     STXXL_MSG("N : " << gen::N);
00079     STXXL_MSG("AE: " << gen::AE);
00080 
00081     stxxl::timer Timer;
00082     Timer.start();
00083 
00084     pq_type p(mem_for_pools / 2, mem_for_pools / 2);
00085     stxxl::int64 nelements = stxxl::int64(volume / sizeof(my_type)) * 1024, i;
00086 
00087     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00088     STXXL_MSG("Max elements: " << nelements);
00089     for (i = 0; i < nelements; i++)
00090     {
00091         if ((i % (1024 * 1024)) == 0)
00092             STXXL_MSG("Inserting element " << i);
00093         p.push(my_type(nelements - i));
00094     }
00095     Timer.stop();
00096     STXXL_MSG("Time spent for filling: " << Timer.seconds() << " s");
00097 
00098     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00099     Timer.reset();
00100     Timer.start();
00101     for (i = 0; i < (nelements); ++i)
00102     {
00103         assert(!p.empty());
00104         //STXXL_MSG( p.top() );
00105         assert(p.top().key == i + 1);
00106         p.pop();
00107         if ((i % (1024 * 1024)) == 0)
00108             STXXL_MSG("Element " << i << " popped");
00109     }
00110     Timer.stop();
00111 
00112     STXXL_MSG("Time spent for removing elements: " << Timer.seconds() << " s");
00113     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00114 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines