From 354b0cbdcb48a66de1af3aca9fc2f5687e46ee42 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Sat, 19 Apr 2014 20:10:59 +0000
Subject: [PATCH] GPU flag in Makefile

---
 src/mini_blas.c |   88 ---------------------
 Makefile        |   10 ++
 src/gpu_gemm.c  |   83 ++++++++++++++++++++
 src/tests.c     |    6 
 4 files changed, 96 insertions(+), 91 deletions(-)

diff --git a/Makefile b/Makefile
index 68ed3ee..2c47fdf 100644
--- a/Makefile
+++ b/Makefile
@@ -1,21 +1,29 @@
 CC=gcc
+GPU=0
 COMMON=-Wall `pkg-config --cflags opencv` -I/usr/local/cuda/include/
 UNAME = $(shell uname)
 OPTS=-O3
 ifeq ($(UNAME), Darwin)
 COMMON+= -isystem /usr/local/Cellar/opencv/2.4.6.1/include/opencv -isystem /usr/local/Cellar/opencv/2.4.6.1/include
+ifeq ($(GPU), 1)
 LDFLAGS= -framework OpenCL
+endif
 else
 OPTS+= -march=native
+ifeq ($(GPU), 1)
 LDFLAGS= -lOpenCL
 endif
+endif
 CFLAGS= $(COMMON) $(OPTS)
 #CFLAGS= $(COMMON) -O0 -g 
 LDFLAGS+=`pkg-config --libs opencv` -lm
 VPATH=./src/
 EXEC=cnn
 
-OBJ=network.o image.o tests.o connected_layer.o maxpool_layer.o activations.o list.o option_list.o parser.o utils.o data.o matrix.o softmax_layer.o mini_blas.o convolutional_layer.o opencl.o gpu_gemm.o cpu_gemm.o normalization_layer.o
+OBJ=network.o image.o tests.o connected_layer.o maxpool_layer.o activations.o list.o option_list.o parser.o utils.o data.o matrix.o softmax_layer.o mini_blas.o convolutional_layer.o cpu_gemm.o normalization_layer.o
+ifeq ($(GPU), 1)
+OBJ+=gpu_gemm.o opencl.o 
+endif
 
 all: $(EXEC)
 
diff --git a/src/gpu_gemm.c b/src/gpu_gemm.c
index 26bb3fe..4a8aaca 100644
--- a/src/gpu_gemm.c
+++ b/src/gpu_gemm.c
@@ -83,6 +83,89 @@
 
 }
 
