|
9 | 9 | #include "utils.h"
|
10 | 10 | #include "bootstrap.h"
|
11 | 11 | #include "net.h"
|
| 12 | +#include "socket.h" |
12 | 13 | #include <unistd.h>
|
13 | 14 | #include <sys/types.h>
|
14 | 15 |
|
15 | 16 | // Always use sockets for bootstrap
|
16 |
| -ncclNet_t* ncclBootstrapNet = &ncclNetSocket; |
| 17 | +struct bootstrapNetHandle { |
| 18 | + union socketAddress connectAddr; |
| 19 | +}; |
| 20 | + |
| 21 | +struct bootstrapNetComm { |
| 22 | + int fd; |
| 23 | +}; |
| 24 | + |
| 25 | +/* Init functions */ |
| 26 | +static char bootstrapNetIfNames[MAX_IF_NAME_SIZE*MAX_IFS]; |
| 27 | +static union socketAddress bootstrapNetIfAddrs[MAX_IFS]; |
| 28 | +static int bootstrapNetIfs = -1; |
| 29 | +pthread_mutex_t bootstrapNetLock = PTHREAD_MUTEX_INITIALIZER; |
| 30 | + |
| 31 | +ncclResult_t bootstrapNetInit() { |
| 32 | + if (bootstrapNetIfs == -1) { |
| 33 | + pthread_mutex_lock(&bootstrapNetLock); |
| 34 | + if (bootstrapNetIfs == -1) { |
| 35 | + bootstrapNetIfs = findInterfaces(bootstrapNetIfNames, bootstrapNetIfAddrs, MAX_IF_NAME_SIZE, MAX_IFS); |
| 36 | + if (bootstrapNetIfs <= 0) { |
| 37 | + WARN("Bootstrap : no socket interface found"); |
| 38 | + return ncclInternalError; |
| 39 | + } else { |
| 40 | + char line[1024]; |
| 41 | + char addrline[1024]; |
| 42 | + line[0] = '\0'; |
| 43 | + for (int i=0; i<bootstrapNetIfs; i++) { |
| 44 | + snprintf(line+strlen(line), 1023-strlen(line), " [%d]%s:%s", i, bootstrapNetIfNames+i*MAX_IF_NAME_SIZE, |
| 45 | + socketToString(&bootstrapNetIfAddrs[i].sa, addrline)); |
| 46 | + } |
| 47 | + line[1023] = '\0'; |
| 48 | + INFO(NCCL_INIT, "Bootstrap : Using%s", line); |
| 49 | + } |
| 50 | + } |
| 51 | + pthread_mutex_unlock(&bootstrapNetLock); |
| 52 | + } |
| 53 | + return ncclSuccess; |
| 54 | +} |
| 55 | + |
| 56 | +static ncclResult_t bootstrapNetNewComm(struct bootstrapNetComm** comm) { |
| 57 | + NCCLCHECK(ncclCalloc(comm, 1)); |
| 58 | + (*comm)->fd = -1; |
| 59 | + return ncclSuccess; |
| 60 | +} |
| 61 | + |
| 62 | +static ncclResult_t bootstrapNetGetSocketAddr(int dev, union socketAddress* addr) { |
| 63 | + if (dev >= bootstrapNetIfs) return ncclInternalError; |
| 64 | + memcpy(addr, bootstrapNetIfAddrs+dev, sizeof(*addr)); |
| 65 | + return ncclSuccess; |
| 66 | +} |
| 67 | + |
| 68 | +/* Socket Interface Selection type */ |
| 69 | +enum bootstrapInterface_t { findSubnetIf = -1, dontCareIf = -2 }; |
| 70 | + |
| 71 | +static ncclResult_t bootstrapNetListen(int dev, void* opaqueHandle, void** listenComm) { |
| 72 | + struct bootstrapNetHandle* handle = (struct bootstrapNetHandle*) opaqueHandle; |
| 73 | + static_assert(sizeof(struct bootstrapNetHandle) < NCCL_NET_HANDLE_MAXSIZE, "bootstrapNetHandle size too large"); |
| 74 | + // if dev >= 0, listen based on dev |
| 75 | + if (dev >= 0) { |
| 76 | + NCCLCHECK(bootstrapNetGetSocketAddr(dev, &(handle->connectAddr))); |
| 77 | + } else if (dev == findSubnetIf) { |
| 78 | + // handle stores a remote address |
| 79 | + // need to find a local addr that is in the same network as the remote addr |
| 80 | + union socketAddress localAddr; |
| 81 | + char ifName[MAX_IF_NAME_SIZE]; |
| 82 | + if (findInterfaceMatchSubnet(ifName, &localAddr, handle->connectAddr, MAX_IF_NAME_SIZE, 1) <= 0) { |
| 83 | + WARN("NET/Socket : No usable listening interface found"); |
| 84 | + return ncclSystemError; |
| 85 | + } |
| 86 | + // pass the local address back |
| 87 | + memcpy(&handle->connectAddr, &localAddr, sizeof(handle->connectAddr)); |
| 88 | + } // Otherwise, handle stores a local address |
| 89 | + struct bootstrapNetComm* comm; |
| 90 | + NCCLCHECK(bootstrapNetNewComm(&comm)); |
| 91 | + NCCLCHECK(createListenSocket(&comm->fd, &handle->connectAddr)); |
| 92 | + *listenComm = comm; |
| 93 | + return ncclSuccess; |
| 94 | +} |
| 95 | + |
| 96 | +static ncclResult_t bootstrapNetConnect(int dev, void* opaqueHandle, void** sendComm) { |
| 97 | + struct bootstrapNetComm* comm; |
| 98 | + NCCLCHECK(bootstrapNetNewComm(&comm)); |
| 99 | + struct bootstrapNetHandle* handle = (struct bootstrapNetHandle*) opaqueHandle; |
| 100 | + NCCLCHECK(connectAddress(&comm->fd, &handle->connectAddr)); |
| 101 | + *sendComm = comm; |
| 102 | + return ncclSuccess; |
| 103 | +} |
| 104 | + |
| 105 | +static ncclResult_t bootstrapNetAccept(void* listenComm, void** recvComm) { |
| 106 | + struct bootstrapNetComm* lComm = (struct bootstrapNetComm*)listenComm; |
| 107 | + struct bootstrapNetComm* rComm; |
| 108 | + NCCLCHECK(bootstrapNetNewComm(&rComm)); |
| 109 | + struct sockaddr_in sockaddr; |
| 110 | + socklen_t socklen = sizeof(struct sockaddr_in); |
| 111 | + SYSCHECKVAL(accept(lComm->fd, (struct sockaddr*)&sockaddr, &socklen), "accept", rComm->fd); |
| 112 | + *recvComm = rComm; |
| 113 | + return ncclSuccess; |
| 114 | +} |
17 | 115 |
|
18 |
| -static ncclResult_t bootstrapNetListen(int dev, void* handle, void** listenComm) { NCCLCHECK(ncclBootstrapNet->listen(dev, handle, listenComm)); return ncclSuccess; } |
19 |
| -static ncclResult_t bootstrapNetConnect(int dev, void* handle, void** sendComm) { NCCLCHECK(ncclBootstrapNet->connect(dev, handle, sendComm)); return ncclSuccess; } |
20 |
| -static ncclResult_t bootstrapNetAccept(void* listenComm, void** recvComm) { NCCLCHECK(ncclBootstrapNet->accept(listenComm, recvComm)); return ncclSuccess; } |
21 |
| -static ncclResult_t bootstrapNetTest(void* request, int* done, int* size) { NCCLCHECK(ncclBootstrapNet->test(request, done, size)); return ncclSuccess; } |
22 |
| -static ncclResult_t bootstrapNetCloseSend(void* sendComm) { NCCLCHECK(ncclBootstrapNet->closeSend(sendComm)); return ncclSuccess; } |
23 |
| -static ncclResult_t bootstrapNetCloseRecv(void* recvComm) { NCCLCHECK(ncclBootstrapNet->closeRecv(recvComm)); return ncclSuccess; } |
24 |
| -static ncclResult_t bootstrapNetCloseListen(void* listenComm) { NCCLCHECK(ncclBootstrapNet->closeListen(listenComm)); return ncclSuccess; } |
| 116 | +static ncclResult_t bootstrapNetClose(void* opaqueComm) { |
| 117 | + struct bootstrapNetComm* comm = (struct bootstrapNetComm*)opaqueComm; |
| 118 | + if (comm) { |
| 119 | + close(comm->fd); |
| 120 | + free(comm); |
| 121 | + } |
| 122 | + return ncclSuccess; |
| 123 | +} |
25 | 124 |
|
26 |
| -// Additional sync functions based on async + test for bootstrap, using host ptrs. |
| 125 | +static ncclResult_t bootstrapNetCloseSend(void* sendComm) { NCCLCHECK(bootstrapNetClose(sendComm)); return ncclSuccess; } |
| 126 | +static ncclResult_t bootstrapNetCloseRecv(void* recvComm) { NCCLCHECK(bootstrapNetClose(recvComm)); return ncclSuccess; } |
| 127 | +static ncclResult_t bootstrapNetCloseListen(void* listenComm) { NCCLCHECK(bootstrapNetClose(listenComm)); return ncclSuccess; } |
| 128 | + |
| 129 | +// Additional sync functions |
27 | 130 | static ncclResult_t bootstrapNetSend(void* sendComm, void* data, int size) {
|
28 |
| - void* request, *mhandle; |
29 |
| - NCCLCHECK(ncclBootstrapNet->regMr(sendComm, data, size, NCCL_PTR_HOST, &mhandle)); |
30 |
| - NCCLCHECK(ncclBootstrapNet->isend(sendComm, data, size, mhandle, &request)); |
31 |
| - NCCLCHECK(ncclBootstrapNet->deregMr(sendComm, mhandle)); |
32 |
| - int done = 0; |
33 |
| - while (!done) NCCLCHECK(bootstrapNetTest(request, &done, NULL)); |
| 131 | + struct bootstrapNetComm* comm = (struct bootstrapNetComm*)sendComm; |
| 132 | + NCCLCHECK(socketSend(comm->fd, &size, sizeof(int))); |
| 133 | + NCCLCHECK(socketSend(comm->fd, data, size)); |
34 | 134 | return ncclSuccess;
|
35 | 135 | }
|
36 | 136 | static ncclResult_t bootstrapNetRecv(void* recvComm, void* data, int size) {
|
37 |
| - void* request, *mhandle; |
38 |
| - NCCLCHECK(ncclBootstrapNet->regMr(recvComm, data, size, NCCL_PTR_HOST, &mhandle)); |
39 |
| - NCCLCHECK(ncclBootstrapNet->irecv(recvComm, data, size, mhandle, &request)); |
40 |
| - NCCLCHECK(ncclBootstrapNet->deregMr(recvComm, mhandle)); |
41 |
| - int done = 0; |
42 |
| - while (!done) NCCLCHECK(bootstrapNetTest(request, &done, NULL)); |
| 137 | + struct bootstrapNetComm* comm = (struct bootstrapNetComm*)recvComm; |
| 138 | + int recvSize; |
| 139 | + NCCLCHECK(socketReceive(comm->fd, &recvSize, sizeof(int))); |
| 140 | + if (recvSize > size) { |
| 141 | + WARN("Message truncated : received %d bytes instead of %d\n", recvSize, size); |
| 142 | + return ncclInternalError; |
| 143 | + } |
| 144 | + NCCLCHECK(socketReceive(comm->fd, data, std::min(recvSize, size))); |
| 145 | + return ncclSuccess; |
| 146 | +} |
| 147 | + |
| 148 | +ncclResult_t bootstrapNetCreateHandle(void* opaqueHandle, const char* str) { |
| 149 | + struct bootstrapNetHandle* handle = (struct bootstrapNetHandle*) opaqueHandle; |
| 150 | + NCCLCHECK(GetSocketAddrFromString(&handle->connectAddr, str)); |
43 | 151 | return ncclSuccess;
|
44 | 152 | }
|
45 | 153 |
|
@@ -148,7 +256,7 @@ ncclResult_t bootstrapGetUniqueId(ncclUniqueId* out) {
|
148 | 256 |
|
149 | 257 | char* env = getenv("NCCL_COMM_ID");
|
150 | 258 | if (env) {
|
151 |
| - if (ncclSocketCreateHandle(&id->extHandleRoot, env) != 0) { |
| 259 | + if (bootstrapNetCreateHandle(&id->extHandleRoot, env) != 0) { |
152 | 260 | WARN("Invalid NCCL_COMM_ID, please use format: <ipv4>:<port> or [<ipv6>]:<port> or <hostname>:<port>");
|
153 | 261 | return ncclInvalidArgument;
|
154 | 262 | }
|
|
0 commit comments