소스 검색

Add NeuralNetwork cluster base

Alexey Edelev 4 년 전
부모
커밋
c3f8406587
4개의 변경된 파일378개의 추가작업 그리고 0개의 파일을 삭제
  1. 78 0
      cluster/cluster.go
  2. 240 0
      cluster/cluster.pb.go
  3. 56 0
      cluster/cluster.proto
  4. 4 0
      updateproto.sh

+ 78 - 0
cluster/cluster.go

@@ -0,0 +1,78 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package cluster
+
+import (
+	fmt "fmt"
+	"net"
+)
+
+type NeuraNetworkCluster struct {
+	address     string
+	activeUnits Units
+	streams     []NeuralNetworkCluster_RegisterServer
+	unitsQueue  chan *Unit
+}
+
+func NewNeuraNetworkCluster(address string) (cl *NeuraNetworkCluster) {
+	cl = nil
+	if address == "" {
+		return
+	}
+
+	cl = &NewNeuraNetworkCluster{
+		address: address,
+		unitsQueue: make(chan *Unit, 10)
+	}
+}
+
+func (cl *NeuraNetworkCluster) serveClients() {
+	stream.Send(&cl.activeUnits)
+	cl.activeUnits.List = append(cl.activeUnits.List, unit)
+	go func() {
+		for _, stream := range cl.streams {
+		}
+	}()
+}
+
+func (cl *NeuraNetworkCluster) Run() {
+	grpcServer := grpc.NewServer()
+	cluster.RegisterNeuralNetworkClusterServer(grpcServer, cl)
+	lis, err := net.Listen("tcp", address)
+	if err != nil {
+		fmt.Printf("Failed to listen: %v\n", err)
+	}
+
+	fmt.Printf("Listen %v\n", address)
+	if err := grpcServer.Serve(lis); err != nil {
+		fmt.Printf("Failed to serve: %v\n", err)
+	}
+}
+
+func (cl *NeuraNetworkCluster) Register(unit *Unit, stream NeuralNetworkCluster_RegisterServer) error {
+	cl.unitsQueue <- unit
+	return nil
+}

+ 240 - 0
cluster/cluster.pb.go

