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_opencl.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file opencl/check_gentle_opencl.cpp (February 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 <chrono>
15 #include <iostream>
16 // \endcond
17 
18 #include "../common/helper/helper.hpp"
19 
20 #include "opencl/opencl.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] (OpenCL version)" << std::endl
40  << std::endl
41  << "Options:" << std::endl
42  << " --first n first odd number to check (3 by default)" << std::endl
43  << " --infos-first-gpu print infos about the first GPU found and exit" << std::endl
44  << " --infos-opencl print infos about OpenCL and exit" << std::endl
45  << " --last n last odd number to check <= 7927*7927 (1000001 by default)" << std::endl
46  << " --nb n number of odd numbers to check" << std::endl
47  << " --nb-opencl n number of OpenCL units (65536 default, must be changed with --par-factor)" << std::endl
48  << " --no-print do not print bad numbers" << std::endl
49  << " --par-factor parallelize the factorization instead numbers" << std::endl;
50 
51  exit(EXIT_FAILURE);
52 }
53 
54 
55 
56 /* ******
57  * Main *
58  ********/
59 int
60 main(int argc, const char* const argv[]) {
61  // Load primes table
63  std::cerr << "! Impossible to load \"" << sigmaodd::prime_filename << '"' << std::endl
64  << std::endl;
65  help_and_exit();
66  }
67 
68 
69  sigmaodd::nat_type first = 3;
70  sigmaodd::nat_type last = 1000001;
71  unsigned int nb_opencl = 65536;
72  bool par_factor = false;
73  bool print_bad = true;
74 
75 
76  // Read command line parameters
77  for (unsigned int i = 1; i < static_cast<unsigned int>(argc); ++i) {
78  const std::string param(argv[i]);
79 
80  if (param == "--first") {
81  first = std::max(3ul, helper::get_ulong(argc, argv, ++i, &help_and_exit));
82  if (sigmaodd::is_even(first)) {
83  ++first;
84  }
85  }
86  else if (param == "--infos-first-gpu") {
87  const cl::Device device = opencl::get_first_device_gpu();
88 
89  std::cout << "OpenCL first GPU device found:" << std::endl;
90  opencl::print_device(device);
91 
92  exit(EXIT_SUCCESS);
93  }
94  else if (param == "--infos-opencl") {
95  std::cout << "OpenCL platforms and devices:" << std::endl;
97 
98  exit(EXIT_SUCCESS);
99  }
100  else if (param == "--last") {
101  last = helper::get_ulong(argc, argv, ++i, &help_and_exit);
102  if (sigmaodd::is_even(last)) {
103  --last;
104  }
105  }
106  else if (param == "--nb") {
107  last = first + helper::get_ulong(argc, argv, ++i, &help_and_exit)*2 - 1;
108  }
109  else if (param == "--nb-opencl") {
110  nb_opencl = helper::get_uint(argc, argv, ++i, &help_and_exit);
111  }
112  else if (param == "--no-print") {
113  print_bad = false;
114  }
115  else if (param == "--par-factor") {
116  par_factor = true;
117  }
118  else {
119  help_and_exit();
120  }
121  }
122 
123 
124  // Print intern configuration
126 
127  // Print parameters
128  std::cout << "opencl/check_gentle"
129  << "\tFirst: " << first
130  << "\tLast: " << last
131  << "\tNb: " << (first <= last
132  ? (last - first)/2 + 1
133  : 0)
134  << "\tNb OpenCL: " << nb_opencl
135  << std::endl;
136 
137  // Print table legend
138  std::cout << std::endl
139  << 'n' << std::endl;
140  std::cout.flush();
141 
142 
143  // Main calculation
144  const std::chrono::steady_clock::time_point clock_start = std::chrono::steady_clock::now();
145 
146  if (par_factor) {
147  if ((nb_opencl < 2) || (sigmaodd::divide_until_odd(nb_opencl) != 1)) {
148  help_and_exit();
149  }
151  }
152  else {
153  opencl::opencl_check_gentle_varsigma_odd(first, last, nb_opencl, print_bad);
154  }
155 
156  // End
157  std::chrono::duration<double> duration = std::chrono::steady_clock::now() - clock_start;
158 
159  std::cout << "Total duration: \t" << helper::duration_to_string(duration)
160  << std::endl;
161 
162  return EXIT_SUCCESS;
163 }
cl::Device get_first_device_gpu()
Return the first GPU device found. If not found then print an error message and exit.
Definition: helper.cpp:194
std::set< nat_type > opencl_check_gentle_varsigma_odd(nat_type first_n, nat_type last_n, unsigned int opencl_nb_number, bool print_bad, bool print_time)
Check in the order all odd gentle numbers between first_n and last_n, and if print_bad then print all...
Definition: opencl.cpp:31
void help_and_exit()
uint64_t nat_type
Type for natural number used in all code, on 64 bits.
Definition: helper.hpp:33
const std::string prime_filename
Default filename for the binary file "big_data/prime28.bin".
Definition: primes.cpp:35
nat_type divide_until_odd(nat_type n)
Return n divided by 2 until the result is odd.
unsigned int get_uint(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:108
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
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
int main(int argc, const char *const argv[])
void print_device(const cl::Device &device)
Print information on this device.
Definition: helper.cpp:225
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
void print_platforms()
Print information on all platforms.
Definition: helper.cpp:407
std::set< nat_type > opencl_check_gentle_varsigma_odd__parallelize_factorization(nat_type first_n, nat_type last_n, unsigned int opencl_nb, bool print_bad, bool print_time)
Like opencl_check_gentle_varsigma_odd() but instead parallelize on group of opencl_nb numbers...
Definition: opencl.cpp:219
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
Implementation of the OpenCL parallel algorithms presented in the report.