From 028696bf15efeca3acb3db8c42a96f7b9e0f55ff Mon Sep 17 00:00:00 2001
From: iovodov <b@ovdv.ru>
Date: Thu, 03 May 2018 13:33:46 +0000
Subject: [PATCH] Output improvements for detector results: When printing detector results, output was done in random order, obfuscating results for interpreting. Now: 1. Text output includes coordinates of rects in (left,right,top,bottom in pixels) along with label and score 2. Text output is sorted by rect lefts to simplify finding appropriate rects on image 3. If several class probs are > thresh for some detection, the most probable is written first and coordinates for others are not repeated 4. Rects are imprinted in image in order by their best class prob, so most probable rects are always on top and not overlayed by less probable ones 5. Most probable label for rect is always written first Also: 6. Message about low GPU memory include required amount
---
src/cuda.c | 121 +++++++++++++++++++++++++++++++++------
1 files changed, 101 insertions(+), 20 deletions(-)
diff --git a/src/cuda.c b/src/cuda.c
index 8849fb1..f19c92d 100644
--- a/src/cuda.c
+++ b/src/cuda.c
@@ -5,19 +5,47 @@
#include "cuda.h"
#include "utils.h"
#include "blas.h"
+#include "assert.h"
#include <stdlib.h>
+#include <time.h>
+void cuda_set_device(int n)
+{
+ gpu_index = n;
+ cudaError_t status = cudaSetDevice(n);
+ check_error(status);
+}
+
+int cuda_get_device()
+{
+ int n = 0;
+ cudaError_t status = cudaGetDevice(&n);
+ check_error(status);
+ return n;
+}
void check_error(cudaError_t status)
{
+ //cudaDeviceSynchronize();
+ cudaError_t status2 = cudaGetLastError();
if (status != cudaSuccess)
{
const char *s = cudaGetErrorString(status);
char buffer[256];
printf("CUDA Error: %s\n", s);
+ assert(0);
snprintf(buffer, 256, "CUDA Error: %s", s);
error(buffer);
}
+ if (status2 != cudaSuccess)
+ {
+ const char *s = cudaGetErrorString(status);
+ char buffer[256];
+ printf("CUDA Error Prev: %s\n", s);
+ assert(0);
+ snprintf(buffer, 256, "CUDA Error Prev: %s", s);
+ error(buffer);
+ }
}
dim3 cuda_gridsize(size_t n){
@@ -25,39 +53,85 @@
size_t x = k;
size_t y = 1;
if(x > 65535){
- x = ceil(sqrt(k));
- y = (n-1)/(x*BLOCK) + 1;
+ x = ceil(sqrt(k));
+ y = (n-1)/(x*BLOCK) + 1;
}
dim3 d = {x, y, 1};
//printf("%ld %ld %ld %ld\n", n, x, y, x*y*BLOCK);
return d;
}
-cublasHandle_t blas_handle()
-{
- static int init = 0;
- static cublasHandle_t handle;
- if(!init) {
- cublasCreate(&handle);
- init = 1;
- }
- return handle;
+static cudaStream_t streamsArray[16]; // cudaStreamSynchronize( get_cuda_stream() );
+static int streamInit[16] = { 0 };
+
+cudaStream_t get_cuda_stream() {
+ int i = cuda_get_device();
+ if (!streamInit[i]) {
+ cudaStreamCreate(&streamsArray[i]);
+ streamInit[i] = 1;
+ }
+ return streamsArray[i];
}
-float *cuda_make_array(float *x, int n)
+
+#ifdef CUDNN
+cudnnHandle_t cudnn_handle()
+{
+ static int init[16] = {0};
+ static cudnnHandle_t handle[16];
+ int i = cuda_get_device();
+ if(!init[i]) {
+ cudnnCreate(&handle[i]);
+ init[i] = 1;
+ cudnnStatus_t status = cudnnSetStream(handle[i], get_cuda_stream());
+ }
+ return handle[i];
+}
+#endif
+
+cublasHandle_t blas_handle()
+{
+ static int init[16] = {0};
+ static cublasHandle_t handle[16];
+ int i = cuda_get_device();
+ if(!init[i]) {
+ cublasCreate(&handle[i]);
+ cublasStatus_t status = cublasSetStream(handle[i], get_cuda_stream());
+ init[i] = 1;
+ }
+ return handle[i];
+}
+
+float *cuda_make_array(float *x, size_t n)
{
float *x_gpu;
size_t size = sizeof(float)*n;
cudaError_t status = cudaMalloc((void **)&x_gpu, size);
check_error(status);
if(x){
- status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
+ //status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
+ status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyHostToDevice, get_cuda_stream());
check_error(status);
}
+ if(!x_gpu) error("Cuda malloc failed\n");
return x_gpu;
}
-float cuda_compare(float *x_gpu, float *x, int n, char *s)
+void cuda_random(float *x_gpu, size_t n)
+{
+ static curandGenerator_t gen[16];
+ static int init[16] = {0};
+ int i = cuda_get_device();
+ if(!init[i]){
+ curandCreateGenerator(&gen[i], CURAND_RNG_PSEUDO_DEFAULT);
+ curandSetPseudoRandomGeneratorSeed(gen[i], time(0));
+ init[i] = 1;
+ }
+ curandGenerateUniform(gen[i], x_gpu, n);
+ check_error(cudaPeekAtLastError());
+}
+
+float cuda_compare(float *x_gpu, float *x, size_t n, char *s)
{
float *tmp = calloc(n, sizeof(float));
cuda_pull_array(x_gpu, tmp, n);
@@ -70,7 +144,7 @@
return err;
}
-int *cuda_make_int_array(int n)
+int *cuda_make_int_array(size_t n)
{
int *x_gpu;
size_t size = sizeof(int)*n;
@@ -81,22 +155,29 @@
void cuda_free(float *x_gpu)
{
+ //cudaStreamSynchronize(get_cuda_stream());
cudaError_t status = cudaFree(x_gpu);
check_error(status);
}
-void cuda_push_array(float *x_gpu, float *x, int n)
+void cuda_push_array(float *x_gpu, float *x, size_t n)
{
size_t size = sizeof(float)*n;
- cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
+ //cudaError_t status = cudaMemcpy(x_gpu, x, size, cudaMemcpyHostToDevice);
+ cudaError_t status = cudaMemcpyAsync(x_gpu, x, size, cudaMemcpyHostToDevice, get_cuda_stream());
check_error(status);
}
-void cuda_pull_array(float *x_gpu, float *x, int n)
+void cuda_pull_array(float *x_gpu, float *x, size_t n)
{
size_t size = sizeof(float)*n;
- cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
+ //cudaError_t status = cudaMemcpy(x, x_gpu, size, cudaMemcpyDeviceToHost);
+ cudaError_t status = cudaMemcpyAsync(x, x_gpu, size, cudaMemcpyDeviceToHost, get_cuda_stream());
check_error(status);
+ cudaStreamSynchronize(get_cuda_stream());
}
-#endif
+#else // GPU
+#include "cuda.h"
+void cuda_set_device(int n) {}
+#endif // GPU
--
Gitblit v1.10.0