Stxxl  1.4.0
containers/test_pqueue.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_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 //! \example containers/test_pqueue.cpp
00015 //! This is an example of how to use \c stxxl::PRIORITY_QUEUE_GENERATOR
00016 //! and \c stxxl::priority_queue
00017 
00018 #include <limits>
00019 #include <stxxl/priority_queue>
00020 #include <stxxl/timer>
00021 
00022 #define RECORD_SIZE 128
00023 
00024 struct my_type
00025 {
00026     typedef int key_type;
00027     key_type key;
00028     char data[RECORD_SIZE - sizeof(key_type)];
00029     my_type() { }
00030     explicit my_type(key_type k) : key(k)
00031     {
00032         #ifdef STXXL_VALGRIND_AVOID_UNINITIALIZED_WRITE_ERRORS
00033         memset(data, 0, sizeof(data));
00034         #endif
00035     }
00036 };
00037 
00038 std::ostream & operator << (std::ostream & o, const my_type & obj)
00039 {
00040     o << obj.key;
00041     return o;
00042 }
00043 
00044 struct my_cmp : std::binary_function<my_type, my_type, bool> // greater
00045 {
00046     bool operator () (const my_type & a, const my_type & b) const
00047     {
00048         return a.key > b.key;
00049     }
00050 
00051     my_type min_value() const
00052     {
00053         return my_type((std::numeric_limits<my_type::key_type>::max)());
00054     }
00055 };
00056 
00057 int main()
00058 {
00059 /*
00060        unsigned BufferSize1_ = 32, // equalize procedure call overheads etc.
00061        unsigned N_ = 512, // bandwidth
00062        unsigned IntKMAX_ = 64, // maximal arity for internal mergers
00063        unsigned IntLevels_ = 4,
00064        unsigned BlockSize_ = (2*1024*1024),
00065        unsigned ExtKMAX_ = 64, // maximal arity for external mergers
00066        unsigned ExtLevels_ = 2,
00067  */
00068     //typedef priority_queue<priority_queue_config<my_type,my_cmp,
00069     //  32,512,64,3,(4*1024),0x7fffffff,1> > pq_type;
00070     const unsigned volume = 1024 * 1024; // in KiB
00071     typedef stxxl::PRIORITY_QUEUE_GENERATOR<my_type, my_cmp, 32 * 1024 * 1024, 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     const unsigned mem_for_pools = 128 * 1024 * 1024;
00085     stxxl::read_write_pool<block_type> pool((mem_for_pools / 2) / block_type::raw_size, (mem_for_pools / 2) / block_type::raw_size);
00086     pq_type p(pool);
00087 
00088     stxxl::int64 nelements = stxxl::int64(volume * 1024 / sizeof(my_type)), i;
00089     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00090     STXXL_MSG("Max elements: " << nelements);
00091     for (i = 0; i < nelements; i++)
00092     {
00093         if ((i % (1024 * 1024)) == 0)
00094             STXXL_MSG("Inserting element " << i);
00095         p.push(my_type(nelements - i));
00096     }
00097     Timer.stop();
00098     STXXL_MSG("Time spent for filling: " << Timer.seconds() << " s");
00099 
00100 #if 0
00101     // test swap
00102     pq_type p1(pool);
00103     std::swap(p, p1);
00104     std::swap(p, p1);
00105 #endif
00106 
00107     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00108     Timer.reset();
00109     Timer.start();
00110     for (i = 0; i < (nelements); ++i)
00111     {
00112         assert(!p.empty());
00113         //STXXL_MSG( p.top() );
00114         assert(p.top().key == i + 1);
00115         p.pop();
00116         if ((i % (1024 * 1024)) == 0)
00117             STXXL_MSG("Element " << i << " popped");
00118     }
00119     Timer.stop();
00120 
00121     STXXL_MSG("Time spent for removing elements: " << Timer.seconds() << " s");
00122     STXXL_MSG("Internal memory consumption of the priority queue: " << p.mem_cons() << " B");
00123 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines