📄 stress.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: stress.cpp,v $ * PRODUCTION Revision 1000.1 2004/06/01 19:40:47 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2 * PRODUCTION * =========================================================================== *//* $Id: stress.cpp,v 1000.1 2004/06/01 19:40:47 gouriano Exp $* ===========================================================================** PUBLIC DOMAIN NOTICE* National Center for Biotechnology Information** This software/database is a "United States Government Work" under the* terms of the United States Copyright Act. It was written as part of* the author's official duties as a United States Government employee and* thus cannot be copyrighted. This software/database is freely available* to the public for use. The National Library of Medicine and the U.S.* Government have not placed any restriction on its use or reproduction.** Although all reasonable efforts have been taken to ensure the accuracy* and reliability of the software and data, the NLM and the U.S.* Government do not and cannot warrant the performance or results that* may be obtained by using this software or data. The NLM and the U.S.* Government disclaim all warranties, express or implied, including* warranties of performance, merchantability or fitness for any particular* purpose.** Please cite the author in any work or product based on this material.** ===========================================================================** Author: Anatoliy Kuznetsov***///#define BM64OPT//#define BM_SET_MMX_GUARD//#define BMSSE2OPT#define BMCOUNTOPT#include <ncbi_pch.hpp>#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <memory.h>#include <iostream>#include <time.h>#include <util/bitset/bm.h>#include <util/bitset/bmalgo.h>using namespace bm;using namespace std;#include "rlebtv.h"#include <util/bitset/encoding.h>#include <limits.h>#define POOL_SIZE 5000//#define MEM_POOLtemplate<class T> T* pool_allocate(T** pool, int& i, size_t n){ return i ? pool[i--] : (T*) ::malloc(n * sizeof(T));}inline void* pool_allocate2(void** pool, int& i, size_t n){ return i ? pool[i--] : malloc(n * sizeof(void*));}template<class T> void pool_free(T** pool, int& i, T* p){ i < POOL_SIZE ? (free(p),(void*)0) : pool[++i]=p;}class pool_block_allocator{public: static bm::word_t* allocate(size_t n, const void *) { int *idx = 0; bm::word_t** pool = 0; switch (n) { case bm::set_block_size: idx = &bit_blocks_idx_; pool = free_bit_blocks_; break; case 64: idx = &gap_blocks_idx0_; pool = gap_bit_blocks0_; break; case 128: idx = &gap_blocks_idx1_; pool = gap_bit_blocks1_; break; case 256: idx = &gap_blocks_idx2_; pool = gap_bit_blocks2_; break; case 512: idx = &gap_blocks_idx3_; pool = gap_bit_blocks3_; break; default: assert(0); } return pool_allocate(pool, *idx, n); } static void deallocate(bm::word_t* p, size_t n) { int *idx = 0; bm::word_t** pool = 0; switch (n) { case bm::set_block_size: idx = &bit_blocks_idx_; pool = free_bit_blocks_; break; case 64: idx = &gap_blocks_idx0_; pool = gap_bit_blocks0_; break; case 128: idx = &gap_blocks_idx1_; pool = gap_bit_blocks1_; break; case 256: idx = &gap_blocks_idx2_; pool = gap_bit_blocks2_; break; case 512: idx = &gap_blocks_idx3_; pool = gap_bit_blocks3_; break; default: assert(0); } pool_free(pool, *idx, p); }private: static bm::word_t* free_bit_blocks_[]; static int bit_blocks_idx_; static bm::word_t* gap_bit_blocks0_[]; static int gap_blocks_idx0_; static bm::word_t* gap_bit_blocks1_[]; static int gap_blocks_idx1_; static bm::word_t* gap_bit_blocks2_[]; static int gap_blocks_idx2_; static bm::word_t* gap_bit_blocks3_[]; static int gap_blocks_idx3_;};bm::word_t* pool_block_allocator::free_bit_blocks_[POOL_SIZE];int pool_block_allocator::bit_blocks_idx_ = 0;bm::word_t* pool_block_allocator::gap_bit_blocks0_[POOL_SIZE];int pool_block_allocator::gap_blocks_idx0_ = 0;bm::word_t* pool_block_allocator::gap_bit_blocks1_[POOL_SIZE];int pool_block_allocator::gap_blocks_idx1_ = 0;bm::word_t* pool_block_allocator::gap_bit_blocks2_[POOL_SIZE];int pool_block_allocator::gap_blocks_idx2_ = 0;bm::word_t* pool_block_allocator::gap_bit_blocks3_[POOL_SIZE];int pool_block_allocator::gap_blocks_idx3_ = 0;class pool_ptr_allocator{public: static void* allocate(size_t n, const void *) { return pool_allocate2(free_ptr_blocks_, ptr_blocks_idx_, n); } static void deallocate(void* p, size_t) { pool_free(free_ptr_blocks_, ptr_blocks_idx_, p); }private: static void* free_ptr_blocks_[]; static int ptr_blocks_idx_;};void* pool_ptr_allocator::free_ptr_blocks_[POOL_SIZE];int pool_ptr_allocator::ptr_blocks_idx_ = 0;//#define MEM_DEBUG #ifdef MEM_DEBUGclass dbg_block_allocator{public:static unsigned na_;static unsigned nf_; static bm::word_t* allocate(size_t n, const void *) { ++na_; assert(n); bm::word_t* p = (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t)); *p = n; return ++p; } static void deallocate(bm::word_t* p, size_t n) { ++nf_; --p; if(*p != n) { printf("Block memory deallocation error!\n"); exit(1); } ::free(p); } static int balance() { return nf_ - na_; }};unsigned dbg_block_allocator::na_ = 0;unsigned dbg_block_allocator::nf_ = 0;class dbg_ptr_allocator{public:static unsigned na_;static unsigned nf_; static void* allocate(size_t n, const void *) { ++na_; assert(sizeof(size_t) == sizeof(void*)); void* p = ::malloc((n+1) * sizeof(void*)); size_t* s = (size_t*) p; *s = n; return (void*)++s; } static void deallocate(void* p, size_t n) { ++nf_; size_t* s = (size_t*) p; --s; if(*s != n) { printf("Ptr memory deallocation error!\n"); exit(1); } ::free(s); } static int balance() { return nf_ - na_; }};unsigned dbg_ptr_allocator::na_ = 0;unsigned dbg_ptr_allocator::nf_ = 0;typedef mem_alloc<dbg_block_allocator, dbg_ptr_allocator> dbg_alloc;typedef bm::bvector<dbg_alloc> bvect;typedef bm::bvector_mini<dbg_block_allocator> bvect_mini;#else#ifdef MEM_POOLtypedef mem_alloc<pool_block_allocator, pool_ptr_allocator> pool_alloc;typedef bm::bvector<pool_alloc> bvect;typedef bm::bvector_mini<bm::block_allocator> bvect_mini;#elsetypedef bm::bvector<> bvect;typedef bm::bvector_mini<bm::block_allocator> bvect_mini;#endif#endif//const unsigned BITVECT_SIZE = 100000000 * 8;// This this setting program will consume around 150M of RAMconst unsigned BITVECT_SIZE = 100000000 * 2;const unsigned ITERATIONS = 100000;const unsigned PROGRESS_PRINT = 2000000;void CheckVectors(bvect_mini &bvect_min, bvect &bvect_full, unsigned size, bool detailed = false);unsigned random_minmax(unsigned min, unsigned max){ unsigned r = (rand() << 16) | rand(); return r % (max-min) + min;}void FillSets(bvect_mini* bvect_min, bvect* bvect_full, unsigned min, unsigned max, unsigned fill_factor){ unsigned i; unsigned id; //Random filling if(fill_factor == 0) { unsigned n_id = (max - min) / 100; printf("random filling : %i\n", n_id); for (i = 0; i < n_id; i++) { id = random_minmax(min, max); bvect_min->set_bit(id); bvect_full->set_bit(id); if (PROGRESS_PRINT) { if ( (i % PROGRESS_PRINT) == 0) { cout << "+" << flush; } } } cout << endl; } else { printf("fill_factor random filling : factor = %i\n", fill_factor); for(i = 0; i < fill_factor; i++) { int k = rand() % 10; if (k == 0) k+=2; //Calculate start unsigned start = min + (max - min) / (fill_factor * k); //Randomize start start += random_minmax(1, (max - min) / (fill_factor * 10)); if (start > max) { start = min; } //Calculate end unsigned end = start + (max - start) / (fill_factor *2); //Randomize end end -= random_minmax(1, (max - start) / (fill_factor * 10)); if (end > max ) { end = max; } if (fill_factor > 1) { for(; start < end;) { int r = rand() % 8; if (r > 7) { int inc = rand() % 3; ++inc; unsigned end2 = start + rand() % 1000; if (end2 > end) end2 = end; while (start < end2) { bvect_min->set_bit(start); bvect_full->set_bit(start); start += inc; } if (PROGRESS_PRINT) { if ( ( ((i+1)*(end-start)) % PROGRESS_PRINT) == 0) { cout << "+" << flush; } } continue; } if (r) { bvect_min->set_bit(start); bvect_full->set_bit(start); ++start; } else { start+=r; bvect_min->set_bit(start); bvect_full->set_bit(start); } if (PROGRESS_PRINT) { if ( ( ((i+1)*(end-start)) % PROGRESS_PRINT) == 0) { cout << "+" << flush; } } } } else { int c = rand() % 15; if (c == 0) ++c; for(; start < end; ++start) { bvect_min->set_bit(start); bvect_full->set_bit(start); if (start % c) { start += c; } if (PROGRESS_PRINT) { if ( ( ((i+1)*(end-start)) % PROGRESS_PRINT) == 0) { cout << "+" << flush; } } } } cout << endl; } }}//// Interval filling.// 111........111111........111111..........11111111.......1111111...//void FillSetsIntervals(bvect_mini* bvect_min, bvect* bvect_full, unsigned min, unsigned max, unsigned fill_factor, bool set_flag=true){ while(fill_factor==0) { fill_factor=rand()%10; } cout << "Intervals filling. Factor=" << fill_factor << endl << endl; unsigned i, j; unsigned factor = 70 * fill_factor; for (i = min; i < max; ++i) { unsigned len, end; do { len = rand() % factor; end = i+len; } while (end >= max);/* if (set_flag == false) { cout << "Cleaning: " << i << "-" << end << endl; } else { cout << "Add: " << i << "-" << end << endl; }*/ if (i < end) { bvect_full->set_range(i, end-1, set_flag); } for (j = i; j < end; ++j) { if (set_flag) { bvect_min->set_bit(j); //bvect_full->set_bit(j); } else { bvect_min->clear_bit(j);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -