Joshua Rutschmann 8 lat temu
rodzic
commit
3a4690361e
1 zmienionych plików z 36 dodań i 23 usunięć
  1. 36
    23
      hw8/task1/src/main.rs

+ 36
- 23
hw8/task1/src/main.rs Wyświetl plik

16
     let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
16
     let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
17
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
17
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
18
     let threads = matches.value_of("threads").unwrap_or(&cpus);
18
     let threads = matches.value_of("threads").unwrap_or(&cpus);
19
+    let sync = matches.is_present("sync");
20
+    let verbosity = matches.occurrences_of("verbose"); 
21
+    
19
     let mut time_measurement = false;
22
     let mut time_measurement = false;
23
+    let mut found = false;
20
 
24
 
21
     if diff.chars().any(|c| !c.is_digit(16)) {
25
     if diff.chars().any(|c| !c.is_digit(16)) {
22
         println!("Difficulty is not hexadecimal.");
26
         println!("Difficulty is not hexadecimal.");
29
         }
33
         }
30
     }
34
     }
31
 
35
 
32
-    if true {
36
+    if verbosity >= 1 {
33
         println!("--------------------------------------------");
37
         println!("--------------------------------------------");
34
         println!("Container: \"{}\"", sys_info::hostname().unwrap_or("-".to_string()));
38
         println!("Container: \"{}\"", sys_info::hostname().unwrap_or("-".to_string()));
35
         println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
39
         println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
42
 
46
 
43
     match (base.parse::<usize>(), threads.parse::<usize>()) {
47
     match (base.parse::<usize>(), threads.parse::<usize>()) {
44
         (Ok(b), Ok(t)) => {
48
         (Ok(b), Ok(t)) => {
45
-            println!("Using base: {}", b);
46
-            println!("Using difficulty: {}", diff);
47
             println!("Please wait...");
49
             println!("Please wait...");
50
+
51
+            if verbosity >= 1 {
52
+                println!("Searching with {} threads", t);
53
+            }
54
+
48
             let start = get_time();
55
             let start = get_time();
49
             let max = <usize>::max_value();
56
             let max = <usize>::max_value();
50
             let mut children = vec![];
57
             let mut children = vec![];
58
                 children.push(thread::spawn(move || {
65
                 children.push(thread::spawn(move || {
59
                     let mut n = i;
66
                     let mut n = i;
60
                     while n < max {
67
                     while n < max {
61
-                        if n < 20 {
62
-                            println!("Thread {}: {}", i, n);
68
+
69
+                        if found && sync {
70
+                            process::exit(0);
63
                         }
71
                         }
64
-                        
65
-                        if let Some(x) = verify_product(b, n, &d) {
72
+
73
+                        if let Some(solution) = verify_product(b, n, &d) {
66
                             let end = get_time();
74
                             let end = get_time();
67
-                            println!("Number: {} --> hash: {}", x.number, x.hash);
75
+                            found = sync;
76
+                            
68
                             if time_measurement {
77
                             if time_measurement {
69
                                 let diff = end - start;
78
                                 let diff = end - start;
70
                                 let s = diff.num_seconds();
79
                                 let s = diff.num_seconds();
72
                                 let us = diff.num_microseconds().unwrap_or(ms * 1000);
81
                                 let us = diff.num_microseconds().unwrap_or(ms * 1000);
73
                                 println!("(Duration {}s / {}ms / {}us)", s, ms, us);
82
                                 println!("(Duration {}s / {}ms / {}us)", s, ms, us);
74
                             }
83
                             }
75
-                            tx.send("Finished").unwrap();
84
+                            tx.send(solution).unwrap();
76
                         }
85
                         }
77
                         n += t;
86
                         n += t;
78
                     }
87
                     }
81
             }
90
             }
82
 
91
 
83
             match rx.recv() {
92
             match rx.recv() {
84
-                Ok(msg) => {
85
-                    println!("{}", msg);
93
+                Ok(sol) => {
94
+                    println!("Number: {} --> hash: {}", sol.number, sol.hash);
86
                 }
95
                 }
87
                 Err(_) => {}
96
                 Err(_) => {}
88
             }
97
             }
89
-            /*
90
-            for child in children {
91
-                let _ = child.join();
92
-            } 
93
-            */
94
         }
98
         }
95
         (_, Err(_)) => {
99
         (_, Err(_)) => {
96
             println!("Number of threads is not integer.");
100
             println!("Number of threads is not integer.");
113
         .arg(
117
         .arg(
114
             Arg::with_name("base")
118
             Arg::with_name("base")
115
                 .value_name("base")
119
                 .value_name("base")
116
-                .help("The base of the hash to be calculated on.")
120
+                .help("Sets the base to use")
117
                 .takes_value(true)
121
                 .takes_value(true)
118
                 .required(true),
122
                 .required(true),
119
         )
123
         )
120
         .arg(
124
         .arg(
121
             Arg::with_name("difficulty")
125
             Arg::with_name("difficulty")
122
                 .value_name("difficulty")
126
                 .value_name("difficulty")
123
-                .help("The difficulty of the calculated hash.")
127
+                .help("Sets the difficulty to use")
124
                 .takes_value(true)
128
                 .takes_value(true)
125
                 .required(true),
129
                 .required(true),
126
         )
130
         )
132
                 )
136
                 )
133
                 .takes_value(true)
137
                 .takes_value(true)
134
                 .required(false),
138
                 .required(false),
139
+        ).arg(Arg::with_name("verbose")
140
+              .short("v")
141
+              .multiple(true)
142
+              .required(false),
135
         )
143
         )
136
-	.arg(Arg::with_name("verbose")
137
-       		.short("v")
138
-        	.multiple(true)
139
-        )
140
-	.subcommand(
144
+    .arg(Arg::with_name("sync")
145
+            .short("s")
146
+            .required(false),
147
+    )
148
+    .arg(Arg::with_name("config")
149
+            .long("config")
150
+            .value_name("VALUE")
151
+            .help("sets special sync parameter")
152
+                               .takes_value(true))
153
+    .subcommand(
141
             SubCommand::with_name("timings")
154
             SubCommand::with_name("timings")
142
                 .about("controls timing features")
155
                 .about("controls timing features")
143
                 .version("1.0")
156
                 .version("1.0")

Ładowanie…
Anuluj
Zapisz