Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GPUArray problem - same code, different results

Subject: GPUArray problem - same code, different results

From: Patrick

Date: 14 Sep, 2012 18:10:08

Message: 1 of 3

I am trying to do a linear interpolation that will return the same results as interp1. From Mathworks interp1 code, I pulled out the following

function yi = glininterp(x, y, xi)
     h = diff(x);
     n = size(y, 1);
     [~,k] = histc(gather(xi), gather(x));
     k = gpuArray(single(k));
     k(xi < x(1) | ~isfinite(x)) = 1;
     k(xi >= x(n)) = n-1;
     s = (xi - x(k))./h(k);
     s = s';
     
     s_rep = repmat(s, 1, size(y, 2));
     yi = y(k, :) + s_rep.*(y(k+1, :) - y(k, :));


This does not produce identical results results as interp1, when run on the same array. In some cases, the difference is zero between interp1, in others the results are off by about 1e-7 to 1e-9

But if make sure all the inputs are standard arrays (and not GPU arrays), and remove the line k = gpuArray(single(k)), so the above code would be run on the CPU and not GPU, it does produce the same results.

Any ideas? I thought it might have been a single/double issue but I made sure all the inputs were single. I'm wondering if my hardware is failing, I'm running a Nvidia GTS 450. There is another card I can try ( a Tesla).

Subject: GPUArray problem - same code, different results

From: Matt J

Date: 14 Sep, 2012 19:46:08

Message: 2 of 3

"Patrick " <praphael@gmail.com> wrote in message <k2vru0$ebf$1@newscl01ah.mathworks.com>...
> I am trying to do a linear interpolation that will return the same results as interp1. From Mathworks interp1 code, I pulled out the following
>
> function yi = glininterp(x, y, xi)
> h = diff(x);
> n = size(y, 1);
> [~,k] = histc(gather(xi), gather(x));
> k = gpuArray(single(k));
> k(xi < x(1) | ~isfinite(x)) = 1;
> k(xi >= x(n)) = n-1;
> s = (xi - x(k))./h(k);
> s = s';
>
> s_rep = repmat(s, 1, size(y, 2));
> yi = y(k, :) + s_rep.*(y(k+1, :) - y(k, :));
>
>
> This does not produce identical results results as interp1, when run on the same array. In some cases, the difference is zero between interp1, in others the results are off by about 1e-7 to 1e-9
>
> But if make sure all the inputs are standard arrays (and not GPU arrays), and remove the line k = gpuArray(single(k)), so the above code would be run on the CPU and not GPU, it does produce the same results.
>
> Any ideas? I thought it might have been a single/double issue but I made sure all the inputs were single. I'm wondering if my hardware is failing, I'm running a Nvidia GTS 450. There is another card I can try ( a Tesla).
===============


I'm amazed it produces identical results to INTERP1. I can't find your code fragment anywhere in interp1.m. Anyway, I do get identical results, both with and without k=gpuArray(single(k)) and they agree with interp1. I'm using a GTX 580.

I can't imagine the discrepancy you're seeing has anything to do with the GPU, however. The code you've shown will manipulate k on the GPU, but since all other variables reside on the CPU, yi will be computed on the CPU in all the cases you've described.
 

Subject: GPUArray problem - same code, different results

From: Patrick

Date: 14 Sep, 2012 20:51:08

Message: 3 of 3

"Matt J" wrote in message <k301i0$7u2$1@newscl01ah.mathworks.com>...

>
>
> I'm amazed it produces identical results to INTERP1. I can't find your code fragment anywhere in interp1.m. Anyway, I do get identical results, both with and without k=gpuArray(single(k)) and they agree with interp1. I'm using a GTX 580.
>
> I can't imagine the discrepancy you're seeing has anything to do with the GPU, however. The code you've shown will manipulate k on the GPU, but since all other variables reside on the CPU, yi will be computed on the CPU in all the cases you've described.
>

Thanks for your reply. The code was not ripped straight out of interp1, but the essential lines of code were patched together . The code interp1.m has valid data checks and deals with more cases, like equally spaced points and cubic/spline inerpolation, and those didn't apply in our circumstance, so I didn't bother to include those to keep things as simple as possible.

When I get a chance, I will check I get the same result on the Tesla C2075, which is the target anyway.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us