Stxxl  1.4.0
containers/test_map.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_map.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2005, 2006 Roman Dementiev <dementiev@ira.uka.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 <algorithm>
00015 #include <cmath>
00016 #include <stxxl/map>
00017 #include <stxxl/stats>
00018 
00019 typedef unsigned int key_type;
00020 typedef unsigned int data_type;
00021 
00022 struct cmp : public std::less<key_type>
00023 {
00024     static key_type min_value()
00025     {
00026         return (std::numeric_limits<key_type>::min)();
00027     }
00028     static key_type max_value()
00029     {
00030         return (std::numeric_limits<key_type>::max)();
00031     }
00032 };
00033 
00034 #define BLOCK_SIZE (32 * 1024)
00035 #define CACHE_SIZE (2 * 1024 * 1024 / BLOCK_SIZE)
00036 
00037 #define CACHE_ELEMENTS (BLOCK_SIZE * CACHE_SIZE / (sizeof(key_type) + sizeof(data_type)))
00038 
00039 typedef stxxl::map<key_type, data_type, cmp, BLOCK_SIZE, BLOCK_SIZE> map_type;
00040 
00041 int main(int argc, char ** argv)
00042 {
00043     stxxl::stats_data stats_begin(*stxxl::stats::get_instance());
00044     stxxl::stats_data stats_elapsed;
00045     STXXL_MSG(stats_begin);
00046 
00047     STXXL_MSG("Block size " << BLOCK_SIZE / 1024 << " KiB");
00048     STXXL_MSG("Cache size " << (CACHE_SIZE * BLOCK_SIZE) / 1024 << " KiB");
00049     int max_mult = 256;
00050     if (argc > 1)
00051         max_mult = atoi(argv[1]);
00052     for (int mult = 1; mult < max_mult; mult *= 2)
00053     {
00054         stats_begin = *stxxl::stats::get_instance();
00055         const unsigned el = mult * (CACHE_ELEMENTS / 8);
00056         STXXL_MSG("Elements to insert " << el << " volume =" <<
00057                   (el * (sizeof(key_type) + sizeof(data_type))) / 1024 << " KiB");
00058         map_type * DMap = new map_type(CACHE_SIZE * BLOCK_SIZE / 2, CACHE_SIZE * BLOCK_SIZE / 2);
00059         //map_type  Map(CACHE_SIZE*BLOCK_SIZE/2,CACHE_SIZE*BLOCK_SIZE/2);
00060         map_type & Map = *DMap;
00061 
00062         for (unsigned i = 0; i < el; ++i)
00063         {
00064             Map[i] = i + 1;
00065         }
00066         stats_elapsed = stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
00067         double writes = double(stats_elapsed.get_writes()) / double(el);
00068         double logel = log(double(el)) / log(double(BLOCK_SIZE));
00069         STXXL_MSG("Logs: writes " << writes << " logel " << logel << " writes/logel " << (writes / logel));
00070         STXXL_MSG(stats_elapsed);
00071 
00072         stats_begin = *stxxl::stats::get_instance();
00073         STXXL_MSG("Doing search");
00074         unsigned queries = el;
00075         const map_type & ConstMap = Map;
00076         stxxl::random_number32 myrandom;
00077         for (unsigned i = 0; i < queries; ++i)
00078         {
00079             key_type key = myrandom() % el;
00080             map_type::const_iterator result = ConstMap.find(key);
00081             assert((*result).second == key + 1);
00082         }
00083         stats_elapsed = stxxl::stats_data(*stxxl::stats::get_instance()) - stats_begin;
00084         double reads = double(stats_elapsed.get_reads()) / logel;
00085         double readsperq = double(stats_elapsed.get_reads()) / queries;
00086         STXXL_MSG("reads/logel " << reads << " readsperq " << readsperq);
00087         STXXL_MSG(stats_elapsed);
00088 
00089         delete DMap;
00090     }
00091 
00092     return 0;
00093 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines