go in 37;; fix so stdout emits, do go, fix py
parent
0bef5b997f
commit
221b2c8096
|
|
@ -56,6 +56,7 @@ gather() {
|
||||||
echo "> wait" >&2
|
echo "> wait" >&2
|
||||||
wait
|
wait
|
||||||
echo "> /gather $ttl" >&2
|
echo "> /gather $ttl" >&2
|
||||||
|
echo bash $ttl
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "$0" == "$BASH_SOURCE" ]; then
|
if [ "$0" == "$BASH_SOURCE" ]; then
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
N := 100
|
||||||
|
P := 3
|
||||||
|
q := make(chan int)
|
||||||
|
scatter(N, P, q)
|
||||||
|
gather(N, P, q)
|
||||||
|
}
|
||||||
|
|
||||||
|
func scatter(N, P int, q chan int) {
|
||||||
|
for i := 0; i < P; i++ {
|
||||||
|
go _scatter(i, N, P, q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _scatter(i, N, P int, q chan int) {
|
||||||
|
start := i * (N / P)
|
||||||
|
chunk := N / P
|
||||||
|
if i == P-1 {
|
||||||
|
chunk += N % P
|
||||||
|
}
|
||||||
|
ttl := 0
|
||||||
|
for j := start; j <= start+chunk; j++ {
|
||||||
|
ttl += j
|
||||||
|
}
|
||||||
|
log.Println(i, N, P, q, start, start+chunk, ttl)
|
||||||
|
q <- ttl
|
||||||
|
}
|
||||||
|
|
||||||
|
func gather(N, P int, q chan int) {
|
||||||
|
ttl := 0
|
||||||
|
for i := 0; i < P; i++ {
|
||||||
|
ttl += <-q
|
||||||
|
}
|
||||||
|
fmt.Println("go", ttl)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import threading
|
import threading
|
||||||
|
from sys import stderr
|
||||||
import queue
|
import queue
|
||||||
|
|
||||||
class T(threading.Thread):
|
class T(threading.Thread):
|
||||||
|
|
@ -10,10 +11,10 @@ class T(threading.Thread):
|
||||||
self.q = q
|
self.q = q
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
print(self.n, self.p, self.i, self.q)
|
|
||||||
start = self.i * ((self.n // self.p))
|
start = self.i * ((self.n // self.p))
|
||||||
chunk = self.n // self.p + (0 if i < self.p-1 else self.n%self.p)
|
chunk = self.n // self.p + (self.n%self.p if self.i == self.p-1 else 0)
|
||||||
self.q.put(sum([j for j in range(start, start+chunk)]))
|
print(self.n, self.p, self.i, self.q, start, start+chunk, file=stderr)
|
||||||
|
self.q.put(sum([j for j in range(start, start+chunk+1)]))
|
||||||
|
|
||||||
Ts = []
|
Ts = []
|
||||||
N = 100
|
N = 100
|
||||||
|
|
@ -28,4 +29,4 @@ for i in range(P):
|
||||||
ttl += Q.get()
|
ttl += Q.get()
|
||||||
for t in Ts:
|
for t in Ts:
|
||||||
t.join()
|
t.join()
|
||||||
print(ttl)
|
print("python3", ttl)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue