Skip to content

Commit

Permalink
still work on sift, why 32f and 32s have same performance?
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Sep 8, 2010
1 parent 3ddae16 commit 52618f9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 38 deletions.
15 changes: 7 additions & 8 deletions lib/ccv.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ void ccv_garbage_collect();
default: ((unsigned char*)(ptr))[(i)] = ccv_clamp((int)(value) >> factor, 0, 255); }

/* unswitch for loop macros */
#define __ccv_get_32s_value(ptr, i) ((int*)(ptr))[(i)]
#define __ccv_get_32f_value(ptr, i) ((float*)(ptr))[(i)]
#define __ccv_get_64f_value(ptr, i) ((double*)(ptr))[(i)]
#define __ccv_get_8u_value(ptr, i) ((unsigned char*)(ptr))[(i)]
#define __ccv_get_32s_value(ptr, i, factor) (((int*)(ptr))[(i)] << factor)
#define __ccv_get_32f_value(ptr, i, factor) ((float*)(ptr))[(i)]
#define __ccv_get_64f_value(ptr, i, factor) ((double*)(ptr))[(i)]
#define __ccv_get_8u_value(ptr, i, factor) (((unsigned char*)(ptr))[(i)] << factor)
#define ccv_matrix_getter(type, block, ...) { switch (CCV_GET_DATA_TYPE(type)) { \
case CCV_32S: { block(__VA_ARGS__, __ccv_get_32s_value); break; } \
case CCV_32F: { block(__VA_ARGS__, __ccv_get_32f_value); break; } \
Expand Down Expand Up @@ -322,8 +322,6 @@ void ccv_garbage_collect();
case CCV_64F: { block(__VA_ARGS__, double, __ccv_set_64f_value, __ccv_get_64f_value); break; } \
default: { block(__VA_ARGS__, unsigned char, __ccv_set_8u_value, __ccv_get_8u_value); } } }



/* basic io */
enum {
CCV_SERIAL_GRAY = 0x100,
Expand Down Expand Up @@ -360,6 +358,8 @@ double ccv_norm(ccv_matrix_t* mat, int type);
double ccv_dot(ccv_matrix_t* a, ccv_matrix_t* b);
double ccv_sum(ccv_matrix_t* mat);
void ccv_zero(ccv_matrix_t* mat);
void ccv_convert(ccv_matrix_t* a, ccv_matrix_t** b, int type, int lr, int rr);
void ccv_substract(ccv_matrix_t* a, ccv_matrix_t* b, ccv_matrix_t** c);

enum {
CCV_A_TRANSPOSE = 0x01,
Expand All @@ -377,7 +377,6 @@ ccv_matrix_cell_t ccv_get_sparse_matrix_cell(ccv_sparse_matrix_t* mat, int row,
void ccv_set_sparse_matrix_cell(ccv_sparse_matrix_t* mat, int row, int col, void* data);
void ccv_compress_sparse_matrix(ccv_sparse_matrix_t* mat, ccv_compressed_sparse_matrix_t** csm);
void ccv_decompress_sparse_matrix(ccv_compressed_sparse_matrix_t* csm, ccv_sparse_matrix_t** smt);
void ccv_convert(ccv_matrix_t* a, ccv_matrix_t** b, int type);
void ccv_slice(ccv_matrix_t* a, ccv_matrix_t** b, int y, int x, int rows, int cols);

/* basic data structures */
Expand Down Expand Up @@ -527,7 +526,7 @@ typedef struct {
double sigma;
} ccv_sift_param_t;

void ccv_sift(ccv_dense_matrix_t* a);
ccv_array_t* ccv_sift(ccv_dense_matrix_t* a, ccv_sift_param_t params);

#define CCV_SGF_POINT_MAX (5)
#define CCV_SGF_POINT_MIN (3)
Expand Down
29 changes: 27 additions & 2 deletions lib/ccv_algebra.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ double ccv_sum(ccv_matrix_t* mat)
for (i = 0; i < dmt->rows; i++) \
{ \
for (j = 0; j < dmt->cols; j++) \
sum += __for_get(m_ptr, j); \
sum += __for_get(m_ptr, j, 0); \
m_ptr += dmt->step; \
}
ccv_matrix_getter(dmt->type, for_block);
Expand All @@ -37,6 +37,31 @@ void ccv_zero(ccv_matrix_t* mat)
memset(dmt->data.ptr, 0, dmt->step * dmt->rows);
}

void ccv_convert(ccv_matrix_t* a, ccv_matrix_t** b, int type, int lr, int rr)
{
ccv_dense_matrix_t* da = ccv_get_dense_matrix(a);
char identifier[64];
memset(identifier, 0, 64);
sprintf(identifier, "ccv_convert(%d,%d,%d)", type, lr, rr);
uint64_t sig = ccv_matrix_generate_signature(identifier, 64, da->sig, 0);
ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, da->rows, da->cols, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(da->type), CCV_GET_DATA_TYPE(type) | CCV_GET_CHANNEL(da->type), sig);
int i, j, ch = CCV_GET_CHANNEL_NUM(da->type);
unsigned char* aptr = da->data.ptr;
unsigned char* bptr = db->data.ptr;
#define for_block(__for_get, __for_set) \
for (i = 0; i < da->rows; i++) \
{ \
for (j = 0; j < da->cols * ch; j++) \
{ \
__for_set(bptr, j, __for_get(aptr, j, lr), rr); \
} \
aptr += da->step; \
bptr += db->step; \
}
ccv_matrix_getter(da->type, ccv_matrix_setter, db->type, for_block);
#undef for_block
}

void ccv_substract(ccv_matrix_t* a, ccv_matrix_t* b, ccv_matrix_t** c)
{
ccv_dense_matrix_t* da = ccv_get_dense_matrix(a);
Expand All @@ -54,7 +79,7 @@ void ccv_substract(ccv_matrix_t* a, ccv_matrix_t* b, ccv_matrix_t** c)
{ \
for (j = 0; j < da->cols; j++) \
{ \
__for_set(cptr, j, __for_get(aptr, j) - __for_get(bptr, j), 0); \
__for_set(cptr, j, __for_get(aptr, j, 0) - __for_get(bptr, j, 0), 0); \
} \
aptr += da->step; \
bptr += db->step; \
Expand Down
32 changes: 16 additions & 16 deletions lib/ccv_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static void __ccv_resample_area(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b)
int dxn = xofs[k].di; \
float alpha = xofs[k].alpha; \
for (i = 0; i < ch; i++) \
buf[dxn + i] += __for_get(a_ptr, xofs[k].si + i) * alpha; \
buf[dxn + i] += __for_get(a_ptr, xofs[k].si + i, 0) * alpha; \
} \
if ((dy + 1) * scale_y <= sy + 1 || sy == a->rows - 1) \
{ \
Expand Down Expand Up @@ -413,21 +413,21 @@ void ccv_sample_down(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b)
int _sy = (sy < 0) ? 1 - sy : (sy >= a->rows) ? a->rows * 2 - 1 - sy : sy; \
unsigned char* a_ptr = a->data.ptr + a->step * _sy; \
for (k = 0; k < ch; k++) \
__for_set(row, k, __for_get_a(a_ptr, k) * 10 + __for_get_a(a_ptr, ch + k) * 5 + __for_get_a(a_ptr, 2 * ch + k), 0); \
__for_set(row, k, __for_get_a(a_ptr, k, 0) * 10 + __for_get_a(a_ptr, ch + k, 0) * 5 + __for_get_a(a_ptr, 2 * ch + k, 0), 0); \
for(dx = ch; dx < cols0 * ch; dx += ch) \
for (k = 0; k < ch; k++) \
__for_set(row, dx + k, __for_get_a(a_ptr, dx * 2 + k) * 6 + (__for_get_a(a_ptr, dx * 2 + k - ch) + __for_get_a(a_ptr, dx * 2 + k + ch)) * 4 + __for_get_a(a_ptr, dx * 2 + k - ch * 2) + __for_get_a(a_ptr, dx * 2 + k + ch * 2), 0); \
__for_set(row, dx + k, __for_get_a(a_ptr, dx * 2 + k, 0) * 6 + (__for_get_a(a_ptr, dx * 2 + k - ch, 0) + __for_get_a(a_ptr, dx * 2 + k + ch, 0)) * 4 + __for_get_a(a_ptr, dx * 2 + k - ch * 2, 0) + __for_get_a(a_ptr, dx * 2 + k + ch * 2, 0), 0); \
if (a->cols - db->cols * 2 == 0) \
for (k = 0; k < ch; k++) \
__for_set(row, (db->cols - 2) * ch + k, __for_get_a(a_ptr, (db->cols - 2) * 2 * ch + k) * 6 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch - ch + k) * 4 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch + ch + k) * 5 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch - 2 * ch + k), 0); \
__for_set(row, (db->cols - 2) * ch + k, __for_get_a(a_ptr, (db->cols - 2) * 2 * ch + k, 0) * 6 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch - ch + k, 0) * 4 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch + ch + k, 0) * 5 + __for_get_a(a_ptr, (db->cols - 2) * 2 * ch - 2 * ch + k, 0), 0); \
for (k = 0; k < ch; k++) \
__for_set(row, (db->cols - 1) * ch + k, __for_get_a(a_ptr, a->cols * ch - ch + k) * 10 + __for_get_a(a_ptr, (a->cols - 2) * ch + k) * 5 + __for_get_a(a_ptr, (a->cols - 3) * ch + k), 0); \
__for_set(row, (db->cols - 1) * ch + k, __for_get_a(a_ptr, a->cols * ch - ch + k, 0) * 10 + __for_get_a(a_ptr, (a->cols - 2) * ch + k, 0) * 5 + __for_get_a(a_ptr, (a->cols - 3) * ch + k, 0), 0); \
} \
int* rows[5]; \
for(k = 0; k < 5; k++) \
rows[k] = buf + ((dy * 2 - 2 + k + 2) % 5) * db->cols * ch; \
for(dx = 0; dx < db->cols * ch; dx++) \
__for_set_b(b_ptr, dx, (__for_get(rows[2], dx) * 6 + (__for_get(rows[1], dx) + __for_get(rows[3], dx)) * 4 + __for_get(rows[0], dx) + __for_get(rows[4], dx)) / 256, 0); \
__for_set_b(b_ptr, dx, (__for_get(rows[2], dx, 0) * 6 + (__for_get(rows[1], dx, 0) + __for_get(rows[3], dx, 0)) * 4 + __for_get(rows[0], dx, 0) + __for_get(rows[4], dx, 0)) / 256, 0); \
b_ptr += db->step; \
}
int no_8u_type = (a->type & CCV_8U) ? CCV_32S : a->type;
Expand Down Expand Up @@ -536,21 +536,21 @@ void ccv_blur(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, double sigma)
{ \
for (j = 0; j < hfz; j++) \
for (k = 0; k < ch; k++) \
__for_set_b(buf, j * ch + k, __for_get_a(aptr, k), 0); \
__for_set_b(buf, j * ch + k, __for_get_a(aptr, k, 0), 0); \
for (j = 0; j < a->cols * ch; j++) \
__for_set_b(buf, j + hfz * ch, __for_get_a(aptr, j), 0); \
__for_set_b(buf, j + hfz * ch, __for_get_a(aptr, j, 0), 0); \
for (j = a->cols; j < hfz + a->cols; j++) \
for (k = 0; k < ch; k++) \
__for_set_b(buf, j * ch + hfz * ch + k, __for_get_a(aptr, (a->cols - 1) * ch + k), 0); \
__for_set_b(buf, j * ch + hfz * ch + k, __for_get_a(aptr, (a->cols - 1) * ch + k, 0), 0); \
for (j = 0; j < a->cols * ch; j++) \
{ \
__for_type sum = 0; \
for (k = 0; k < fsz; k++) \
sum += __for_get_b(buf, k * ch + j) * __for_get_b(filter, k); \
sum += __for_get_b(buf, k * ch + j, 0) * __for_get_b(filter, k, 0); \
__for_set_b(buf, j, sum, 8); \
} \
for (j = 0; j < a->cols * ch; j++) \
__for_set_a(bptr, j, __for_get_b(buf, j), 0); \
__for_set_a(bptr, j, __for_get_b(buf, j, 0), 0); \
aptr += a->step; \
bptr += db->step; \
}
Expand All @@ -562,20 +562,20 @@ void ccv_blur(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, double sigma)
for (i = 0; i < a->cols * ch; i++) \
{ \
for (j = 0; j < hfz; j++) \
__for_set_b(buf, j, __for_get_a(bptr, i), 0); \
__for_set_b(buf, j, __for_get_a(bptr, i, 0), 0); \
for (j = 0; j < a->rows; j++) \
__for_set_b(buf, j + hfz, __for_get_a(bptr + j * a->step, i), 0); \
__for_set_b(buf, j + hfz, __for_get_a(bptr + j * a->step, i, 0), 0); \
for (j = a->rows; j < hfz + a->rows; j++) \
__for_set_b(buf, j + hfz, __for_get_a(bptr + (a->rows - 1) * a->step, i), 0); \
__for_set_b(buf, j + hfz, __for_get_a(bptr + (a->rows - 1) * a->step, i, 0), 0); \
for (j = 0; j < a->rows; j++) \
{ \
__for_type sum = 0; \
for (k = 0; k < fsz; k++) \
sum += __for_get_b(buf, k + j) * __for_get_b(filter, k); \
sum += __for_get_b(buf, k + j, 0) * __for_get_b(filter, k, 0); \
__for_set_b(buf, j, sum, 8); \
} \
for (j = 0; j < a->rows; j++) \
__for_set_a(bptr + j * a->step, i, __for_get_b(buf, j), 0); \
__for_set_a(bptr + j * a->step, i, __for_get_b(buf, j, 0), 0); \
}
ccv_matrix_typeof_setter_getter(no_8u_type, ccv_matrix_setter_getter, a->type, for_block);
#undef for_block
Expand Down
2 changes: 1 addition & 1 deletion lib/ccv_numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ static void __ccv_filter_fftw(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b, ccv_
for (y = 0; y < rows; y++) \
{ \
for (x = 0; x < cols; x++) \
fftw_ptr[x] = __for_get(m_ptr, x); \
fftw_ptr[x] = __for_get(m_ptr, x, 0); \
fftw_ptr += cols_2c; \
m_ptr += a->step; \
} \
Expand Down
6 changes: 3 additions & 3 deletions lib/ccv_sift.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
ccv_array_t* ccv_sift(ccv_dense_matrix_t* a, ccv_sift_param_t params)
{
ccv_dense_matrix_t** g = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * params.nlevels * params.noctaves);
memset(g, sizeof(ccv_dense_matrix_t*) * params.nlevels * params.noctaves, 0);
memset(g, 0, sizeof(ccv_dense_matrix_t*) * params.nlevels * params.noctaves);
ccv_dense_matrix_t** dog = (ccv_dense_matrix_t**)alloca(sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * params.noctaves);
memset(dog, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * params.noctaves, 0);
memset(dog, 0, sizeof(ccv_dense_matrix_t*) * (params.nlevels - 1) * params.noctaves);
int i, j;
g[0] = a;
ccv_convert(a, &g[0], CCV_32S, 8, 0);
for (j = 1; j < params.nlevels; j++)
{
ccv_blur(g[j - 1], &g[j], params.sigma);
Expand Down
12 changes: 6 additions & 6 deletions lib/ccv_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void ccv_compress_sparse_matrix(ccv_sparse_matrix_t* mat, ccv_compressed_sparse_
if (mat->type & CCV_DENSE_VECTOR) \
{ \
for (j = 0; j < vector->length; j++) \
if (__while_get(vector->data.ptr, j) != 0) \
if (__while_get(vector->data.ptr, j, 0) != 0) \
nnz++; \
} else { \
nnz += vector->load_factor; \
Expand Down Expand Up @@ -274,9 +274,9 @@ void ccv_compress_sparse_matrix(ccv_sparse_matrix_t* mat, ccv_compressed_sparse_
int k = 0;
#define for_block(__for_set, __for_get) \
for (j = 0; j < vector->length; j++) \
if (__for_get(vector->data.ptr, j) != 0) \
if (__for_get(vector->data.ptr, j, 0) != 0) \
{ \
__for_set(m_ptr, k, __for_get(vector->data.ptr, j), 0); \
__for_set(m_ptr, k, __for_get(vector->data.ptr, j, 0), 0); \
idx[k] = j; \
k++; \
}
Expand All @@ -291,7 +291,7 @@ void ccv_compress_sparse_matrix(ccv_sparse_matrix_t* mat, ccv_compressed_sparse_
for (j = 0; j < vector->length; j++) \
if (vector->indice[j] != -1) \
{ \
__for_set(m_ptr, k, __for_get(vector->data.ptr, j), 0); \
__for_set(m_ptr, k, __for_get(vector->data.ptr, j, 0), 0); \
idx[k] = vector->indice[j]; \
k++; \
}
Expand Down Expand Up @@ -356,7 +356,7 @@ int ccv_matrix_equal(ccv_matrix_t* a, ccv_matrix_t* b)
{ \
for (j = 0; j < da->cols * ch; j++) \
{ \
if (fabs(__for_get(b_ptr, j) - __for_get(a_ptr, j)) > 1e-6) \
if (fabs(__for_get(b_ptr, j, 0) - __for_get(a_ptr, j, 0)) > 1e-6) \
return -1; \
} \
a_ptr += da->step; \
Expand Down Expand Up @@ -388,7 +388,7 @@ void ccv_slice(ccv_matrix_t* a, ccv_matrix_t** b, int y, int x, int rows, int co
{ \
for (j = 0; j < cols * ch; j++) \
{ \
__for_set(b_ptr, j, __for_get(a_ptr, j), 0); \
__for_set(b_ptr, j, __for_get(a_ptr, j, 0), 0); \
} \
a_ptr += da->step; \
b_ptr += db->step; \
Expand Down
2 changes: 1 addition & 1 deletion lib/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ all: libccv.a
clean:
rm *.o libccv.a

libccv.a: ccv_cache.o ccv_memory.o 3rdparty/sha1.o ccv_io.o ccv_numeric.o ccv_algebra.o ccv_util.o ccv_basic.o ccv_daisy.o ccv_sgf.o
libccv.a: ccv_cache.o ccv_memory.o 3rdparty/sha1.o ccv_io.o ccv_numeric.o ccv_algebra.o ccv_util.o ccv_basic.o ccv_daisy.o ccv_sgf.o ccv_sift.o
ar rcs $@ $^

%.o: %.c ccv.h
Expand Down
2 changes: 1 addition & 1 deletion test/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CC = clang
LDFLAGS = -L"../lib" -pthread -static -lccv -ljpeg -lpng -lfftw3 -lz -lgsl -lblas -lm # -lgslcblas
CXXFLAGS = -O3 -msse2 -Wall -I"../lib"

TARGETS = sobel_test hog_test cache_test sparse_matrix_test io_test filter_test algebra_test compress_matrix_test atan2_test resample_test sample_down_test daisy_test daisy_kmeans kmeans array_test group_test slice_test flip_test minimize_test gc_test blur_test
TARGETS = sobel_test hog_test cache_test sparse_matrix_test io_test filter_test algebra_test compress_matrix_test atan2_test resample_test sample_down_test daisy_test daisy_kmeans kmeans array_test group_test slice_test flip_test minimize_test gc_test blur_test sift_test

all: $(TARGETS)

Expand Down
27 changes: 27 additions & 0 deletions test/sift_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "ccv.h"
#include <sys/time.h>

unsigned int get_current_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

int main(int argc, char** argv)
{
int i, j;
ccv_dense_matrix_t* image = NULL;
ccv_unserialize(argv[1], &image, CCV_SERIAL_GRAY | CCV_SERIAL_ANY_FILE);
unsigned int elapsed_time = get_current_time();
ccv_sift_param_t param;
param.noctaves = 3;
param.nlevels = 5;
param.sigma = 1.2;
ccv_sift(image, param);
printf("elpased time : %d\n", get_current_time() - elapsed_time);
ccv_matrix_free(image);
ccv_garbage_collect();
return 0;
}

0 comments on commit 52618f9

Please sign in to comment.