Extracting range of elements from 3D array

169 views
Skip to first unread message

Jiayun Liu

unread,
Aug 17, 2023, 5:53:13 AM8/17/23
to ArrayFire Users
Hi,

I have a 3D array Main and a 2D array idx that holds the index of the max of Main along the third dimension. I want to create another 3D array to extract the max and +-2 index from that.is there a good way to do this? It seems I need the 3D version of approx2 to do something similar as in the tutorial but for 3D array.

Thanks in advance

John Melonakos

unread,
Aug 17, 2023, 2:21:54 PM8/17/23
to ArrayFire Users
I am not sure what you mean by "I want to create another 3D array to extract the max and +-2 index from that." Can you share the code you are trying to implement? Thanks!
-John

Jiayun Liu

unread,
Aug 17, 2023, 10:42:53 PM8/17/23
to ArrayFire Users
Lets say we have an array A with size NxMxO and we look at a specific index say A(0,0,span) and I find the max value and index (Idx). On this index I want to extract A(0,0,seq(Idx-2,Idx+2) and copy to another NxMx5 array B(0,0,seq(0,4)). I want to repeat this for the remaining NxM index to populate my B array. Normally I will specify three NxMx5 arrays and do B=A(X,Y,Z) in other language like matlab or python but it seems that I can't do this with arrayfire. Please let me know if this is clearer.

Thanks!

Jiayun Liu

unread,
Aug 18, 2023, 5:47:03 AM8/18/23
to ArrayFire Users
I was also doing some testing with time taken for fft. The calculation below takes around 300 ms but matlab takes less than 1 ms. Did I do something wrong with the timing or initialization?

        af::array ftA = af::constant(0, 500, 1);
        auto startgpu = std::chrono::high_resolution_clock::now();

        ftA = af::fft(af::constant(1, 500, 1));

        auto endgpu = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> gpuduration = endgpu - startgpu;
        std::cout << "gpu convolution time: " << gpuduration.count() << " seconds" << std::endl;   

John Melonakos

unread,
Aug 18, 2023, 11:35:39 PM8/18/23
to Jiayun Liu, ArrayFire Users
We will be releasing upgrading timing documentation with ArrayFire v3.9. Have you seen these timing functions?  https://arrayfire.org/docs/timing.htm#gsc.tab=0

But the first run exercises the JIT while subsequent runs cache the code generated previously and avoid duplicating that compile step. It's always best to warm up the code with one run. Then time subsequent runs to see if how things perform once the compile time is removed via the cached kernels.

--
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/82fa0f33-f101-4a9d-89b6-9d3f7d052a2cn%40googlegroups.com.

Jiayun Liu

unread,
Aug 19, 2023, 8:13:51 PM8/19/23
to ArrayFire Users
Hi,

I have tried using the timer function but the time taken is still around 300-400 ms and this is the time taken after I ran the code a few times.

Regarding the previous question, are there any ways to achieve what I wanted without using for loop?

Message has been deleted
Message has been deleted

John Melonakos

unread,
Aug 22, 2023, 4:02:35 PM8/22/23
to ArrayFire Users
I understand your question now. Since the extracted data will not be in a local neighborhood, it is impossible to pull out of the structure via block indexing. You can create a linear array of all the correct 5-element neighborhood indices, then extract the correct elements into another linear array, and finally reformat the linear array into the 3-D array you desire. However, this will likely not perform well. I am not aware of a better way to accomplish this.

Jiayun Liu

unread,
Aug 24, 2023, 1:37:00 AM8/24/23
to ArrayFire Users
Thanks. Regarding the slow fft speed problem, it seems that the fft is only slow if I use lower compute capability GPU and the corresponding arrayfire binary. There is no problem if I use GPU with cc=8.6 and CUDA 12.0 compatible arrayfire.

John Melonakos

unread,
Aug 24, 2023, 12:37:17 PM8/24/23
to Jiayun Liu, ArrayFire Users
Ah, that makes sense. Thank you for the update.

You received this message because you are subscribed to a topic in the Google Groups "ArrayFire Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/arrayfire-users/TvpWhYerRhA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to arrayfire-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/arrayfire-users/34568e44-4301-4799-91a9-194693b00b9cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages