Joseph Redmon
2014-04-19 354b0cbdcb48a66de1af3aca9fc2f5687e46ee42
GPU flag in Makefile
4 files modified
187 ■■■■ changed files
Makefile 10 ●●●●● patch | view | raw | blame | history
src/gpu_gemm.c 83 ●●●●● patch | view | raw | blame | history
src/mini_blas.c 88 ●●●●● patch | view | raw | blame | history
src/tests.c 6 ●●●● patch | view | raw | blame | history
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)
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()
{
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);
}
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();