|
| 1 | +/* |
| 2 | + * Copyright (C) Mellanox Technologies Ltd. 2020. ALL RIGHTS RESERVED. |
| 3 | + * See file LICENSE for terms. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <pthread.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#include "server_xccl.h" |
| 12 | +#include "host_channel.h" |
| 13 | + |
| 14 | +#define MAX_THREADS 128 |
| 15 | +typedef struct { |
| 16 | + pthread_t id; |
| 17 | + int idx, nthreads; |
| 18 | + dpu_xccl_comm_t comm; |
| 19 | + dpu_hc_t *hc; |
| 20 | + unsigned int itt; |
| 21 | +} thread_ctx_t; |
| 22 | + |
| 23 | +/* thread accisble data - split reader/writer */ |
| 24 | +typedef struct { |
| 25 | + volatile unsigned long g_itt; /* first cache line */ |
| 26 | + volatile unsigned long pad[3]; /* pad to 64bytes */ |
| 27 | + volatile unsigned long l_itt; /* second cache line */ |
| 28 | + volatile unsigned long pad2[3]; /* pad to 64 bytes */ |
| 29 | +} thread_sync_t; |
| 30 | + |
| 31 | +static thread_sync_t *thread_sync = NULL; |
| 32 | + |
| 33 | +void *dpu_worker(void *arg) |
| 34 | +{ |
| 35 | + int i = 0; |
| 36 | + thread_ctx_t *ctx = (thread_ctx_t*)arg; |
| 37 | + xccl_coll_req_h request; |
| 38 | + |
| 39 | + while(1) { |
| 40 | + ctx->itt++; |
| 41 | + if (ctx->idx > 0) { |
| 42 | + while (thread_sync[ctx->idx].g_itt < ctx->itt) { |
| 43 | + /* busy wait */ |
| 44 | + } |
| 45 | + } |
| 46 | + else { |
| 47 | + dpu_hc_wait(ctx->hc, ctx->itt); |
| 48 | + for (i = 0; i < ctx->nthreads; i++) { |
| 49 | + thread_sync[i].g_itt++; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + int offset, block; |
| 54 | + int count = dpu_hc_get_count(ctx->hc); |
| 55 | + int ready = 0; |
| 56 | + |
| 57 | + block = count / ctx->nthreads; |
| 58 | + offset = block * ctx->idx; |
| 59 | + if(ctx->idx < (count % ctx->nthreads)) { |
| 60 | + offset += ctx->idx; |
| 61 | + block++; |
| 62 | + } else { |
| 63 | + offset += (count % ctx->nthreads); |
| 64 | + } |
| 65 | + |
| 66 | + xccl_coll_op_args_t coll = { |
| 67 | + .field_mask = 0, |
| 68 | + .coll_type = XCCL_ALLREDUCE, |
| 69 | + .buffer_info = { |
| 70 | + .src_buffer = ctx->hc->mem_segs.put.base + offset * sizeof(int), |
| 71 | + .dst_buffer = ctx->hc->mem_segs.get.base + offset * sizeof(int), |
| 72 | + .len = block * xccl_dt_size(dpu_hc_get_dtype(ctx->hc)), |
| 73 | + }, |
| 74 | + .reduce_info = { |
| 75 | + .dt = dpu_hc_get_dtype(ctx->hc), |
| 76 | + .op = dpu_hc_get_op(ctx->hc), |
| 77 | + .count = block, |
| 78 | + }, |
| 79 | + .alg.set_by_user = 0, |
| 80 | + .tag = 123, //todo |
| 81 | + }; |
| 82 | + |
| 83 | + if (coll.reduce_info.op == XCCL_OP_UNSUPPORTED) { |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + XCCL_CHECK(xccl_collective_init(&coll, &request, ctx->comm.team)); |
| 88 | + XCCL_CHECK(xccl_collective_post(request)); |
| 89 | + while (XCCL_OK != xccl_collective_test(request)) { |
| 90 | + xccl_context_progress(ctx->comm.ctx); |
| 91 | + } |
| 92 | + XCCL_CHECK(xccl_collective_finalize(request)); |
| 93 | + |
| 94 | + thread_sync[ctx->idx].l_itt++; |
| 95 | + |
| 96 | + if (ctx->idx == 0) { |
| 97 | + while (ready != ctx->nthreads) { |
| 98 | + ready = 0; |
| 99 | + for (i = 0; i < ctx->nthreads; i++) { |
| 100 | + if (thread_sync[i].l_itt == ctx->itt) { |
| 101 | + ready++; |
| 102 | + } |
| 103 | + else { |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + dpu_hc_reply(ctx->hc, ctx->itt); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return NULL; |
| 114 | +} |
| 115 | + |
| 116 | +int main(int argc, char **argv) |
| 117 | +{ |
| 118 | + int nthreads = 0, i; |
| 119 | + thread_ctx_t *tctx_pool = NULL; |
| 120 | + dpu_xccl_global_t xccl_glob; |
| 121 | + dpu_hc_t hc_b, *hc = &hc_b; |
| 122 | + |
| 123 | + if (argc < 2 ) { |
| 124 | + printf("Need thread # as an argument\n"); |
| 125 | + return 1; |
| 126 | + } |
| 127 | + nthreads = atoi(argv[1]); |
| 128 | + if (MAX_THREADS < nthreads || 0 >= nthreads) { |
| 129 | + printf("ERROR: bad thread #: %d\n", nthreads); |
| 130 | + return 1; |
| 131 | + } |
| 132 | + printf("DPU daemon: Running with %d threads\n", nthreads); |
| 133 | + tctx_pool = calloc(nthreads, sizeof(*tctx_pool)); |
| 134 | + XCCL_CHECK(dpu_xccl_init(argc, argv, &xccl_glob)); |
| 135 | + |
| 136 | +// thread_sync = calloc(nthreads, sizeof(*thread_sync)); |
| 137 | + thread_sync = aligned_alloc(64, nthreads * sizeof(*thread_sync)); |
| 138 | + memset(thread_sync, 0, nthreads * sizeof(*thread_sync)); |
| 139 | + |
| 140 | + dpu_hc_init(hc); |
| 141 | + dpu_hc_accept(hc); |
| 142 | + |
| 143 | + for(i = 0; i < nthreads; i++) { |
| 144 | +// printf("Thread %d spawned!\n", i); |
| 145 | + XCCL_CHECK(dpu_xccl_alloc_team(&xccl_glob, &tctx_pool[i].comm)); |
| 146 | + |
| 147 | + tctx_pool[i].idx = i; |
| 148 | + tctx_pool[i].nthreads = nthreads; |
| 149 | + tctx_pool[i].hc = hc; |
| 150 | + tctx_pool[i].itt = 0; |
| 151 | + |
| 152 | + if (i < nthreads - 1) { |
| 153 | + pthread_create(&tctx_pool[i].id, NULL, dpu_worker, |
| 154 | + (void*)&tctx_pool[i]); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /* The final DPU worker is executed in this context */ |
| 159 | + dpu_worker((void*)&tctx_pool[i-1]); |
| 160 | + |
| 161 | + for(i = 0; i < nthreads; i++) { |
| 162 | + if (i < nthreads - 1) { |
| 163 | + pthread_join(tctx_pool[i].id, NULL); |
| 164 | + } |
| 165 | + dpu_xccl_free_team(&xccl_glob, &tctx_pool[i].comm); |
| 166 | +// printf("Thread %d joined!\n", i); |
| 167 | + } |
| 168 | + |
| 169 | + dpu_xccl_finalize(&xccl_glob); |
| 170 | + return 0; |
| 171 | +} |
0 commit comments