aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/training/server_lib.i
blob: 94250304f853ba1f942506bcfb3240a4adab3797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

%nothread tensorflow::ServerInterface::Join;

%include "tensorflow/python/platform/base.i"

//%newobject tensorflow::NewServer;

%typemap(in) const ServerDef& (tensorflow::ServerDef temp) {
  char* c_string;
  Py_ssize_t py_size;
  if (PyBytes_AsStringAndSize($input, &c_string, &py_size) == -1) {
    // Python has raised an error (likely TypeError or UnicodeEncodeError).
    SWIG_fail;
  }

  if (!temp.ParseFromString(string(c_string, py_size))) {
    PyErr_SetString(
        PyExc_TypeError,
        "The ServerDef could not be parsed as a valid protocol buffer");
    SWIG_fail;
  }
  $1 = &temp;
}

%typemap(in, numinputs=0)
    std::unique_ptr<tensorflow::ServerInterface>* out_server (
        std::unique_ptr<tensorflow::ServerInterface> temp) {
  $1 = &temp;
}

%typemap(argout) std::unique_ptr<tensorflow::ServerInterface>* out_server {
  // TODO(mrry): Convert this to SWIG_POINTER_OWN when the issues with freeing
  // a server are fixed.
  $result = SWIG_NewPointerObj($1->release(),
                               $descriptor(tensorflow::ServerInterface*),
                               0);
}

%feature("except") tensorflow::ServerInterface::Join {
  // Let other threads run while we wait for the server to shut down.
  Py_BEGIN_ALLOW_THREADS
  $action
  Py_END_ALLOW_THREADS
}

%{
#include "tensorflow/c/tf_status_helper.h"
#include "tensorflow/core/distributed_runtime/server_lib.h"
#include "tensorflow/core/lib/core/status.h"

using tensorflow::ServerDef;

static void PyServer_New(const ServerDef& server_def,
                         std::unique_ptr<tensorflow::ServerInterface>* out_server,
                         TF_Status* out_status) {
  tensorflow::Status status =
      tensorflow::NewServer(server_def, out_server);
  tensorflow::Set_TF_Status_from_Status(out_status, status);
}

static void PyServer_Start(
    tensorflow::ServerInterface* in_server,
    TF_Status* out_status) {
  tensorflow::Set_TF_Status_from_Status(out_status, in_server->Start());
}

static void PyServer_Stop(
    tensorflow::ServerInterface* in_server,
    TF_Status* out_status) {
  tensorflow::Set_TF_Status_from_Status(out_status, in_server->Stop());
}

static void PyServer_Join(
    tensorflow::ServerInterface* in_server,
    TF_Status* out_status) {
  tensorflow::Set_TF_Status_from_Status(out_status, in_server->Join());
}
%}

// Wrap this function.
void PyServer_New(const ServerDef& server_def,
                  std::unique_ptr<tensorflow::ServerInterface>* out_server,
                  TF_Status* out_status);
void PyServer_Start(tensorflow::ServerInterface* in_server,
                    TF_Status* out_status);
void PyServer_Stop(tensorflow::ServerInterface* in_server,
                   TF_Status* out_status);
void PyServer_Join(tensorflow::ServerInterface* in_server,
                   TF_Status* out_status);

%ignoreall

%unignore tensorflow;
%unignore tensorflow::ServerDef;
%unignore tensorflow::ServerInterface;
%unignore tensorflow::ServerInterface::~ServerInterface;
%unignore tensorflow::ServerInterface::target;

%unignore PyServer_New;
%unignore PyServer_Start;
%unignore PyServer_Stop;
%unignore PyServer_Join;

%include "tensorflow/core/distributed_runtime/server_lib.h"

%unignoreall