GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
threads.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef GLVIS_THREADS_HPP
13 #define GLVIS_THREADS_HPP
14 
15 #include "vsdata.hpp"
16 #include "stream_reader.hpp"
17 #include <mfem.hpp>
18 #include <thread>
19 #include <atomic>
20 #include <condition_variable>
21 
23 {
24 private:
25  // Pointers to global GLVis data
27  StreamState& curr_state;
28  bool *keep_attr;
29  SdlWindow *thread_wnd;
30 
31  std::mutex glvis_mutex;
32  std::condition_variable glvis_cond;
33 
34  int num_waiting;
35  bool terminating;
36 
37  enum
38  {
39  NO_COMMAND = 0,
40  NEW_MESH_AND_SOLUTION = 1,
41  SCREENSHOT = 2,
42  KEY_COMMANDS = 3,
43  WINDOW_SIZE = 4,
44  WINDOW_TITLE = 5,
45  PAUSE = 6,
46  VIEW_ANGLES = 7,
47  ZOOM = 8,
48  SUBDIVISIONS = 9,
49  VALUE_RANGE = 10,
50  SHADING = 11,
51  VIEW_CENTER = 12,
52  AUTOSCALE = 13,
53  PALETTE = 14,
54  CAMERA = 15,
55  AUTOPAUSE = 16,
56  WINDOW_GEOMETRY = 17,
57  PLOT_CAPTION = 18,
58  AXIS_LABELS = 19,
59  PALETTE_REPEAT = 20
60  };
61 
62  std::atomic<bool> command_ready{false};
63 
64  // command to be executed
65  int command;
66 
67  // command arguments
68  StreamState new_state;
69  std::string screenshot_filename;
70  std::string key_commands;
71  int window_x, window_y;
72  int window_w, window_h;
73  std::string window_title;
74  std::string plot_caption;
75  std::string axis_label_x;
76  std::string axis_label_y;
77  std::string axis_label_z;
78  double view_ang_theta, view_ang_phi;
79  double zoom_factor;
80  int subdiv_tot, subdiv_bdr;
81  double val_min, val_max;
82  std::string shading;
83  double view_center_x, view_center_y;
84  std::string autoscale_mode;
85  int palette, palette_repeat;
86  double camera[9];
87  std::string autopause_mode;
88 
89  // internal variables
90  int autopause;
91 
92  int lock();
93  int signal();
94  void unlock();
95 
96 public:
97  // called by the main execution thread
99  StreamState& thread_state, bool *_keep_attr);
100 
101  // to be used by worker threads
102  bool KeepAttrib() { return *keep_attr; } // may need to sync this
103  bool FixElementOrientations() { return curr_state.fix_elem_orient; }
104 
105  // called by worker threads
106  int NewMeshAndSolution(std::unique_ptr<Mesh> _new_m,
107  std::unique_ptr<GridFunction> _new_g);
108  int Screenshot(const char *filename);
109  int KeyCommands(const char *keys);
110  int WindowSize(int w, int h);
111  int WindowGeometry(int x, int y, int w, int h);
112  int WindowTitle(const char *title);
113  int PlotCaption(const char *caption);
114  int AxisLabels(const char *a_x, const char *a_y, const char *a_z);
115  int Pause();
116  int ViewAngles(double theta, double phi);
117  int Zoom(double factor);
118  int Subdivisions(int tot, int bdr);
119  int ValueRange(double minv, double maxv);
120  int SetShading(const char *shd);
121  int ViewCenter(double x, double y);
122  int Autoscale(const char *mode);
123  int Palette(int pal);
124  int PaletteRepeat(int n);
125  int Camera(const double cam[]);
126  int Autopause(const char *mode);
127 
128  // called by the main execution thread
129  int Execute();
130 
131  // called by the main execution thread
132  void Terminate();
133 
134  void ToggleAutopause();
135 
136  // called by the main execution thread
137  ~GLVisCommand();
138 };
139 
141 {
142 public:
143  using StreamCollection = std::vector<std::unique_ptr<std::istream>>;
144 
145 private:
146  // streams to read data from
147  StreamCollection is;
148 
149  GLVisCommand* glvis_command;
150 
151  // data that may be dynamically allocated by the thread
152  std::unique_ptr<Mesh> new_m;
153  std::unique_ptr<GridFunction> new_g;
154  std::string ident;
155 
156  // thread object
157  std::thread tid;
158  // signal for thread cancellation
159  std::atomic<bool> terminate_thread {false};
160 
161  void execute();
162 
163 public:
165 
167 };
168 
169 #endif
void Terminate()
Definition: threads.cpp:673
int ValueRange(double minv, double maxv)
Definition: threads.cpp:271
int Palette(int pal)
Definition: threads.cpp:333
bool FixElementOrientations()
Definition: threads.hpp:103
int WindowTitle(const char *title)
Definition: threads.cpp:163
int WindowGeometry(int x, int y, int w, int h)
Definition: threads.cpp:145
int SetShading(const char *shd)
Definition: threads.cpp:287
int AxisLabels(const char *a_x, const char *a_y, const char *a_z)
Definition: threads.cpp:193
vector< unique_ptr< istream >> StreamCollection
Definition: glvis.cpp:84
int PaletteRepeat(int n)
Definition: threads.cpp:348
std::vector< std::unique_ptr< std::istream >> StreamCollection
Definition: threads.hpp:143
communication_thread(StreamCollection _is, GLVisCommand *cmd)
Definition: threads.cpp:711
int NewMeshAndSolution(std::unique_ptr< Mesh > _new_m, std::unique_ptr< GridFunction > _new_g)
Definition: threads.cpp:82
bool KeepAttrib()
Definition: threads.hpp:102
int PlotCaption(const char *caption)
Definition: threads.cpp:178
int Execute()
Definition: threads.cpp:396
void ToggleAutopause()
Definition: threads.cpp:688
int KeyCommands(const char *keys)
Definition: threads.cpp:114
bool fix_elem_orient
int WindowSize(int w, int h)
Definition: threads.cpp:129
int Zoom(double factor)
Definition: threads.cpp:240
int Autoscale(const char *mode)
Definition: threads.cpp:318
int Subdivisions(int tot, int bdr)
Definition: threads.cpp:255
int Pause()
Definition: threads.cpp:210
int Screenshot(const char *filename)
Definition: threads.cpp:99
int Autopause(const char *mode)
Definition: threads.cpp:381
GLVisCommand(VisualizationSceneScalarData **_vs, StreamState &thread_state, bool *_keep_attr)
Definition: threads.cpp:20
int ViewCenter(double x, double y)
Definition: threads.cpp:302
int ViewAngles(double theta, double phi)
Definition: threads.cpp:224
int Camera(const double cam[])
Definition: threads.cpp:363