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 #define VIDEO_WINMAIN_ARGS
18 
19 #include <iostream>
20 
21 #include "oneapi/tbb/tick_count.h"
22 #include "oneapi/tbb/global_control.h"
23 
24 #include "common/utility/utility.hpp"
25 #include "common/utility/get_default_num_threads.hpp"
26 
27 #include "seismic_video.hpp"
28 #include "universe.hpp"
29 
30 Universe u;
31 
32 struct RunOptions {
33     //! It is used for console mode for test with different number of threads and also has
34     //! meaning for GUI: threads.first  - use separate event/updating loop thread (>0) or not (0).
35     //!                  threads.second - initialization value for scheduler
36     utility::thread_number_range threads;
37     int numberOfFrames;
38     bool silent;
39     bool parallel;
RunOptionsRunOptions40     RunOptions(utility::thread_number_range threads_,
41                int number_of_frames_,
42                bool silent_,
43                bool parallel_)
44             : threads(threads_),
45               numberOfFrames(number_of_frames_),
46               silent(silent_),
47               parallel(parallel_) {}
48 };
49 
ParseCommandLine(int argc,char * argv[])50 RunOptions ParseCommandLine(int argc, char *argv[]) {
51     // zero number of threads means to run serial version
52     utility::thread_number_range threads(
53         utility::get_default_num_threads, 0, utility::get_default_num_threads());
54 
55     int numberOfFrames = 0;
56     bool silent = false;
57     bool serial = false;
58 
59     utility::parse_cli_arguments(
60         argc,
61         argv,
62         utility::cli_argument_pack()
63             //"-h" option for displaying help is present implicitly
64             .positional_arg(threads, "n-of-threads", utility::thread_number_range_desc)
65             .positional_arg(numberOfFrames,
66                             "n-of-frames",
67                             "number of frames the example processes internally (0 means unlimited)")
68             .arg(silent, "silent", "no output except elapsed time")
69             .arg(serial, "serial", "in GUI mode start with serial version of algorithm"));
70     return RunOptions(threads, numberOfFrames, silent, !serial);
71 }
72 
main(int argc,char * argv[])73 int main(int argc, char *argv[]) {
74     oneapi::tbb::tick_count mainStartTime = oneapi::tbb::tick_count::now();
75     RunOptions options = ParseCommandLine(argc, argv);
76     SeismicVideo video(u, options.numberOfFrames, options.threads.last, options.parallel);
77 
78     // video layer init
79     if (video.init_window(u.UniverseWidth, u.UniverseHeight)) {
80         video.calc_fps = true;
81         video.threaded = options.threads.first > 0;
82         // video is ok, init Universe
83         u.InitializeUniverse(video);
84         // main loop
85         video.main_loop();
86     }
87     else if (video.init_console()) {
88         // do console mode
89         if (options.numberOfFrames == 0) {
90             options.numberOfFrames = 1000;
91             std::cout << "Substituting 1000 for unlimited frames because not running interactively"
92                       << "\n";
93         }
94         for (int p = options.threads.first; p <= options.threads.last;
95              p = options.threads.step(p)) {
96             oneapi::tbb::tick_count xwayParallelismStartTime = oneapi::tbb::tick_count::now();
97             u.InitializeUniverse(video);
98             int numberOfFrames = options.numberOfFrames;
99 
100             if (p == 0) {
101                 //run a serial version
102                 for (int i = 0; i < numberOfFrames; ++i) {
103                     u.SerialUpdateUniverse();
104                 }
105             }
106             else {
107                 oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism,
108                                               p);
109                 for (int i = 0; i < numberOfFrames; ++i) {
110                     u.ParallelUpdateUniverse();
111                 }
112             }
113 
114             if (!options.silent) {
115                 double fps =
116                     options.numberOfFrames /
117                     ((oneapi::tbb::tick_count::now() - xwayParallelismStartTime).seconds());
118                 std::cout << fps << " frame per sec with ";
119                 if (p == 0) {
120                     std::cout << "serial code"
121                               << "\n";
122                 }
123                 else {
124                     std::cout << p << " way parallelism"
125                               << "\n";
126                 }
127             }
128         }
129     }
130     video.terminate();
131     utility::report_elapsed_time((oneapi::tbb::tick_count::now() - mainStartTime).seconds());
132     return 0;
133 }
134