% AUTHOR: Nikos Sismanis
% Date: Mar 2012

clear all
close all

N = 2^15;
d = 128;
q = 2^7;
k = 64;
streams = 1;

sprintf('Testing with 128 dimensional random vectors, single precision')

%corpusfile = sprintf('../data/TestCorpusSingle.bin');
%queryfile = sprintf('../data/TestQueriesSingle.bin');

%fid = fopen(corpusfile);
%data = single(fread(fid, [d, N], 'single'));
%fclose(fid);

%fid = fopen(queryfile);
%query = single(fread(fid, [d, q], 'single'));
%fclose(fid);

data = single(rand(d, N));
query = single(rand(d, q));

%data = single(rand(d, N));
%query = single(rand(d, N));

[distBF idxBF] = knn(query, data, k);


[distBS idxBS timeBS] = gpuknnBitonic(query, data, k, streams);

[distHS idxHS timeHS] = gpuknnHeap(query, data, k, streams);

ns = abs(max(distBF(:)));
er(1) = norm(distBS(:) - distBF(:)) / ns;
er(2) = norm(distHS(:) - distBF(:)) / ns;

if all(er < 1.0e-02)
sprintf('PASS\n')
else
sprintf('FAIL\n')
end

sprintf('Testing with 1024 dimensional random vectors, single precision')

d = 1024;

data = single(rand(d, N));
query = single(rand(d, q));

[distBF idxBF] = knn(query, data, k);

[distBS idxBS timeBS] = gpuknnBitonic(query, data, k, streams);

[distHS idxHS timeHS] = gpuknnHeap(query, data, k, streams);

ns = abs(max(distBF(:)));
er(1) = norm(distBS(:) - distBF(:)) / ns;
er(2) = norm(distHS(:) - distBF(:)) / ns;

if all(er < 1.0e-02)
sprintf('PASS\n')
else
sprintf('FAIL\n')
end

sprintf('Testing with 2048 dimensional random vectors, single precision')

d = 2048;

data = single(rand(d, N));
query = single(rand(d, q));

[distBF idxBF] = knn(query, data, k);

[distBS idxBS timeBS] = gpuknnBitonic(query, data, k, streams);

[distHS idxHS timeHS] = gpuknnHeap(query, data, k, streams);

ns = abs(max(distBF(:)));
er(1) = norm(distBS(:) - distBF(:)) / ns;
er(2) = norm(distHS(:) - distBF(:)) / ns;

if all(er < 1.0e-02)
sprintf('PASS\n')
else
sprintf('FAIL\n')
end

