pserver.go 1.7 KB
Newer Older
1 2 3 4 5 6 7
package main

import (
	"net"
	"net/http"
	"net/rpc"
	"strconv"
W
wuyi05 已提交
8
	"time"
9

H
Helin Wang 已提交
10 11
	"github.com/namsral/flag"

12
	"github.com/PaddlePaddle/Paddle/go/pserver"
W
wuyi05 已提交
13
	log "github.com/sirupsen/logrus"
14 15 16
)

func main() {
H
Helin Wang 已提交
17
	port := flag.Int("port", 0, "port of the pserver")
Q
Qiao Longfei 已提交
18
	index := flag.Int("index", -1, "index of this pserver, should be larger or equal than 0")
W
wuyi05 已提交
19 20 21
	etcdEndpoint := flag.String("etcd-endpoint", "http://127.0.0.1:2379",
		"comma separated endpoint string for pserver to connect to etcd")
	etcdTimeout := flag.Int("etcd-timeout", 5, "timeout for etcd calls")
Y
yi.wu 已提交
22
	numPservers := flag.Int("num-pservers", 1, "total pserver count in a training job")
D
dongzhihong 已提交
23
	checkpointPath := flag.String("checkpoint-path", "/checkpoints/", "save checkpoint path")
24
	checkpointInterval := flag.Int("checkpoint-interval", 600, "save checkpoint per interval seconds")
W
wuyi05 已提交
25 26
	logLevel := flag.String("log-level", "info",
		"log level, possible values: debug, info, warning, error, fatal, panic")
27 28
	flag.Parse()

W
wuyi05 已提交
29 30 31 32 33 34
	level, err := log.ParseLevel(*logLevel)
	if err != nil {
		panic(err)
	}
	log.SetLevel(level)

Q
Qiao Longfei 已提交
35
	var idx int
D
dongzhihong 已提交
36
	var cp pserver.Checkpoint
D
dongzhihong 已提交
37
	var e *pserver.EtcdClient
Q
Qiao Longfei 已提交
38 39 40 41
	if *index >= 0 {
		idx = *index
	} else {
		timeout := time.Second * time.Duration((*etcdTimeout))
D
dongzhihong 已提交
42
		e = pserver.NewEtcdClient(*etcdEndpoint, *numPservers, timeout)
Q
Qiao Longfei 已提交
43 44 45 46
		idx, err = e.Register()
		if err != nil {
			panic(err)
		}
47 48
	}

D
dongzhihong 已提交
49
	s, err := pserver.NewService(idx, *checkpointInterval, *checkpointPath, e, cp)
W
wuyi05 已提交
50 51 52 53
	if err != nil {
		panic(err)
	}
	err = rpc.Register(s)
54 55 56 57 58 59 60 61 62 63
	if err != nil {
		panic(err)
	}

	rpc.HandleHTTP()
	l, err := net.Listen("tcp", ":"+strconv.Itoa(*port))
	if err != nil {
		panic(err)
	}

W
wuyi05 已提交
64
	log.Infof("start pserver at port %d", *port)
65
	err = http.Serve(l, nil)
W
wuyi05 已提交
66

67 68 69 70
	if err != nil {
		panic(err)
	}
}