| 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_fractal_H |
| 18 |
#define TBB_examples_fractal_H |
| 19 |
|
| 20 |
#include <atomic> |
| 21 |
|
| 22 |
#include "oneapi/tbb/task_arena.h" |
| 23 |
#include "oneapi/tbb/task_group.h" |
| 24 |
|
| 25 |
#include "common/gui/video.hpp" |
| 26 |
#include "common/utility/get_default_num_threads.hpp" |
| 27 |
|
| 28 |
//! Fractal class |
| 29 |
class fractal { |
| 30 |
//! Left corner of the fractal area |
| 31 |
int off_x, off_y; |
| 32 |
//! Size of the fractal area |
| 33 |
int size_x, size_y; |
| 34 |
|
| 35 |
//! Fractal properties |
| 36 |
float cx, cy; |
| 37 |
float magn; |
| 38 |
float step; |
| 39 |
unsigned int max_iterations; |
| 40 |
|
| 41 |
//! Drawing memory object for rendering |
| 42 |
const drawing_memory &dm; |
| 43 |
|
| 44 |
//! One pixel calculation routine |
| 45 |
color_t calc_one_pixel(int x, int y) const; |
| 46 |
//! Clears the fractal area |
| 47 |
void clear(); |
| 48 |
//! Draws the border around the fractal area |
| 49 |
void draw_border(bool is_active); |
| 50 |
//! Renders the fractal |
| 51 |
void render(oneapi::tbb::task_group_context &context); |
| 52 |
//! Check if the point is inside the fractal area |
| 53 |
bool check_point(int x, int y) const; |
| 54 |
|
| 55 |
public: |
| 56 |
//! Constructor |
| 57 |
fractal(const drawing_memory &dm) : step(0.2), dm(dm) { |
| 58 |
#if _MSC_VER && _WIN64 && !__INTEL_COMPILER |
| 59 |
// Workaround for MSVC x64 compiler issue |
| 60 |
volatile int i = 0; |
| 61 |
#endif |
| 62 |
} |
| 63 |
//! Runs the fractal calculation |
| 64 |
void run(oneapi::tbb::task_group_context &context); |
| 65 |
//! Renders the fractal rectangular area |
| 66 |
void render_rect(int x0, int y0, int x1, int y1) const; |
| 67 |
|
| 68 |
void move_up() { |
| 69 |
cy += step; |
| 70 |
} |
| 71 |
void move_down() { |
| 72 |
cy -= step; |
| 73 |
} |
| 74 |
void move_left() { |
| 75 |
cx += step; |
| 76 |
} |
| 77 |
void move_right() { |
| 78 |
cx -= step; |
| 79 |
} |
| 80 |
|
| 81 |
void zoom_in() { |
| 82 |
magn *= 2.; |
| 83 |
step /= 2.; |
| 84 |
} |
| 85 |
void zoom_out() { |
| 86 |
magn /= 2.; |
| 87 |
step *= 2.; |
| 88 |
} |
| 89 |
|
| 90 |
void quality_inc() { |
| 91 |
max_iterations += max_iterations / 2; |
| 92 |
} |
| 93 |
void quality_dec() { |
| 94 |
max_iterations -= max_iterations / 2; |
| 95 |
} |
| 96 |
|
| 97 |
friend class fractal_group; |
| 98 |
}; |
| 99 |
|
| 100 |
//! The group of fractals |
| 101 |
class fractal_group { |
| 102 |
//! Fractals definition |
| 103 |
fractal f0, f1; |
| 104 |
//! Number of frames to calculate |
| 105 |
std::atomic<int> num_frames[2]; |
| 106 |
|
| 107 |
//! Contexts, arenas and groups for concurrent computation |
| 108 |
oneapi::tbb::task_group_context context[2]; |
| 109 |
oneapi::tbb::task_arena arenas[2]; |
| 110 |
oneapi::tbb::task_group groups[2]; |
| 111 |
|
| 112 |
//! Border type enumeration |
| 113 |
enum BORDER_TYPE { BORDER_INACTIVE = 0, BORDER_ACTIVE }; |
| 114 |
|
| 115 |
//! The number of the threads |
| 116 |
int num_threads; |
| 117 |
//! The active (high priority) fractal number |
| 118 |
int active; |
| 119 |
|
| 120 |
//! Draws the borders around the fractals |
| 121 |
void draw_borders(); |
| 122 |
//! Sets priorities for fractals calculations |
| 123 |
void set_priorities(); |
| 124 |
|
| 125 |
public: |
| 126 |
//! Constructor |
| 127 |
fractal_group(const drawing_memory &_dm, |
| 128 |
int num_threads = utility::get_default_num_threads(), |
| 129 |
unsigned int max_iterations = 100000, |
| 130 |
int num_frames = 1); |
| 131 |
//! Run calculation |
| 132 |
void run(bool create_second_fractal = true); |
| 133 |
//! Mouse event handler |
| 134 |
void mouse_click(int x, int y); |
| 135 |
//! Fractal calculation routine |
| 136 |
void calc_fractal(int num); |
| 137 |
//! Get number of threads |
| 138 |
int get_num_threads() const { |
| 139 |
return num_threads; |
| 140 |
} |
| 141 |
//! Reset the number of frames to be not less than the given value |
| 142 |
void set_num_frames_at_least(int n); |
| 143 |
//! Switch active fractal |
| 144 |
void switch_active(int new_active = -1); |
| 145 |
//! Get active fractal |
| 146 |
fractal &get_active_fractal() { |
| 147 |
return active ? f1 : f0; |
| 148 |
} |
| 149 |
|
| 150 |
void active_fractal_zoom_in() { |
| 151 |
get_active_fractal().zoom_in(); |
| 152 |
context[active].cancel_group_execution(); |
| 153 |
} |
| 154 |
void active_fractal_zoom_out() { |
| 155 |
get_active_fractal().zoom_out(); |
| 156 |
context[active].cancel_group_execution(); |
| 157 |
} |
| 158 |
void active_fractal_quality_inc() { |
| 159 |
get_active_fractal().quality_inc(); |
| 160 |
context[active].cancel_group_execution(); |
| 161 |
} |
| 162 |
void active_fractal_quality_dec() { |
| 163 |
get_active_fractal().quality_dec(); |
| 164 |
context[active].cancel_group_execution(); |
| 165 |
} |
| 166 |
void active_fractal_move_up() { |
| 167 |
get_active_fractal().move_up(); |
| 168 |
context[active].cancel_group_execution(); |
| 169 |
} |
| 170 |
void active_fractal_move_down() { |
| 171 |
get_active_fractal().move_down(); |
| 172 |
context[active].cancel_group_execution(); |
| 173 |
} |
| 174 |
void active_fractal_move_left() { |
| 175 |
get_active_fractal().move_left(); |
| 176 |
context[active].cancel_group_execution(); |
| 177 |
} |
| 178 |
void active_fractal_move_right() { |
| 179 |
get_active_fractal().move_right(); |
| 180 |
context[active].cancel_group_execution(); |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
#endif /* TBB_examples_fractal_H */ |