From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: histc for gpu
Date: Thu, 20 Feb 2014 13:39:09 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 20
Message-ID: <le50ht$i1u$>
References: <lc1a97$6tt$>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1392903549 18494 (20 Feb 2014 13:39:09 GMT)
NNTP-Posting-Date: Thu, 20 Feb 2014 13:39:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: comp.soft-sys.matlab:809610

"Christian " <> wrote in message <lc1a97$6tt$>...
> Hi,
> I hope to use histc on CUDA, but it's not a supported function. So I tried a workaround because I'm actually just interested in the second output argument:
> [~,ind] = histc(x,x_grid);
> is more or less equivalent to the following code (except for points outside the grid).
> ind=ones(size(x));
> for j=1:length(x_grid)-1
>     ind(x>=x_grid(j) & x<x_grid(j+1)) = j;
> end
The best way is perhaps to sort x and x_grid concatenated together, which has an O(N*log(N)) complexity where N is numel(x)+ numel(xgrid). Furthermore the histc engine is possibly programmed in C and very optimal way.

You won't beat it with the trivial for-loop, even with CUDA.