473,412 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Problem with big matrices

Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:
#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[i][j] = (double)i+j;

return EXIT_SUCCESS;
}
In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?

Thanks a lot in advance!

Diego Andres
Nov 14 '05 #1
4 2084
Diego Andres Alvarez Marin wrote:
Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:
#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[i][j] = (double)i+j;

return EXIT_SUCCESS;
}
In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

The array is M*N*sizeof(double) bytes, which is 2000*2000*8 == 32MB.
That isn't much, but a lot if you put it on the stack.

How can I allocate this matrix?
Maybe allocate it from heap or make it static?

Boa


Thanks a lot in advance!

Diego Andres

Nov 14 '05 #2
Diego Andres Alvarez Marin wrote:

Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:

#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[i][j] = (double)i+j;

return EXIT_SUCCESS;
}

In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?

#include <stdlib.h>
#include <stdio.h>

#define M 2001
#define N 2000

int main(void)
{
int i, j;
double (*A)[N];

A = malloc(M * sizeof *A);
if (A == NULL) {
fputs("malloc problem\n", stderr);
return EXIT_FAILURE;
}
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
A[i][j] = (double)i + j;
}
}
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("%f\n", A[i][j]);
}
}
free(A);
return 0;
}

--
pete
Nov 14 '05 #3
There are probably some limits in the stack size
of the OS you are running on.

Under windows, for instance, stack size is by default 1MB only.

Using lcc-win32 you can increase the stack size at link time.

I compiled your program with
lc tmat.c -stack-reserve 40000000

and it run without any problems.

By the way, your matrix is 2000 * 2000 * 8 == 32MB, not 16.

Maybe there are compiler specific flags to increase the stack size
in your compiler documentation.

jacob
Nov 14 '05 #4
di****************@lycos.co.uk (Diego Andres Alvarez Marin) wrote in message news:<55**************************@posting.google. com>...
Hi!

I have problems with memory allocation of matrices bigger than 4 Mb.

So, here is the example:
#include <stdlib.h>
#include <stdio.h>

#define M 2000
#define N 2000

/*
Compile with:
gcc -Wall -O3 crash.c -o crash
*/

int main(){
int i,j;
double A[M][N];
for (i=0; i<M; i++) for (j=0; j<N; j++) A[i][j] = (double)i+j;

return EXIT_SUCCESS;
}
In the command line:

[daa@ysh06-8-13 r4]$ gcc -Wall -O3 crash.c -o crash
[daa@ysh06-8-13 r4]$ ./crash
Segmentation fault
[daa@ysh06-8-13 r4]$

My PC has 512 Mb RAM and this matrix has only 16 Mb.

How can I allocate this matrix?

Thanks a lot in advance!

Diego Andres


First, it is probably 2000*2000*8 for 32 MBS, since they are doubles.
Now 32MB is not that much, however this is going right on the stack
and there are limits set by the compiler to what that max is.

You can
1) Look into compiler options to increase that number
2) Just malloc and put it on the heap
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nils Wagner | last post by:
Hi all, Has someone written a C binding to transfer matrices from C to Python and vice versa ? Any pointer would be appreciated. Nils
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
3
by: Prototipo | last post by:
Hi! I need to dynamically create X vectors of matrices (with a known size of 5x5). It's like a three-dimensional matrix with 2 known dimensions and the third unknown. I have something like: ...
8
by: beginner10 | last post by:
Hello! Can anyone help me? This code doesnt work. Where is the problem? Inside the files mata and matb there is (should be) 10*10 matrix. It should count a sum to new matrix. It does not work....
1
emaghero
by: emaghero | last post by:
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices? I have an algorithm, which I'm not too pleased with, that does the job. Does anybody know a faster...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
5
by: td0g03 | last post by:
This program adds two square matrices - Algorithm 1-3, Page 35. Change it to implement the generalized algorithm to add two m x n matrices. Matrices, in general, are not square or n x n...
6
by: crazygrey | last post by:
Hi, I have created a program that multiply matrices, but the problem has to do with not showing 0, instead showing 1.795e-09 or some other smaller numbers. One of the matrices I get for example :...
5
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.