Joshua Rutschmann 8 anni fa
parent
commit
3a4690361e
1 ha cambiato i file con 36 aggiunte e 23 eliminazioni
  1. 36
    23
      hw8/task1/src/main.rs

+ 36
- 23
hw8/task1/src/main.rs Vedi File

@@ -16,7 +16,11 @@ pub fn main() {
16 16
     let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string());
17 17
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
18 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 22
     let mut time_measurement = false;
23
+    let mut found = false;
20 24
 
21 25
     if diff.chars().any(|c| !c.is_digit(16)) {
22 26
         println!("Difficulty is not hexadecimal.");
@@ -29,7 +33,7 @@ pub fn main() {
29 33
         }
30 34
     }
31 35
 
32
-    if true {
36
+    if verbosity >= 1 {
33 37
         println!("--------------------------------------------");
34 38
         println!("Container: \"{}\"", sys_info::hostname().unwrap_or("-".to_string()));
35 39
         println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0));
@@ -42,9 +46,12 @@ pub fn main() {
42 46
 
43 47
     match (base.parse::<usize>(), threads.parse::<usize>()) {
44 48
         (Ok(b), Ok(t)) => {
45
-            println!("Using base: {}", b);
46
-            println!("Using difficulty: {}", diff);
47 49
             println!("Please wait...");
50
+
51
+            if verbosity >= 1 {
52
+                println!("Searching with {} threads", t);
53
+            }
54
+
48 55
             let start = get_time();
49 56
             let max = <usize>::max_value();
50 57
             let mut children = vec![];
@@ -58,13 +65,15 @@ pub fn main() {
58 65
                 children.push(thread::spawn(move || {
59 66
                     let mut n = i;
60 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 74
                             let end = get_time();
67
-                            println!("Number: {} --> hash: {}", x.number, x.hash);
75
+                            found = sync;
76
+                            
68 77
                             if time_measurement {
69 78
                                 let diff = end - start;
70 79
                                 let s = diff.num_seconds();
@@ -72,7 +81,7 @@ pub fn main() {
72 81
                                 let us = diff.num_microseconds().unwrap_or(ms * 1000);
73 82
                                 println!("(Duration {}s / {}ms / {}us)", s, ms, us);
74 83
                             }
75
-                            tx.send("Finished").unwrap();
84
+                            tx.send(solution).unwrap();
76 85
                         }
77 86
                         n += t;
78 87
                     }
@@ -81,16 +90,11 @@ pub fn main() {
81 90
             }
82 91
 
83 92
             match rx.recv() {
84
-                Ok(msg) => {
85
-                    println!("{}", msg);
93
+                Ok(sol) => {
94
+                    println!("Number: {} --> hash: {}", sol.number, sol.hash);
86 95
                 }
87 96
                 Err(_) => {}
88 97
             }
89
-            /*
90
-            for child in children {
91
-                let _ = child.join();
92
-            } 
93
-            */
94 98
         }
95 99
         (_, Err(_)) => {
96 100
             println!("Number of threads is not integer.");
@@ -113,14 +117,14 @@ fn create_app<'a, 'b>() -> App<'a, 'b> {
113 117
         .arg(
114 118
             Arg::with_name("base")
115 119
                 .value_name("base")
116
-                .help("The base of the hash to be calculated on.")
120
+                .help("Sets the base to use")
117 121
                 .takes_value(true)
118 122
                 .required(true),
119 123
         )
120 124
         .arg(
121 125
             Arg::with_name("difficulty")
122 126
                 .value_name("difficulty")
123
-                .help("The difficulty of the calculated hash.")
127
+                .help("Sets the difficulty to use")
124 128
                 .takes_value(true)
125 129
                 .required(true),
126 130
         )
@@ -132,12 +136,21 @@ fn create_app<'a, 'b>() -> App<'a, 'b> {
132 136
                 )
133 137
                 .takes_value(true)
134 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 154
             SubCommand::with_name("timings")
142 155
                 .about("controls timing features")
143 156
                 .version("1.0")

Loading…
Annulla
Salva