Skip to content

Commit 3a3e44a

Browse files
committed
Added test run to pipeline; added more output
Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com>
1 parent 0f50967 commit 3a3e44a

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ jobs:
2525
run: go build -o ./bin/pi cmd/example/main.go
2626

2727
- name: Perform Test run
28-
run: ./bin/pi
28+
run: ./bin/pi -n 10000000000

cmd/example/main.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,92 @@
11
package main
22

33
import (
4-
"fmt"
4+
"flag"
5+
"math"
56
"math/rand"
67
"runtime"
78
"time"
89

910
"github.com/qcserestipy/gohpc/pkg/workerpool"
11+
"github.com/sirupsen/logrus"
1012
)
1113

14+
func init() {
15+
formatter := &logrus.TextFormatter{}
16+
formatter.FullTimestamp = true
17+
formatter.TimestampFormat = time.RFC3339
18+
logrus.SetLevel(logrus.DebugLevel)
19+
logrus.SetFormatter(formatter)
20+
}
21+
1222
func main() {
13-
const nTests = 10_000_000_000
23+
logrus.Info("Starting Monte Carlo π approximation")
24+
numbPtr := flag.Int("n", 10000000000, "Number of Trials")
25+
flag.Parse()
26+
nTests := *numbPtr
27+
logrus.Infof("Number of Trials: %d", nTests)
1428

1529
// Split work into tasks
1630
numWorkers := runtime.NumCPU()
31+
logrus.Infof("System: %d CPU cores available", numWorkers)
32+
1733
type Task struct{ Count int }
1834
tasks := make([]Task, numWorkers)
1935
chunk := nTests / numWorkers
36+
remainder := nTests % numWorkers
37+
38+
var totalAllocated int
2039
for i := range tasks {
21-
tasks[i] = Task{Count: chunk}
40+
extraPoints := 0
41+
if i < remainder {
42+
extraPoints = 1
43+
}
44+
tasks[i] = Task{Count: chunk + extraPoints}
45+
totalAllocated += tasks[i].Count
2246
}
2347

48+
logrus.WithFields(logrus.Fields{
49+
"workers": numWorkers,
50+
"points_per_task": chunk,
51+
"remainder": remainder,
52+
"total_allocated": totalAllocated,
53+
}).Info("Work distribution prepared")
54+
55+
setupStart := time.Now()
2456
// Create pool
2557
pool := workerpool.New[Task, float64](numWorkers)
58+
logrus.Infof("Worker pool initialized in %v", time.Since(setupStart))
2659

2760
// Define work
2861
work := func(t Task) float64 {
62+
taskStart := time.Now()
2963
inCircle := 0
30-
r := rand.New(rand.NewSource(time.Now().UnixNano()))
64+
seed := time.Now().UnixNano()
65+
r := rand.New(rand.NewSource(seed))
66+
logrus.Debugf("Worker started with seed %d for %d points", seed, t.Count)
67+
3168
for i := 0; i < t.Count; i++ {
3269
x, y := r.Float64(), r.Float64()
3370
if x*x+y*y < 1 {
3471
inCircle++
3572
}
3673
}
74+
75+
ratio := float64(inCircle) / float64(t.Count)
76+
logrus.WithFields(logrus.Fields{
77+
"points_processed": t.Count,
78+
"points_in_circle": inCircle,
79+
"local_ratio": ratio,
80+
"local_pi_approx": 4 * ratio,
81+
"duration": time.Since(taskStart),
82+
}).Debug("Worker completed")
83+
3784
return float64(inCircle)
3885
}
3986

4087
// Run
4188
start := time.Now()
89+
logrus.Info("Starting computation...")
4290
results := pool.Run(tasks, work)
4391
elapsed := time.Since(start)
4492

@@ -49,5 +97,13 @@ func main() {
4997
}
5098
piApprox := 4 * (total / float64(nTests))
5199

52-
fmt.Printf("π ≈ %0.8f (computed in %s)", piApprox, elapsed)
100+
logrus.WithFields(logrus.Fields{
101+
"pi_approximation": piApprox,
102+
"error": math.Abs(piApprox - math.Pi),
103+
"duration": elapsed,
104+
"points_per_sec": float64(nTests) / elapsed.Seconds(),
105+
}).Info("Computation completed")
106+
107+
logrus.Infof("π ≈ %0.8f (error: %0.8f, computed in %s)",
108+
piApprox, math.Abs(piApprox-math.Pi), elapsed)
53109
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/qcserestipy/gohpc
22

33
go 1.23.7
4+
5+
require (
6+
github.com/sirupsen/logrus v1.9.3 // indirect
7+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
8+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
5+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
9+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)