summaryrefslogtreecommitdiff
path: root/modules/remote-sycl/c++/remote.hpp
blob: 187366994543e577c56e43ff4f62b981d4728eb5 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#pragma once

#include "common.hpp"
#include "data.hpp"

namespace saw {

template<>
class remote<rmt::Sycl>;

template<typename T>
class device;

/**
 * Remote data class for the Sycl backend.
 */
template<typename T, typename Encoding, typename Storage>
class remote_data<T, Encoding, Storage, rmt::Sycl> {
private:
	/**
	 * Id representing the remote data
	 */
	id<T> id_;
	/**
	 * Storage for the
	 */
	id_map<T,Encoding,rmt::Sycl>* map_;
public:
	/**
	 * Main constructor
	 */
	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(){
		auto eov = map_->find(id_);
		if(eov.is_error()){
			auto& err = eov.get_error();
			return std::move(err);
		}
		auto& val = eov.get_value();
		std::cout<<"Values Sycl in Map: "<<val->size()<<std::endl;

		{
			auto eocop = val->template copy_to_host<storage::Default>();
			if(eocop.is_error()){
				return eocop;
			}
			return eocop.get_value();
		}
	}

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

/**
 * Sycl data class for handling the array Schema.
 */
template<typename T, uint64_t D>
class data<schema::Array<T,D>, encode::Native, rmt::Sycl> {
public:
	using Schema = schema::Array<T,D>;
private:
	/**
	 * Absolute size of the stored elementes.
	 */
	uint64_t total_length_;
	/**
	 * The data itself.
	 */
	data<T,encode::Native,storage::Default>* device_data_;
	/**
	 * Referenced sycl queue
	 */
	cl::sycl::queue* queue_;

	static_assert(is_primitive<T>::value, "Only supports primitives for now");
	static_assert(D==1u, "For now we only support 1D Arrays");
public:
	data(uint64_t size, cl::sycl::queue& q__):
		total_length_{size},
		device_data_{cl::sycl::malloc_device<data<T,encode::Native,storage::Default>>(size, q__)},
		queue_{&q__}
	{
		if(!device_data_){
			total_length_ = 0u;
			return;
		}
		queue_->wait();
	}

	template<typename Encoding, typename Storage>
	data(const data<Schema, Encoding, Storage>& from, cl::sycl::queue& q__):
		total_length_{from.size()},
		device_data_{cl::sycl::malloc_device<data<T,encode::Native,storage::Default>>(from.size(), q__)},
		queue_{&q__}
	{
		if(!device_data_){
			total_length_ = 0u;
			return;
		}
		queue_->template copy<data<T,encode::Native,storage::Default>>(&from.at(0), device_data_, total_length_);
		queue_->wait();
	}
	
	data(const data<Schema, encode::Native, rmt::Sycl>& from):
		total_length_{from.size()},
		device_data_{nullptr},
		queue_{from.queue_}
	{
		if(total_length_ == 0u || !queue_){
			return;
		}
		device_data_ = cl::sycl::malloc_device<data<T,encode::Native,storage::Default>>(from.size(), *queue_);
		// device_data_ = cl::sycl::malloc_device<typename native_data_type<T>::type>(from.size(), *queue_);
		if(!device_data_){
			total_length_ = 0u;
			return;
		}

		queue_->template copy<data<T,encode::Native,storage::Default>>(from.device_data_, device_data_, total_length_);
	}
	
	data(data<Schema, encode::Native, rmt::Sycl>&& rhs):
		total_length_{rhs.total_length_},
		device_data_{rhs.device_data_},
		queue_{rhs.queue_}
	{
		rhs.total_length_ = 0u;
		rhs.device_data_ = nullptr;
		rhs.queue_ = nullptr;
	}

	data<Schema, encode::Native, rmt::Sycl>& operator=(data<Schema, encode::Native, rmt::Sycl>&& rhs){
		total_length_ = rhs.total_length_;
		device_data_ = rhs.device_data_;
		queue_ = rhs.queue_;
		rhs.total_length_ = 0u;
		rhs.device_data_ = nullptr;
		rhs.queue_ = nullptr;
		return *this;
	}

	~data(){
		// free data
		if(device_data_){
			/// SYCL FREE
			cl::sycl::free(device_data_, *queue_);
		}
	}

	/**
	 * Allocate appropriate meta data and then copy to host
	 */
	template<typename Storage>
	error_or<data<Schema, encode::Native, Storage>> copy_to_host() const {
		data<Schema,encode::Native, Storage> data_{total_length_};

		/// TODO Check success
		queue_->template copy<data<T,encode::Native,storage::Default>>(device_data_, &data_.at(0), total_length_);
		queue_->wait();
		return data_;
	}

	template<typename Storage>
	static error_or<data<Schema, encode::Native, rmt::Sycl>> copy_to_device(const data<Schema, encode::Native, Storage>& host_data, device<rmt::Sycl>& dev);


	data<T, encode::Native, storage::Default>& at(uint64_t i){
		return device_data_[i];
	}