+void time_gpu_random_matrix(int TA, int TB, int m, int k, int n)
+{
+    float *a;
+    if(!TA) a = random_matrix(m,k);
+    else a = random_matrix(k,m);
+    int lda = (!TA)?k:m;
+    float *b;
+    if(!TB) b = random_matrix(k,n);
+    else b = random_matrix(n,k);
+    int ldb = (!TB)?n:k;
+
+    float *c = random_matrix(m,n);
+    int i;
+    clock_t start = clock(), end;
+    for(i = 0; i<1000; ++i){
+        gpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
+    }
+    end = clock();
+    printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf ms\n",m,k,k,n, TA, TB, (float)(end-start)/CLOCKS_PER_SEC);
+    free(a);
+    free(b);
+    free(c);
+}
+
+void test_gpu_accuracy(int TA, int TB, int m, int k, int n)
+{
+    srand(0);
+    float *a;
+    if(!TA) a = random_matrix(m,k);
+    else a = random_matrix(k,m);
+    int lda = (!TA)?k:m;
+    float *b;
+    if(!TB) b = random_matrix(k,n);
+    else b = random_matrix(n,k);
+    int ldb = (!TB)?n:k;
+
+    float *c = random_matrix(m,n);
+    float *c_gpu = random_matrix(m,n);
+    memset(c, 0, m*n*sizeof(float));
+    memset(c_gpu, 0, m*n*sizeof(float));
+    int i;
+        //pm(m,k,b);
+        gpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c_gpu,n);
+        //pm(m, n, c_gpu);
+        cpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
+        //pm(m, n, c);
+    double sse = 0;
+    for(i = 0; i < m*n; ++i) {
+        //printf("%f %f\n", c[i], c_gpu[i]);
+        sse += pow(c[i]-c_gpu[i], 2);
+    }
+    printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %g MSE\n",m,k,k,n, TA, TB, sse/(m*n));
+    free(a);
+    free(b);
+    free(c);
+}
+
+void test_gpu_blas()
+{
+    test_gpu_accuracy(0,0,17,10,10); 
+    test_gpu_accuracy(1,0,17,10,10); 
+    test_gpu_accuracy(0,1,17,10,10); 
+    test_gpu_accuracy(1,1,17,10,10); 
+
+    test_gpu_accuracy(0,0,1000,10,100); 
+    test_gpu_accuracy(1,0,1000,10,100); 
+    test_gpu_accuracy(0,1,1000,10,100); 
+    test_gpu_accuracy(1,1,1000,10,100); 
+
+    time_gpu_random_matrix(0,0,1000,1000,100); 
+    time_random_matrix(0,0,1000,1000,100); 
+
+    time_gpu_random_matrix(0,1,1000,1000,100); 
+    time_random_matrix(0,1,1000,1000,100); 
+
+    time_gpu_random_matrix(1,0,1000,1000,100); 
+    time_random_matrix(1,0,1000,1000,100); 
+
+    time_gpu_random_matrix(1,1,1000,1000,100); 
+    time_random_matrix(1,1,1000,1000,100); 
+
+}
+
 /*
 cl_kernel get_gemm_kernel_slow()
 {
diff --git a/src/mini_blas.c b/src/mini_blas.c
index bac3e22..4c7d3d0 100644
--- a/src/mini_blas.c
+++ b/src/mini_blas.c
@@ -24,7 +24,7 @@
         float BETA,
         float *C, int ldc)
 {
-    gpu_gemm( TA,  TB,  M, N, K, ALPHA,A,lda, B, ldb,BETA,C,ldc);
+    cpu_gemm( TA,  TB,  M, N, K, ALPHA,A,lda, B, ldb,BETA,C,ldc);
 }
 
 void im2row(float *image, int h, int w, int c, int size, int stride, float *matrix)
@@ -155,91 +155,5 @@
     time_random_matrix(1,0,1000,100,100); 
     time_random_matrix(0,1,1000,100,100); 
     time_random_matrix(1,1,1000,100,100); 
-
-
 }
 
-void time_gpu_random_matrix(int TA, int TB, int m, int k, int n)
-{
-    float *a;
-    if(!TA) a = random_matrix(m,k);
-    else a = random_matrix(k,m);
-    int lda = (!TA)?k:m;
-    float *b;
-    if(!TB) b = random_matrix(k,n);
-    else b = random_matrix(n,k);
-    int ldb = (!TB)?n:k;
-
-    float *c = random_matrix(m,n);
-    int i;
-    clock_t start = clock(), end;
-    for(i = 0; i<1000; ++i){
-        gpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
-    }
-    end = clock();
-    printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %lf ms\n",m,k,k,n, TA, TB, (float)(end-start)/CLOCKS_PER_SEC);
-    free(a);
-    free(b);
-    free(c);
-}
-
-void test_gpu_accuracy(int TA, int TB, int m, int k, int n)
-{
-    srand(0);
-    float *a;
-    if(!TA) a = random_matrix(m,k);
-    else a = random_matrix(k,m);
-    int lda = (!TA)?k:m;
-    float *b;
-    if(!TB) b = random_matrix(k,n);
-    else b = random_matrix(n,k);
-    int ldb = (!TB)?n:k;
-
-    float *c = random_matrix(m,n);
-    float *c_gpu = random_matrix(m,n);
-    memset(c, 0, m*n*sizeof(float));
-    memset(c_gpu, 0, m*n*sizeof(float));
-    int i;
-        //pm(m,k,b);
-        gpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c_gpu,n);
-        //pm(m, n, c_gpu);
-        cpu_gemm(TA,TB,m,n,k,1,a,lda,b,ldb,1,c,n);
-        //pm(m, n, c);
-    double sse = 0;
-    for(i = 0; i < m*n; ++i) {
-        //printf("%f %f\n", c[i], c_gpu[i]);
-        sse += pow(c[i]-c_gpu[i], 2);
-    }
-    printf("Matrix Multiplication %dx%d * %dx%d, TA=%d, TB=%d: %g MSE\n",m,k,k,n, TA, TB, sse/(m*n));
-    free(a);
-    free(b);
-    free(c);
-}
-
-void test_gpu_blas()
-{
-    test_gpu_accuracy(0,0,17,10,10); 
-    test_gpu_accuracy(1,0,17,10,10); 
-    test_gpu_accuracy(0,1,17,10,10); 
-    test_gpu_accuracy(1,1,17,10,10); 
-
-    test_gpu_accuracy(0,0,1000,10,100); 
-    test_gpu_accuracy(1,0,1000,10,100); 
-    test_gpu_accuracy(0,1,1000,10,100); 
-    test_gpu_accuracy(1,1,1000,10,100); 
-
-    time_gpu_random_matrix(0,0,1000,1000,100); 
-    time_random_matrix(0,0,1000,1000,100); 
-
-    time_gpu_random_matrix(0,1,1000,1000,100); 
-    time_random_matrix(0,1,1000,1000,100); 
-
-    time_gpu_random_matrix(1,0,1000,1000,100); 
-    time_random_matrix(1,0,1000,1000,100); 
-
-    time_gpu_random_matrix(1,1,1000,1000,100); 
-    time_random_matrix(1,1,1000,1000,100); 
-
-}
-
-
diff --git a/src/tests.c b/src/tests.c
index e91fb63..851d781 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -789,9 +789,9 @@
 	//test_vince();
 	//test_full();
 	//train_VOC();
-	features_VOC_image(argv[1], argv[2], argv[3], 0);
-	features_VOC_image(argv[1], argv[2], argv[3], 1);
-	//features_VOC_image_size(argv[1], atoi(argv[2]), atoi(argv[3]));
+	//features_VOC_image(argv[1], argv[2], argv[3], 0);
+	//features_VOC_image(argv[1], argv[2], argv[3], 1);
+	features_VOC_image_size(argv[1], atoi(argv[2]), atoi(argv[3]));
 	//visualize_imagenet_features("data/assira/train.list");
 	//visualize_imagenet_topk("data/VOC2012.list");
 	//visualize_cat();

--
Gitblit v1.10.0