service.go 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package pserver

import (
	"errors"
	"fmt"
	"sync"
)

// ElementType is the type of elements of a Parameter.
type ElementType int

12
const (
W
wuyi05 已提交
13
	// AlreadyInitialized is true if pserver is initialized
14
	AlreadyInitialized = "pserver already initialized"
W
wuyi05 已提交
15 16
	// Uninitialized is true if pserver not fully initialized
	Uninitialized = "pserver not fully initialized"
17
)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

// Supported element types
const (
	Int32 ElementType = iota
	UInt32
	Int64
	UInt64
	Float32
	Float64
)

// Parameter is a piece of data to sync with the parameter server.
type Parameter struct {
	Name        string
	ElementType ElementType
D
dzhwinter 已提交
33
	Content     []byte
34 35 36 37 38 39 40 41 42 43 44
}

// ParameterWithConfig contains the parameter and the configuration.
type ParameterWithConfig struct {
	Param  Parameter
	Config []byte // parameter configuration in Proto Buffer format
}

// Gradient is the gradient of the parameter.
type Gradient Parameter

H
Helin Wang 已提交
45
// Service is the RPC service for pserver.
46 47
type Service struct {
	initialized chan struct{}
48
	idx         int
49

50
	mu     sync.Mutex
D
dzhwinter 已提交
51
	optMap map[string]*optimizer
52 53
}

W
wuyi05 已提交
54 55
// NewService creates a new service, will bypass etcd registration if no
// endpoints specified.
56 57 58 59
func NewService(idx int) (*Service, error) {
	s := &Service{
		idx: idx,
	}
W
wuyi05 已提交
60
	s.optMap = make(map[string]*optimizer)
61
	s.initialized = make(chan struct{})
W
wuyi05 已提交
62
	return s, nil
63 64
}

H
Helin Wang 已提交
65
// InitParam initializes a parameter.
66 67 68
func (s *Service) InitParam(paramWithConfigs ParameterWithConfig, dummy *int) error {
	select {
	case <-s.initialized:
69
		return errors.New(AlreadyInitialized)
70 71 72 73 74 75 76 77 78 79 80
	default:
	}

	// TODO(helin): parse parameter config

	s.mu.Lock()
	defer s.mu.Unlock()

	// TODO(helin): check if paramWithConfigs.Param.Content is
	// properly memory aligned, if not, make copy to a memory
	// aligned region.
D
dzhwinter 已提交
81
	s.optMap[paramWithConfigs.Param.Name] = newOptimizer(paramWithConfigs)
82 83 84
	return nil
}

H
Helin Wang 已提交
85 86
// FinishInitParams tells the parameter server that the parameter
// initialization has finished.
87 88 89
func (s *Service) FinishInitParams(dummy0 int, dummy1 *int) error {
	select {
	case <-s.initialized:
90
		return errors.New(AlreadyInitialized)
91 92 93 94 95 96 97
	default:
	}

	close(s.initialized)
	return nil
}

98
// SendGrad sends gradient to parameter servers for parameter
H
Helin Wang 已提交
99
// optimization.
100
func (s *Service) SendGrad(g Gradient, dummy *int) error {
101 102 103
	select {
	case <-s.initialized:
	default:
104
		return errors.New(Uninitialized)
105
	}
106 107

	s.mu.Lock()
H
Helin Wang 已提交
108
	defer s.mu.Unlock()
109

D
dzhwinter 已提交
110 111
	o, ok := s.optMap[g.Name]
	if !ok {
D
dzhwinter 已提交
112
		return fmt.Errorf("parameter: %s does not exist", g.Name)
D
dzhwinter 已提交
113
	}
114

D
dongzhihong 已提交
115
	return o.UpdateParameter(g)
116 117
}

118 119
// GetParam gets parameters from the parameter server.
func (s *Service) GetParam(name string, parameter *Parameter) error {
120 121
	<-s.initialized
	s.mu.Lock()
H
Helin Wang 已提交
122
	defer s.mu.Unlock()
123

D
dongzhihong 已提交
124
	opt, ok := s.optMap[name]
125 126
	if !ok {
		return fmt.Errorf("parameter: %s does not exist", name)
127 128
	}

129 130 131 132 133 134 135
	// The parameter content (a byte slice) may change
	// during RPC serialization due to write from other
	// goroutine, we allow it since mini-batch based deep
	// learning optimization methods are stochastic in
	// nature. This race condition is allowed deliberately
	// to save the program from making a copy of the
	// paramter content.
D
dongzhihong 已提交
136
	parameter.Name = name
D
dongzhihong 已提交
137
	parameter.ElementType = opt.elementType
138 139
	parameter.Content = opt.GetWeights()
	return nil
140 141
}

H
Helin Wang 已提交
142 143
// Save tells the parameter server to save parameters.
func (s *Service) Save(path string, dummy *int) error {
144 145 146 147 148
	<-s.initialized

	// TODO
	return nil
}