af::matmul precision problem

58 views
Skip to first unread message

Patrick Zhou

unread,
Nov 3, 2015, 8:45:19 AM11/3/15
to ArrayFire Users
Hi All,

I am first time using arrayfire and encountered the following matmul precsion problem.

In the arrayfire helloworld example, I inserted the following code:

int device = argc > 1 ? atoi(argv[1]) : 0;

af::setDevice(device);

af::info();

af::array afaJp = af::randu (8, 9216, f32);

af::array afaFp = af::randu (9216, 1, f32);

float afResults0[8*9216];

afaJp.host(afResults0);

float afResults1[9216];

afaFp.host(afResults1);

af::array twoMul = af::matmul(afaJp, afaFp);

float afResults2[8];

twoMul.host(afResults2);

float check = 0.0f;

for (int i = 0; i < 9216; i++)

{

//check += Jp.at<float>(0,i)*Fp.at<float>(i,0);

check += afResults0[i]*afResults1[i];

}

std::cout << "arrayfire result: " << afResults2[0] << " and CPU result is " << check << std::endl;



and the print out info are as below:


WARNING: af_set_device not supported for CPU
ArrayFire v3.1.3 (CPU, 64-bit Windows, build 35c89f5)
[0] Intel:        Intel(R) Xeon(R) CPU E5-1620 0 @ 3.60GHz Max threads(8)
arrayfire result: 2319.69 and CPU result is 2336.96



The result is a bit off between two. I am running visual studio 2015 on Windows 7 x64 and also tried visual studio 2012 same result. Visual studio project is CPU_x64 and release as set default in the helloworld example.


Any idea?

Thanks,

Patrick.

Shehzan Mohammed

unread,
Nov 3, 2015, 9:16:31 AM11/3/15
to ArrayFire Users
Patrick,

The CPU "check" multiplication that you are doing is incorrect by indexing.
ArrayFire uses column-major indexing, which means an 8x9216 array is 8 rows x 9216 cols => The first col is 0-7, second col is 8-15 and so on.

The correct indexing for your case would be:
check += afResults0[8 * i]*afResults1[i];

-Shehzan

Patrick Zhou

unread,
Nov 3, 2015, 9:54:24 AM11/3/15
to ArrayFire Users
I see. Any specific reason doing column-major indexing in arrayfire for openCV did it opposite way? Thanks, -Patrick.

Pavan Yalamanchili

unread,
Nov 3, 2015, 9:57:54 AM11/3/15
to Patrick Zhou, ArrayFire Users
HI Patrick,

Many Linear Algebra libraries default to column major format. Some of them do not even support row major formats. This is one of the reasons we use column major format for arrayfire.

--
You received this message because you are subscribed to the Google Groups "ArrayFire Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arrayfire-use...@googlegroups.com.
To post to this group, send email to arrayfi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/arrayfire-users/4d0b1c61-41ab-4538-adac-36775f47eb32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Patrick Zhou

unread,
Nov 3, 2015, 10:13:41 AM11/3/15
to ArrayFire Users, zxpa...@gmail.com
Thanks Pavan. I am mix-using OpenCV and arrayfire at this point. Hopefully I will eventually convert all the codes toward arrayfire for performance but I still looking for the counterpart for the cv::remap function in arrayfire in case you know anything related. -Patrick.

Pavan Yalamanchili

unread,
Nov 3, 2015, 10:17:15 AM11/3/15
to Patrick Zhou, ArrayFire Users
Hi Patrick,

If you want, you can "mimic" row-major matrix multiplcation by using "af::matmulTT(B, A)" instead of "af::matmul(A, B)". This can decrease the number of transposes required. 

Patrick Zhou

unread,
Nov 3, 2015, 10:25:18 AM11/3/15
to ArrayFire Users, zxpa...@gmail.com
Yes Pavan.  I am going to basically apply transpose to all my algorithms and make the transpose at the last if necessary. In some cases, the result is of shape (n, 1) or (1, n) then the transpose can be omitted.


Thanks,
Patrick.

Pavan Yalamanchili

unread,
Nov 3, 2015, 10:30:51 AM11/3/15
to Patrick Zhou, ArrayFire Users

Patrick Zhou

unread,
Nov 3, 2015, 10:35:39 AM11/3/15
to ArrayFire Users, zxpa...@gmail.com
Thanks. Will try out.
Reply all
Reply to author
Forward
0 new messages