How is af_array data arranged in memory?

39 views
Skip to first unread message

Alex Shroyer

unread,
Dec 4, 2021, 10:37:18 PM12/4/21
to ArrayFire Users
Hi, 

I'm trying to bind some ArrayFire functions with an interpreted language (which has a C FFI) and wondering how to get data in and out.

Currently I'm just focusing on getting data *out* of ArrayFire.
I'm using functions like af_create_handle and af_identity to generate data, but I do not know how to interpret the resulting pointers.

For example, if I want to transfer a 2x3 matrix of 64-bit ints back into my interpreted language, is there a specific function I should use to put the data into some kind of canonical form, or alternatively is there documentation about the array/struct layout so that I can deserialize it on the interpreter side?

Thanks,
Alex

uma...@gmail.com

unread,
Dec 4, 2021, 10:50:17 PM12/4/21
to ArrayFire Users
You can use af_get_data_ptr to transfer data to host memory. The pointer returned will be in column major order.

So if your matrix looks like this:
1 2 3
4 5 6

The data pointer will return an array that will contain the following values:
1 4 2 5 3 6

Best,

Umar

John Melonakos

unread,
Dec 4, 2021, 11:04:40 PM12/4/21
to Alex Shroyer, ArrayFire Users
You can use af_get_data_ptr to transfer data to host memory. The pointer returned will be in column major order. 

So if your matrix looks like this:
1 2 3
4 5 6

The data pointer will return an array that will contain the following values:
1 4 2 5 3 6

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/arrayfire-users/dd9ffeb6-a60e-4d54-b981-858c6590b0c9n%40googlegroups.com.
--
Sent from Gmail Mobile

Alex Shroyer

unread,
Dec 5, 2021, 12:02:06 PM12/5/21
to ArrayFire Users
Thanks for the tip!

I'm trying this (using python-like pseudocode)
1. p1 = host_allocate(num_bytes=16)
2. h1 = ffi(af_create_handle(p1,  ndims=2, dims=[4,4],  'f64'))
3. i1 = ffi(af_identity(h1, ndims=2, dims=[4,4], 'f64'))
4. result = ffi(af_get_data_ptr(p1, h1))

The problem I get with the above is that result is 0 (null pointer), so it appears that af_get_data_ptr did not succeed, even though AF_ERROR == AF_SUCCESS for all ffi calls.
What does it mean that af_get_data_ptr sets the destination pointer to 0?

Alex Shroyer

unread,
Dec 5, 2021, 12:12:34 PM12/5/21
to ArrayFire Users
Oh, I found my error! 
I should have used af_get_data_ptr(p1, i1).

Thanks again,
Alex

John Melonakos

unread,
Dec 5, 2021, 12:18:52 PM12/5/21
to Alex Shroyer, ArrayFire Users
Wonderful, let us know if you have any other questions.


Reply all
Reply to author
Forward
0 new messages