File: stream_msg.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (277 lines) | stat: -rw-r--r-- 5,808 bytes parent folder | download | duplicates (7)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (c) 2018 Intel Corporation.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "shared.h"
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <rdma/fi_cm.h>

const char *msg = "hello stream!";


int send_stream(struct fid_ep *ep, const char *msg, size_t msg_len)
{
	int offset, ret;

	for (offset = 0; offset < msg_len; ) {
		ret = fi_send(ep, (msg + offset), (msg_len - offset), NULL, 0, NULL);
		if (ret < 0 && ret != -FI_EAGAIN) {
			fprintf(stderr, "%s error %s\n", __func__, fi_strerror(-ret));
			return ret;
		}

		if (ret > 0)
			offset += ret;
	}

	return offset;
}

int recv_stream(struct fid_ep *ep, char *msg, size_t msg_len)
{
	int offset, ret;

	for (offset = 0; offset < msg_len; ) {
		ret = fi_recv(ep, (msg + offset), (msg_len - offset), NULL, 0, NULL);
		if (ret < 0 && ret != -FI_EAGAIN) {
			fprintf(stderr, "%s error %s\n", __func__, fi_strerror(-ret));
			return ret;
		}
		if (ret > 0)
			offset += ret;
	}

	return offset;
}

static int send_greeting(struct fid_ep *ep)
{
	const size_t msg_len = strlen(msg);
	char buffer[msg_len];
	int ret;

	ret = send_stream(ep, msg, msg_len);
	if (ret < 0)
		return ret;

	ret = recv_stream(ep, buffer, msg_len);
	if (ret < 0)
		return ret;

	if (strncmp(buffer, msg, msg_len) != 0) {
		printf("error recv: %s\n", buffer);
		return -FI_EIO;
	}

	return 0;
}

static int recv_greeting(struct fid_ep *ep)
{
	const size_t msg_len = strlen(msg);
	char buffer[msg_len];
	int ret;

	ret = recv_stream(ep, buffer, msg_len);
	if (ret < 0)
		return ret;

	if (strncmp(buffer, msg, msg_len) != 0) {
		printf("error recv: %s\n", buffer);
		return -FI_EIO;
	}

	ret = send_stream(ep, msg, msg_len);
	if (ret < 0)
		return ret;

	return 0;
}

static int send_recv_greeting(struct fid_ep *ep)
{
	return opts.dst_addr ? recv_greeting(ep) : send_greeting(ep);
}

int stream_init_ep()
{
	int ret = fi_endpoint(domain, fi, &ep, NULL);
	if (ret) {
		FT_PRINTERR("fi_endpoint", ret);
		return ret;
	}

	FT_EP_BIND(ep, eq, 0);

	ret = fi_enable(ep);
	if (ret) {
		FT_PRINTERR("fi_enable", ret);
		return ret;
	}
	return 0;
}

void print_address(struct sockaddr_in *addr)
{
	printf(" accepted IPv4: %s port: %u\n", inet_ntoa(addr->sin_addr),
		ntohs(addr->sin_port));
}

int stream_server_connect(void)
{
	int ret;
	struct sockaddr_in peer_addr;
	size_t addrlen = sizeof(struct sockaddr_in);

	ret = ft_retrieve_conn_req(eq, &fi);
	if (ret)
		goto err;

	ret = fi_domain(fabric, fi, &domain, NULL);
	if (ret) {
		FT_PRINTERR("fi_domain", ret);
		goto err;
	}

	ret = stream_init_ep();
	if (ret)
		goto err;

	ret = ft_accept_connection(ep, eq);
	if (ret)
		goto err;

	ret = fi_getpeer(ep, &peer_addr, &addrlen);
	print_address(&peer_addr);
	return 0;

err:
	return ret;
}


int stream_client_connect()
{
	int ret;

	ret = ft_getinfo(hints, &fi);
	if (ret)
		return ret;

	ret = ft_open_fabric_res();
	if (ret)
		return ret;

	ret = stream_init_ep();
	if (ret)
		return ret;

	ret = ft_connect_ep(ep, eq, fi->dest_addr);
	if (ret)
		return ret;

	return 0;
}

void set_stream_hints(void) {
	hints->ep_attr->type		= FI_EP_SOCK_STREAM;
	hints->caps			= FI_MSG;
	hints->domain_attr->mr_mode	= 0;
	hints->addr_format		= FI_SOCKADDR;
	hints->domain_attr->threading = FI_THREAD_SAFE;
	hints->domain_attr->data_progress = FI_PROGRESS_MANUAL;
	hints->domain_attr->control_progress = FI_PROGRESS_AUTO;
	hints->tx_attr->msg_order = FI_ORDER_SAS;
	hints->rx_attr->msg_order = FI_ORDER_SAS;
}
static int stream_run(void)
{
	int ret;

	if (!opts.dst_addr) {
		ret = ft_start_server();
		if (ret)
			return ret;
	}

	ret = opts.dst_addr ? stream_client_connect() : stream_server_connect();
	if (ret) {
		return ret;
	}

	ret = send_recv_greeting(ep);
	if (ret < 0)
		return ret;

	fi_shutdown(ep, 0);
	return ret;
}

int main(int argc, char **argv)
{
	int op, ret;

	opts = INIT_OPTS;
	/* remove CQ usage on ep */
	opts.options = FT_OPT_SIZE;

	hints = fi_allocinfo();
	if (!hints)
		return EXIT_FAILURE;

	while ((op = getopt(argc, argv, "h" ADDR_OPTS INFO_OPTS)) != -1) {
		switch (op) {
		default:
			ft_parse_addr_opts(op, optarg, &opts);
			ft_parseinfo(op, optarg, hints, &opts);
			break;
		case '?':
		case 'h':
			ft_usage(argv[0], "A simple MSG client-sever example.");
			return EXIT_FAILURE;
		}
	}

	if (optind < argc)
		opts.dst_addr = argv[optind];

	set_stream_hints();

	ret = stream_run();

	ft_free_res();
	return ft_exit_code(ret);
}