Welcome to mirror list, hosted at ThFree Co, Russian Federation.

proxy.cc « src - github.com/marian-nmt/nccl.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3824f2346130c00c9fd9181f1bc07ab1af7bc33 (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*************************************************************************
 * Copyright (c) 2016-2020, NVIDIA CORPORATION. All rights reserved.
 *
 * See LICENSE.txt for license information
 ************************************************************************/

#include "comm.h"
#include "info.h"
#include "graph.h"
#include "collectives.h"

enum { proxyRecv=0, proxySend=1 };

static bool NeedProxy(int type, int pattern, int root, struct ncclRing* ring, int nranks) {
  if (pattern == ncclPatternRing || pattern == ncclPatternRingTwice) return true;

  /* In chains, one rank does not need a proxy. Let's figure out which one it is */
  // Which index in the reorganized rings should we compare root against */
  const int myrank = 0, nextrank = 1, prevrank = nranks-1;
  int index = pattern == ncclPatternPipelineFrom ?
      /*                            no recv /  no send    if root = */
      /* bcast  */ (type == proxyRecv ?   myrank : nextrank ):
      /* reduce */ (type == proxyRecv ? prevrank :   myrank );
  int rank = ring->userRanks[index];
  return (root != rank);
}

#define PROXYARGS_ALLOCATE_SIZE 128
struct ncclProxyPool {
  struct ncclProxyPool *next;
  struct ncclProxyArgs elems[PROXYARGS_ALLOCATE_SIZE];
};

static ncclResult_t allocateArgs(struct ncclComm* comm, struct ncclProxyArgs** argsptr) {
  struct ncclProxyState* state = &comm->proxyState;
  struct ncclProxyArgs* elem;
  pthread_mutex_lock(&state->poolMutex);
  if (state->pool == NULL) {
    // Allocate a new pool of elements
    struct ncclProxyPool* newPool;
    NCCLCHECK(ncclCalloc(&newPool, 1));
    struct ncclProxyArgs* newElems = newPool->elems;
    // Chain newly allocated elements
    for (int i=0; i<PROXYARGS_ALLOCATE_SIZE; i++) {
      if (i+1 < PROXYARGS_ALLOCATE_SIZE) newElems[i].next = newElems+i+1;
    }
    // Add them all to the pool list
    state->pool = newElems;
    // Save the pool memory block for later resource release
    newPool->next = state->pools;
    state->pools = newPool;
  }
  elem = state->pool;
  state->pool = state->pool->next;
  pthread_mutex_unlock(&state->poolMutex);
  elem->next = elem->nextPeer = elem->nextGroup = NULL;
  *argsptr = elem;
  return ncclSuccess;
}

//#define DEBUG_PROXY 1
#ifdef DEBUG_PROXY
#define DEBUG_PROXY_PRINT printf
#else
#define DEBUG_PROXY_PRINT(...)
#endif

#define OP_INDEX(op) ((op) ? (op)-state->pools->elems : -1)
#define OP_SEEN 0x100000
ncclResult_t dumpProxyState(struct ncclProxyState* state) {
#ifdef DEBUG_PROXY
  struct ncclProxyArgs* op = state->ops;
  while (op) {
    if (op->idle & OP_SEEN) {
      WARN("Active list loop at element %ld\n", OP_INDEX(op));
    }
    op->idle |= OP_SEEN;
    printf("[%ld]", OP_INDEX(op));
    if (op->nextPeer) {
      printf("(%ld)", OP_INDEX(op->nextPeer));
      struct ncclProxyArgs* n = op->nextPeer;
      n->idle |= OP_SEEN;
      while (n->nextGroup || n->nextPeer) {
        n = n->nextGroup ? n->nextGroup : n->nextPeer;
        n->idle |= OP_SEEN;
      }
    }
    if (op->nextGroup)  {
      printf("--G->");
      op = op->nextGroup;
    } else {
      printf("--N->");
      op = op->next;
    }
  }
  printf("[X]\n");

  struct ncclProxyArgs* free = state->pool;
  while (free) {
    if (free->idle & OP_SEEN) {
      WARN("Free list loop at element %ld\n", OP_INDEX(free));
    }
    free->idle |= OP_SEEN;
    free = free->next;
  }

  struct ncclProxyPool* p = state->pools;
  int i = 0;
  while (p) {
    for (int e=0; e<PROXYARGS_ALLOCATE_SIZE; e++) {
      if ((p->elems[e].idle & OP_SEEN) == 0) {
        WARN("Element %d of pool %d has been lost\n", e, i);
        struct ncclProxyArgs* free = state->pool;
        printf("Free list ");
        while (free) {
          printf("--> %ld ", OP_INDEX(free));
          free = free->next;
        }
        printf("\n");
        return ncclInternalError;
      }
      p->elems[e].idle -= OP_SEEN;
    }
    p = p->next;
    i++;
  }
#endif
  return ncclSuccess;
}

static ncclResult_t ProxyAppend(struct ncclProxyState* state, struct ncclProxyArgs* args, int shared) {
  struct ncclProxyArgs* proxyAppend = *args->proxyAppendPtr;
  if (proxyAppend) {
    if (shared && proxyAppend->opCount == args->opCount) {
      args->next = proxyAppend->next;
      proxyAppend->next = NULL;
      proxyAppend->nextGroup = args;
      DEBUG_PROXY_PRINT("Insert  %5ld (%d/%5ld/%5ld) as group, prevGroup %5ld, next %5ld : \n", OP_INDEX(args), shared, proxyAppend->opCount, args->opCount, OP_INDEX(proxyAppend), OP_INDEX(args->next));
    } else {
      proxyAppend->nextPeer = args;
      DEBUG_PROXY_PRINT("Insert  %5ld (%d/%5ld/%5ld) as nextPeer of %5ld                  : \n", OP_INDEX(args), shared, proxyAppend->opCount, args->opCount, OP_INDEX(proxyAppend));
    }
  } else {
    // Nothing running for that peer. Add to the list
    if (state->ops == NULL) {
      // Create the list
      DEBUG_PROXY_PRINT("Insert  %5ld (%d/%5ld) as first element                            : \n", OP_INDEX(args), shared, args->opCount);
      state->ops = args;
    } else {
      // Append element at the end of the list
      struct ncclProxyArgs* last = state->ops;
      while (last->nextGroup || last->next) last = last->nextGroup ? last->nextGroup : last->next;
      last->next = args;
      DEBUG_PROXY_PRINT("Insert  %5ld (%d/%5ld) as last element                             : \n", OP_INDEX(args),shared, args->opCount);
    }
  }
  *(args->proxyAppendPtr) = args;
  return ncclSuccess;
}

static ncclResult_t SaveProxy(int type, int peer, struct ncclProxyArgs* args) {
  if (peer < 0) return ncclSuccess;

  struct ncclPeer* peerComm = args->channel->peers+peer;
  struct ncclConnector* connector = type == proxyRecv ? &peerComm->recv : &peerComm->send;
  if (connector->transportComm == NULL) {
    WARN("[%d] Error no transport for %s peer %d on channel %d\n", connector->comm->rank,
        type == proxyRecv ? "recv" : "send", peer, args->channel->id);
    return ncclInternalError;
  }
  if (connector->transportComm->proxy == NULL) return ncclSuccess;

  struct ncclProxyState* state = &connector->comm->proxyState;
  struct ncclProxyArgs* op;
  NCCLCHECK(allocateArgs(connector->comm, &op));
  memcpy(op, args, sizeof(struct ncclProxyArgs));
  op->connector = connector;
  op->progress = connector->transportComm->proxy;
  op->state = ncclProxyOpReady;

  op->proxyAppendPtr =
    connector->conn.shared ?
    state->sharedBuffs->proxyAppend+2*args->channel->id+type : // Shared buffers
    &connector->proxyAppend;  // Dedicated buffers

  if (state->nextOps == NULL) state->nextOps = op;
  else state->nextOpsEnd->next = op;
  state->nextOpsEnd = op;
  return ncclSuccess;
}

ncclResult_t ncclProxySaveColl(struct ncclProxyArgs* args, int pattern, int root, int nranks) {
  if (pattern == ncclPatternRing || pattern == ncclPatternRingTwice || pattern == ncclPatternPipelineFrom || pattern == ncclPatternPipelineTo) {
    struct ncclRing* ring = &args->channel->ring;
    if (NeedProxy(proxyRecv, pattern, root, ring, nranks)) NCCLCHECK(SaveProxy(proxyRecv, ring->prev, args));
    if (NeedProxy(proxySend, pattern, root, ring, nranks)) NCCLCHECK(SaveProxy(proxySend, ring->next, args));
  }
  if (pattern == ncclPatternTreeUp || pattern == ncclPatternTreeUpDown) {
    // Tree up
    struct ncclTree* tree = &args->channel->tree;
    for (int i=0; i<NCCL_MAX_TREE_ARITY; i++) NCCLCHECK(SaveProxy(proxyRecv, tree->down[i], args));
    NCCLCHECK(SaveProxy(proxySend, tree->up, args));
  }
  if (pattern == ncclPatternTreeDown || pattern == ncclPatternTreeUpDown) {
    // Tree down
    struct ncclTree* tree = &args->channel->tree;
    for (int i=0; i< NCCL_MAX_TREE_ARITY; i++) NCCLCHECK(SaveProxy(proxySend, tree->down[i], args));
    NCCLCHECK(SaveProxy(proxyRecv, tree->up, args));
  }
  if (pattern == ncclPatternCollTreeUp) {
    // CollTree up
    struct ncclTree* tree = &args->channel->collTree;
    NCCLCHECK(SaveProxy(proxyRecv, tree->down[0], args));
    NCCLCHECK(SaveProxy(proxySend, tree->up, args));
  }
  if (pattern == ncclPatternCollTreeDown) {
    // CollTree down
    struct ncclTree* tree = &args->channel->collTree;
    NCCLCHECK(SaveProxy(proxySend, tree->down[0], args));
    NCCLCHECK(SaveProxy(proxyRecv, tree->up, args));
  }
  return ncclSuccess;
}

ncclResult_t ncclProxySaveP2p(struct ncclInfo* info, struct ncclChannel* channel, int segment) {
  struct ncclProxyArgs args;
  memset(&args, 0, sizeof(struct ncclProxyArgs));
  args.channel = channel;
  args.sliceSteps = 1;
  args.chunkSteps = 1;
  args.protocol = NCCL_PROTO_SIMPLE;
  args.segment = segment;
  args.opCount = channel->workFifoTail-1;
  args.dtype = info->datatype;
  if (info->delta > 0 && info->recvbytes >= 0) {
    int peerrecv = (info->comm->nRanks+info->comm->rank-info->delta)%info->comm->nRanks;
    args.nsteps = DIVUP(info->recvbytes, info->comm->buffSizes[NCCL_PROTO_SIMPLE]/NCCL_STEPS/SENDRECV_SLICEFACTOR);
    if (args.nsteps == 0) args.nsteps = 1;
    args.recvbytes = info->recvbytes;
    args.sendbytes = 0;
    NCCLCHECK(SaveProxy(proxyRecv, peerrecv, &args));
  }
  if (info->delta > 0 && info->sendbytes >= 0) {
    int peersend = (info->comm->rank+info->delta)%info->comm->nRanks;
    args.nsteps = DIVUP(info->sendbytes, info->comm->buffSizes[NCCL_PROTO_SIMPLE]/NCCL_STEPS/SENDRECV_SLICEFACTOR);
    if (args.nsteps == 0) args.nsteps = 1;
    args.sendbytes = info->sendbytes;
    args.recvbytes = 0;
    NCCLCHECK(SaveProxy(proxySend, peersend, &args));
  }
  return ncclSuccess;
}

static ncclResult_t removeOp(struct ncclProxyState* state, struct ncclProxyArgs** opPtr, struct ncclProxyArgs** prevOpPtr, struct ncclProxyArgs** prevGroupPtr) {
  struct ncclProxyArgs* freeOp = *opPtr;
  DEBUG_PROXY_PRINT("Remove %ld/%ld -> %ld -> %ld/%ld\n", OP_INDEX(*prevOpPtr), OP_INDEX(*prevGroupPtr), OP_INDEX(freeOp), OP_INDEX(freeOp->next), OP_INDEX(freeOp->nextGroup));
  if (*prevGroupPtr && *prevOpPtr) return ncclInternalError;
  if (freeOp->nextGroup) {
    // Part of a group : remove the element
    struct ncclProxyArgs* next = freeOp->nextGroup;
    *opPtr = next;
    if (*prevGroupPtr) {
      (*prevGroupPtr)->nextGroup = next;
    } else if (*prevOpPtr) {
      (*prevOpPtr)->next = next;
    } else {
      state->ops = next;
    }
  } else {
    struct ncclProxyArgs* next = freeOp->next;
    *opPtr = next;
    if ((*prevGroupPtr)) {
      (*prevGroupPtr)->next = next;
      (*prevGroupPtr)->nextGroup = NULL;
      (*prevGroupPtr)->nextPeer = freeOp->nextPeer;
      if (*(freeOp->proxyAppendPtr) == freeOp) *(freeOp->proxyAppendPtr) = *prevGroupPtr;
      (*prevOpPtr) = *prevGroupPtr;
      (*prevGroupPtr) = NULL;
    } else {
      if (freeOp->nextPeer) {
        // replace op by nextPeer
        struct ncclProxyArgs* nextPeer = freeOp->nextPeer;
        if (*prevOpPtr) {
          (*prevOpPtr)->next = nextPeer;
        } else {
          state->ops = nextPeer;
        }
        struct ncclProxyArgs* lastGroup = nextPeer;
        while (lastGroup->nextGroup) lastGroup = lastGroup->nextGroup;
        lastGroup->next = next;
        *(prevOpPtr) = lastGroup;
      } else {
        *(freeOp->proxyAppendPtr) = NULL;
        if (*prevOpPtr) {
          (*prevOpPtr)->next = next;
        } else {
          state->ops = next;
        }
      }
    }
  }
  pthread_mutex_lock(&state->poolMutex);
  freeOp->next = state->pool;
  state->pool = freeOp;
  pthread_mutex_unlock(&state->poolMutex);
  DEBUG_PROXY_PRINT("Removed %5ld (%5ld)                                               : ", OP_INDEX(freeOp), OP_INDEX(*freeOp->proxyAppendPtr));
  NCCLCHECK(dumpProxyState(state));
  return ncclSuccess;
}

static ncclResult_t progressOps(struct ncclProxyState* state, struct ncclProxyArgs** opsPtr, int* idle, struct ncclComm* comm) {
  struct ncclProxyArgs* prevOp = NULL;
  struct ncclProxyArgs* prevGroup = NULL;
  struct ncclProxyArgs* op = *opsPtr;
  while (op) {
    if (op->state == ncclProxyOpNone) return ncclInternalError;
    // opCount >= lastOpCount are part of an ongoing GroupStart/GroupEnd that hasn't started
    // yet and might be cancelled before they even start. Hold on on those.
    if (op->opCount < comm->lastOpCount) {
      NCCLCHECK(op->progress(op));
      *idle &= op->idle;
    }
    if (op->state == ncclProxyOpNone) {
      NCCLCHECK(removeOp(state, &op, &prevOp, &prevGroup));
    } else {
      if (op->nextGroup) {
        prevGroup = op;
        prevOp = NULL;
        op = op->nextGroup;
      } else {
        prevOp = op;
        prevGroup = NULL;
        op = op->next;
      }
    }
  }
  return ncclSuccess;
}

void* persistentThread(void *comm_) {
  struct ncclComm* comm = (struct ncclComm*)comm_;
  struct ncclProxyState* state = &comm->proxyState;
  char threadName[16];
  sprintf(threadName, "NCCLproxy %5d", comm->rank);
  nvtxNameOsThreadA(syscall(SYS_gettid), threadName);

  pthread_mutex_lock(&state->opsMutex);
  struct ncclProxyArgs** opsPtr = &state->ops;
  while (1) {
    if (*comm->abortFlag) {
      pthread_mutex_unlock(&state->opsMutex);
      return NULL;
    }

    while (*opsPtr == NULL) {
      if (state->stop) {
        // No more commands to process and proxy has been requested to stop
        pthread_mutex_unlock(&state->opsMutex);
        return NULL;
      }
      pthread_cond_wait(&state->cond, &state->opsMutex);
    }
    int idle = 1;
    ncclResult_t ret = progressOps(state, opsPtr, &idle, comm);
    if (ret != ncclSuccess) {
      comm->fatalError = ret;
      INFO(NCCL_ALL,"%s:%d -> %d [Proxy Thread]", __FILE__, __LINE__, ret);
      pthread_mutex_unlock(&state->opsMutex);
      return NULL;
    }
    if (idle) {
      pthread_mutex_unlock(&state->opsMutex);
      sched_yield(); // No request progressed. Let others run.
      pthread_mutex_lock(&state->opsMutex);
    }
  }
}

ncclResult_t ncclProxyStart(struct ncclComm* comm) {
  struct ncclProxyState* state = &comm->proxyState;
  pthread_mutex_lock(&state->opsMutex);

  // Sort operations as we append them : collectives and
  // receives first, then sends.
  ncclProxyArgs* next, *prev = NULL, *op = state->nextOps;
  while (op) {
    next = op->next;
    if (op->sendbytes) {
      if (prev) prev->next = next;
      else state->nextOps = next;
      op->next = NULL;
      NCCLCHECK(ProxyAppend(state, op, op->connector->conn.shared));
    } else prev = op;
    op = next;
  }
  op = state->nextOps;
  while (op) {
    next = op->next;
    op->next = NULL;
    NCCLCHECK(ProxyAppend(state, op, op->connector->conn.shared));
    op = next;
  }
  state->nextOps = state->nextOpsEnd = NULL;
  NCCLCHECK(dumpProxyState(state));

  if (state->ops != NULL)
    pthread_cond_signal(&state->cond);
  pthread_mutex_unlock(&state->opsMutex);
  return ncclSuccess;
}

NCCL_PARAM(ProxySharedBuffersCount, "SHARED_BUFF_COUNT", -2);

ncclResult_t ncclProxySharedBuffersInit(struct ncclComm* comm, int cuda, int* size, char** ptr) {
  struct ncclProxySharedBuffers* state = comm->proxyState.sharedBuffs;
  if (state == NULL) {
    NCCLCHECK(ncclCalloc(&state, 1));
    comm->proxyState.sharedBuffs = state;
    state->nslots = ncclParamProxySharedBuffersCount();
    if (state->nslots == -2)  {
      state->nslots = NCCL_STEPS*NCCL_MAX_WORK_ELEMENTS;
    }
    state->slotSize = comm->buffSizes[NCCL_PROTO_SIMPLE]/(NCCL_STEPS*SENDRECV_SLICEFACTOR);
  }

  char* buff;
  int* used;
  *size = 2*comm->p2pnChannels*state->slotSize*state->nslots;

  if (cuda && state->cudaBuff[0] == NULL) {
    NCCLCHECK(ncclCudaCalloc(&buff, *size));
    NCCLCHECK(ncclCalloc(&used, 2*comm->p2pnChannels*state->nslots));
    for (int i=0; i<2*comm->p2pnChannels; i++) {
      state->cudaBuff[i] = buff + state->nslots*state->slotSize*i;
      state->cudaUsed[i] = used + state->nslots*i;
    }
  } else if (state->hostBuff[0] == NULL) {
    NCCLCHECK(ncclCudaHostCalloc(&buff, *size));
    NCCLCHECK(ncclCalloc(&used, 2*comm->p2pnChannels*state->nslots));
    for (int i=0; i<2*comm->p2pnChannels; i++) {
      state->hostBuff[i] = buff + state->nslots*state->slotSize*i;
      state->hostUsed[i] = used + state->nslots*i;
    }
  }
  buff = cuda ? state->cudaBuff[0] : state->hostBuff[0];

  *ptr = buff;
  return ncclSuccess;
}

ncclResult_t ncclProxySharedBuffersAlloc(struct ncclComm* comm, int cuda, int type, int channel, int size, char** ptr) {
  struct ncclProxySharedBuffers* state = comm->proxyState.sharedBuffs;
  // Use different pools for different channels and also separate send/recv.
  int p = 2*channel+type;
  int* used = cuda ? state->cudaUsed[p] : state->hostUsed[p];
  char* buff = cuda ? state->cudaBuff[p] : state->hostBuff[p];
  if (buff == NULL) return ncclInternalError;
  int nslots = 1;
  while (nslots*state->slotSize < size) nslots *= 2;
  for (int s=0; s<state->nslots; s+=nslots) {
    int u = 0;
    for (int i=0; i<nslots; i++) u += used[s+i];
    if (u == 0) {
      for (int i=0; i<nslots; i++) used[s+i] = 1;
      *ptr = buff+state->slotSize*s;
      return ncclSuccess;
    }
  }
  *ptr = NULL;
  return ncclSuccess;
}

ncclResult_t ncclProxySharedBuffersFree(struct ncclComm* comm, int cuda, int type, int channel, int size, char* ptr) {
  struct ncclProxySharedBuffers* state = comm->proxyState.sharedBuffs;
  int p = 2*channel+type;
  int* used = cuda ? state->cudaUsed[p] : state->hostUsed[p];
  char* buff = cuda ? state->cudaBuff[p] : state->hostBuff[p];
  if (buff == NULL) return ncclInternalError;
  int nslots = 1;
  while (nslots*state->slotSize < size) nslots *= 2;
  int s = (ptr-buff)/state->slotSize;
  if (s < 0 || s+nslots > state->nslots) {
    WARN("Error freeing shared buffer : freeing ptr %p size %d (start %p slot size %d nslots %d)\n", ptr, size, buff, state->slotSize, state->nslots);
    return ncclInternalError;
  }
  for (int i=0; i<nslots; i++) used[s+i] = 0;
  return ncclSuccess;
}

ncclResult_t ncclProxySharedBuffersDestroy(struct ncclComm* comm) {
  struct ncclProxySharedBuffers* state = comm->proxyState.sharedBuffs;
  if (state) {
    CUDACHECK(cudaFree(state->cudaBuff[0]));
    free(state->cudaUsed[0]);
    NCCLCHECK(ncclCudaHostFree(state->hostBuff[0]));
    free(state->hostUsed[0]);
    free(state);
  }
  return ncclSuccess;
}

ncclResult_t ncclProxyCreate(struct ncclComm* comm) {
  if (!comm->proxyThread) {
    comm->proxyState.cond = PTHREAD_COND_INITIALIZER;
    comm->proxyState.opsMutex = PTHREAD_MUTEX_INITIALIZER;
    comm->proxyState.poolMutex = PTHREAD_MUTEX_INITIALIZER;
    comm->proxyState.ops = NULL;
    pthread_create(&comm->proxyThread, NULL, persistentThread, comm);
  }
  return ncclSuccess;
}

ncclResult_t ncclProxyDestroy(struct ncclComm* comm) {
  struct ncclProxyState* state = &comm->proxyState;

  // Request the proxy to stop and then wake it
  pthread_mutex_lock(&state->opsMutex);
  state->stop = true;
  pthread_cond_signal(&state->cond);
  pthread_mutex_unlock(&state->opsMutex);
  if (comm->proxyThread) pthread_join(comm->proxyThread, NULL);

  // Free off any memory allocated for the proxy arg pools
  pthread_mutex_lock(&state->poolMutex);
  struct ncclProxyState* proxyState = &comm->proxyState;
  while (proxyState->pools != NULL) {
    struct ncclProxyPool *next = proxyState->pools->next;
    free(proxyState->pools);
    proxyState->pools = next;
  }
  pthread_mutex_unlock(&state->poolMutex);

  NCCLCHECK(ncclProxySharedBuffersDestroy(comm));

  return ncclSuccess;
}