	uint64_t size() const {
		return total_length_;
	}
};

namespace impl {
template<typename Iface, typename Encoding, typename Storage>
struct rpc_id_map_helper {
	static_assert(always_false<Iface, Encoding,Storage>, "Only supports Interface schema types.");
};

template<typename... Members, typename Encoding, typename Storage>
struct rpc_id_map_helper<schema::Interface<Members...>, Encoding, Storage> {
	std::tuple<id_map<typename Members::ValueType::ResponseT, Encoding, Storage>...> maps;
};
}

/**
 * Represents a remote Sycl device.
 *
 */
template<>
class device<rmt::Sycl> final {
private:
	cl::sycl::queue cmd_queue_;
public:
	device() = default;

	SAW_FORBID_COPY(device);
	SAW_FORBID_MOVE(device);

	/**
	 * Copy data to device
	 */
	template<typename Schema, typename Encoding, typename Storage>
	error_or<data<Schema, Encoding, rmt::Sycl>> copy_to_device(const data<Schema, Encoding, Storage>& host_data){
		return data<Schema, Encoding, rmt::Sycl>::copy_to_device(host_data);
	}
	
	/**
	 * Copy data to host
	 */
	template<typename Schema, typename Encoding, typename Storage>
	error_or<data<Schema, Encoding, Storage>> copy_to_host(const data<Schema, Encoding, rmt::Sycl>& device_data){
		return device_data.copy_to_host();
	}
	
	/**
	 * Get a reference to the handle
	 */
	cl::sycl::queue& get_handle(){
		return cmd_queue_;
	}
};

/**
 * Device data transport
 */


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

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

	/**
	 * 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, Storage>& 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, Encoding, rmt::Sycl, InterfaceCtxT>;
private:
	/**
	 * Device instance enabling the use of the remote device.
	 */
	device<rmt::Sycl>* device_;

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

	/**
	 * Basic storage for response data.
	 */
	impl::rpc_id_map_helper<Iface, Encoding, rmt::Sycl> storage_;
public:

	/**
	 * Main constructor
	 */
	rpc_server(device<rmt::Sycl>& dev__, interface<Iface, Encoding, rmt::Sycl, InterfaceCtxT> cl_iface):
		device_{&dev__},
		cl_interface_{std::move(cl_iface)},
		storage_{}
	{}
	
	/**
	 * 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, typename ClientAllocation>
	error_or<
		id<
			typename schema_member_type<Name, Iface>::type::ResponseT
		>
	> call(data_or_id<typename schema_member_type<Name, Iface>::type::RequestT, Encoding, ClientAllocation> 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, Encoding, rmt::Sycl>> 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, Encoding, rmt::Sycl>* > {
			if(input.is_id()){
				// storage_.maps
				auto& inner_map = std::get<id_map<typename FuncT::RequestT, Encoding, rmt::Sycl>> (storage_.maps);
				auto eov = inner_map.find(input.get_id());
				if(eov.is_error()){
					return std::move(eov.get_error());
				}
				return eov.get_value();
			} else {
				auto& client_data = input.get_data();
				dev_tmp_inp = heap<data<typename FuncT::RequestT, Encoding, rmt::Sycl>>(client_data, device_->get_handle());
				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& inner_map = std::get<id_map<typename schema_member_type<Name, Iface>::type::RequestT, Encoding,rmt::Sycl>> (storage_.maps);
		auto eoid = inner_map.insert_as(std::move(val), rpc_id);
		if(eoid.is_error()){
			return std::move(eoid.get_error());
		}
		return rpc_id;
	}
};


template<>
struct remote_address<rmt::Sycl> {
private:
	remote<rmt::Sycl>* ctx_;

	SAW_FORBID_COPY(remote_address);
	SAW_FORBID_MOVE(remote_address);
public:
	remote_address(remote<rmt::Sycl>& r_ctx):
		ctx_{&r_ctx}
	{}
};

template<>
class remote<rmt::Sycl> {
private:
	SAW_FORBID_COPY(remote);
	SAW_FORBID_MOVE(remote);

public:
	/**
	 * Default constructor
	 */
	remote(){}

	/**
	 * For now we don't need to specify the location since
	 * we just create a default.
	 */
	conveyor<own<remote_address<rmt::Sycl>>> resolve_address(){
		return heap<remote_address<rmt::Sycl>>(*this);
	}

	/**
	 * Connect to a device
	 */
	device<rmt::Sycl> connect_device(const remote_address<rmt::Sycl>&){
		return {};
	}

	/**
	 * Spin up a rpc server
	 */
	template<typename Iface, typename Encoding>
	rpc_server<Iface, Encoding, rmt::Sycl> listen(device<rmt::Sycl>& dev, typename rpc_server<Iface, Encoding, rmt::Sycl>::InterfaceT iface){
		using RpcServerT = rpc_server<Iface, Encoding, rmt::Sycl>;
		using InterfaceT = typename RpcServerT::InterfaceT;
		return {dev, std::move(iface)};
	}
};

template<typename T, uint64_t D>
template<typename Storage>
error_or<data<schema::Array<T,D>, encode::Native, rmt::Sycl>> data<schema::Array<T,D>, encode::Native, rmt::Sycl>::copy_to_device(const data<schema::Array<T,D>, encode::Native, Storage>& host_data, device<rmt::Sycl>& dev){
		data<schema::Array<T,D>, encode::Native, rmt::Sycl> device_data{host_data.size(), dev.get_handle()};
		return make_error<err::not_implemented>();
}

}