solve the linear equation

I am trying to solve the linear equation A.x=C. I created the matrix as shown below to find the x. When I increase the dimension (n) to for example n=100000, I get an error as:
"Unhandled exception at at 0x778EDDC2 in Project7.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00A4F65C."

but the code works for n<1000.

Does anyone have any idea to solve this error or any other fast method to solve this linear equation?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	int i,n=100000,m=100;
	float **A, *C;

	A=new float*[n*m];      
	for(i=0;i<n*m;i++)
		A[i]=new float[n*m];
	
	C=new float[n*m];
.
.
...
return 0;

}
Last edited on
First you create an array of ten million pointers, and then for each of those 10 million pointers you create an array of 10 million floats, thus requiring a total memory of 364 terabytes.

Is x a vector, or a matrix of NxM? If A really is a 4D matrix of NxMxNxM then you'll need to use a sparse matrix. No way to store that in flat memory.
I don't understand the sizes you're making your matrices.

Okay, so n = 100,000, m = 100.

You have a for loop that runs (n*m) times, or 10,000,000 times.
Each of those 10,000,000 times, you create an array of length 10,000,000.

10,000,000 * 10,000,000 = 1014.

1014 bytes is about 100,000 gigabytes. Multiple that by 4 most likely, due to sizeof(int). There no way your computer can handle 400,000 gigabytes of memory unless it's some sort of supercomputer.

__________________________________________

You should only need to allocate one or two N*M sized arrays to be used as matrices.
One 100000x100 array is only about 40 MB.
Last edited on
yes, x could be a vector. and A is a spars matrix with a dimension of n*m in row and n*m in column.
so how can I solve it?
Last edited on
Actually in Matrix A which is spars, I need to keep track of a relatively small number of values (usually a value of 1, and in rare cases more than 1) in a sea of zeros in the matrix (multidimensional array).
is there any solution to create this matrix?
You should use a linear algebra library that implements sparse matrices.
If this is related to your other thread @morteza naji, this matrix is probably banded. Let me guess: it has five non-zero bands (so requires 5n storage for each time level). Is that so?
to use the linear algebra library, I should first create a matrix A and then I can use the library. but the problem is that I cannot create the matrix because as I said its dimension is too large
As @helios and @ganado have shown you, you cannot afford to store a matrix of that size, most of whose elements are zero. It is sparse and, if it is derived from your other thread, it will not just be sparse but it will have a banded structure. If it does come from your other thread then each node in your mesh will be connected to two nodes in each direction, so 5 bands per time level, and 2 time levels.
Topic archived. No new replies allowed.