Loading...
Searching...
No Matches
worker.h
1
5#pragma once
6
7#include <atomic>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <queue>
12#include <string>
13#include <thread>
14#include <utility>
15#include <variant>
16
17#include <ucp/api/ucp.h>
18
19#include <ucxx/address_builder.h>
20#include <ucxx/buffer.h>
21#include <ucxx/component.h>
22#include <ucxx/context.h>
23#include <ucxx/delayed_submission.h>
24#include <ucxx/endpoint_builder.h>
25#include <ucxx/future.h>
26#include <ucxx/inflight_requests.h>
27#include <ucxx/listener_builder.h>
28#include <ucxx/notifier.h>
29#include <ucxx/request_flush_builder.h>
30#include <ucxx/request_tag_builder.h>
31#include <ucxx/typedefs.h>
32#include <ucxx/worker_builder.h>
33#include <ucxx/worker_progress_thread.h>
34
35namespace ucxx {
36
37class Component;
38class Request;
39
40class WorkerBuilder;
41
42namespace detail {
43void registerInflightRequest(std::shared_ptr<Component> component, std::shared_ptr<Request> req);
44} // namespace detail
45
46class Address;
47class Buffer;
48class Endpoint;
49class Listener;
50class RequestAm;
51
52namespace internal {
53class AmData;
54} // namespace internal
55
62class Worker : public Component {
63 private:
64 ucp_worker_h _handle{nullptr};
65 int _epollFileDescriptor{-1};
66 int _workerFileDescriptor{-1};
67 std::mutex _inflightRequestsMutex{};
68 std::unique_ptr<InflightRequests> _inflightRequests{
69 std::make_unique<InflightRequests>()};
70 std::mutex
71 _inflightRequestsToCancelMutex{};
72 std::unique_ptr<InflightRequests> _inflightRequestsToCancel{
73 std::make_unique<InflightRequests>()};
74 WorkerProgressThread _progressThread{};
75 std::atomic<bool> _progressThreadRunning{false};
76 std::atomic<bool> _progressThreadPollingMode{false};
77 std::atomic<std::thread::id> _progressThreadId{std::thread::id()};
78 std::function<void(void*)> _progressThreadStartCallback{
79 nullptr};
80 void* _progressThreadStartCallbackArg{
81 nullptr};
82 std::shared_ptr<DelayedSubmissionCollection> _delayedSubmissionCollection{
83 nullptr};
84
85 friend class detail::ConstructorFactory;
86
87 protected:
89 false};
91 false};
92 std::mutex _futuresPoolMutex{};
93 std::queue<std::shared_ptr<Future>>
95 std::shared_ptr<Notifier> _notifier{nullptr};
96 std::shared_ptr<internal::AmData>
98 BufferType _cudaBufferType{BufferType::Invalid};
99
100 private:
107 void drainWorkerTagRecv();
108
123 [[nodiscard]] std::shared_ptr<RequestAm> getAmRecv(
124 ucp_ep_h ep, std::function<std::shared_ptr<RequestAm>()> createAmRecvRequestFunction);
125
132 void stopProgressThreadNoWarn();
133
144 [[nodiscard]] std::shared_ptr<Request> registerInflightRequest(std::shared_ptr<Request> request);
145
153 bool progressPending();
154
166 void setCudaBufferType(BufferType bufferType);
167
168 protected:
189 explicit Worker(std::shared_ptr<Context> context,
190 const bool enableDelayedSubmission = false,
191 const bool enableFuture = false);
192
193 public:
194 Worker() = delete;
195 Worker(const Worker&) = delete;
196 Worker& operator=(Worker const&) = delete;
197 Worker(Worker&& o) = delete;
198 Worker& operator=(Worker&& o) = delete;
199
203 friend class WorkerBuilder;
204
208 friend void detail::registerInflightRequest(std::shared_ptr<Component> component,
209 std::shared_ptr<Request> req);
210
214 virtual ~Worker();
215
231 [[nodiscard]] ucp_worker_h getHandle();
232
241 [[nodiscard]] std::string getInfo();
242
272
289
300 bool arm();
301
329 bool progressWorkerEvent(const int epollTimeout = -1);
330
365 void signal();
366
389
405
421 bool progress();
422
439 void registerDelayedSubmission(std::shared_ptr<Request> request,
441
470 uint64_t period = 0);
471
499 uint64_t period = 0);
500
508 [[nodiscard]] bool isDelayedRequestSubmissionEnabled() const;
509
517 [[nodiscard]] bool isFutureEnabled() const;
518
530 [[nodiscard]] bool isRequestAttributesEnabled() const noexcept;
531
541 [[nodiscard]] BufferType getCudaBufferType() const;
542
554 virtual void populateFuturesPool();
555
565 virtual void clearFuturesPool();
566
578 [[nodiscard]] virtual std::shared_ptr<Future> getFuture();
579
594 [[nodiscard]] virtual RequestNotifierWaitState waitRequestNotifier(uint64_t periodNs);
595
608 virtual void runRequestNotifier();
609
618
629 void setProgressThreadStartCallback(std::function<void(void*)> callback, void* callbackArg);
630
643 void startProgressThread(const bool pollingMode = false, const int epollTimeout = 1);
644
654
662 [[nodiscard]] bool isProgressThreadRunning();
663
671 [[nodiscard]] std::thread::id getProgressThreadId();
672
693 size_t cancelInflightRequests(uint64_t period = 0, uint64_t maxAttempts = 1);
694
708
718 void removeInflightRequest(std::shared_ptr<Request> request);
719
762 [[nodiscard]] std::shared_ptr<TagProbeInfo> tagProbe(const Tag tag,
763 const TagMask tagMask = TagMaskFull,
764 const bool remove = false) const;
765
780 [[nodiscard]] RequestTagBuilder tagRecvBuilder(void* buffer,
781 size_t length,
782 Tag tag,
783 TagMask tagMask);
784
797 [[nodiscard]] RequestTagBuilder tagRecvWithHandleBuilder(void* buffer,
798 std::shared_ptr<TagProbeInfo> probeInfo);
799
813 [[nodiscard]] RequestTagBuilder tagRecvWithHandleBuilder(void* buffer,
814 size_t length,
815 std::shared_ptr<TagProbeInfo> probeInfo);
816
826
837 [[nodiscard]] EndpointBuilder endpointBuilder(std::string ipAddress, uint16_t port);
838
848 [[nodiscard]] EndpointBuilder endpointBuilder(std::shared_ptr<Address> address);
849
861 [[nodiscard]] ListenerBuilder listenerBuilder(uint16_t port,
862 ucp_listener_conn_callback_t callback,
863 void* callbackArgs);
864
894 void registerAmAllocator(ucs_memory_type_t memoryType, AmAllocatorType allocator);
895
931
951 [[nodiscard]] bool amProbe(const ucp_ep_h endpointHandle) const;
952
962
966 struct Attributes {
968 ucs_thread_mode_t threadMode{UCS_THREAD_MODE_MULTI};
970 size_t maxAmHeader{0};
972 std::string name{};
974 size_t maxDebugString{0};
975 };
976
987 [[nodiscard]] Attributes queryAttributes() const;
988};
989
990} // namespace ucxx
Builder class for constructing std::shared_ptr<ucxx::Address> objects.
Definition address_builder.h:23
Component encapsulating the address of a UCP worker.
Definition address.h:26
Information of an Active Message receiver callback.
Definition typedefs.h:224
A simple object to simplify managing buffers.
Definition buffer.h:31
A UCXX component class to prevent early destruction of parent object.
Definition component.h:21
Builder class for constructing std::shared_ptr<ucxx::Endpoint> objects.
Definition endpoint_builder.h:28
Component encapsulating a UCP endpoint.
Definition endpoint.h:69
Represent a future that may be notified by a specialized notifier.
Definition future.h:21
Builder class for constructing std::shared_ptr<ucxx::Listener> objects.
Definition listener_builder.h:26
Component encapsulating a UCP listener.
Definition listener.h:26
Send or receive a message with the UCX Active Message API.
Definition request_am.h:33
Builder class for constructing std::shared_ptr<ucxx::RequestFlush> objects.
Definition request_flush_builder.h:39
Builder class for constructing std::shared_ptr<ucxx::RequestTag> objects.
Definition request_tag_builder.h:40
Base type for a UCXX transfer request.
Definition request.h:39
Information about probed tag message.
Definition tag_probe.h:65
Builder class for constructing std::shared_ptr<ucxx::Worker> objects.
Definition worker_builder.h:45
A thread to progress a ucxx::Worker.
Definition worker_progress_thread.h:50
bool amProbe(const ucp_ep_h endpointHandle) const
Check for uncaught active messages.
int getEpollFileDescriptor()
Get the epoll file descriptor associated with the worker.
bool progress()
Progress the worker until all communication events are completed.
void registerAmReceiverCallback(AmReceiverCallbackInfo info, AmReceiverCallbackType callback)
Register receiver callback for active messages.
ListenerBuilder listenerBuilder(uint16_t port, ucp_listener_conn_callback_t callback, void *callbackArgs)
Create a builder for a listener on this worker.
AddressBuilder addressBuilder()
Create a builder for this worker's address.
bool isRequestAttributesEnabled() const noexcept
Inquire if worker has been created with request attributes querying enabled.
bool _enableRequestAttributes
Whether request attributes (e.g. UCP debug info) are queried for each request.
Definition worker.h:90
void signal()
Signal the worker that an event happened.
virtual void populateFuturesPool()
Populate the futures pool.
bool registerGenericPre(DelayedSubmissionCallbackType callback, uint64_t period=0)
Register callback to be executed in progress thread before progressing.
std::shared_ptr< TagProbeInfo > tagProbe(const Tag tag, const TagMask tagMask=TagMaskFull, const bool remove=false) const
Check for uncaught tag messages.
void setProgressThreadStartCallback(std::function< void(void *)> callback, void *callbackArg)
Set callback to be executed at the progress thread start.
bool progressOnce()
Progress the worker only once.
void startProgressThread(const bool pollingMode=false, const int epollTimeout=1)
Start the progress thread.
bool isProgressThreadRunning()
Inquire if worker has a progress thread running.
RequestTagBuilder tagRecvWithHandleBuilder(void *buffer, std::shared_ptr< TagProbeInfo > probeInfo)
Create a builder for a tag receive operation using a message handle.
friend class WorkerBuilder
Allow WorkerBuilder to access protected/private constructor.
Definition worker.h:203
void registerAmAllocator(ucs_memory_type_t memoryType, AmAllocatorType allocator)
Register allocator for active messages.
void stopProgressThread()
Stop the progress thread.
Worker(std::shared_ptr< Context > context, const bool enableDelayedSubmission=false, const bool enableFuture=false)
Protected constructor of ucxx::Worker.
virtual std::shared_ptr< Future > getFuture()
Get a future from the pool.
std::queue< std::shared_ptr< Future > > _futuresPool
Futures pool to prevent running out of fresh futures.
Definition worker.h:94
EndpointBuilder endpointBuilder(std::string ipAddress, uint16_t port)
Create a builder for an endpoint to a worker listening on an IP and port.
BufferType _cudaBufferType
Preferred buffer type for CUDA allocations.
Definition worker.h:98
bool isDelayedRequestSubmissionEnabled() const
Inquire if worker has been created with delayed submission enabled.
virtual ~Worker()
ucxx::Worker destructor.
virtual void clearFuturesPool()
Clear the futures pool.
ucp_worker_h getHandle()
Get the underlying ucp_worker_h handle.
std::mutex _futuresPoolMutex
Mutex to access the futures pool.
Definition worker.h:92
virtual RequestNotifierWaitState waitRequestNotifier(uint64_t periodNs)
Block until a request event.
bool arm()
Arm the UCP worker.
bool waitProgress()
Block until an event has happened, then progresses.
virtual void runRequestNotifier()
Notify futures of each completed communication request.
bool registerGenericPost(DelayedSubmissionCallbackType callback, uint64_t period=0)
Register callback to be executed in progress thread before progressing.
std::shared_ptr< Notifier > _notifier
Notifier object.
Definition worker.h:95
RequestTagBuilder tagRecvBuilder(void *buffer, size_t length, Tag tag, TagMask tagMask)
Create a builder for a tag receive operation.
void registerDelayedSubmission(std::shared_ptr< Request > request, DelayedSubmissionCallbackType callback)
Register delayed request submission.
std::thread::id getProgressThreadId()
Get the progress thread ID.
size_t cancelInflightRequests(uint64_t period=0, uint64_t maxAttempts=1)
Cancel inflight requests.
bool _enableFuture
Boolean identifying whether the worker was created with future capability.
Definition worker.h:88
bool isFutureEnabled() const
Inquire if worker has been created with future support.
RequestFlushBuilder flushBuilder()
Create a builder for a flush operation.
Attributes queryAttributes() const
Get the worker's attributes.
virtual void stopRequestNotifierThread()
Signal the notifier to terminate.
void removeInflightRequest(std::shared_ptr< Request > request)
Remove reference to request from internal container.
bool progressWorkerEvent(const int epollTimeout=-1)
Progress worker event while in blocking progress mode.
BufferType getCudaBufferType() const
Get the preferred buffer type for CUDA allocations.
std::shared_ptr< internal::AmData > _amData
Worker data made available to Active Messages callback.
Definition worker.h:97
void scheduleRequestCancel(TrackedRequests trackedRequests)
Schedule cancelation of inflight requests.
void initBlockingProgressMode()
Initialize blocking progress mode.
std::string getInfo()
Get information about the underlying ucp_worker_h object.
Active Message data owned by a ucxx::Worker.
Definition request_am.h:102
Definition address.h:16
std::function< void()> DelayedSubmissionCallbackType
A user-defined function to execute as part of delayed submission callback.
Definition delayed_submission.h:32
std::function< std::shared_ptr< Buffer >(size_t)> AmAllocatorType
Custom Active Message allocator type.
Definition typedefs.h:129
RequestNotifierWaitState
The state with which a wait operation completed.
Definition notifier.h:26
TagMask
Strong type for a UCP tag mask.
Definition typedefs.h:74
Tag
Strong type for a UCP tag.
Definition typedefs.h:66
std::function< void(std::shared_ptr< Request >, ucp_ep_h)> AmReceiverCallbackType
Active Message receiver callback.
Definition typedefs.h:138
BufferType
The type of a buffer.
Definition buffer.h:17
Container for transferring tracked requests between InflightRequests instances.
Definition inflight_requests.h:23
Worker attributes reported by ucp_worker_query.
Definition worker.h:966
size_t maxDebugString
Maximum debug-string buffer size accepted by ucp_request_query.
Definition worker.h:974
std::string name
Worker name used by tracing and analysis tools.
Definition worker.h:972
size_t maxAmHeader
Maximum allowed header size for ucp_am_send_nbx.
Definition worker.h:970
ucs_thread_mode_t threadMode
Thread safety level the worker was created with.
Definition worker.h:968