Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
check_gentle_threads.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file threads/check_gentle_threads.cpp (January 6, 2018)
3  * \brief
4  * Check odd gentle numbers for the varsigma_odd problem
5  * and print bad numbers.
6  *
7  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
8  * http://www.opimedia.be/
9  */
10 
11 // \cond
12 #include <cstdlib>
13 
14 #include <algorithm>
15 #include <chrono>
16 #include <iostream>
17 // \endcond
18 
19 #include "../common/helper/helper.hpp"
20 #include "threads/threads.hpp"
21 
22 
23 
24 /* ***********
25  * Prototype *
26  *************/
27 
28 void
30 
31 
32 
33 /* **********
34  * Function *
35  ************/
36 
37 void
39  std::cerr << "Usage: check_gentle [options] (multi-threads version)" << std::endl
40  << std::endl
41  << "Version options of the parallel algorithm:" << std::endl
42  << " --by-range range of number for each thread with a barrier" << std::endl
43  << " --dynamic like by range but without barrier (by default)" << std::endl
44  << " --one-by-one one number for each thread with a barrier" << std::endl
45  << std::endl
46  << "Options:" << std::endl
47  << " --bad-table filename load bad numbers from this file" << std::endl
48  << " (If load bad numbers then must be contains all bad numbers < first." << std::endl
49  << " Can load them with several files.)" << std::endl
50  << " --first n first odd number to check (3 by default)" << std::endl
51  << " --last n last odd number to check (1000001 by default)" << std::endl
52  << " --nb n number of odd numbers to check" << std::endl
53  << " --nb-thread n number of threads (2 by default)" << std::endl
54  << " --no-print do not print bad numbers" << std::endl;
55 
56  exit(EXIT_FAILURE);
57 }
58 
59 
60 
61 /* ******
62  * Main *
63  ********/
64 int
65 main(int argc, const char* const argv[]) {
66  // Load primes table
68  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
69  << std::endl;
70  help_and_exit();
71  }
72 
73 
74  enum Algos: unsigned int {by_range, dynamic, one_by_one};
75 
76  const std::string algo_strings[3] = {"by-range",
77  "dynamic",
78  "one-by-one"};
79 
80  unsigned int algo = dynamic;
81  std::vector<sigmaodd::nat_type> bad_table;
82  sigmaodd::nat_type first = 3;
83  sigmaodd::nat_type last = 1000001;
84  unsigned int nb_thread = 2;
85  bool print_bad = true;
86 
87 
88  // Read command line parameters
89  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
90  const std::string param(argv[i]);
91 
92  if (param == "--bad-table") {
93  std::string filename = helper::get_string(argc, argv, ++i, &help_and_exit);
94 
95  if (filename.empty() || !helper::is_file_exists(filename)) {
96  std::cerr << "! Failed reading file \"" << filename << '"' << std::endl;
97  help_and_exit();
98  }
99 
100  bad_table = sigmaodd::load_bad_table(filename, bad_table);
101  if (bad_table.empty()) {
102  std::cerr << "! Wrong format for the bad table file \"" << filename << '"' << std::endl;
103  help_and_exit();
104  }
105  }
106  else if (param == "--by-range") {
107  algo = by_range;
108  }
109  else if (param == "--first") {
110  first = std::max(3ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
111  if (sigmaodd::is_even(first)) {
112  ++first;
113  }
114  }
115  else if (param == "--dynamic") {
116  algo = dynamic;
117  }
118  else if (param == "--last") {
119  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
120  if (sigmaodd::is_even(last)) {
121  --last;
122  }
123  }
124  else if (param == "--nb") {
125  last = first + helper::get_ulong(argc, argv, ++i, &help_and_exit)*2 - 1;
126  }
127  else if (param == "--nb-thread") {
128  nb_thread = static_cast<unsigned int>(helper::get_ulong(argc, argv, ++i, &help_and_exit));
129  if ((nb_thread == 0) || (nb_thread > 128)) {
130  help_and_exit();
131  }
132  }
133  else if (param == "--no-print") {
134  print_bad = false;
135  }
136  else if (param == "--one-by-one") {
137  algo = one_by_one;
138  }
139  else {
140  help_and_exit();
141  }
142  }
143 
144  const std::set<sigmaodd::nat_type> bad_table_set(bad_table.cbegin(), bad_table.cend());
145 
146  bad_table.clear();
147 
148  const auto minmax = std::minmax_element(bad_table_set.cbegin(), bad_table_set.cend());
149 
150 
151  // Print intern configuration
153  std::cout << "hardware_concurrency: " << std::thread::hardware_concurrency() << std::endl;
154 
155  // Print parameters
156  std::cout << "threads/check_gentle"
157  << "\t# threads: " << nb_thread
158  << "\tFirst: " << first
159  << "\tLast: " << last
160  << "\tNb: " << (first <= last
161  ? (last - first)/2 + 1
162  : 0)
163  << "\tAlgorithm: " << algo_strings[algo];
164  if (!bad_table_set.empty()) {
165  std::cout << "\t# bad numbers loaded: " << bad_table_set.size()
166  << "\tBetween " << *minmax.first
167  << "\tand " << *minmax.second << std::endl;
168  }
169  std::cout << std::endl;
170 
171  // Print table legend
172  std::cout << std::endl
173  << 'n' << std::endl;
174  std::cout.flush();
175 
176 
177  // Main calculation
178  const std::chrono::steady_clock::time_point clock_start = std::chrono::steady_clock::now();
179 
180  if (algo == by_range) {
182  bad_table_set,
183  print_bad);
184  }
185  else if (algo == dynamic) {
187  bad_table_set,
188  print_bad);
189  }
190  else if (algo == one_by_one) {
192  bad_table_set,
193  print_bad);
194  }
195  else {
196  exit(EXIT_FAILURE);
197  }
198 
199  // End
200  std::chrono::duration<double> duration = std::chrono::steady_clock::now() - clock_start;
201 
202  std::cout << "Total duration: " << helper::duration_to_string(duration)
203  << std::endl;
204 
205  return EXIT_SUCCESS;
206 }
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
int main(int argc, const char *const argv[])
const std::string prime_filename
Default filename for the binary file "big_data/prime28.bin".
Definition: primes.cpp:35
constexpr bool is_even(nat_type n)
Return true iff n is even.
std::string duration_to_string(std::chrono::duration< double > duration_second)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:61
std::set< nat_type > threads_check_gentle_varsigma_odd__dynamic(unsigned int nb_thread, nat_type first_n, nat_type last_n, const std::set< nat_type > &previous_bad_table, bool print_bad, unsigned int range_size, unsigned int master_range_size)
Check in the order all odd gentle numbers between first_n and last_n, and if print_bad then print all...
Definition: threads.cpp:107
Implementation of the threads parallel algorithms presented in the report.
bool read_primes_table()
Read the binary file prime_filename to fill the table with all primes < 2^28. This table must be read...
Definition: primes.cpp:241
bool is_file_exists(std::string filename)
Return true iff the file (or directory) exists.
Definition: helper.cpp:140
void help_and_exit()
std::set< nat_type > threads_check_gentle_varsigma_odd__by_range(unsigned int nb_thread, nat_type first_n, nat_type last_n, const std::set< nat_type > &previous_bad_table, bool print_bad, unsigned int range_size)
Definition: threads.cpp:27
std::string get_string(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in string.
Definition: helper.cpp:92
std::set< nat_type > threads_check_gentle_varsigma_odd__one_by_one(unsigned int nb_thread, nat_type first_n, nat_type last_n, const std::set< nat_type > &previous_bad_table, bool print_bad)
Definition: threads.cpp:227
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
unsigned long get_ulong(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:124
std::vector< nat_type > load_bad_table(const std::string &filename, const std::vector< nat_type > &bad_table)
Read a file that contains list of bad numbers, add after bad_table, and return the result...
Definition: helper.cpp:100