Browse Source

Merge branch 'hw8' of github.com:themultiplexer/bsys-ws17-grp11 into hw8

Lorenz Bung 7 years ago
parent
commit
97c6ff145e
2 changed files with 27 additions and 7 deletions
  1. 3
    1
      hw8/task1/src/cli.yml
  2. 24
    6
      hw8/task1/src/main.rs

+ 3
- 1
hw8/task1/src/cli.yml View File

1
-name: "task1"
1
+name: "Proof of Work Mechanism 0.2"
2
+author: "Lorenz Bung & Joshua Rutschmann"
3
+about: "now the basics in Rust, no C anymore"
2
 args:
4
 args:
3
     - base:
5
     - base:
4
         value_name: "base"
6
         value_name: "base"

+ 24
- 6
hw8/task1/src/main.rs View File

19
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
19
     let cpus = sys_info::cpu_num().unwrap_or(1).to_string();
20
     let threads = matches.value_of("threads").unwrap_or(&cpus);
20
     let threads = matches.value_of("threads").unwrap_or(&cpus);
21
     let sync = matches.is_present("sync");
21
     let sync = matches.is_present("sync");
22
+    let wait = matches.is_present("wait");
22
     let verbosity = matches.occurrences_of("verbose"); 
23
     let verbosity = matches.occurrences_of("verbose"); 
23
     
24
     
24
     let mut time_measurement = false;
25
     let mut time_measurement = false;
25
-    let mut found = false;
26
 
26
 
27
+    // Falls irgendein Zeichen nicht hexadezimal ist, breche ab.
27
     if diff.chars().any(|c| !c.is_digit(16)) {
28
     if diff.chars().any(|c| !c.is_digit(16)) {
28
         println!("Difficulty is not hexadecimal.");
29
         println!("Difficulty is not hexadecimal.");
29
         process::exit(1)
30
         process::exit(1)
30
     }
31
     }
31
 
32
 
33
+    // Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung.
32
     if let Some(ref sub_command) = matches.subcommand {
34
     if let Some(ref sub_command) = matches.subcommand {
33
         if sub_command.name.eq("timings") {
35
         if sub_command.name.eq("timings") {
34
             time_measurement = true;
36
             time_measurement = true;
59
             let mut children = vec![];
61
             let mut children = vec![];
60
 
62
 
61
             let (tx, rx) = channel();
63
             let (tx, rx) = channel();
64
+            let mut found = &false;
62
 
65
 
63
             for i in 0..t {
66
             for i in 0..t {
64
                 let d = diff.clone();
67
                 let d = diff.clone();
65
                 let tx = tx.clone();
68
                 let tx = tx.clone();
66
-
69
+                
67
                 children.push(thread::spawn(move || {
70
                 children.push(thread::spawn(move || {
68
                     let mut n = i;
71
                     let mut n = i;
69
                     while n < max {
72
                     while n < max {
70
 
73
 
71
-                        if found && sync {
72
-                            process::exit(0);
74
+                        if n % (100000 + i)  == 0 {
75
+                            println!("Thread {}: {}", i, n);
76
+                        }
77
+
78
+                        if *found && sync {
79
+                            return n; 
73
                         }
80
                         }
74
 
81
 
75
                         if let Some(solution) = verify_product(b, n, &d) {
82
                         if let Some(solution) = verify_product(b, n, &d) {
76
-                            found = sync;
83
+                            *found = true;
77
                             tx.send(solution).unwrap();
84
                             tx.send(solution).unwrap();
85
+                            if sync {
86
+                                break;
87
+                            }
78
                         }
88
                         }
79
                         n += t;
89
                         n += t;
80
                     }
90
                     }
81
-
91
+                    n
82
                 }));
92
                 }));
83
             }
93
             }
84
 
94
 
96
                 }
106
                 }
97
                 Err(_) => {}
107
                 Err(_) => {}
98
             }
108
             }
109
+
110
+            if wait {
111
+                for child in children {
112
+                    let loops = child.join();
113
+                    println!("Loops: {}", loops.unwrap());
114
+                }
115
+            }
116
+        
99
         }
117
         }
100
         (_, Err(_)) => {
118
         (_, Err(_)) => {
101
             println!("Number of threads is not integer.");
119
             println!("Number of threads is not integer.");

Loading…
Cancel
Save