Skip to content

Commit

Permalink
Merge pull request #217 from spacemeshos/use-global-mtx-for-gpu-access
Browse files Browse the repository at this point in the history
Use one mutex for any Device besides CPU
  • Loading branch information
fasmat authored Aug 12, 2023
2 parents 6b2fd63 + 9031fda commit 2356d00
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 78 deletions.
9 changes: 5 additions & 4 deletions internal/postrs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ func NewScrypt(opts ...OptionFunc) (*Scrypt, error) {
return nil, err
}

if *options.providerID != cCPUProviderID() {
gpuMtx.Lock()
}
init, err := cNewInitializer(options)
if err != nil {
gpuMtx.Unlock()
return nil, err
}
if *options.providerID != cCPUProviderID() {
gpuMtx.Device(*options.providerID).Lock()
}

return &Scrypt{
options: options,
Expand All @@ -156,7 +157,7 @@ func (s *Scrypt) Close() error {

cFreeInitializer(s.init)
if *s.options.providerID != cCPUProviderID() {
gpuMtx.Device(*s.options.providerID).Unlock()
gpuMtx.Unlock()
}
s.init = nil
return nil
Expand Down
28 changes: 0 additions & 28 deletions internal/postrs/device_mutex.go

This file was deleted.

43 changes: 0 additions & 43 deletions internal/postrs/device_mutex_test.go

This file was deleted.

7 changes: 4 additions & 3 deletions internal/postrs/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import "C"

import (
"errors"
"sync"
)

// gpuMtx is an instance of deviceMutex that can be used to prevent concurrent calls
// to the same GPU (by ProviderID) from multiple goroutines.
var gpuMtx deviceMutex
// gpuMtx is a mutual exclusion lock preventing concurrent access
// to any GPU device. CPU can still be used concurrently.
var gpuMtx sync.Mutex

// DeviceClass is an enum for the type of device (CPU or GPU).
type DeviceClass int
Expand Down

0 comments on commit 2356d00

Please sign in to comment.