How to change the size of an existed array?

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

Moderator: pavanky

How to change the size of an existed array?

Postby brumal » Mon Apr 21, 2014 8:40 am

I am coding on C++.
I found it difficult to "resize" an array. I describe it as follows:

///////////////////////////////////////////////////////
array A;
...
A = array(100,100); // this operation failed due to size mis-match.
///////////////////////////////////////////////////////

I know it can be done via
array A = array(100,100);
But sometimes it is convenient to make a declaration firstly and then dynamically change its size.

Is there a workaround for this?
brumal
 
Posts: 2
Joined: Mon Apr 21, 2014 8:25 am

Re: How to change the size of an existed array?

Postby shehzan » Mon Apr 21, 2014 6:00 pm

Hi,

There are multiple ways to resize an array. It depends on what you really want to get done. I am listing some ways below.

Before I get started listing the methods, A = array(100, 100) does not work because there is no constructor to handle this case.

1) Create a new array with a constant "c" in all elements of size (x,y) and assign it to A
Code: Select all
A = constant(c, x, y);


2) Create a new array for random numbers (f32 type) of size (x,y) and assign it to A.
Code: Select all
A = randu(x, y);


3) Modify the dimensions of the array such that the total number of elements remains same. This is a noop. No memory is copied unlike the operations before.
Code: Select all
A = moddim(A, new_x, new_y);             // where new_x * new_y = A.elements();


If you initialize an array without declaring size, no memory is allocated. To allocate memory after declaring an array, it is best to use constant().

See ArrayFire documentation for more functions: http://www.accelereyes.com/arrayfire/c/
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm

Re: How to change the size of an existed array?

Postby brumal » Tue Apr 22, 2014 1:13 am

Thank you!

Found the problem.

I was debugging in vs2012 and found that
Code: Select all
array A;
A = constant(0.f,100,100);
// A.m_dims is [1,1,1,1] after this operation
// but A.dims() actually returns [100,100,1,1]

It seems A.m_dims gives wrong information.
brumal
 
Posts: 2
Joined: Mon Apr 21, 2014 8:25 am

Re: How to change the size of an existed array?

Postby shehzan » Tue Apr 22, 2014 10:24 am

Great.

The VS intellisense debugger has not been worked on from our side. It still needs more work.
You can use other debugging output utilities such as stats, top, print, printdims.
Stats prints min, mean max of an array.
Top prints the first 8x8 elements of an array.
Print prints the dimensions as well as the entire array.
Printdims prints only the dimensions of the array.

Check the header file arrayfire/include/af/utils.h for more. Note, you will need to include this file explicitly in your code as
Code: Select all
#include<af/utils.h>
.
----
Shehzan
Developer
AccelerEyes
User avatar
shehzan
 
Posts: 121
Joined: Tue Feb 12, 2013 7:20 pm


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

cron