GPU flag in Makefile

This commit is contained in:
Joseph Redmon 2014-04-19 13:10:59 -07:00
parent 324e0a33dd
commit 354b0cbdcb
4 changed files with 96 additions and 91 deletions

View File

@ -1,21 +1,29 @@
CC=gcc CC=gcc
GPU=0
COMMON=-Wall `pkg-config --cflags opencv` -I/usr/local/cuda/include/ COMMON=-Wall `pkg-config --cflags opencv` -I/usr/local/cuda/include/
UNAME = $(shell uname) UNAME = $(shell uname)
OPTS=-O3 OPTS=-O3
ifeq ($(UNAME), Darwin) 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 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 LDFLAGS= -framework OpenCL
endif
else else
OPTS+= -march=native OPTS+= -march=native
ifeq ($(GPU), 1)
LDFLAGS= -lOpenCL LDFLAGS= -lOpenCL
endif endif
endif
CFLAGS= $(COMMON) $(OPTS) CFLAGS= $(COMMON) $(OPTS)
#CFLAGS= $(COMMON) -O0 -g #CFLAGS= $(COMMON) -O0 -g
LDFLAGS+=`pkg-config --libs opencv` -lm LDFLAGS+=`pkg-config --libs opencv` -lm
VPATH=./src/ VPATH=./src/
EXEC=cnn 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) all: $(EXEC)

View File

@ -83,6 +83,89 @@ void gpu_gemm(int TA, int TB, int M, int N, int K, float ALPHA,
} }
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() cl_kernel get_gemm_kernel_slow()
{ {

View File

@ -24,7 +24,7 @@ void gemm(int TA, int TB, int M, int N, int K, float ALPHA,
float BETA, float BETA,
float *C, int ldc) 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) void im2row(float *image, int h, int w, int c, int size, int stride, float *matrix)
@ -155,91 +155,5 @@ void test_blas()
time_random_matrix(1,0,1000,100,100); time_random_matrix(1,0,1000,100,100);
time_random_matrix(0,1,1000,100,100); time_random_matrix(0,1,1000,100,100);
time_random_matrix(1,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);
}

View File

@ -789,9 +789,9 @@ int main(int argc, char *argv[])
//test_vince(); //test_vince();
//test_full(); //test_full();
//train_VOC(); //train_VOC();
features_VOC_image(argv[1], argv[2], argv[3], 0); //features_VOC_image(argv[1], argv[2], argv[3], 0);
features_VOC_image(argv[1], argv[2], argv[3], 1); //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_size(argv[1], atoi(argv[2]), atoi(argv[3]));
//visualize_imagenet_features("data/assira/train.list"); //visualize_imagenet_features("data/assira/train.list");
//visualize_imagenet_topk("data/VOC2012.list"); //visualize_imagenet_topk("data/VOC2012.list");
//visualize_cat(); //visualize_cat();