|
[Sponsors] |
May 12, 2008, 03:54 |
Dynamic multidimensional array in C++?
|
#1 |
Guest
Posts: n/a
|
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.
|
|
May 12, 2008, 06:37 |
Re: Dynamic multidimensional array in C++?
|
#2 |
Guest
Posts: n/a
|
You can use a single pointer to access an array in C/C++ no matter how many dimensions using the construct like
float *myArrary; |
|
May 12, 2008, 07:42 |
Re: Dynamic multidimensional array in C++?
|
#3 |
Guest
Posts: n/a
|
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. |
|
May 12, 2008, 08:07 |
Re: Dynamic multidimensional array in C++?
|
#4 |
Guest
Posts: n/a
|
I have tried using vector class as dynamic array in my md program, but it was really very slow.
|
|
May 12, 2008, 08:12 |
Re: Dynamic multidimensional array in C++?
|
#5 |
Guest
Posts: n/a
|
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?
|
|
May 12, 2008, 09:41 |
Re: Dynamic multidimensional array in C++?
|
#6 |
Guest
Posts: n/a
|
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 ... |
|
May 12, 2008, 13:44 |
Re: Dynamic multidimensional array in C++?
|
#7 |
Guest
Posts: n/a
|
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. |
|
May 13, 2008, 03:07 |
Re: Dynamic multidimensional array in C++?
|
#8 |
Guest
Posts: n/a
|
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. |
|
May 13, 2008, 08:20 |
Re: Dynamic multidimensional array in C++?
|
#9 |
Guest
Posts: n/a
|
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. |
|
May 22, 2008, 22:15 |
Re: Dynamic multidimensional array in C++?
|
#10 |
Guest
Posts: n/a
|
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. |
|
May 22, 2008, 22:16 |
Re: Dynamic multidimensional array in C++?
|
#11 |
Guest
Posts: n/a
|
He asked about C not C++. May as well write this code in FORTRAN if you are just going to do that.
|
|
May 22, 2008, 22:16 |
Re: Dynamic multidimensional array in C++?
|
#12 |
Guest
Posts: n/a
|
Of course I mean he asked about C++, not C!
|
|
May 22, 2008, 22:17 |
Re: Dynamic multidimensional array in C++?
|
#13 |
Guest
Posts: n/a
|
Yes, what about a 5 dimensional array now....
|
|
May 22, 2008, 22:18 |
Re: Dynamic multidimensional array in C++?
|
#14 |
Guest
Posts: n/a
|
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.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Dynamic Mesh Problem. | Tom Clark | FLUENT | 10 | June 21, 2021 05:27 |
Dynamic Mesh on Pintle type injector. | herntan | FLUENT | 16 | September 4, 2020 09:27 |
Regarding Negative volume detected in Dynamic mesh | Vinay Morabad | FLUENT | 10 | December 16, 2015 01:31 |
Dynamic Mesh moving interface help | akash.iitb | FLUENT | 0 | August 24, 2010 00:53 |
Dynamic mesh + grid adapt = Crash! (Files included | BillH | FLUENT | 4 | July 24, 2007 16:31 |