473,322 Members | 1,526 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,322 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 2082
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.