@@ -0,0 +1,240 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: cluster.proto
+
+package cluster
+
+import (
+	context "context"
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	grpc "google.golang.org/grpc"
+	codes "google.golang.org/grpc/codes"
+	status "google.golang.org/grpc/status"
+	math "math"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+type Unit struct {
+	Url                  string   `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`
+	AvailableThreads     int32    `protobuf:"zigzag32,2,opt,name=availableThreads,proto3" json:"availableThreads,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *Unit) Reset()         { *m = Unit{} }
+func (m *Unit) String() string { return proto.CompactTextString(m) }
+func (*Unit) ProtoMessage()    {}
+func (*Unit) Descriptor() ([]byte, []int) {
+	return fileDescriptor_3cfb3b8ec240c376, []int{0}
+}
+
+func (m *Unit) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Unit.Unmarshal(m, b)
+}
+func (m *Unit) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Unit.Marshal(b, m, deterministic)
+}
+func (m *Unit) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Unit.Merge(m, src)
+}
+func (m *Unit) XXX_Size() int {
+	return xxx_messageInfo_Unit.Size(m)
+}
+func (m *Unit) XXX_DiscardUnknown() {
+	xxx_messageInfo_Unit.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Unit proto.InternalMessageInfo
+
+func (m *Unit) GetUrl() string {
+	if m != nil {
+		return m.Url
+	}
+	return ""
+}
+
+func (m *Unit) GetAvailableThreads() int32 {
+	if m != nil {
+		return m.AvailableThreads
+	}
+	return 0
+}
+
+type Units struct {
+	List                 []*Unit  `protobuf:"bytes,1,rep,name=list,proto3" json:"list,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
+}
+
+func (m *Units) Reset()         { *m = Units{} }
+func (m *Units) String() string { return proto.CompactTextString(m) }
+func (*Units) ProtoMessage()    {}
+func (*Units) Descriptor() ([]byte, []int) {
+	return fileDescriptor_3cfb3b8ec240c376, []int{1}
+}
+
+func (m *Units) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_Units.Unmarshal(m, b)
+}
+func (m *Units) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_Units.Marshal(b, m, deterministic)
+}
+func (m *Units) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_Units.Merge(m, src)
+}
+func (m *Units) XXX_Size() int {
+	return xxx_messageInfo_Units.Size(m)
+}
+func (m *Units) XXX_DiscardUnknown() {
+	xxx_messageInfo_Units.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_Units proto.InternalMessageInfo
+
+func (m *Units) GetList() []*Unit {
+	if m != nil {
+		return m.List
+	}
+	return nil
+}
+
+func init() {
+	proto.RegisterType((*Unit)(nil), "cluster.Unit")
+	proto.RegisterType((*Units)(nil), "cluster.Units")
+}
+
+func init() { proto.RegisterFile("cluster.proto", fileDescriptor_3cfb3b8ec240c376) }
+
+var fileDescriptor_3cfb3b8ec240c376 = []byte{
+	// 173 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0xce, 0x29, 0x2d,
+	0x2e, 0x49, 0x2d, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x87, 0x72, 0x95, 0x5c, 0xb8,
+	0x58, 0x42, 0xf3, 0x32, 0x4b, 0x84, 0x04, 0xb8, 0x98, 0x4b, 0x8b, 0x72, 0x24, 0x18, 0x15, 0x18,
+	0x35, 0x38, 0x83, 0x40, 0x4c, 0x21, 0x2d, 0x2e, 0x81, 0xc4, 0xb2, 0xc4, 0xcc, 0x9c, 0xc4, 0xa4,
+	0x9c, 0xd4, 0x90, 0x8c, 0xa2, 0xd4, 0xc4, 0x94, 0x62, 0x09, 0x26, 0x05, 0x46, 0x0d, 0xc1, 0x20,
+	0x0c, 0x71, 0x25, 0x2d, 0x2e, 0x56, 0x90, 0x29, 0xc5, 0x42, 0x8a, 0x5c, 0x2c, 0x39, 0x99, 0xc5,
+	0x25, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, 0x46, 0xbc, 0x7a, 0x30, 0x5b, 0x41, 0xb2, 0x41, 0x60,
+	0x29, 0x23, 0x57, 0x2e, 0x11, 0xbf, 0xd4, 0xd2, 0xa2, 0xc4, 0x1c, 0xbf, 0xd4, 0x92, 0xf2, 0xfc,
+	0xa2, 0x6c, 0x67, 0x88, 0x12, 0x21, 0x5d, 0x2e, 0x8e, 0xa0, 0xd4, 0xf4, 0x4c, 0x30, 0x1b, 0x55,
+	0xa3, 0x14, 0x1f, 0x0a, 0xb7, 0x58, 0x89, 0xc1, 0x80, 0x31, 0x89, 0x0d, 0xec, 0x11, 0x63, 0x40,
+	0x00, 0x00, 0x00, 0xff, 0xff, 0x5f, 0x84, 0xe4, 0x41, 0xd9, 0x00, 0x00, 0x00,
+}
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ context.Context
+var _ grpc.ClientConnInterface
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+const _ = grpc.SupportPackageIsVersion6
+
+// NeuralNetworkClusterClient is the client API for NeuralNetworkCluster service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
+type NeuralNetworkClusterClient interface {
+	Register(ctx context.Context, in *Unit, opts ...grpc.CallOption) (NeuralNetworkCluster_RegisterClient, error)
+}
+
+type neuralNetworkClusterClient struct {
+	cc grpc.ClientConnInterface
+}
+
+func NewNeuralNetworkClusterClient(cc grpc.ClientConnInterface) NeuralNetworkClusterClient {
+	return &neuralNetworkClusterClient{cc}
+}
+
+func (c *neuralNetworkClusterClient) Register(ctx context.Context, in *Unit, opts ...grpc.CallOption) (NeuralNetworkCluster_RegisterClient, error) {
+	stream, err := c.cc.NewStream(ctx, &_NeuralNetworkCluster_serviceDesc.Streams[0], "/cluster.NeuralNetworkCluster/Register", opts...)
+	if err != nil {
+		return nil, err
+	}
+	x := &neuralNetworkClusterRegisterClient{stream}
+	if err := x.ClientStream.SendMsg(in); err != nil {
+		return nil, err
+	}
+	if err := x.ClientStream.CloseSend(); err != nil {
+		return nil, err
+	}
+	return x, nil
+}
+
+type NeuralNetworkCluster_RegisterClient interface {
+	Recv() (*Units, error)
+	grpc.ClientStream
+}
+
+type neuralNetworkClusterRegisterClient struct {
+	grpc.ClientStream
+}
+
+func (x *neuralNetworkClusterRegisterClient) Recv() (*Units, error) {
+	m := new(Units)
+	if err := x.ClientStream.RecvMsg(m); err != nil {
+		return nil, err
+	}
+	return m, nil
+}
+
+// NeuralNetworkClusterServer is the server API for NeuralNetworkCluster service.
+type NeuralNetworkClusterServer interface {
+	Register(*Unit, NeuralNetworkCluster_RegisterServer) error
+}
+
+// UnimplementedNeuralNetworkClusterServer can be embedded to have forward compatible implementations.
+type UnimplementedNeuralNetworkClusterServer struct {
+}
+
+func (*UnimplementedNeuralNetworkClusterServer) Register(req *Unit, srv NeuralNetworkCluster_RegisterServer) error {
+	return status.Errorf(codes.Unimplemented, "method Register not implemented")
+}
+
+func RegisterNeuralNetworkClusterServer(s *grpc.Server, srv NeuralNetworkClusterServer) {
+	s.RegisterService(&_NeuralNetworkCluster_serviceDesc, srv)
+}
+
+func _NeuralNetworkCluster_Register_Handler(srv interface{}, stream grpc.ServerStream) error {
+	m := new(Unit)
+	if err := stream.RecvMsg(m); err != nil {
+		return err
+	}
+	return srv.(NeuralNetworkClusterServer).Register(m, &neuralNetworkClusterRegisterServer{stream})
+}
+
+type NeuralNetworkCluster_RegisterServer interface {
+	Send(*Units) error
+	grpc.ServerStream
+}
+
+type neuralNetworkClusterRegisterServer struct {
+	grpc.ServerStream
+}
+
+func (x *neuralNetworkClusterRegisterServer) Send(m *Units) error {
+	return x.ServerStream.SendMsg(m)
+}
+
+var _NeuralNetworkCluster_serviceDesc = grpc.ServiceDesc{
+	ServiceName: "cluster.NeuralNetworkCluster",
+	HandlerType: (*NeuralNetworkClusterServer)(nil),
+	Methods:     []grpc.MethodDesc{},
+	Streams: []grpc.StreamDesc{
+		{
+			StreamName:    "Register",
+			Handler:       _NeuralNetworkCluster_Register_Handler,
+			ServerStreams: true,
+		},
+	},
+	Metadata: "cluster.proto",
+}

+ 56 - 0
cluster/cluster.proto

@@ -0,0 +1,56 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+syntax = "proto3";
+
+package cluster;
+
+message UnitInfo {
+    string url = 1;
+    sint32 availableThreads = 2;
+}
+
+message UnitInfos {
+    repeated UnitInfo list = 1;
+}
+
+service NeuralNetworkCluster {
+    rpc Register(UnitInfo) returns (stream UnitInfo) {}
+}
+
+message Matrix {
+    bytes matrix = 1;
+}
+
+message DataSet {
+    repeated Matrix biases = 1;
+    repeated Matrix weights = 2;
+    repeated Matrix data = 3;
+    repeated Matrix result = 4;
+}
+
+service NeuralNetworkUnit {
+    rpc Run(DataSet) returns (Matrix) {}
+}

+ 4 - 0
updateproto.sh

@@ -10,3 +10,7 @@ mkdir -p $RPC_PATH
 rm -f $RPC_PATH/*.pb.go
 protoc -I$RPC_PATH --go_out=plugins=grpc:$RPC_PATH $RPC_PATH/remotecontrol.proto
 
+# Cluster interface
+export CUSTER_PATH=$PWD/cluster
+rm -f $CUSTER_PATH/*.pb.go
+protoc -I$CUSTER_PATH --go_out=plugins=grpc:$CUSTER_PATH $CUSTER_PATH/cluster.proto