summaryrefslogtreecommitdiff
path: root/modules/remote-sycl/c++/rpc.hpp
blob: 65e2df57ce252478793275e82766c6f7e711ddfa (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
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
#pragma once

#include "common.hpp"
#include "remote.hpp"
#include "data.hpp"
#include "device.hpp"
#include "transfer.hpp"

namespace saw {
/**
 * Remote data class for the Sycl backend.
 */
template<typename T, typename Encoding, typename Storage>
class remote_data<T, Encoding, Storage, rmt::Sycl> final {
private:
	/**
	 * An identifier to the data being held on the remote
	 */
	id<T> data_id_;

	/**
	 * The sycl queue object
	 */
	cl::sycl::queue* queue_;
public:
	/**
	 * Main constructor
	 */
	remote_data(id<T> data_id__, cl::sycl::queue& queue__):
		data_id_{data_id__},
		queue_{&queue__}
	{}

	/**
	 * Destructor specifically designed to deallocate on the device.
	 */
	~remote_data(){}

	SAW_FORBID_COPY(remote_data);
	SAW_FORBID_MOVE(remote_data);
	/**
	remote_data(const id<T>& id, id_map<T, Encoding, rmt::Sycl>& map, cl::sycl::queue& queue__):
		id_{id},
		map_{&map}
	{}
	*/

	/**
	 * Wait for the data
	 */
	error_or<data<T,Encoding,Storage>> wait(){
		return make_error<err::not_implemented>();
	}

	/**
	 * Request data asynchronously
	 */
	// conveyor<data<T,Encoding,Storage>> on_receive(); /// Stopped here
};

/**
 * Meant to be a helper object which holds the allocated data on the sycl side
 */
//template<typename Schema, typename Encoding, typename Backend>
//class device_data;

/**
 * This class helps in regards to the ownership on the server side
template<typename Schema, typename Encoding>
class device_data<Schema, Encoding, rmt::Sycl> {
private:
	data<Schema,Encoding,storage::Default>* device_data_;
	cl::sycl::queue* queue_;
public:
	device_data(data<Schema,Encoding,storage::Default>& device_data__, cl::sycl::queue& queue__):
		device_data_{&device_data__},
		queue_{&queue__}
	{}
	
	~device_data(){
		if(device_data_){
			cl::sycl::free(device_data_,queue_);
			device_data_ = nullptr;
		}
	}

	SAW_FORBID_COPY(device_data);
	SAW_FORBID_MOVE(device_data);
};
 */

}
// Maybe a helper impl tmpl file?
namespace saw {


namespace impl {
template<typename Func>
struct rpc_func_type_helper;

template<typename Response, typename Request> 
struct rpc_func_type_helper<schema::Function<Request, Response>>{
	using type = tmpl_group<Response, Request>;
};

template<typename Iface>
struct rpc_iface_type_helper {
	using type = tmpl_group<>;
};

template<typename Func, string_literal K, typename... Functions, string_literal... Keys>
struct rpc_iface_type_helper<schema::Interface<schema::Member<Func,K>,schema::Member<Functions,Keys>...>> {
	using inner_type = typename rpc_func_type_helper<Func>::type;
	using type = typename tmpl_concat<inner_type, typename rpc_iface_type_helper<schema::Interface<schema::Member<Functions,Keys>...>>::type>::type;
};
}

/**
 * Rpc Client class for the Sycl backend.
 */
template<typename Iface, typename Encoding>
class rpc_client<Iface, Encoding, rmt::Sycl> {
public:
private:
	/**
	 * Server this client is tied to
	 */
	rpc_server<Iface, Encoding, rmt::Sycl>* srv_;

	/**
	 * TransferClient created from the internal RPC data server
	 */
	data_client<typename impl::rpc_iface_type_helper<Iface>::type, Encoding, rmt::Sycl> data_client_;

	/**
	 * Generated some sort of id for the request.
	 */
public:
	rpc_client(rpc_server<Iface, Encoding, rmt::Sycl>& srv):
		srv_{&srv},
		data_client_{srv_->data_server}
	{}

	/**
	 * Rpc call
	 */
	template<string_literal Name>
	error_or<
		id<
			typename schema_member_type<Name, Iface>::type::ResponseT
		>
	> call(const data_or_id<typename schema_member_type<Name, Iface>::type::RequestT, Encoding>& input){
		auto next_free_id = srv_->template next_free_id<typename schema_member_type<Name, Iface>::type::ResponseT>();
		return srv_->template call<Name, Storage>(input, next_free_id);
	}

};

/**
 * Rpc Server class for the Sycl backend.
 */
template<typename Iface, typename Encoding>
class rpc_server<Iface, Encoding, rmt::Sycl> {
public:
	using InterfaceCtxT = cl::sycl::queue*;
	using InterfaceT = interface<Iface, encode::Sycl<Encoding>, InterfaceCtxT>;

private:
	/**
	 * Device instance enabling the use of the remote device.
	 */
	our<device<rmt::Sycl>> device_;

	using DataServerT = data_server<typename impl::rpc_iface_type_helper<Iface>::type, Encoding, rmt::Sycl>;
	/**
	 * Data server storing the relevant data
	 */
	DataServerT* data_server_;

	/**
	 * The interface including the relevant context class.
	 */
	interface<Iface, Encoding, storage::Default, InterfaceCtxT> cl_interface_;

public:

	/**
	 * Main constructor
	 */
	rpc_server(our<device<rmt::Sycl>> dev__, DataServerT& data_server__, InterfaceT cl_iface):
		device_{std::move(dev__)},
		data_server_{&data_server__},
		cl_interface_{std::move(cl_iface)}
	{}
	
	/**
	 * Ask which id the server prefers as the next one. Only available for fast requests on no roundtrip setups.
	 */
	/**
	template<typename T>
	id<T> next_free_id() const {
		return std::get<id_map<T,Encoding,rmt::Sycl>>(storage_.maps).next_free_id();
	}
	*/

	/**
	template<typename IdT, typename Storage>
	remote_data<IdT, Encoding, Storage, rmt::Sycl> request_data(id<IdT> dat_id){
		return {dat_id, std::get<id_map<IdT,Encoding,rmt::Sycl>>(storage_.maps), device_->get_handle()};
	}
	*/

	/**
	 * Rpc call based on the name
	 */
	template<string_literal Name>
	error_or<
		id<
			typename schema_member_type<Name, Iface>::type::ResponseT
		>
	> call(data_or_id<typename schema_member_type<Name, Iface>::type::RequestT, Encoding> input, id<typename schema_member_type<Name,Iface>::type::ResponseT> rpc_id){
		using FuncT = typename schema_member_type<Name, Iface>::type;

		/**
		 * Object needed if and only if the provided data type is not an id
		 */
		own<data<typename FuncT::RequestT, encode::Sycl<Encoding>>> dev_tmp_inp = nullptr;
		/**
		 * First check if it's data or an id.
		 * If it's an id, check if it's registered within the storage and retrieve it.
		 */
		auto eoinp = [&,this]() -> error_or<data<typename FuncT::RequestT, encode::Sycl<Encoding>>* > {
			if(input.is_id()){
				// storage_.maps
				auto eov = data_server_->template find<typename FuncT::RequestT>(input.get_id());
				if(eov.is_error()){
					return std::move(eov.get_error());
				}
				return eov.get_value();
			} else {
				auto& client_data = input.get_data();

				auto eov = device_->template copy_to_device(client_data);
				if(eov.is_error()){
					return std::move(eov.get_error());
				}
				auto& val = eov.get_value();

				dev_tmp_inp = heap<data<typename FuncT::RequestT, encode::Sycl<Encoding>>>(std::move(val));
				device_->get_handle().wait();
				return dev_tmp_inp.get();
			}
		}();
		if(eoinp.is_error()){
			return std::move(eoinp.get_error());
		}
		auto& inp = *(eoinp.get_value());

		auto eod = cl_interface_.template call<Name>(inp, &(device_->get_handle()));

		if(eod.is_error()){
			return std::move(eod.get_error());
		}

		auto& val = eod.get_value();
		/**
		 * Store returned data in rpc storage
		 */
		auto eoid = data_server_->template insert<typename schema_member_type<Name, Iface>::type::RequestT>(std::move(val), rpc_id);
		if(eoid.is_error()){
			return std::move(eoid.get_error());
		}
		return rpc_id;
	}
};
}