CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Dynamic multidimensional array in C++?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 12, 2008, 03:54
Default Dynamic multidimensional array in C++?
  #1
Chen Zhi
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.

  Reply With Quote

Old   May 12, 2008, 06:37
Default Re: Dynamic multidimensional array in C++?
  #2
javaid
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;
  Reply With Quote

Old   May 12, 2008, 07:42
Default Re: Dynamic multidimensional array in C++?
  #3
Steve
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.
  Reply With Quote

Old   May 12, 2008, 08:07
Default Re: Dynamic multidimensional array in C++?
  #4
Chen Zhi
Guest
 
Posts: n/a
I have tried using vector class as dynamic array in my md program, but it was really very slow.
  Reply With Quote

Old   May 12, 2008, 08:12
Default Re: Dynamic multidimensional array in C++?
  #5
Chen Zhi
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?
  Reply With Quote

Old   May 12, 2008, 09:41
Default Re: Dynamic multidimensional array in C++?
  #6
rt
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 ...

  Reply With Quote

Old   May 12, 2008, 13:44
Default Re: Dynamic multidimensional array in C++?
  #7
Harish
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.
  Reply With Quote

Old   May 13, 2008, 03:07
Default Re: Dynamic multidimensional array in C++?
  #8
Alexey
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.

  Reply With Quote

Old   May 13, 2008, 08:20
Default Re: Dynamic multidimensional array in C++?
  #9
Jed
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.
  Reply With Quote

Old   May 22, 2008, 22:15
Default Re: Dynamic multidimensional array in C++?
  #10
HekLeR
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.

  Reply With Quote

Old   May 22, 2008, 22:16
Default Re: Dynamic multidimensional array in C++?
  #11
HekLeR
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.
  Reply With Quote

Old   May 22, 2008, 22:16
Default Re: Dynamic multidimensional array in C++?
  #12
HekLeR
Guest
 
Posts: n/a
Of course I mean he asked about C++, not C!
  Reply With Quote

Old   May 22, 2008, 22:17
Default Re: Dynamic multidimensional array in C++?
  #13
HekLeR
Guest
 
Posts: n/a
Yes, what about a 5 dimensional array now....
  Reply With Quote

Old   May 22, 2008, 22:18
Default Re: Dynamic multidimensional array in C++?
  #14
HekLeR
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.
  Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


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


All times are GMT -4. The time now is 02:54.