Created
May 6, 2020 20:03
-
-
Save olehcambel/21fb4d01fe716cd7b65e69ec6434530d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
var numCPU = runtime.NumCPU() | |
// Vector c | |
type Vector []float64 | |
func (v Vector) doPart(i, n int, u Vector, done chan<- int) { | |
for ; i < n; i++ { | |
v[i] = v.op(u[i]) | |
} | |
done <- 1 | |
} | |
func (v Vector) doAll(u Vector) { | |
c := make(chan int, numCPU) | |
for i := 0; i < numCPU; i++ { | |
go v.doPart(i*len(v)/numCPU, (i+1)*len(v)/numCPU, u, c) | |
} | |
for i := 0; i < numCPU; i++ { | |
<-c | |
} | |
} | |
func (v Vector) op(value float64) float64 { | |
time.Sleep(time.Second) | |
fmt.Println(value) | |
return value | |
} | |
func main() { | |
v := Vector(make([]float64, 100)) | |
u := Vector(make([]float64, 100)) | |
for i := 0; i < len(v); i++ { | |
v[i] = float64(i) | |
u[i] = float64(i) | |
} | |
v.doAll(u) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment