void test(size_t nth, size_t interval, size_t proc_time) { std::printf("Testing with #th = %zu, interval = %zu ms, proc_time = %zu ms\n", nth, interval, proc_time); clue::thread_pool tpool(nth); size_t ntsks = 20; for (size_t i = 0; i < ntsks; ++i) { std::this_thread::sleep_for(std::chrono::milliseconds(interval)); tpool.schedule([i, proc_time](size_t tidx){ proc(tidx, i+1, proc_time); }); } tpool.wait_done(); std::printf("\n"); }
static void run_tests(size_t threads, size_t startsize, size_t maxsize, size_t cycles, KeyT *keys, uint32_t *randnums, ostream &out, bool terse) { if_SHOW_CHAINS(size_t *chains[6]); if_SHOW_CHAINS(size_t max_chain[6]) ; if_SHOW_NEIGHBORS(size_t *neighborhoods[5]) ; if_SHOW_NEIGHBORS(size_t max_neighbors[5]) ; HashT *ht = new HashT(startsize) ; FrThreadPool tpool(threads) ; #if 1 out << "Filling hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_ADD,terse) ; if_SHOW_CHAINS(chains[0] = ht->chainLengths(max_chain[0])); if_SHOW_NEIGHBORS(neighborhoods[0] = ht->neighborhoodDensities(max_neighbors[0])) ; out << "Lookups (100% present) " << endl ; hash_test(&tpool,out,threads,cycles,ht,maxsize,keys,Op_CHECK,terse) ; out << "Lookups (50% present) " << endl ; size_t half_cycles = (cycles + 1) / 2 ; swap_segments(keys,2*maxsize,threads) ; hash_test(&tpool,out,threads,half_cycles,ht,2*maxsize,keys,Op_CHECK,terse,false) ; swap_segments(keys,2*maxsize,threads) ; out << "Lookups (0% present) " << endl ; hash_test(&tpool,out,threads,cycles,ht,maxsize,keys+maxsize,Op_CHECKMISS,terse) ; out << "Emptying hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_REMOVE,terse) ; out << "Lookups in empty table " << endl ; hash_test(&tpool,out,threads,cycles,ht,maxsize,keys,Op_CHECKMISS,terse) ; delete ht ; ht = new HashT(startsize) ; out << "Random additions " << endl ; hash_test(&tpool,out,threads,half_cycles,ht,maxsize,keys,Op_RANDOM_ADDONLY,terse,true, randnums) ; if_SHOW_CHAINS(chains[1] = ht->chainLengths(max_chain[1])) ; out << "Emptying hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_REMOVE,terse,false) ; delete ht ; ht = new HashT(startsize) ; out << "Random ops (del=1) " << endl ; hash_test(&tpool,out,threads,half_cycles,ht,maxsize,keys,Op_RANDOM_LOWREMOVE,terse,true, randnums + maxsize/2 - 1) ; if_SHOW_CHAINS(chains[2] = ht->chainLengths(max_chain[2])) ; out << "Random ops (del=1,full) " << endl ; hash_test(&tpool,out,threads,half_cycles,ht,maxsize,keys,Op_RANDOM_LOWREMOVE,terse,true, randnums + maxsize/2 - 1) ; if_SHOW_CHAINS(chains[2] = ht->chainLengths(max_chain[2])) ; out << "Emptying hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_REMOVE,terse,false) ; delete ht ; ht = new HashT(startsize) ; #else if_SHOW_CHAINS(chains[0] = chains[1] = chains[2] = chains[3] = 0) ; if_SHOW_NEIGHBORS(neighborhoods[0] = neighborhoods[1] = 0) ; #endif /* 0 */ out << "Random ops (del=3) " << endl ; hash_test(&tpool,out,threads,cycles,ht,maxsize,keys,Op_RANDOM,terse,true, randnums + maxsize - 1) ; if_SHOW_CHAINS(chains[3] = ht->chainLengths(max_chain[3])) ; if_SHOW_NEIGHBORS(neighborhoods[1] = ht->neighborhoodDensities(max_neighbors[1])) ; out << "Emptying hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_REMOVE,terse,false) ; delete ht ; ht = new HashT(startsize) ; out << "Random ops (del=7) " << endl ; hash_test(&tpool,out,threads,cycles,ht,maxsize,keys,Op_RANDOM,terse,true, randnums + maxsize - 1) ; if_SHOW_CHAINS(chains[4] = ht->chainLengths(max_chain[4])) ; if_SHOW_NEIGHBORS(neighborhoods[1] = ht->neighborhoodDensities(max_neighbors[1])) ; out << "Emptying hash table " << endl ; hash_test(&tpool,out,threads,1,ht,maxsize,keys,Op_REMOVE,terse,false) ; if_SHOW_CHAINS(chains[5] = ht->chainLengths(max_chain[5])) ; #ifdef SHOW_CHAINS for (size_t i = 0 ; i < 6 ; i++) { print_chain_lengths(out,i,chains[i],max_chain[i]) ; delete [] chains[i] ; } #if defined(SHOW_LOST_CHAINS) if (chains[5] && max_chain[5] > 0) { // try to display the lost deletions out << "lost keys:" << endl ; size_t count = 0 ; ht->iterate(show_lost_keys,&out,&count) ; out << "(end of list)" << endl ; } #endif /* SHOW_LOST_CHAINS */ #endif /* SHOW_CHAINS */ delete ht ; #ifdef SHOW_NEIGHBORHOODS for (size_t i = 0 ; i < 2 ; i++) { print_neighborhoods(out,i,neighborhoods[i],max_neighbors[i]) ; delete [] neighborhoods[i] ; } #endif /* SHOW_NEIGHBORHOODS */ return ; }
void testPool() { ThreadPool tpool(3, 20); int argc = 2; std::cout << "==========test fucn===num = "<< argc << std::endl; time_t ts = std::time(nullptr); for(int i = 0; i < argc; i ++) { std::tr1::function<void()> trun; trun = std::tr1::bind(srun, i); tpool.start(trun); /* std::time_t cr = std::time(nullptr); while((cr - ts) < 2) { cr = std::time(nullptr); } ts = std::time(nullptr); */ } tpool.collect(); std::cout << "thread pool size = " << tpool.allocate() << std::endl; tpool.joinAll(); tpool.stopAll(); argc = 30; std::cout << "==========test member fucn===num = "<< argc << std::endl; for(int i = 0; i < argc; i ++) { //std::tr1::function<void()> trun; //trun = std::tr1::bind(&TestThreadPool::run, this, i); setNumber(i); bool success = false; unsigned long cnt = 0; while(!success && cnt < 3) { success = tpool.start(this); ts = std::time(nullptr); if(success) { std::cout <<"===new add thread id = "<< i <<",current thread size = "<< tpool.allocate() << std::endl; } else { std::cout <<"===add thread failed id = "<< i <<",current thread size = "<< tpool.allocate() << std::endl; std::time_t cr = std::time(nullptr); while((cr - ts) < 10) { cr = std::time(nullptr); } cnt ++; tpool.collect(); std::cout << "thread pool size = "<< tpool.allocate() <<",the cnt = "<< _cnt << std::endl; } } /* std::time_t cr = std::time(nullptr); while((cr - ts) < 2) { cr = std::time(nullptr); } ts = std::time(nullptr); */ } ts = std::time(nullptr); std::time_t cr = std::time(nullptr); while((cr - ts) < 100) { cr = std::time(nullptr); } tpool.collect(); std::cout << "thread pool size = "<< tpool.allocate() <<",the cnt = "<< _cnt << std::endl; tpool.joinAll(); tpool.stopAll(); LOGGER_STREAM(Logger::getLogger("http.log")) << "the pool stop,thread pool size =" << "the cnt = " << _cnt << "\n"; Logger::releseLogger("http.log"); }
void simulate( ) { // use the last population as the first population for the next round std::swap( m_pop[ 0 ], m_pop[ m_pop.size() - 1 ] ); std::swap( m_fits[ 0 ], m_fits[ m_fits.size() - 1] ); size_t all_size = buildProjectedPopulations( ); #ifdef DEBUGGING BOOST_LOG_TRIVIAL( debug ) << "Generation " << m_generation << ": " << pN << " individuals; " << pM << " new alleles"; BOOST_LOG_TRIVIAL( debug ) << "Free space: " << free_count << "; alleles: " << m_allele_space.size(); BOOST_LOG_TRIVIAL( debug ) << "Rescaling child population to be: " << pN << " individuals x " << all_size << " alleles"; #endif // DEBUGGING // grow the free space buffer to a size relative to the last projected population m_free_space.resetBuffers( m_pop.back()->getMaxBlocks() ); // bool is_all_neutral = m_allele_space.isAllNeutral(); bool is_all_neutral = m_trait_space.isAllNeutral(); thread_pool_type tpool( m_thread_count.m_tc ); const size_t TC = m_thread_count.m_tc + 1; std::vector< parameter_type * > params; for( int j = 0; j < m_thread_count.m_tc; ++j ) { free_buffer_type tbuf = m_free_space.getThreadBuffer( j ); m_worker_off_gens[ j ]->reset( tbuf ); unsigned int prev_start = 0, prev_end = m_pop[ 0 ]->getIndividualCount(); for( unsigned int p = 1; p < m_pop.size(); ++p ) { unsigned int pN = m_pop[ p ]->getIndividualCount(); size_t BATCH_SIZE = (pN / TC) + ((pN % TC > 0) ? 1 : 0); unsigned int off_idx = j * BATCH_SIZE; parameter_type * param = new parameter_type( m_pop[ p - 1 ], m_pop[ p ], m_fits[ p - 1], m_fits[ p ], m_mut_pools[ p - 1], m_mut_dists[ p - 1], prev_start, prev_end, off_idx, off_idx + BATCH_SIZE, is_all_neutral ); m_worker_off_gens[ j ]->addPopulation( param ); params.push_back( param ); prev_start = off_idx; prev_end = off_idx + BATCH_SIZE; } } m_main_off_gen.reset( m_free_space.getThreadBuffer( m_thread_count.m_tc ) ); unsigned int prev_start = 0, prev_end = m_pop[ 0 ]->getIndividualCount(); for( unsigned int p = 1; p < m_pop.size(); ++p ) { unsigned int pN = m_pop[ p ]->getIndividualCount(); size_t BATCH_SIZE = (pN / TC) + ((pN % TC > 0) ? 1 : 0); unsigned int off_idx = m_thread_count.m_tc * BATCH_SIZE; parameter_type * param = new parameter_type( m_pop[ p - 1 ], m_pop[ p ], m_fits[ p - 1], m_fits[ p ], m_mut_pools[ p - 1], m_mut_dists[ p - 1], prev_start, prev_end, off_idx, pN, is_all_neutral ); m_main_off_gen.addPopulation( param ); params.push_back( param ); prev_start = off_idx; prev_end = pN; } tpool.post_list( m_worker_off_gens, m_thread_count.m_tc ); m_main_off_gen(); tpool.sync(); m_free_space.finalize(all_size); recordAlleles(); while( ! params.empty() ) { parameter_type * p = params.back(); params.pop_back(); delete p; } ++m_generation; }