Sierra Toolkit Version of the Day
MemoryUsage.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #include <stk_mesh/base/MemoryUsage.hpp>
00011 #include <stk_mesh/base/GetEntities.hpp>
00012 
00013 #include <iostream>
00014 
00015 namespace stk {
00016 namespace mesh {
00017 
00018 void compute_memory_usage(const BulkData& bulk, MemoryUsage& mem_usage)
00019 {
00020   mem_usage.entity_rank_names = bulk.mesh_meta_data().entity_rank_names();
00021 
00022   const FieldVector& fields = bulk.mesh_meta_data().get_fields();
00023   mem_usage.num_fields = fields.size();
00024   mem_usage.field_bytes = fields.size()*sizeof(FieldBase);
00025   for(size_t i=0; i<fields.size(); ++i) {
00026     mem_usage.field_bytes += fields[i]->name().length();
00027     mem_usage.field_bytes += sizeof(FieldRestriction)*fields[i]->restrictions().size();
00028   }
00029 
00030   const PartVector& parts = bulk.mesh_meta_data().get_parts();
00031   mem_usage.num_parts = parts.size();
00032   mem_usage.part_bytes = parts.size()*sizeof(Part);
00033   for(size_t i=0; i<parts.size(); ++i) {
00034     mem_usage.part_bytes += parts[i]->name().length();
00035     mem_usage.part_bytes += sizeof(Part*)        * parts[i]->supersets().size();
00036     mem_usage.part_bytes += sizeof(Part*)        * parts[i]->subsets().size();
00037     mem_usage.part_bytes += sizeof(Part*)        * parts[i]->intersection_of().size();
00038     mem_usage.part_bytes += sizeof(PartRelation) * parts[i]->relations().size();
00039   }
00040 
00041   size_t total_bytes = mem_usage.field_bytes + mem_usage.part_bytes;
00042 
00043   mem_usage.entity_counts.clear();
00044   mem_usage.downward_relation_counts.clear();
00045   mem_usage.upward_relation_counts.clear();
00046   mem_usage.bucket_counts.clear();
00047   mem_usage.bucket_bytes.clear();
00048 
00049   Selector all = bulk.mesh_meta_data().universal_part();
00050   count_entities(all, bulk, mem_usage.entity_counts);
00051 
00052   size_t nranks = mem_usage.entity_counts.size();
00053   mem_usage.downward_relation_counts.resize(nranks, 0);
00054   mem_usage.upward_relation_counts.resize(nranks, 0);
00055   mem_usage.bucket_counts.resize(nranks, 0);
00056   mem_usage.bucket_bytes.resize(nranks, 0);
00057 
00058   std::vector<Entity*> entities;
00059   for(size_t i=0; i<nranks; ++i) {
00060     EntityRank rank = i;
00061     total_bytes += mem_usage.entity_counts[rank]*sizeof(Entity);
00062 
00063     get_entities(bulk, rank, entities);
00064 
00065     for(size_t n=0; n<entities.size(); ++n) {
00066       Entity& entity = *entities[n];
00067       for(EntityRank r=0; r<i; ++r) {
00068         unsigned num_rels = entity.relations(r).size();
00069         mem_usage.downward_relation_counts[r] += num_rels;
00070         total_bytes += num_rels*sizeof(Relation);
00071       }
00072       for(EntityRank r=i+1; r<nranks; ++r) {
00073         unsigned num_rels = entity.relations(r).size();
00074         mem_usage.upward_relation_counts[r] += num_rels;
00075         total_bytes += num_rels*sizeof(Relation);
00076       }
00077     }
00078 
00079     const std::vector<Bucket*>& buckets = bulk.buckets(rank);
00080     mem_usage.bucket_counts[rank] = buckets.size();
00081     for(size_t b=0; b<buckets.size(); ++b) {
00082       Bucket& bucket = *buckets[b];
00083       mem_usage.bucket_bytes[rank] += bucket.allocation_size();
00084       total_bytes += bucket.allocation_size();
00085     }
00086   }
00087 
00088   mem_usage.total_bytes = total_bytes;
00089 }
00090 
00091 void print_memory_usage(const MemoryUsage& mem_usage, std::ostream& os)
00092 {
00093   os << "----- stk_mesh Memory Usage: ------"<<std::endl;
00094   os << "Fields:"<<std::endl;
00095   os << "  "<<mem_usage.num_fields<<" fields, "<<mem_usage.field_bytes<<" bytes"<<std::endl;
00096   os << "Parts:"<<std::endl;
00097   os << "  "<<mem_usage.num_parts<<" parts, "<<mem_usage.part_bytes<<" bytes"<<std::endl;
00098   os << "Entities:"<<std::endl;
00099   for(size_t i=0; i<mem_usage.entity_counts.size(); ++i) {
00100     int n = mem_usage.entity_counts[i];
00101     unsigned bytes = n*sizeof(Entity);
00102     if (mem_usage.entity_rank_names.size() > i)
00103       os << "  "<<mem_usage.entity_rank_names[i]<<": ";
00104     else
00105       os << "  Rank "<<i<<": ";
00106     os << n << " entities, "<< bytes<<" bytes"<<std::endl;
00107   }
00108   os << "Downward Relations:"<<std::endl;
00109   for(size_t i=0; i<mem_usage.downward_relation_counts.size(); ++i) {
00110     int n = mem_usage.downward_relation_counts[i];
00111     unsigned bytes = n*sizeof(Relation);
00112     if (mem_usage.entity_rank_names.size() > i)
00113       os << "  "<<mem_usage.entity_rank_names[i]<<": ";
00114     else
00115       os << "  Rank "<<i<<": ";
00116     os << n << " relations, "<< bytes<<" bytes"<<std::endl;
00117   }
00118   os << "Upward Relations:"<<std::endl;
00119   for(size_t i=0; i<mem_usage.upward_relation_counts.size(); ++i) {
00120     int n = mem_usage.upward_relation_counts[i];
00121     unsigned bytes = n*sizeof(Relation);
00122     if (mem_usage.entity_rank_names.size() > i)
00123       os << "  "<<mem_usage.entity_rank_names[i]<<": ";
00124     else
00125       os << "  Rank "<<i<<": ";
00126     os << n << " relations, "<< bytes<<" bytes"<<std::endl;
00127   }
00128   os << "Buckets:"<<std::endl;
00129   for(size_t i=0; i<mem_usage.bucket_counts.size(); ++i) {
00130     int n = mem_usage.bucket_counts[i];
00131     unsigned bytes = mem_usage.bucket_bytes[i];
00132     if (mem_usage.entity_rank_names.size() > i)
00133       os << "  "<<mem_usage.entity_rank_names[i]<<": ";
00134     else
00135       os << "  Rank "<<i<<": ";
00136     os << n << " buckets, "<< bytes<<" bytes"<<std::endl;
00137   }
00138   os << "Total bytes: "<<mem_usage.total_bytes<<" ("<<((double)mem_usage.total_bytes)/(1024*1024)<<"MB)"<<std::endl;
00139 }
00140 
00141 }//namespace mesh
00142 }//namespace stk
00143 
00144 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines