Stxxl  1.4.0
algo/sort_file.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  algo/sort_file.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-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 algo/sort_file.cpp
00015 //! This example imports a file into an \c stxxl::vector without copying its
00016 //! content and then sorts it using stxxl::sort / stxxl::ksort / ...
00017 
00018 #include <stxxl/io>
00019 #include <stxxl/mng>
00020 #include <stxxl/ksort>
00021 #include <stxxl/sort>
00022 #include <stxxl/stable_ksort>
00023 #include <stxxl/vector>
00024 
00025 
00026 struct my_type
00027 {
00028     typedef unsigned key_type;
00029 
00030     key_type _key;
00031     char _data[128 - sizeof(key_type)];
00032     key_type key() const
00033     {
00034         return _key;
00035     }
00036 
00037     my_type() { }
00038     my_type(key_type __key) : _key(__key) { }
00039 
00040     static my_type min_value()
00041     {
00042         return my_type((std::numeric_limits<key_type>::min)());
00043     }
00044     static my_type max_value()
00045     {
00046         return my_type((std::numeric_limits<key_type>::max)());
00047     }
00048 };
00049 
00050 
00051 inline bool operator < (const my_type & a, const my_type & b)
00052 {
00053     return a.key() < b.key();
00054 }
00055 
00056 inline bool operator == (const my_type & a, const my_type & b)
00057 {
00058     return a.key() == b.key();
00059 }
00060 
00061 struct Cmp
00062 {
00063     typedef my_type first_argument_type;
00064     typedef my_type second_argument_type;
00065     typedef bool result_type;
00066     bool operator () (const my_type & a, const my_type & b) const
00067     {
00068         return a < b;
00069     }
00070     static my_type min_value()
00071     {
00072         return my_type::min_value();
00073     }
00074     static my_type max_value()
00075     {
00076         return my_type::max_value();
00077     }
00078 };
00079 
00080 std::ostream & operator << (std::ostream & o, const my_type & obj)
00081 {
00082     o << obj._key;
00083     return o;
00084 }
00085 
00086 int main(int argc, char ** argv)
00087 {
00088     if (argc < 3)
00089     {
00090         std::cout << "Usage: " << argv[0] << " action file" << std::endl;
00091         std::cout << "       where action is one of generate, sort, ksort, stable_sort, stable_ksort" << std::endl;
00092         return -1;
00093     }
00094 
00095     const unsigned int block_size = sizeof(my_type) * 4096;
00096     if (strcmp(argv[1], "generate") == 0) {
00097         const my_type::key_type num_elements = 1 * 1024 * 1024;
00098         const unsigned int records_in_block = block_size / sizeof(my_type);
00099         stxxl::syscall_file f(argv[2], stxxl::file::CREAT | stxxl::file::RDWR);
00100         my_type * array = (my_type *)stxxl::aligned_alloc<BLOCK_ALIGN>(block_size);
00101         memset(array, 0, block_size);
00102 
00103         my_type::key_type cur_key = num_elements;
00104         for (unsigned i = 0; i < num_elements / records_in_block; i++)
00105         {
00106             for (unsigned j = 0; j < records_in_block; j++)
00107                 array[j]._key = cur_key--;
00108 
00109             stxxl::request_ptr req = f.awrite((void *)array, stxxl::int64(i) * block_size, block_size, stxxl::default_completion_handler());
00110             req->wait();
00111         }
00112         stxxl::aligned_dealloc<BLOCK_ALIGN>(array);
00113     } else {
00114 #if STXXL_PARALLEL_MULTIWAY_MERGE
00115         STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
00116 #endif
00117         stxxl::syscall_file f(argv[2], stxxl::file::DIRECT | stxxl::file::RDWR);
00118         unsigned memory_to_use = 50 * 1024 * 1024;
00119         typedef stxxl::vector<my_type, 1, stxxl::lru_pager<8>, block_size> vector_type;
00120         vector_type v(&f);
00121 
00122         /*
00123         STXXL_MSG("Printing...");
00124         for(stxxl::int64 i=0; i < v.size(); i++)
00125             STXXL_MSG(v[i].key());
00126          */
00127 
00128         STXXL_MSG("Checking order...");
00129         STXXL_MSG((stxxl::is_sorted(v.begin(), v.end()) ? "OK" : "WRONG"));
00130 
00131         STXXL_MSG("Sorting...");
00132         if (strcmp(argv[1], "sort") == 0) {
00133             stxxl::sort(v.begin(), v.end(), Cmp(), memory_to_use);
00134 #if 0       // stable_sort is not yet implemented
00135         } else if (strcmp(argv[1], "stable_sort") == 0) {
00136             stxxl::stable_sort(v.begin(), v.end(), memory_to_use);
00137 #endif
00138         } else if (strcmp(argv[1], "ksort") == 0) {
00139             stxxl::ksort(v.begin(), v.end(), memory_to_use);
00140         } else if (strcmp(argv[1], "stable_ksort") == 0) {
00141             stxxl::stable_ksort(v.begin(), v.end(), memory_to_use);
00142         } else {
00143             STXXL_MSG("Not implemented: " << argv[1]);
00144         }
00145 
00146         STXXL_MSG("Checking order...");
00147         STXXL_MSG((stxxl::is_sorted(v.begin(), v.end()) ? "OK" : "WRONG"));
00148     }
00149 
00150     return 0;
00151 }
00152 
00153 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines