Strange result between Arrayfire and Matlab?

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Strange result between Arrayfire and Matlab?

Postby huahongquan2007 » Thu May 08, 2014 3:57 am

Hi,
I want to compute sum and mean of a vector ( I attached the data). It is a 76x1 array.
Here is my code
Code: Select all
        array care = rX(span,0);    // this is the 76x1 array
   float * t = care.host<float>();
   float mysum = 0;
   for( auto i = 0 ; i < care.dims(0) ; i++)
   {
      mysum = mysum + t[i];
   }
   cout << "MySum : " << mysum << endl; // result =  -0.000274658
   print(sum(care)); // result =  0.0004

Matlab result = -0.0023

You can see that I get 3 different result for computing SUM and I don't know where i am wrong. The same situation for MEAN function.
Please help.
Attachments
data.txt
(651 Bytes) Downloaded 455 times
huahongquan2007
 
Posts: 10
Joined: Fri May 02, 2014 1:56 pm

Re: Strange result between Arrayfire and Matlab?

Postby shehzan » Thu May 08, 2014 11:32 am

Hi

I've tested out the data you have sent out, and ran some tests. Here are my findings:

[*] The sum computed on host using the for loop is 0.001740
[*] The sum computed on device is 0.00227
[*] The sum computed using a calculation tool is 0.002350
[*] But when you use double as data type, all the tests come out to 0.002350.
[*] If you use floats everywhere, and compute mysum as a double, the host result matches the ArrayFire result.

Having drawn these conclusions, I think it is quite certainly that some of the values cannot be stored in the IEEE float format and that is what is causing the issue.

Using doubles is your choice, but ArrayFire should get you the right result irrespective of float or double.

Mean is also going off for the same reason since mean calls sum().

PS. I have not been able to match the printed results that you have written in the comments. The results I received are above in the bullet points. Also note that print(array) rounds off results to 4 decimal points. So if you want 6 decimal points, then copy it to host and print it out.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: Strange result between Arrayfire and Matlab?

Postby huahongquan2007 » Fri May 09, 2014 12:44 am

Thank you very much. :D
huahongquan2007
 
Posts: 10
Joined: Fri May 02, 2014 1:56 pm


Return to [archive-commercial] Programming & Development with ArrayFire

cron