|
| 1 | +#include "ed25519.h" |
| 2 | +#include "gpu_ctx.h" |
| 3 | +#include <pthread.h> |
| 4 | +#include "gpu_common.h" |
| 5 | + |
| 6 | +static pthread_mutex_t g_ctx_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 7 | + |
| 8 | +#define MAX_NUM_GPUS 8 |
| 9 | +#define MAX_QUEUE_SIZE 8 |
| 10 | + |
| 11 | +static gpu_ctx_t g_gpu_ctx[MAX_NUM_GPUS][MAX_QUEUE_SIZE] = {0}; |
| 12 | +static uint32_t g_cur_gpu = 0; |
| 13 | +static uint32_t g_cur_queue[MAX_NUM_GPUS] = {0}; |
| 14 | +static int32_t g_total_gpus = -1; |
| 15 | + |
| 16 | +static bool cuda_crypt_init_locked() { |
| 17 | + if (g_total_gpus == -1) { |
| 18 | + cudaGetDeviceCount(&g_total_gpus); |
| 19 | + g_total_gpus = min(MAX_NUM_GPUS, g_total_gpus); |
| 20 | + LOG("total_gpus: %d\n", g_total_gpus); |
| 21 | + for (int gpu = 0; gpu < g_total_gpus; gpu++) { |
| 22 | + CUDA_CHK(cudaSetDevice(gpu)); |
| 23 | + for (int queue = 0; queue < MAX_QUEUE_SIZE; queue++) { |
| 24 | + int err = pthread_mutex_init(&g_gpu_ctx[gpu][queue].mutex, NULL); |
| 25 | + if (err != 0) { |
| 26 | + fprintf(stderr, "pthread_mutex_init error %d gpu: %d queue: %d\n", |
| 27 | + err, gpu, queue); |
| 28 | + g_total_gpus = 0; |
| 29 | + return false; |
| 30 | + } |
| 31 | + CUDA_CHK(cudaStreamCreate(&g_gpu_ctx[gpu][queue].stream)); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return g_total_gpus > 0; |
| 36 | +} |
| 37 | + |
| 38 | +bool ed25519_init() { |
| 39 | + cudaFree(0); |
| 40 | + pthread_mutex_lock(&g_ctx_mutex); |
| 41 | + bool success = cuda_crypt_init_locked(); |
| 42 | + pthread_mutex_unlock(&g_ctx_mutex); |
| 43 | + return success; |
| 44 | +} |
| 45 | + |
| 46 | +gpu_ctx_t* get_gpu_ctx() { |
| 47 | + int32_t cur_gpu, cur_queue; |
| 48 | + |
| 49 | + LOG("locking global mutex"); |
| 50 | + pthread_mutex_lock(&g_ctx_mutex); |
| 51 | + if (!cuda_crypt_init_locked()) { |
| 52 | + pthread_mutex_unlock(&g_ctx_mutex); |
| 53 | + LOG("No GPUs, exiting...\n"); |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + cur_gpu = g_cur_gpu; |
| 57 | + g_cur_gpu++; |
| 58 | + g_cur_gpu %= g_total_gpus; |
| 59 | + cur_queue = g_cur_queue[cur_gpu]; |
| 60 | + g_cur_queue[cur_gpu]++; |
| 61 | + g_cur_queue[cur_gpu] %= MAX_QUEUE_SIZE; |
| 62 | + pthread_mutex_unlock(&g_ctx_mutex); |
| 63 | + |
| 64 | + gpu_ctx_t* cur_ctx = &g_gpu_ctx[cur_gpu][cur_queue]; |
| 65 | + LOG("locking contex mutex queue: %d gpu: %d", cur_queue, cur_gpu); |
| 66 | + pthread_mutex_lock(&cur_ctx->mutex); |
| 67 | + |
| 68 | + CUDA_CHK(cudaSetDevice(cur_gpu)); |
| 69 | + |
| 70 | + LOG("selecting gpu: %d queue: %d\n", cur_gpu, cur_queue); |
| 71 | + |
| 72 | + return cur_ctx; |
| 73 | +} |
| 74 | + |
| 75 | +void setup_gpu_ctx(verify_ctx_t* cur_ctx, |
| 76 | + const gpu_Elems* elems, |
| 77 | + uint32_t num_elems, |
| 78 | + uint32_t message_size, |
| 79 | + uint32_t total_packets, |
| 80 | + uint32_t total_packets_size, |
| 81 | + uint32_t total_signatures, |
| 82 | + const uint32_t* message_lens, |
| 83 | + const uint32_t* public_key_offsets, |
| 84 | + const uint32_t* signature_offsets, |
| 85 | + const uint32_t* message_start_offsets, |
| 86 | + size_t out_size, |
| 87 | + cudaStream_t stream |
| 88 | + ) { |
| 89 | + size_t offsets_size = total_signatures * sizeof(uint32_t); |
| 90 | + |
| 91 | + LOG("device allocate. packets: %d out: %d offsets_size: %zu\n", |
| 92 | + total_packets_size, (int)out_size, offsets_size); |
| 93 | + |
| 94 | + if (cur_ctx->packets == NULL || |
| 95 | + total_packets_size > cur_ctx->packets_size_bytes) { |
| 96 | + CUDA_CHK(cudaFree(cur_ctx->packets)); |
| 97 | + CUDA_CHK(cudaMalloc(&cur_ctx->packets, total_packets_size)); |
| 98 | + |
| 99 | + cur_ctx->packets_size_bytes = total_packets_size; |
| 100 | + } |
| 101 | + |
| 102 | + if (cur_ctx->out == NULL || cur_ctx->out_size_bytes < out_size) { |
| 103 | + CUDA_CHK(cudaFree(cur_ctx->out)); |
| 104 | + CUDA_CHK(cudaMalloc(&cur_ctx->out, out_size)); |
| 105 | + |
| 106 | + cur_ctx->out_size_bytes = total_signatures; |
| 107 | + } |
| 108 | + |
| 109 | + if (cur_ctx->public_key_offsets == NULL || cur_ctx->offsets_len < total_signatures) { |
| 110 | + CUDA_CHK(cudaFree(cur_ctx->public_key_offsets)); |
| 111 | + CUDA_CHK(cudaMalloc(&cur_ctx->public_key_offsets, offsets_size)); |
| 112 | + |
| 113 | + CUDA_CHK(cudaFree(cur_ctx->signature_offsets)); |
| 114 | + CUDA_CHK(cudaMalloc(&cur_ctx->signature_offsets, offsets_size)); |
| 115 | + |
| 116 | + CUDA_CHK(cudaFree(cur_ctx->message_start_offsets)); |
| 117 | + CUDA_CHK(cudaMalloc(&cur_ctx->message_start_offsets, offsets_size)); |
| 118 | + |
| 119 | + CUDA_CHK(cudaFree(cur_ctx->message_lens)); |
| 120 | + CUDA_CHK(cudaMalloc(&cur_ctx->message_lens, offsets_size)); |
| 121 | + |
| 122 | + cur_ctx->offsets_len = total_signatures; |
| 123 | + } |
| 124 | + |
| 125 | + LOG("Done alloc"); |
| 126 | + |
| 127 | + CUDA_CHK(cudaMemcpyAsync(cur_ctx->public_key_offsets, public_key_offsets, offsets_size, cudaMemcpyHostToDevice, stream)); |
| 128 | + CUDA_CHK(cudaMemcpyAsync(cur_ctx->signature_offsets, signature_offsets, offsets_size, cudaMemcpyHostToDevice, stream)); |
| 129 | + CUDA_CHK(cudaMemcpyAsync(cur_ctx->message_start_offsets, message_start_offsets, offsets_size, cudaMemcpyHostToDevice, stream)); |
| 130 | + CUDA_CHK(cudaMemcpyAsync(cur_ctx->message_lens, message_lens, offsets_size, cudaMemcpyHostToDevice, stream)); |
| 131 | + |
| 132 | + size_t cur = 0; |
| 133 | + for (size_t i = 0; i < num_elems; i++) { |
| 134 | + LOG("i: %zu size: %d\n", i, elems[i].num * message_size); |
| 135 | + CUDA_CHK(cudaMemcpyAsync(&cur_ctx->packets[cur * message_size], elems[i].elems, elems[i].num * message_size, cudaMemcpyHostToDevice, stream)); |
| 136 | + cur += elems[i].num; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | +void release_gpu_ctx(gpu_ctx_t* cur_ctx) { |
| 142 | + pthread_mutex_unlock(&cur_ctx->mutex); |
| 143 | +} |
| 144 | + |
| 145 | +void ed25519_free_gpu_mem() { |
| 146 | + for (size_t gpu = 0; gpu < MAX_NUM_GPUS; gpu++) { |
| 147 | + for (size_t queue = 0; queue < MAX_QUEUE_SIZE; queue++) { |
| 148 | + gpu_ctx_t* cur_ctx = &g_gpu_ctx[gpu][queue]; |
| 149 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.packets)); |
| 150 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.out)); |
| 151 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.message_lens)); |
| 152 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.public_key_offsets)); |
| 153 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.private_key_offsets)); |
| 154 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.signature_offsets)); |
| 155 | + CUDA_CHK(cudaFree(cur_ctx->verify_ctx.message_start_offsets)); |
| 156 | + if (cur_ctx->stream != 0) { |
| 157 | + CUDA_CHK(cudaStreamDestroy(cur_ctx->stream)); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments