[go: up one dir, main page]

Menu

[e222c1]: / mng / test.cpp  Maximize  Restore  History

Download this file

101 lines (78 with data), 2.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
/***************************************************************************
* test.cpp
*
* Sat Aug 24 23:52:33 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#include <iostream>
#include "mng.h"
//! \example mng/test.cpp
//! This is an example of use of completion handlers, \c stxxl::block_manager, and
//! \c stxxl::typed_block
#define BLOCK_SIZE (1024*512)
struct MyType
{
int integer;
//char chars[4];
};
using namespace stxxl;
struct my_handler
{
void operator () (request * req)
{
STXXL_MSG( req << " done, type="<<req->io_type() )
}
};
typedef typed_block<BLOCK_SIZE,MyType> block_type;
int main ()
{
STXXL_MSG(sizeof(MyType)<<" "<<(BLOCK_SIZE % sizeof(MyType)));
STXXL_MSG(sizeof(block_type)<<" "<<BLOCK_SIZE);
const unsigned nblocks = 2;
BIDArray < BLOCK_SIZE > bids (nblocks);
std::vector < int >disks (nblocks, 2);
stxxl::request_ptr * reqs = new stxxl::request_ptr[nblocks];
block_manager *bm = block_manager::get_instance ();
bm->new_blocks (striping (), bids.begin (), bids.end ());
block_type * block = new block_type;
STXXL_MSG(std::hex)
STXXL_MSG("Allocated block address : "<<long(block))
STXXL_MSG("Allocated block address + 1: "<<long(block+1))
STXXL_MSG(std::dec)
unsigned i = 0;
for (i = 0; i < block_type::size; ++i)
{
block->elem[i].integer = i;
//memcpy (block->elem[i].chars, "STXXL", 4);
}
for (i = 0; i < nblocks; ++i)
reqs[i] = block->write (bids[i], my_handler());
std::cout << "Waiting " << std::endl;
stxxl::wait_all (reqs, nblocks);
for (i = 0; i < nblocks; ++i)
{
reqs[i] = block->read (bids[i], my_handler());
reqs[i]->wait();
for(int j=0;j<block_type::size;++j)
{
if(j!=block->elem[j].integer)
{
STXXL_MSG("Error in block "<<std::hex <<i<<" pos: "<<j
<<" value read: "<<block->elem[j].integer)
}
}
}
bm->delete_blocks (bids.begin(), bids.end ());
delete [] reqs;
delete block;
// variable-size blocks, not supported currently
/*
BIDArray<0> vbids (nblocks);
for(i=0;i<nblocks;i++)
vbids[i].size = 1024 + i;
bm->new_blocks (striping (), vbids.begin (), vbids.end ());
for(i=0;i<nblocks;i++)
STXXL_MSG("Allocated block: offset="<<vbids[i].offset<<", size="<<vbids[i].size)
bm->delete_blocks(vbids.begin (), vbids.end ()); */
}