BioDynaMo  v1.03.58-27764645
plot_memory_layout.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
16 
17 #include <algorithm>
18 #include <limits>
19 #include <string>
20 // ROOT
21 #include <TAxis.h>
22 #include <TCanvas.h>
23 #include <TFrame.h>
24 #include <TGraph.h>
25 #include <TGraphAsymmErrors.h>
26 #include <TGraphErrors.h>
27 #include <TH1F.h>
28 #include <TLegend.h>
29 #include <TMath.h>
30 #include <TMultiGraph.h>
31 #include <TPad.h>
32 #include <TStyle.h>
33 // BioDynaMo
34 #include "core/agent/agent.h"
36 #include "core/scheduler.h"
37 #include "core/simulation.h"
38 
39 namespace bdm {
40 
41 // -----------------------------------------------------------------------------
42 void PlotMemoryLayout(const std::vector<Agent*>& agents, int numa_node) {
43  TCanvas c;
44  c.SetCanvasSize(1920, 1200);
45  std::vector<real_t> x(agents.size());
46  std::vector<real_t> y(agents.size());
47 
48  uint64_t min = std::numeric_limits<uint64_t>::max();
49  for (uint64_t i = 0; i < agents.size(); ++i) {
50  auto val = reinterpret_cast<uint64_t>(agents[i]);
51  if (val < min) {
52  min = val;
53  }
54  }
55  for (uint64_t i = 1; i < agents.size(); ++i) {
56  x[i] = i;
57  y[i] = reinterpret_cast<uint64_t>(agents[i]) - min;
58  }
59  TGraph graph(agents.size(), x.data(), y.data());
60  graph.SetTitle(";Agent element index; Virtual memory address");
61  graph.Draw("ap");
62 
63  c.Update();
64  gPad->Modified();
65  gPad->Update();
66  c.Modified();
67  c.cd(0);
69  auto dir = Simulation::GetActive()->GetOutputDir();
70  c.SaveAs(Concat(dir, "/mem-layout-", steps, "-", numa_node, ".png").c_str());
71 }
72 
73 // -----------------------------------------------------------------------------
74 void PlotMemoryHistogram(const std::vector<Agent*>& agents, int numa_node) {
75  TCanvas c;
76  c.SetCanvasSize(1920, 1200);
77 
78  TH1F hist("", "", 100, 1, 10000);
79  hist.SetTitle(";#Delta bytes; Count");
80  for (uint64_t i = 1; i < agents.size(); ++i) {
81  float val = 0;
82  auto t = reinterpret_cast<uint64_t>(agents[i]);
83  auto l = reinterpret_cast<uint64_t>(agents[i - 1]);
84  if (t > l) {
85  val = t - l;
86  } else {
87  val = l - t;
88  }
89  val = std::min(val, 10000.f - 1);
90  hist.Fill(val);
91  }
92  hist.Draw();
93 
94  c.Update();
95  gPad->SetLogy();
96  gPad->Modified();
97  gPad->Update();
98  c.Modified();
99  c.cd(0);
101  auto dir = Simulation::GetActive()->GetOutputDir();
102  c.SaveAs(
103  Concat(dir, "/mem-layout-hist-", steps, "-", numa_node, ".png").c_str());
104 }
105 
106 // -----------------------------------------------------------------------------
107 struct Fen : public Functor<void, Agent*, real_t> {
108  std::vector<int64_t>& diffs;
110  Fen(std::vector<int64_t>& diffs, Agent* query) : diffs(diffs), query(query) {}
111 
112  void operator()(Agent* neighbor, real_t) {
113  if (neighbor == query) {
114  return;
115  }
116  auto t = reinterpret_cast<int64_t>(query);
117  auto l = reinterpret_cast<int64_t>(neighbor);
118  diffs.push_back(t - l);
119  }
120 };
121 
122 // -----------------------------------------------------------------------------
123 struct Fea : public Functor<void, Agent*, AgentHandle> {
124  std::vector<int64_t>& diffs;
125  explicit Fea(std::vector<int64_t>& diffs) : diffs(diffs) {}
126 
127  void operator()(Agent* agent, AgentHandle) {
128  Fen fen(diffs, agent);
129  auto* sim = Simulation::GetActive();
130  auto* env = sim->GetEnvironment();
131  auto squared_radius = env->GetLargestAgentSizeSquared();
132  sim->GetExecutionContext()->ForEachNeighbor(fen, *agent, squared_radius);
133  }
134 };
135 
136 // -----------------------------------------------------------------------------
137 void PlotNeighborMemoryHistogram(bool before) {
138  TCanvas c;
139  c.SetCanvasSize(1920, 1200);
141  if (rm->GetNumAgents() == 0) {
142  return;
143  }
144  std::vector<int64_t> diffs;
145  diffs.reserve(rm->GetNumAgents() * 3);
146  if (!before) {
148  }
149  Fea fea(diffs);
150  rm->ForEachAgent(fea);
151  auto min = std::numeric_limits<float>::max();
152  auto max = std::numeric_limits<float>::min();
153  for (uint64_t i = 0; i < diffs.size(); ++i) {
154  if (diffs[i] < min) {
155  min = diffs[i];
156  }
157  if (diffs[i] > max) {
158  max = diffs[i];
159  }
160  }
161  min--;
162  max++;
163  uint64_t nbins = std::max(static_cast<uint64_t>(100u),
164  static_cast<uint64_t>((max - min) / 20000));
165  TH1F hist("", "", nbins, min, max);
166  hist.SetTitle(";#Delta bytes; Count");
167  for (uint64_t i = 0; i < diffs.size(); ++i) {
168  hist.Fill(diffs[i]);
169  }
170  hist.Draw();
171 
172  c.Update();
173  gPad->SetLogy();
174  gPad->Modified();
175  gPad->Update();
176  c.Modified();
177  c.cd(0);
179  auto dir = Simulation::GetActive()->GetOutputDir();
180  std::string suffix = "-end";
181  if (before) {
182  suffix = "-begin";
183  }
184  c.SaveAs(
185  Concat(dir, "/mem-layout-neighbor-hist-", steps, suffix, ".png").c_str());
186  c.SaveAs(
187  Concat(dir, "/mem-layout-neighbor-hist-", steps, suffix, ".C").c_str());
188 }
189 
190 } // namespace bdm
bdm::Fea::Fea
Fea(std::vector< int64_t > &diffs)
Definition: plot_memory_layout.cc:125
bdm
Definition: agent.cc:39
bdm::PlotMemoryHistogram
void PlotMemoryHistogram(const std::vector< Agent * > &agents, int numa_node)
Definition: plot_memory_layout.cc:74
bdm::Fen
Definition: plot_memory_layout.cc:107
plot_memory_layout.h
bdm::real_t
double real_t
Definition: real_t.h:21
scheduler.h
bdm::Fea::operator()
void operator()(Agent *agent, AgentHandle)
Definition: plot_memory_layout.cc:127
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::PlotNeighborMemoryHistogram
void PlotNeighborMemoryHistogram(bool before)
Definition: plot_memory_layout.cc:137
bdm::Simulation::GetScheduler
Scheduler * GetScheduler()
Definition: simulation.cc:279
bdm::Functor
Definition: functor.h:24
bdm::Fen::query
Agent * query
Definition: plot_memory_layout.cc:109
bdm::Environment::Update
void Update()
Definition: environment.h:48
bdm::Fea::diffs
std::vector< int64_t > & diffs
Definition: plot_memory_layout.cc:124
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:261
bdm::Fea
Definition: plot_memory_layout.cc:123
bdm::Concat
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
Definition: string.h:70
agent.h
bdm::Fen::diffs
std::vector< int64_t > & diffs
Definition: plot_memory_layout.cc:108
bdm::Simulation::GetEnvironment
Environment * GetEnvironment()
Definition: simulation.cc:277
bdm::Fen::operator()
void operator()(Agent *neighbor, real_t)
Definition: plot_memory_layout.cc:112
bdm::PlotMemoryLayout
void PlotMemoryLayout(const std::vector< Agent * > &agents, int numa_node)
Definition: plot_memory_layout.cc:42
environment.h
simulation.h
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:67
bdm::Simulation::GetOutputDir
const std::string & GetOutputDir() const
Returns the output directory for this specific simulation.
Definition: simulation.cc:302
bdm::Fen::Fen
Fen(std::vector< int64_t > &diffs, Agent *query)
Definition: plot_memory_layout.cc:110
bdm::Scheduler::GetSimulatedSteps
uint64_t GetSimulatedSteps() const
This function returns the numer of simulated steps (=iterations).
Definition: scheduler.cc:160
bdm::AgentHandle
Definition: agent_handle.h:29