Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: histc for gpu
Replies: 3   Last Post: Feb 20, 2014 8:39 AM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Edric Ellis Posts: 721 Registered: 12/7/04
Re: histc for gpu
Posted: Jan 27, 2014 5:06 AM
 Plain Text Reply

"Christian " <proechri@umich.edu> writes:

> Path: news.mathworks.com!not-for-mail
> Newsgroups: comp.soft-sys.matlab
> Subject: histc for gpu
> Date: Sat, 25 Jan 2014 21:28:07 +0000 (UTC)
> Organization: The MathWorks, Inc.
>
> 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
>
> But this latter code is much slower than histc, probably due to the loop. For instance, for my inputs the first code requires 0.03 seconds, the latter needs 1.8 seconds.
>
> Is it possible to speed up the second code so that I can still run it
> on CUDA?

I haven't profiled this, but you can run just the binning part of HISTC
on the GPU using arrayfun and up-level variables to access the bins:

%%%%%%%%%%%%%%%%%%%%
function test()

% Test inputs
q = gpuArray.randi([1 10], 1, 1000);
edges = 1:8;

% Implementation
nEdges = numel(edges);
function b = bindata(x)
b = 0;
if x == edges(nEdges)
b = nEdges;
else
for idx = 1:(nEdges-1)
if x >= edges(idx) && x < edges(idx + 1)
b = idx;
break;
end
end
end
end
b = arrayfun(@bindata, q);

% Check against the CPU
[~, bcheck] = histc(gather(q), edges);
assert(isequal(b, bcheck));
end
%%%%%%%%%%%%%%%%%%%%

Cheers,

Edric.

Date Subject Author
1/25/14 Christian
1/27/14 Edric Ellis
2/15/14 Christian
2/20/14 Bruno Luong

© The Math Forum at NCTM 1994-2018. All Rights Reserved.