From 5ef74c2031a040f30a670dc7d60790fc6a9ec720 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Fri, 02 May 2014 22:20:34 +0000
Subject: [PATCH] Slowly refactoring and pushing to GPU

---
 src/mini_blas.c |  134 +++++++++++++++++++-------------------------
 1 files changed, 57 insertions(+), 77 deletions(-)

diff --git a/src/mini_blas.c b/src/mini_blas.c
index 3af36e5..eb6953d 100644
--- a/src/mini_blas.c
+++ b/src/mini_blas.c
@@ -1,8 +1,11 @@
-
 #include <stdlib.h>
+#include <stdio.h>
 #include <math.h>
+#include <time.h>
+#include <string.h>
+#include "mini_blas.h"
 
-void pm(int M, int N, double *A)
+void pm(int M, int N, float *A)
 {
     int i,j;
     for(i =0 ; i < M; ++i){
@@ -14,79 +17,10 @@
     printf("\n");
 }
 
-void gemm(int TA, int TB, int M, int N, int K, double ALPHA, 
-                    double *A, int lda, 
-                    double *B, int ldb,
-                    double BETA,
-                    double *C, int ldc)
-{
-    // Assume TA = 0, beta = 1 LULZ
-    int i,j,k;
-    if(TB && !TA){
-        for(i = 0; i < M; ++i){
-            for(j = 0; j < N; ++j){
-                register double sum = 0;
-                for(k = 0; k < K; ++k){
-                    sum += ALPHA*A[i*lda+k]*B[k+j*ldb];
-                }
-                C[i*ldc+j] += sum;
-            }
-        }
-    }else{
-        for(i = 0; i < M; ++i){
-            for(k = 0; k < K; ++k){
-                register double A_PART = ALPHA*A[i*lda+k];
-                for(j = 0; j < N; ++j){
-                    C[i*ldc+j] += A_PART*B[k*ldb+j];
-                }
-            }
-        }
-    }
-}
-
-void im2row(double *image, int h, int w, int c, int size, int stride, double *matrix)
-{
-    int i;
-    int mc = c;
-    int mw = (size*size);
-    int mh = ((h-size)/stride+1)*((w-size)/stride+1);
-    int msize = mc*mw*mh;
-    for(i = 0; i < msize; ++i){
-        int channel = i/(mh*mw);
-        int block =   (i%(mh*mw))/mw;
-        int position = i%mw;
-        int block_h = block/((w-size)/stride+1);
-        int block_w = block%((w-size)/stride+1);
-        int ph, pw, pc;
-        ph = position/size+block_h;
-        pw = position%size+block_w;
-        pc = channel;
-        matrix[i] = image[pc*h*w+ph*w+pw];
-    }
-}
-void im2col(double *image, int h, int w, int c, int size, int stride, double *matrix)
-{
-    int b,p;
-    int blocks = ((h-size)/stride+1)*((w-size)/stride+1);
-    int pixels = (size*size*c);
-    for(b = 0; b < blocks; ++b){
-        int block_h = b/((w-size)/stride+1);
-        int block_w = b%((w-size)/stride+1);
-        for(p = 0; p < pixels; ++p){
-            int ph, pw, pc;
-            int position = p%(size*size);
-            pc = p/(size*size);
-            ph = position/size+block_h;
-            pw = position%size+block_w;
-            matrix[b+p*blocks] = image[pc*h*w+ph*w+pw];
-        }
-    }
-}
-
-//From Berkeley Vision's Caffe!
-void im2col_cpu(double* data_im, const int channels,
+//This one might be too, can't remember.
+void col2im_cpu(float* data_col, const int channels,
         const int height, const int width, const int ksize, const int stride,
-        double* data_col) 
+        float* data_im) 
 {
     int c,h,w;
     int height_col = (height - ksize) / stride + 1;
@@ -98,11 +32,57 @@
         int c_im = c / ksize / ksize;
         for ( h = 0; h < height_col; ++h) {
             for ( w = 0; w < width_col; ++w) {
-                data_col[(c * height_col + h) * width_col + w] =
-                    data_im[(c_im * height + h * stride + h_offset) * width
-                    + w * stride + w_offset];
+                data_im[(c_im * height + h * stride + h_offset) * width
+                    + w * stride + w_offset]+= data_col[(c * height_col + h) * width_col + w];
             }
         }
     }
 }
 
+float *random_matrix(int rows, int cols)
+{
+    int i;
+    float *m = calloc(rows*cols, sizeof(float));
+    for(i = 0; i < rows*cols; ++i){
+        m[i] = (float)rand()/RAND_MAX;
+    }
+    return m;
+}
+
+void time_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){
+        gemm_cpu(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_blas()
+{
+    time_random_matrix(0,0,100,100,100); 
+    time_random_matrix(1,0,100,100,100); 
+    time_random_matrix(0,1,100,100,100); 
+    time_random_matrix(1,1,100,100,100); 
+
+    time_random_matrix(0,0,1000,100,100); 
+    time_random_matrix(1,0,1000,100,100); 
+    time_random_matrix(0,1,1000,100,100); 
+    time_random_matrix(1,1,1000,100,100); 
+}
+

--
Gitblit v1.10.0