-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.cu
208 lines (177 loc) · 5.24 KB
/
server.cu
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <arpa/inet.h>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <memory>
#include <nvml.h>
#include <future>
#include <stdio.h>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include <pthread.h>
#include <sys/uio.h>
#include "codegen/gen_server.h"
#define DEFAULT_PORT 14833
#define MAX_CLIENTS 10
typedef struct
{
int connfd;
int read_request_id;
int write_request_id;
pthread_mutex_t read_mutex, write_mutex;
struct iovec write_iov[128];
int write_iov_count = 0;
} conn_t;
int request_handler(const conn_t *conn)
{
unsigned int op;
// Attempt to read the operation code from the client
if (read(conn->connfd, &op, sizeof(unsigned int)) < 0)
return -1;
auto opHandler = get_handler(op);
if (opHandler == NULL)
{
std::cerr << "Unknown or unsupported operation: " << op << std::endl;
return -1;
}
return opHandler((void *)conn);
}
void client_handler(int connfd)
{
conn_t conn = {connfd};
if (pthread_mutex_init(&conn.read_mutex, NULL) < 0 ||
pthread_mutex_init(&conn.write_mutex, NULL) < 0)
{
std::cerr << "Error initializing mutex." << std::endl;
return;
}
#ifdef VERBOSE
printf("Client connected.\n");
#endif
while (1)
{
if (pthread_mutex_lock(&conn.read_mutex) < 0)
{
std::cerr << "Error locking mutex." << std::endl;
break;
}
int n = read(connfd, &conn.read_request_id, sizeof(int));
if (n == 0)
{
printf("client disconnected, loop continuing. \n");
break;
}
else if (n < 0)
{
printf("error reading from client.\n");
break;
}
// TODO: this can't be multithreaded as some of the __cuda* functions
// assume that they are running in the same thread as the one that
// calls cudaLaunchKernel. we'll need to find a better way to map
// function calls to threads. maybe each rpc maps to an optional
// thread id that is passed to the handler?
if (request_handler(&conn) < 0)
std::cerr << "Error handling request." << std::endl;
}
if (pthread_mutex_destroy(&conn.read_mutex) < 0 ||
pthread_mutex_destroy(&conn.write_mutex) < 0)
std::cerr << "Error destroying mutex." << std::endl;
close(connfd);
}
int rpc_read(const void *conn, void *data, size_t size)
{
return recv(((conn_t *)conn)->connfd, data, size, MSG_WAITALL);
}
int rpc_write(const void *conn, const void *data, const size_t size)
{
((conn_t *)conn)->write_iov[((conn_t *)conn)->write_iov_count++] = (struct iovec){(void *)data, size};
return 0;
}
// signal from the handler that the request read is complete.
int rpc_end_request(const void *conn)
{
int request_id = ((conn_t *)conn)->read_request_id;
if (pthread_mutex_unlock(&((conn_t *)conn)->read_mutex) < 0)
return -1;
return request_id;
}
int rpc_start_response(const void *conn, const int request_id)
{
if (pthread_mutex_lock(&((conn_t *)conn)->write_mutex) < 0)
return -1;
((conn_t *)conn)->write_request_id = request_id;
((conn_t *)conn)->write_iov_count = 1;
return 0;
}
int rpc_end_response(const void *conn, void *result)
{
((conn_t *)conn)->write_iov[0] = (struct iovec){&((conn_t *)conn)->write_request_id, sizeof(int)};
((conn_t *)conn)->write_iov[((conn_t *)conn)->write_iov_count++] = (struct iovec){result, sizeof(int)};
if (writev(((conn_t *)conn)->connfd, ((conn_t *)conn)->write_iov, ((conn_t *)conn)->write_iov_count) < 0 ||
pthread_mutex_unlock(&((conn_t *)conn)->write_mutex) < 0)
return -1;
return 0;
}
int main()
{
int port = DEFAULT_PORT;
struct sockaddr_in servaddr, cli;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("Socket creation failed.\n");
exit(EXIT_FAILURE);
}
char *p = getenv("SCUDA_PORT");
if (p == NULL)
{
port = DEFAULT_PORT;
}
else
{
port = atoi(p);
}
// Bind the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
const int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
printf("Socket bind failed.\n");
exit(EXIT_FAILURE);
}
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
{
printf("Socket bind failed.\n");
exit(EXIT_FAILURE);
}
if (listen(sockfd, MAX_CLIENTS) != 0)
{
printf("Listen failed.\n");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", port);
// Server loop
while (1)
{
socklen_t len = sizeof(cli);
int connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0)
{
std::cerr << "Server accept failed." << std::endl;
continue;
}
std::thread client_thread(client_handler, connfd);
// detach the thread so it runs independently
client_thread.detach();
}
close(sockfd);
return 0;
}