| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <gtest/gtest.h> |
| 36 |
#include <stk_util/parallel/Parallel.hpp> |
| 37 |
#include <stk_util/util/MemoryTracking.hpp> |
| 38 |
#include <stk_util/environment/perf_util.hpp> |
| 39 |
#include <stk_mesh/base/BulkData.hpp> |
| 40 |
#include <stk_mesh/baseImpl/elementGraph/GraphEdgeData.hpp> |
| 41 |
#include <stk_unit_test_utils/timer.hpp> |
| 42 |
#include <cstdlib> |
| 43 |
|
| 44 |
size_t size_in_bytes(const stk::mesh::impl::ParallelGraphInfo& pllGraphInfo) |
| 45 |
{ |
| 46 |
return sizeof(stk::mesh::impl::ParallelGraphInfo)+pllGraphInfo.capacity()*sizeof(stk::mesh::impl::ParallelGraphInfo::value_type); |
| 47 |
} |
| 48 |
|
| 49 |
TEST(ParallelGraphInfo, TimingInsertErase) |
| 50 |
{ |
| 51 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { GTEST_SKIP(); } |
| 52 |
|
| 53 |
std::cout << "HWM at startup: "<<stk::get_max_hwm_across_procs(MPI_COMM_WORLD)<<std::endl; |
| 54 |
std::cout << "sizeof(GraphEdge): "<<sizeof(stk::mesh::GraphEdge)<<", sizeof(ParallelInfo): "<<sizeof(stk::mesh::impl::ParallelInfo)<<std::endl; |
| 55 |
|
| 56 |
const int arbitrarySeed = 42121; |
| 57 |
std::srand(arbitrarySeed); |
| 58 |
|
| 59 |
const stk::mesh::impl::LocalId minId = 0; |
| 60 |
const stk::mesh::impl::LocalId maxId = 75000; |
| 61 |
|
| 62 |
auto randomId = [&](){ return minId + std::rand()%(maxId-minId); }; |
| 63 |
|
| 64 |
#ifndef NDEBUG |
| 65 |
const unsigned numIters = 100000; |
| 66 |
#else |
| 67 |
const unsigned numIters = 5000000; |
| 68 |
#endif |
| 69 |
const unsigned NUM_RUNS = 5; |
| 70 |
|
| 71 |
constexpr unsigned insertBatchSize = 1000; |
| 72 |
constexpr unsigned deleteBatchSize = 1000; |
| 73 |
|
| 74 |
stk::unit_test_util::BatchTimer batchTimer(MPI_COMM_WORLD); |
| 75 |
batchTimer.initialize_batch_timer(); |
| 76 |
|
| 77 |
size_t memValueToCompareAgainstGold = 0; |
| 78 |
|
| 79 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 80 |
batchTimer.start_batch_timer(); |
| 81 |
|
| 82 |
size_t curMemStart = 0, hwm = 0; |
| 83 |
stk::get_memory_usage(curMemStart, hwm); |
| 84 |
std::cout<<"Start cur-mem: "<<curMemStart<<std::endl; |
| 85 |
|
| 86 |
const int proc = 1; |
| 87 |
stk::mesh::ParallelInfoForGraphEdges pllGraphInfo(proc); |
| 88 |
|
| 89 |
const int otherProc = 2; |
| 90 |
|
| 91 |
{ |
| 92 |
stk::mesh::impl::ParallelGraphInfo insertBatch; |
| 93 |
std::vector<stk::mesh::GraphEdge> deleteBatch; |
| 94 |
|
| 95 |
for(unsigned i=0; i<numIters; ++i) { |
| 96 |
{ |
| 97 |
stk::mesh::impl::LocalId localElemId = randomId(); |
| 98 |
int localSide = 1; |
| 99 |
stk::mesh::impl::LocalId negativeRemoteId = -localElemId; |
| 100 |
int remoteSide = 4; |
| 101 |
stk::mesh::GraphEdge graphEdge(localElemId, localSide, negativeRemoteId, remoteSide); |
| 102 |
|
| 103 |
stk::mesh::impl::ParallelInfo parInfo(otherProc, stk::mesh::INVALID_PERMUTATION, |
| 104 |
stk::topology::HEX_8); |
| 105 |
|
| 106 |
if (!pllGraphInfo.find_parallel_info_for_graph_edge(graphEdge)) { |
| 107 |
insertBatch.push_back(std::make_pair(graphEdge, parInfo)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
{ |
| 112 |
stk::mesh::impl::LocalId localElemId = randomId(); |
| 113 |
int localSide = 1; |
| 114 |
stk::mesh::impl::LocalId negativeRemoteId = -localElemId; |
| 115 |
int remoteSide = 4; |
| 116 |
stk::mesh::GraphEdge graphEdge(localElemId, localSide, negativeRemoteId, remoteSide); |
| 117 |
|
| 118 |
if (pllGraphInfo.find_parallel_info_for_graph_edge(graphEdge)) { |
| 119 |
deleteBatch.push_back(graphEdge); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (!insertBatch.empty() && i%insertBatchSize == 0) { |
| 124 |
stk::util::sort_and_unique(insertBatch, stk::mesh::GraphEdgeLessByElem2()); |
| 125 |
pllGraphInfo.insert_sorted_edges(insertBatch); |
| 126 |
insertBatch.clear(); |
| 127 |
} |
| 128 |
|
| 129 |
if (!deleteBatch.empty() && i%deleteBatchSize == 0) { |
| 130 |
stk::util::sort_and_unique(deleteBatch, stk::mesh::GraphEdgeLessByElem2()); |
| 131 |
pllGraphInfo.erase_edges(deleteBatch); |
| 132 |
deleteBatch.clear(); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
size_t curMemEnd = 0; |
| 138 |
stk::get_memory_usage(curMemEnd, hwm); |
| 139 |
size_t curMemUsed = curMemEnd - curMemStart; |
| 140 |
size_t actualBytes = size_in_bytes(pllGraphInfo.get_parallel_graph_info()); |
| 141 |
memValueToCompareAgainstGold = std::max(actualBytes, memValueToCompareAgainstGold); |
| 142 |
std::cout << "ParallelInfoForGraphEdges Memory Usage: " << stk::human_bytes(curMemUsed) << " pllGraphInfo bytes: "<<actualBytes<<std::endl; |
| 143 |
size_t nedges = pllGraphInfo.get_parallel_graph_info().size(); |
| 144 |
std::cout << "ParallelInfoForGraphEdges (nedges="<<nedges<<") uses approx "<<(actualBytes/nedges)<<" bytes per edge"<<std::endl; |
| 145 |
|
| 146 |
batchTimer.stop_batch_timer(); |
| 147 |
} |
| 148 |
|
| 149 |
batchTimer.print_batch_timing(numIters, memValueToCompareAgainstGold); |
| 150 |
} |
| 151 |
|