Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * algo/gen_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 #include <limits> 00015 #include <stxxl/io> 00016 #include <stxxl/aligned_alloc> 00017 00018 00019 struct my_type 00020 { 00021 typedef unsigned key_type; 00022 00023 key_type _key; 00024 char _data[128 - sizeof(key_type)]; 00025 key_type key() const 00026 { 00027 return _key; 00028 } 00029 00030 my_type() { } 00031 my_type(key_type __key) : _key(__key) { } 00032 00033 static my_type min_value() 00034 { 00035 return my_type((std::numeric_limits<key_type>::min)()); 00036 } 00037 static my_type max_value() 00038 { 00039 return my_type((std::numeric_limits<key_type>::max)()); 00040 } 00041 }; 00042 00043 bool operator < (const my_type & a, const my_type & b) 00044 { 00045 return a.key() < b.key(); 00046 } 00047 00048 00049 int main(int argc, char ** argv) 00050 { 00051 if (argc < 2) 00052 { 00053 std::cout << "Usage: " << argv[0] << " output_file" << std::endl; 00054 return -1; 00055 } 00056 00057 const my_type::key_type max_key = 1 * 1024 * 1024; 00058 const unsigned int block_size = 1 * 1024 * 1024; 00059 const unsigned int records_in_block = block_size / sizeof(my_type); 00060 my_type * array = (my_type *)stxxl::aligned_alloc<BLOCK_ALIGN>(block_size); 00061 memset(array, 0, block_size); 00062 stxxl::syscall_file f(argv[1], stxxl::file::CREAT | stxxl::file::RDWR); 00063 stxxl::request_ptr req; 00064 00065 my_type::key_type cur_key = max_key; 00066 for (unsigned i = 0; i < max_key / records_in_block; i++) 00067 { 00068 for (unsigned j = 0; j < records_in_block; j++) 00069 array[j]._key = cur_key--; 00070 00071 req = f.awrite((void *)array, stxxl::int64(i) * block_size, block_size, stxxl::default_completion_handler()); 00072 req->wait(); 00073 } 00074 00075 stxxl::aligned_dealloc<BLOCK_ALIGN>(array); 00076 00077 return 0; 00078 }