CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Dynamic multidimensional array in C++? (https://www.cfd-online.com/Forums/main/15179-dynamic-multidimensional-array-c.html)

Chen Zhi May 12, 2008 02:54

Dynamic multidimensional array in C++?
 
It's very easy to setup a dynamic multidimensional array in Fortran 90. But, how to do that in c++? Of course, it also has be effecient.


javaid May 12, 2008 05:37

Re: Dynamic multidimensional array in C++?
 
You can use a single pointer to access an array in C/C++ no matter how many dimensions using the construct like

float *myArrary;

Steve May 12, 2008 06:42

Re: Dynamic multidimensional array in C++?
 
Think of using STL vectors. A multi-D array is a vector of vectors of vectors of ...

A half decent compiler will understand what you are doing.

Chen Zhi May 12, 2008 07:07

Re: Dynamic multidimensional array in C++?
 
I have tried using vector class as dynamic array in my md program, but it was really very slow.

Chen Zhi May 12, 2008 07:12

Re: Dynamic multidimensional array in C++?
 
Do you mean if I want to allocate a array with size 100x100 then, I allocate a one dimentional array with size 10000, and let a pointer point to it?

rt May 12, 2008 08:41

Re: Dynamic multidimensional array in C++?
 
Assme you need a(nx,ny) of type double

First you should allocate an array include double pointer (double *), then assign for each entry an array, i.e., something like this:

double **a;

a = malloc(nx * sizeof(double *));

loop on [0,nx) a[i] = malloc(ny * sizeof(double));

then you could access to array a like this a[i][j],

but since you need performance in CFD i recommend vectorized array, ie., you allocate an array with dimension nx*ny and access to entry like this (it is faster),

a(i,j) ~ a[i+j*imax]

assumnig entries are started from zero.

hope this helps ...


Harish May 12, 2008 12:44

Re: Dynamic multidimensional array in C++?
 
The way I do is

double **x

x=new double*[imax]

for(i=0;i<=imax-1;++i) x[i]=new double[jmax]

This would allow you to access the arrays like a static array.

Alexey May 13, 2008 02:07

Re: Dynamic multidimensional array in C++?
 
Yes, the last case is the best end efficient. For example, 3D linearized array with three indexes (i,j,k) looks like:

a(i,j,k) = a[(k*jmax + j)*imax + i];

and so on. I compared this way with Fortran 90 direct definition a(i,j,k). C++ code proved to be more faster.


Jed May 13, 2008 07:20

Re: Dynamic multidimensional array in C++?
 
The second option is really the only reasonable way to allocate the memory. In C, the somewhat clunky indexing is your only choice. Note that the C index a[i][j] becomes a[i*jmax+j] unless you actually wanted to change to Fortran-style (column-major) ordering. In C++, you can use boost::multi_array or blitz++ to give you nicer indexing.

Think of the way you will be traversing the array when you choose the order of the indices. This can easily make an order of magnitude performance difference.

HekLeR May 22, 2008 21:15

Re: Dynamic multidimensional array in C++?
 
yep, that is what he means. Sort of an obtuse way of doing things though isn't it. So, the suggestion is not so useful.

I recommend you look at how this is done with Boost, free Pooma, Blitz++, etc...

With scientific C++ you need to think hard about the concepts you require first, then implement the class.

It's great if you can reuse something like the vector STL class because they have gone through a lot of thought for you.


HekLeR May 22, 2008 21:16

Re: Dynamic multidimensional array in C++?
 
He asked about C not C++. May as well write this code in FORTRAN if you are just going to do that.

HekLeR May 22, 2008 21:16

Re: Dynamic multidimensional array in C++?
 
Of course I mean he asked about C++, not C!

HekLeR May 22, 2008 21:17

Re: Dynamic multidimensional array in C++?
 
Yes, what about a 5 dimensional array now....

HekLeR May 22, 2008 21:18

Re: Dynamic multidimensional array in C++?
 
Then you did not do an apples to apples comparison. If you compute the indices in FORTRAN, as you suggest here, and use a 1D array it would be just as fast in FORTRAN.


All times are GMT -4. The time now is 22:24.