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

C++ Memory Management

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 24, 2018, 21:42
Default C++ Memory Management
  #1
nsi
New Member
 
Join Date: Oct 2017
Posts: 7
Rep Power: 8
nsi is on a distinguished road
Dear CFD friends!

I am writing a CFD code solving 3D Euler equations (compressible, inviscid flow). The first problem is, I am not allowed to generate a big matrix to store the conserved variables, if I am having a 1000x1000x1000 Cartesian structured mesh.

Here is what I have tried.

(1) Using "new" to generate a dynamic array;

new.JPG

(2) Using "vector" to generate a dynamic array;

vector.JPG

(3) Generating a static array variable.

static.JPG

But none of them worked.

It makes me think that it is necessary that I change the data structure/organization. But anyway I need a matrix like this to store the conserved variables at each time step. Any idea will be much appreciated!!!

N. Si
nsi is offline   Reply With Quote

Old   March 25, 2018, 03:19
Default
  #2
New Member
 
John Peralta
Join Date: Mar 2018
Location: Paris
Posts: 2
Rep Power: 0
Peralta is on a distinguished road
Have you calculated the required RAM to store an array of this size?
Peralta is offline   Reply With Quote

Old   March 25, 2018, 07:50
Default
  #3
Super Moderator
 
Praveen. C
Join Date: Mar 2009
Location: Bangalore
Posts: 342
Blog Entries: 6
Rep Power: 18
praveen is on a distinguished road
All of the methods you tried are bad ways to use multi-d array in c/c++.

One way is to store it as a one-d array and wrap the indexing (I haven't checked the index wrapping, but you see the general idea)

Code:
#include<iostream>
  
#define u(i,j,k)  u_[(i) + (j)*imax + (k)*imax*jmax]

using namespace std;

int main()
{
   const int imax = 1000;
   const int jmax = 1000;
   const int kmax = 1000;

   double* u_ = new double[imax*jmax*kmax];
   u(0,0,0) = 100.0;
   cout << u(0,0,0) << endl;
}
Another way with multi-d indexing is

Code:
#include<iostream>
using namespace std;

int main()
{
   const int imax = 1000;
   const int jmax = 1000;
   const int kmax = 1000;

   double (*u)[jmax][kmax];

   u = new double[imax][jmax][kmax];
   u[0][0][0] = 100.0;
   cout << u[0][0][0] << endl;
}
praveen is offline   Reply With Quote

Old   March 27, 2018, 10:03
Default
  #4
New Member
 
Biswajit Ghosh
Join Date: Oct 2014
Location: Durgapur, India
Posts: 21
Rep Power: 11
Alisha is on a distinguished road
Using vector is a good option. Additionally I would say, as you are into scientific computing, consider using some reliable array library. I personally like blitz++
Alisha is offline   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
Lenovo C30 memory configuration and discussions with Lenovo matthewe Hardware 3 October 17, 2013 10:23
Memory management incongruence Flavio CFX 5 November 1, 2006 12:18
CFX CPU time & real time Nick Strantzias CFX 8 July 23, 2006 17:50
memory management under unix system nicolas Siemens 22 January 30, 2006 22:40
memory management! Tingguang Ma Main CFD Forum 3 October 25, 2000 08:35


All times are GMT -4. The time now is 11:23.