Stxxl  1.4.0
mng/test_mng.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  mng/test_mng.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 //! \example mng/test_mng.cpp
00014 //! This is an example of use of completion handlers, \c stxxl::block_manager, and
00015 //! \c stxxl::typed_block
00016 
00017 #include <iostream>
00018 #include <stxxl/request>
00019 #include <stxxl/mng>
00020 
00021 #define BLOCK_SIZE (1024 * 512)
00022 
00023 struct MyType
00024 {
00025     int integer;
00026     //char chars[4];
00027     ~MyType() { }
00028 };
00029 
00030 struct my_handler
00031 {
00032     void operator () (stxxl::request * req)
00033     {
00034         STXXL_MSG(req << " done, type=" << req->io_type());
00035     }
00036 };
00037 
00038 typedef stxxl::typed_block<BLOCK_SIZE, MyType> block_type;
00039 
00040 int main()
00041 {
00042     STXXL_MSG(sizeof(MyType) << " " << (BLOCK_SIZE % sizeof(MyType)));
00043     STXXL_MSG(sizeof(block_type) << " " << BLOCK_SIZE);
00044     const unsigned nblocks = 2;
00045     stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
00046     std::vector<int> disks(nblocks, 2);
00047     stxxl::request_ptr * reqs = new stxxl::request_ptr[nblocks];
00048     stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00049     bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
00050 
00051     block_type * block = new block_type[2];
00052     STXXL_MSG(std::hex);
00053     STXXL_MSG("Allocated block address    : " << long(block));
00054     STXXL_MSG("Allocated block address + 1: " << long(block + 1));
00055     STXXL_MSG(std::dec);
00056     unsigned i = 0;
00057     for (i = 0; i < block_type::size; ++i)
00058     {
00059         block->elem[i].integer = i;
00060         //memcpy (block->elem[i].chars, "STXXL", 4);
00061     }
00062     for (i = 0; i < nblocks; ++i)
00063         reqs[i] = block->write(bids[i], my_handler());
00064 
00065 
00066     std::cout << "Waiting " << std::endl;
00067     stxxl::wait_all(reqs, nblocks);
00068 
00069     for (i = 0; i < nblocks; ++i)
00070     {
00071         reqs[i] = block->read(bids[i], my_handler());
00072         reqs[i]->wait();
00073         for (int j = 0; j < block_type::size; ++j)
00074         {
00075             if (j != block->elem[j].integer)
00076             {
00077                 STXXL_MSG("Error in block " << std::hex << i << " pos: " << j
00078                                             << " value read: " << block->elem[j].integer);
00079             }
00080         }
00081     }
00082 
00083 
00084     bm->delete_blocks(bids.begin(), bids.end());
00085 
00086     delete[] reqs;
00087     delete[] block;
00088 
00089 #if 0
00090     // variable-size blocks, not supported currently
00091 
00092     BIDArray<0> vbids(nblocks);
00093     for (i = 0; i < nblocks; i++)
00094         vbids[i].size = 1024 + i;
00095 
00096     bm->new_blocks(striping(), vbids.begin(), vbids.end());
00097 
00098     for (i = 0; i < nblocks; i++)
00099         STXXL_MSG("Allocated block: offset=" << vbids[i].offset << ", size=" << vbids[i].size);
00100 
00101     bm->delete_blocks(vbids.begin(), vbids.end());
00102 #endif
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines