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 13 14 15
const (
	AlreadyInitialized = "pserver already initialized"
	Uninitialized      = "pserver not fully initialized"
)
16 17 18 19 20 21 22 23 24 25 26

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

W
wuyi05 已提交
27 28 29
// PsDesired is etcd path for store desired pserver count
const PsDesired = "/ps_desired"

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

// 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 已提交
46
// Service is the RPC service for pserver.
47 48
type Service struct {
	initialized chan struct{}
49
	idx         int
50

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

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

H
Helin Wang 已提交
66
// InitParam initializes a parameter.
67 68 69
func (s *Service) InitParam(paramWithConfigs ParameterWithConfig, dummy *int) error {
	select {
	case <-s.initialized:
70
		return errors.New(AlreadyInitialized)
71 72 73 74 75 76 77 78 79 80 81
	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 已提交
82
	s.optMap[paramWithConfigs.Param.Name] = newOptimizer(paramWithConfigs)
83 84 85
	return nil
}

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

	close(s.initialized)
	return nil
}

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

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

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

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

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

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

130 131 132 133 134 135 136
	// 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 已提交
137
	parameter.Name = name
D
dongzhihong 已提交
138
	parameter.ElementType = opt.elementType
139 140
	parameter.Content = opt.GetWeights()
	return nil
141 142
}

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

	// TODO
	return nil
}