Skip to content

Commit

Permalink
Add optional ed25519_init()
Browse files Browse the repository at this point in the history
This function permits ed25519_verify_many() users to ensure the GPU is
initialized correctly and if not take action rather than having the failure
suppressed.

ed25519_verify_many() retains its previous behavior of implicit initialization
if ed25519_init() is not explicitly called.
  • Loading branch information
mvines committed Jul 15, 2018
1 parent bfb0cc7 commit f81494d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/cuda-ecc-ed25119/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void ED25519_DECLSPEC ed25519_free_gpu_mem();
void ED25519_DECLSPEC ed25519_set_verbose(bool val);

const char* ED25519_DECLSPEC ed25519_license();
bool ED25519_DECLSPEC ed25519_init();

#ifdef __cplusplus
}
Expand Down
44 changes: 28 additions & 16 deletions src/cuda-ecc-ed25119/verify.cu
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ void ed25519_set_verbose(bool val) {
g_verbose = val;
}

static bool ed25519_init_locked() {
if (g_total_gpus == -1) {
cudaGetDeviceCount(&g_total_gpus);
g_total_gpus = min(8, g_total_gpus);
LOG("total_gpus: %d\n", g_total_gpus);
for (int gpu = 0; gpu < g_total_gpus; gpu++) {
for (int queue = 0; queue < MAX_QUEUE_SIZE; queue++) {
int err = pthread_mutex_init(&g_gpu_ctx[gpu][queue].mutex, NULL);
if (err != 0) {
fprintf(stderr, "pthread_mutex_init error %d gpu: %d queue: %d\n",
err, gpu, queue);
g_total_gpus = 0;
return false;
}
}
}
}
return g_total_gpus > 0;
}

bool ed25519_init() {
pthread_mutex_lock(&g_ctx_mutex);
bool success = ed25519_init_locked();
pthread_mutex_unlock(&g_ctx_mutex);
return success;
}

void ed25519_verify_many(const gpu_Elems* elems,
uint32_t num,
uint32_t message_size,
Expand Down Expand Up @@ -184,22 +211,7 @@ void ed25519_verify_many(const gpu_Elems* elems,
// Device allocate

pthread_mutex_lock(&g_ctx_mutex);
if (g_total_gpus == -1) {
cudaGetDeviceCount(&g_total_gpus);
g_total_gpus = min(8, g_total_gpus);
LOG("total_gpus: %d\n", g_total_gpus);
for (int gpu = 0; gpu < g_total_gpus; gpu++) {
for (int queue = 0; queue < MAX_QUEUE_SIZE; queue++) {
int err = pthread_mutex_init(&g_gpu_ctx[gpu][queue].mutex, NULL);
if (err != 0) {
fprintf(stderr, "pthread_mutex_init error %d gpu: %d queue: %d\n",
err, gpu, queue);
return;
}
}
}
}
if (g_total_gpus <= 0) {
if (!ed25519_init_locked()) {
pthread_mutex_unlock(&g_ctx_mutex);
LOG("No GPUs, exiting...\n");
return;
Expand Down

0 comments on commit f81494d

Please sign in to comment.