/** * Computer player moves * * @param game_state the game state * @return a new game state */ struct State * computer_player( struct State * game_state ) { struct State * state; struct Move * move; struct GameNode * root; time_t stop; /* create a new game node */ root = new_game_node( game_state, NULL ); /* computer player */ time( &timer ); move = alpha_beta_search( root ); time( &stop ); /* print time */ printf( "Time taken: %d\n", stop - timer ); printf( "Memory used: %lu\n", memory_usage() ); /* print move */ printf( "Move chosen: " ); print_move( move ); /* create a new state and apply move */ state = result( game_state, move ); Free( move, sizeof( struct Move ) ); state->player = opposite_player( game_state->player ); delete_game_node( &root ); return state; }
static void absorb_response(block_request_t* request, const int recv_size) { const int lines = CHECK_CAST_INT(request->dependent_lines.size()); GEODE_ASSERT(lines); const auto first_line = request->dependent_lines[0]; const auto first_block_data = first_line->input_block_data(request->block); const int nodes = first_block_data.size()-PENTAGO_MPI_COMPRESS; const auto event = request->block_lines_event(); #if PENTAGO_MPI_COMPRESS // Uncompress data into a temporary buffer, then copy back to first dependent line const auto buffer = local_fast_uncompress(char_view(first_block_data).slice(0,recv_size),event); memcpy(first_block_data.data(),buffer.data(),memory_usage(buffer)); #endif // Copy data to dependent lines other than the first if (lines>1) for (int i=1;i<lines;i++) { const auto line = request->dependent_lines[i]; const auto block_data = line->input_block_data(request->block); GEODE_ASSERT(block_data.size()==nodes+PENTAGO_MPI_COMPRESS); memcpy(block_data.data(),first_block_data.data(),sizeof(Vector<super_t,2>)*nodes); line->decrement_missing_input_blocks(); } // Decrement here so that the line doesn't deallocate itself before we copy the data to other lines first_line->decrement_missing_input_blocks(); // Deallocate request delete request; }
// Absorb a compressed output block static void absorb_compressed_output(accumulating_block_store_t* output_blocks, const local_id_t local_block_id, const uint8_t dimension, Array<const uint8_t> compressed, Array<Vector<super_t,2>> buffer) { // Uncompress block into temporary buffer, then copy back to buffer so that accumulate can use the temporary. const auto local_buffer = local_fast_uncompress(compressed,output_blocks->local_block_line_event(local_block_id,dimension)); memcpy(buffer.data(),local_buffer.data(),memory_usage(local_buffer)); const auto block_data = buffer.slice(0,local_buffer.size()); // Send to block store output_blocks->accumulate(local_block_id,dimension,block_data); }
static uint64_t max_rank_memory_usage(Ptr<const partition_t> prev_partition_, Ptr<const load_balance_t> prev_load_, const partition_t& partition, const load_balance_t& load) { GEODE_ASSERT(!!prev_partition_==!!prev_load_); const auto prev_partition = prev_partition_?ref(prev_partition_):empty_partition(partition.ranks,0); const auto prev_load = prev_load_?ref(prev_load_):serial_load_balance(prev_partition); GEODE_ASSERT(prev_partition->ranks==partition.ranks); const auto partition_memory = memory_usage(prev_partition)+memory_usage(partition); const auto lines = int(load.lines.max); const auto blocks = load.blocks.max+prev_load->blocks.max; const auto nodes = load.block_nodes.max+prev_load->block_nodes.max; const auto heap = estimate_block_heap_size(blocks,nodes); const auto memory = partition_memory // partition_t + 2*sizeof(accumulating_block_store_t) // block_store + sizeof(block_info_t)*blocks // block_store.block_info + sizeof(Vector<uint64_t,3>)*(prev_partition->sections->sections.size()+partition.sections->sections.size()) // block_store.section_counts + compacting_store_t::memory_usage(blocks,heap) // compacting_store_t + sizeof(line_t)*lines+base_compute_memory_usage(lines); // line_t and line_data_t return memory; }
TEST_F( SystemInformation_TEST , SetUsageSwapAfterSettingTotalSwap__ReturnsTrue ) { std::string memory_total( "1060252k" ); std::string memory_usage( "860252k" ); EXPECT_TRUE( sys_info__.setTotalSwap( memory_total ) ); EXPECT_TRUE( sys_info__.setSwapUsage( memory_usage ) ); }
TEST_F( SystemInformation_TEST , SetUsageSwapToEmptyString__ReturnsFalse ) { std::string memory_total( "1060252k" ); std::string memory_usage( "" ); EXPECT_TRUE( sys_info__.setTotalSwap( memory_total ) ); EXPECT_FALSE( sys_info__.setSwapUsage( memory_usage ) ); }
/* * Only returns data for instances, not for static fields, those might * be larger, or hold larger structures */ int PrintMemoryUsage (MonoObject *obj) { GHashTable *visited = g_hash_table_new (NULL, NULL); int n; n = memory_usage (obj, visited, printObjectSize); g_hash_table_destroy (visited); return n; }
TEST_F( SystemInformation_TEST , GetSwapUsage__ReturnsPercentageOfTotalSwapInUse ) { std::string memory_total( "1060252k" ); std::string memory_usage( "860252k" ); int memory_rxed( 81 ); EXPECT_TRUE( sys_info__.setTotalSwap( memory_total ) ); EXPECT_TRUE( sys_info__.setSwapUsage( memory_usage ) ); EXPECT_EQ( memory_rxed , sys_info__.getSwapUsage() ); }
MultiShearCatalog::MultiShearCatalog( const CoaddCatalog& coaddCat, const ConfigFile& params) : _id(coaddCat.getIdList()), _chippos(coaddCat.getPosList()), _skypos(coaddCat.getSkyPosList()), _flags(coaddCat.getFlagsList()), _skybounds(coaddCat.getSkyBounds()), _params(params) { dbg<<"Start MultiShearCatalog constructor\n"; xdbg<<"memory_usage = "<<memory_usage()<<std::endl; std::complex<double> shear_default(DEFVALPOS,DEFVALPOS); DSmallMatrix22 cov_default; cov_default << DEFVALPOS, 0, 0, DEFVALPOS; const int n = coaddCat.size(); _pix_list.resize(n); _psf_list.resize(n); _se_num.resize(n); _se_pos.resize(n); _input_flags.resize(n,0); _nimages_found.resize(n, 0); _nimages_gotpix.resize(n, 0); _meas_galorder.resize(n,DEFVALNEG); _shear.resize(n,shear_default); _nu.resize(n,DEFVALNEG); _cov.resize(n,cov_default); int galorder = _params.get("shear_gal_order"); BVec shape_default(galorder,1.); shape_default.vec().TMV_setAllTo(DEFVALNEG); _shape.resize(n,shape_default); xdbg<<"after resize, memory_usage = "<<memory_usage()<<std::endl; // Read the names of the component image and catalog files from // the srclist file (given as params.coadd_srclist) readFileLists(); xdbg<<"after readfilelists, memory_usage = "<<memory_usage()<<std::endl; }
void flow_t::post_output_recv(Array<Vector<super_t,2>>* buffer) { PENTAGO_MPI_TRACE("post output recv"); MPI_Request request; if (!buffer->size()) *buffer = large_buffer<Vector<super_t,2>>(sqr(sqr(block_size))+PENTAGO_MPI_COMPRESS_OUTPUTS,uninit); GEODE_ASSERT(buffer->size()==sqr(sqr(block_size))+PENTAGO_MPI_COMPRESS_OUTPUTS); { thread_time_t time(mpi_kind,unevent); if (PENTAGO_MPI_COMPRESS_OUTPUTS) CHECK(MPI_Irecv(buffer->data(),memory_usage(*buffer),MPI_BYTE,MPI_ANY_SOURCE,MPI_ANY_TAG,comms.output_comm,&request)); else CHECK(MPI_Irecv((uint64_t*)buffer->data(),8*buffer->size(),datatype<uint64_t>(),MPI_ANY_SOURCE,MPI_ANY_TAG,comms.output_comm,&request)); } requests.add(request,curry(&flow_t::process_output,this,buffer),true); }
static int memory_usage_array (MonoArray *array, GHashTable *visited) { int total = 0; MonoClass *array_class = mono_object_get_class ((MonoObject *) array); MonoClass *element_class = mono_class_get_element_class (array_class); MonoType *element_type = mono_class_get_type (element_class); if (MONO_TYPE_IS_REFERENCE (element_type)) { int i; for (i = 0; i < mono_array_length (array); i++) { MonoObject *element = mono_array_get (array, gpointer, i); if (element != NULL) total += memory_usage (element, visited); } } return total; }
total += mono_object_get_size (obj); return total; } /* * Only returns data for instances, not for static fields, those might * be larger, or hold larger structures */ static int GetMemoryUsage (MonoObject *this) { GHashTable *visited = g_hash_table_new (NULL, NULL); int n; n = memory_usage (this, visited); g_hash_table_destroy (visited); return n; } static int installed = 0; void install_icall (MonoProfiler *prof, MonoMethod *method, MonoJitInfo* jinfo, int result) { if (installed) return; mono_add_internal_call ("Mono.ObjectServices.ObjectInspector::GetMemoryUsage", GetMemoryUsage); installed = 1;
int computer_vs_computer( char *file ) { char board[ SIZE ][ SIZE ]; /* set up board */ /*int _player = 0; for( int i = 0; i < 8 ; i++ ) { for( int j = 0; j < 8; j++ ) { if( _player == 0 ) { board[i][j] = 'B'; _player = 1; } else { board[i][j] = 'W'; _player = 0; } } if( _player == 0 ) _player = 1; else _player = 0; }*/ setup_board(file,board); /* create a new state */ struct State * state = new_state( board, 'B' ); struct State * temp_state; /* start game */ print_state( state ); temp_state = computer_player_first( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* second move */ printf( "\n" ); print_state( state ); temp_state = computer_player_second( state ); Free( state, sizeof( struct State ) ); state = temp_state; /* regular game */ for( ;; ) { printf( "\n" ); print_state( state ); printf( "\n>> Mem usage before: %ld\n", memory_usage() ); temp_state = computer_player( state ); Free( state, sizeof( struct State ) ); state = temp_state; printf( ">> Mem usage after: %ld\n", memory_usage() ); if( terminal_test( state ) == 1 ) { printf( "\n" ); print_state( state ); printf( "\nNo moves left!... \n" ); printf( "\n%c wins!!!\n", opposite_player( state->player ) ); Free( state, sizeof( struct State ) ); break; } } return 1; }
bool MultiShearCatalog::getPixels(const Bounds& bounds) { // The pixlist object takes up a lot of memory, so at the start // of this function, I clear it out, along with psflist, etc. // Then we loop over sections of the coaddCat and only load the information // for each single-epoch observation that actually falls within the // bounds for the section we are working on. const int nPix = _pix_list.size(); dbg<<"Start getPixels: memory_usage = "<<memory_usage()<<std::endl; for (int i=0;i<nPix;++i) { _pix_list[i].clear(); _psf_list[i].clear(); } dbg<<"After clear: memory_usage = "<<memory_usage()<<std::endl; bool des_qa = _params.read("des_qa",false); try { dbg<<"Start GetPixels for b = "<<bounds<<std::endl; memory_usage(dbgout); // Loop over the files and read pixel lists for each object. // The Transformation and FittedPSF constructors get the name // information from the parameter file, so we use that to set the // names of each component image here. const int nfiles = _image_file_list.size(); for (int ifile=0; ifile<nfiles; ++ifile) { #ifdef ONLY_N_IMAGES if (ifile >= ONLY_N_IMAGES) break; #endif dbg<<"ifile = "<<ifile<<std::endl; // Get the file names Assert(ifile < int(_image_file_list.size())); Assert(ifile < int(_fitpsf_file_list.size())); std::string image_file = _image_file_list[ifile]; std::string fitpsf_file = _fitpsf_file_list[ifile]; dbg<<"Reading image file: "<<image_file<<"\n"; // Set the appropriate parameters _params["image_file"] = image_file; SetRoot(_params,image_file); _params["fitpsf_file"] = fitpsf_file; if (_shear_file_list.size() > 0) { Assert(ifile < int(_shear_file_list.size())); std::string shear_file = _shear_file_list[ifile]; _params["shear_file"] = shear_file; } if (_skymap_file_list.size() > 0) { Assert(ifile < int(_skymap_file_list.size())); std::string skymap_file = _skymap_file_list[ifile]; _params["skymap_file"] = skymap_file; } // Load the pixels dbg<<"Before load pixels for file "<<ifile<< ": memory_usage = "<<memory_usage()<<std::endl; if (!getImagePixelLists(ifile,bounds)) { for (int i=0;i<nPix;++i) { _pix_list[i].clear(); _psf_list[i].clear(); } PixelList::reclaimMemory(); return false; } dbg<<"After load pixels for file "<<ifile<< ": memory_usage = "<<memory_usage()<<std::endl; } } catch (std::bad_alloc) { dbg<<"Caught bad_alloc\n"; double mem = memory_usage(dbgout); double peak_mem = peak_memory_usage(); dbg<<"memory usage = "<<mem<<" MB\n"; dbg<<"peak memory usage = "<<peak_mem<<" MB\n"; if (des_qa) std::cerr<<"STATUS5BEG "; std::cerr << "Memory exhausted in MultShearCatalog.\n" << "Memory Usage in MultiShearCatalog = " << calculateMemoryFootprint()<<" MB \n" << "Actual Virtual Memory Usage = " << mem<<" MB \n" << "Try reducing multishear_section_size or " << "reducing mam_vmem\n" << "(Current values = " << _params["multishear_section_size"] << " , "<<_params["max_vmem"]<<")"; if (des_qa) std::cerr<<" STATUS5END"; std::cerr<<std::endl; dbg << "Memory exhausted in MultShearCatalog.\n" << "Memory Usage in MultiShearCatalog = " << calculateMemoryFootprint()<<" MB \n" << "Actual Virtual Memory Usage = " << mem<<" MB \n" << "Try reducing multishear_section_size or " << "reducing mam_vmem\n" << "(Current values = " << _params["multishear_section_size"] << " , "<<_params["max_vmem"]<<")" << std::endl; exit(1); } dbg <<"Done getPixels\n"; dbg << "Memory Usage in MultiShearCatalog = " << calculateMemoryFootprint()<<" MB \n"; double mem = memory_usage(dbgout); double peak_mem = peak_memory_usage(); double max_mem = double(_params["max_vmem"])*1024.; _params["peak_mem"] = peak_mem; // Keep track... dbg<<"Actual memory usage = "<<mem<<" MB\n"; dbg<<"Peak memory usage = "<<peak_mem<<" MB\n"; dbg<<"Max allowed memory usage = "<<max_mem<<" MB\n"; return true; }
static int memory_usage (MonoObject *obj, GHashTable *visited) { int total = 0; MonoClass *klass; MonoType *type; gpointer iter = NULL; MonoClassField *field; if (g_hash_table_lookup (visited, obj)) return 0; g_hash_table_insert (visited, obj, obj); klass = mono_object_get_class (obj); type = mono_class_get_type (klass); /* This is an array, so drill down into it */ if (type->type == MONO_TYPE_SZARRAY) total += memory_usage_array ((MonoArray *) obj, visited); while ((field = mono_class_get_fields (klass, &iter)) != NULL) { MonoType *ftype = mono_field_get_type (field); gpointer value; if ((ftype->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA)) != 0) continue; /* FIXME: There are probably other types we need to drill down into */ switch (ftype->type) { case MONO_TYPE_CLASS: case MONO_TYPE_OBJECT: mono_field_get_value (obj, field, &value); if (value != NULL) total += memory_usage ((MonoObject *) value, visited); break; case MONO_TYPE_STRING: mono_field_get_value (obj, field, &value); if (value != NULL) total += mono_object_get_size ((MonoObject *) value); break; case MONO_TYPE_SZARRAY: mono_field_get_value (obj, field, &value); if (value != NULL) { total += memory_usage_array ((MonoArray *) value, visited); total += mono_object_get_size ((MonoObject *) value); } break; default: /* printf ("Got type 0x%x\n", ftype->type); */ /* ignore, this will be included in mono_object_get_size () */ break; } } total += mono_object_get_size (obj); return total; }
int computer_vs_computer( void ) { char board[ SIZE ][ SIZE ]; /* set up board */ int _player = 0; for( int i = 0; i < 8 ; i++ ) { for( int j = 0; j < 8; j++ ) { if( _player == 0 ) { board[i][j] = 'B'; _player = 1; } else { board[i][j] = 'W'; _player = 0; } } if( _player == 0 ) _player = 1; else _player = 0; } /* create a new state */ struct State * state = new_state( board, 'B' ); /* create a new game node */ struct GameNode * root = new_game_node( state, NULL ); print_state( root->state ); /* START GAME */ /* start game ( B first ) */ struct GameNode * current_state; struct GameNode * temp_state; temp_state = computer_player_first( root ); current_state = new_game_node( temp_state->state, NULL ); delete_game_node( temp_state ); /* second move */ printf( "\n" ); print_state( current_state->state ); temp_state = computer_player_second( current_state ); current_state = new_game_node( temp_state->state, NULL ); delete_game_node( temp_state ); /* regular game */ for( ;; ) { printf( "\n" ); print_state( current_state->state ); printf( ">> Mem usage before: %ld\n", memory_usage() ); temp_state = computer_player( current_state ); current_state = new_game_node( temp_state->state, NULL ); delete_game_node( temp_state ); printf( ">> Mem usage after: %ld\n", memory_usage() ); if( terminal_test( current_state->state ) == 1 ) { printf( "\n" ); print_state( current_state->state ); printf( "\nNo moves left!... \n" ); printf( "\n%c wins!!!\n", opposite_player( current_state->state->player ) ); delete_game_node( current_state ); break; } } return 1; }
/** * Min of alpha beta * * @param game_state the current game tree state * @param depth the depth of the tree * @param alpha * @param beta * @return a utility value */ static int min_value( struct GameNode * game_state, int depth, int alpha, int beta ) { time_t current_time; time( ¤t_time ); if( ( (current_time - timer) > THINKING_TIME - 1 ) || cutoff_test( game_state->state, depth ) || memory_usage() > MEMORYSIZE ) return eval( game_state->state ); ++depth; int v = INT_MAX; int max_val; struct ListNode * current_b; struct List * a = actions( game_state->state ); /* get possible actions */ /* iterate over all actions */ struct ListNode * current = a->head; while( current != NULL ) { /* create new game node */ struct State * state = result( game_state->state, current->data ); struct GameNode * node = new_game_node( state, game_state ); add_child_game_node( game_state, node ); /* add child node */ max_val = max_value( node, depth, alpha, beta ); if( max_val < v ) { game_state->best_util_val = v; //game_state->best_move = current->data; if( game_state->best_move != NULL ) Free( game_state->best_move, sizeof( struct Move ) ); game_state->best_move = clone_move( current->data ); } v = min( v, max_val ); if( v <= alpha ) { if( game_state->best_move != NULL ) Free( game_state->best_move, sizeof( struct Move ) ); game_state->best_move = clone_move( current->data ); /* free list of actions except for chosen action */ current_b = a->head; while( current_b != NULL ) { Free( current_b->data, sizeof( struct Move ) ); current_b = current_b->next; } delete_list( &a ); return v; } beta = min( beta, v ); current = current->next; } /* free list of actions */ current_b = a->head; while( current_b != NULL ) { Free( current_b->data, sizeof( struct Move ) ); current_b = current_b->next; } delete_list( &a ); return v; }
TEST_F( SystemInformation_TEST , SetUsageSwapWithoutSettingTotalSwap__ReturnsFalse ) { std::string memory_usage( "860252k" ); EXPECT_FALSE( sys_info__.setSwapUsage( memory_usage ) ); }