Skip to content

Commit

Permalink
add timer package as copy of emergent one, to avoid circular dependen…
Browse files Browse the repository at this point in the history
…cy; update go.mod
  • Loading branch information
rcoreilly committed Apr 16, 2024
1 parent fa5c57c commit fcf346c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 deletions.
14 changes: 4 additions & 10 deletions examples/axon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

"cogentcore.org/core/math32"
"cogentcore.org/core/vgpu"
"github.com/emer/emergent/v2/timer"
"github.com/emer/gosl/v2/sltype"
"github.com/emer/gosl/v2/threading"
"github.com/emer/gosl/v2/timer"
)

// DiffTol is tolerance on testing diff between cpu and gpu values
Expand Down Expand Up @@ -114,7 +114,7 @@ func main() {
// fmt.Printf("%d\ttime.RandCtr: %v\n", cy, time.RandCtr.Uint2())
}

cpuTmr.Stop()
// cpuTmr.Stop()

time.Reset()

Expand Down Expand Up @@ -145,9 +145,6 @@ func main() {
// seti.ConfigValues(1) // one val per var
sy.Config() // configures vars, allocates vals, configs pipelines..

gpuFullTmr := timer.Time{}
gpuFullTmr.Start()

// this copy is pretty fast -- most of time is below
lvl, _ := layv.Values.ValueByIndexTry(0)
lvl.CopyFromBytes(unsafe.Pointer(&lays[0]))
Expand All @@ -158,9 +155,6 @@ func main() {
// ivl, _ := idxv.Values.ValueByIndexTry(0)
// ivl.CopyFromBytes(unsafe.Pointer(&idxs[0]))

// gpuFullTmr := timer.Time{}
// gpuFullTmr.Start()

sy.Mem.SyncToGPU()

vars.BindDynamicValueIndex(0, "Layers", 0)
Expand All @@ -171,8 +165,8 @@ func main() {
cmd := sy.ComputeCmdBuff()
sy.CmdResetBindVars(cmd, 0)

// gpuFullTmr := timer.Time{}
// gpuFullTmr.Start()
gpuFullTmr := timer.Time{}
gpuFullTmr.Start()

gpuTmr := timer.Time{}
gpuTmr.Start()
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"cogentcore.org/core/math32"
"cogentcore.org/core/vgpu"
"github.com/emer/emergent/v2/timer"
"github.com/emer/gosl/v2/timer"
)

// note: standard one to use is plain "gosl" which should be go install'd
Expand Down
2 changes: 1 addition & 1 deletion examples/rand/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"cogentcore.org/core/math32"
"cogentcore.org/core/vgpu"
"github.com/emer/emergent/v2/timer"
"github.com/emer/gosl/v2/sltype"
"github.com/emer/gosl/v2/timer"
)

// note: standard one to use is plain "gosl" which should be go install'd
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module github.com/emer/gosl/v2
go 1.22

require (
cogentcore.org/core v0.0.9
github.com/emer/emergent/v2 v2.0.0-dev0.0.11
cogentcore.org/core v0.1.0
golang.org/x/tools v0.19.0
)

Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cogentcore.org/core v0.0.9 h1:Ird5o4wewSe8/dvsmd47YvkP5JZCiaE9hUSrb2692N8=
cogentcore.org/core v0.0.9/go.mod h1:SAJedgd5nd0iB2cHl//DyCO+rugd/u7KCHXHNjnQOmA=
cogentcore.org/core v0.1.0 h1:fJjpk7jd77IIUCDoauwukOeTVsWU+SuDyIhDXb3PYTM=
cogentcore.org/core v0.1.0/go.mod h1:SAJedgd5nd0iB2cHl//DyCO+rugd/u7KCHXHNjnQOmA=
github.com/Bios-Marcel/wastebasket v0.0.4-0.20240213135800-f26f1ae0a7c4 h1:6lx9xzJAhdjq0LvVfbITeC3IH9Fzvo1aBahyPu2FuG8=
github.com/Bios-Marcel/wastebasket v0.0.4-0.20240213135800-f26f1ae0a7c4/go.mod h1:FChzXi1izqzdPb6BiNZmcZLGyTYiT61iGx9Rxx9GNeI=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand All @@ -21,8 +21,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emer/emergent/v2 v2.0.0-dev0.0.11 h1:c0gTXdE5SUl590TjhFPXykCyhFe+jMlH4aegFrrqa4g=
github.com/emer/emergent/v2 v2.0.0-dev0.0.11/go.mod h1:i+OqOi5bam3Nl6QZxwp5QSA2djUZ1+y9e+Gtixv6bFc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
Expand Down
86 changes: 86 additions & 0 deletions timer/timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package timer provides a simple wall-clock duration timer based on standard
// time. Accumulates total and average over multiple Start / Stop intervals.
// This is copied from emergent/timer, to avoid circular dependency.
package timer

//go:generate core generate -add-types

import "time"

// Time manages the timer accumulated time and count
type Time struct {

// the most recent starting time
St time.Time

// the total accumulated time
Total time.Duration

// the number of start/stops
N int
}

// Reset resets the overall accumulated Total and N counters and start time to zero
func (t *Time) Reset() {
t.St = time.Time{}
t.Total = 0
t.N = 0
}

// Start starts the timer
func (t *Time) Start() {
t.St = time.Now()
}

// ResetStart reset then start the timer
func (t *Time) ResetStart() {
t.Reset()
t.Start()
}

// Stop stops the timer and accumulates the latest start - stop interval, and also returns it
func (t *Time) Stop() time.Duration {
if t.St.IsZero() {
t.Total = 0
t.N = 0
return 0
}
iv := time.Now().Sub(t.St)
t.Total += iv
t.N++
return iv
}

// Avg returns the average start / stop interval (assumes each was measuring the same thing).
func (t *Time) Avg() time.Duration {
if t.N == 0 {
return 0
}
return t.Total / time.Duration(t.N)
}

// AvgSecs returns the average start / stop interval (assumes each was measuring the same thing)
// as a float64 of seconds
func (t *Time) AvgSecs() float64 {
if t.N == 0 {
return 0
}
return float64(t.Total) / (float64(t.N) * float64(time.Second))
}

// AvgMSecs returns the average start / stop interval as a float64 of milliseconds
func (t *Time) AvgMSecs() float64 {
if t.N == 0 {
return 0
}
return float64(t.Total) / (float64(t.N) * float64(time.Millisecond))
}

// TotalSecs returns the total start / stop intervals as a float64 of seconds.
func (t *Time) TotalSecs() float64 {
return float64(t.Total) / float64(time.Second)
}

0 comments on commit fcf346c

Please sign in to comment.