1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef TBB_examples_seismic_universe_H
18 #define TBB_examples_seismic_universe_H
19 
20 #ifndef UNIVERSE_WIDTH
21 #define UNIVERSE_WIDTH 1024
22 #endif
23 #ifndef UNIVERSE_HEIGHT
24 #define UNIVERSE_HEIGHT 512
25 #endif
26 
27 #include "oneapi/tbb/partitioner.h"
28 
29 #include "common/gui/video.hpp"
30 
31 class Universe {
32 public:
33     enum { UniverseWidth = UNIVERSE_WIDTH, UniverseHeight = UNIVERSE_HEIGHT };
34 
35 private:
36     //in order to avoid performance degradation due to cache aliasing issue
37     //some padding is needed after each row in array, and between array themselves.
38     //the padding is achieved by adjusting number of rows and columns.
39     //as the compiler is forced to place class members of the same clause in order of the
40     //declaration this seems to be the right way of padding.
41 
42     //magic constants added below are chosen experimentally for 1024x512.
43     enum { MaxWidth = UniverseWidth + 1, MaxHeight = UniverseHeight + 3 };
44 
45     typedef float ValueType;
46 
47     //! Horizontal stress
48     ValueType S[MaxHeight][MaxWidth];
49 
50     //! Velocity at each grid point
51     ValueType V[MaxHeight][MaxWidth];
52 
53     //! Vertical stress
54     ValueType T[MaxHeight][MaxWidth];
55 
56     //! Coefficient related to modulus
57     ValueType M[MaxHeight][MaxWidth];
58 
59     //! Damping coefficients
60     ValueType D[MaxHeight][MaxWidth];
61 
62     //! Coefficient related to lightness
63     ValueType L[MaxHeight][MaxWidth];
64 
65     enum { ColorMapSize = 1024 };
66     color_t ColorMap[4][ColorMapSize];
67 
68     enum MaterialType { WATER = 0, SANDSTONE = 1, SHALE = 2 };
69 
70     //! Values are MaterialType, cast to an unsigned char to save space.
71     unsigned char material[MaxHeight][MaxWidth];
72 
73 private:
74     enum { DamperSize = 32 };
75 
76     int pulseTime;
77     int pulseCounter;
78     int pulseX;
79     int pulseY;
80 
81     drawing_memory drawingMemory;
82 
83 public:
84     void InitializeUniverse(video const& colorizer);
85 
86     void SerialUpdateUniverse();
87     void ParallelUpdateUniverse();
88     bool TryPutNewPulseSource(int x, int y);
89     void SetDrawingMemory(const drawing_memory& dmem);
90 
91 private:
92     struct Rectangle;
93     void UpdatePulse();
94     void UpdateStress(Rectangle const& r);
95 
96     void SerialUpdateStress();
97     friend struct UpdateStressBody;
98     friend struct UpdateVelocityBody;
99     void ParallelUpdateStress(oneapi::tbb::affinity_partitioner& affinity);
100 
101     void UpdateVelocity(Rectangle const& r);
102 
103     void SerialUpdateVelocity();
104     void ParallelUpdateVelocity(oneapi::tbb::affinity_partitioner& affinity);
105 };
106 
107 #endif /* TBB_examples_seismic_universe_H */
108