Segmentation fault in Matrix Multiplication

A

amitnanda

Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".

I need this huge size as part of my assignment.

Can anyone tell me what's going wrong?

Thanks.
 
C

Chris Dollin

Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".

I need this huge size as part of my assignment.

Can anyone tell me what's going wrong?

There's a bug in your program.

It's probably the mis-use of store on line 17.
 
A

amitnanda

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks
 
T

Tim Prince

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks

He was following up on your implied suggestion of use of ESP to guess
what you have done. You couldn't have done anything along the lines of
running out of memory and not checking for success in malloc(), or you
would have let us see it.
 
C

Christopher Hulbert

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks
Please quote your reply. If you're using google, search the newsgroup and you
will receive a plethora of hints on hwo to do so.

Add quote:
There's a bug in your program.

It's probably the mis-use of store on line 17.



That was his point. How can someone help you without seeng some code. It's
always best if you can post a minimal program that compiles and exhibits the
behavior you describe. In some cases just the function and other releavent
parts would do. Perhaps your code doesn't check memory allocations or writes to
memory outside of what you have allocated.
 
A

amitnanda

Im very sorry about that. Don't know what ESP means. Here's the code.

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

#define N 850
#define T 1000


void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N]);
void fillMatrix(double a[N][N]);
void prdoubleMatrix(double a[N][N]);

/* Main Method */
main()
{
printf("Hi");
clock_t now,later;
double passed;

//srand( (unsigned)time( NULL ) ); /* Set
initial seed */
printf("RAND MAX is %d\n",RAND_MAX);

double p[N][N] = { 0 }; /* Intializing p
matrix to 0 */
double q[N][N] = { 0 }; /* Intializing q
matrix to 0 */
double r[N][N]= { 0 }; /* Intializing r
matrix to 0 */

now = clock();
fillMatrix(p); /* Randomly fill
Matrix p */
printf("Hi");
printf("Matrix p \n");
printf("======== \n");
//printMatrix(p); /* Display
Matrix p */

fillMatrix(q); /* Randomly fill
Matrix q */
printf("Matrix q \n");
printf("======== \n");
//printMatrix(q); /* Display
Matrix q */

//now = clock();
multiplyMatrices(p, q, r); /* Multiply p and q and put
results in r */
later = clock();
passed = (double) (later - now) / CLOCKS_PER_SEC;

printf("Time taken for multiplication %0.20lf seconds\n", passed);
printf("Matrix r (Matrix p * Matrix q) \n");
printf("============================== \n");
//printMatrix(r); /* Display
Matrix r */

} /* end of main function */

void fillMatrix(double a[N][N]) /* function to fill a matrix with
random numbers */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
a[j] = (double) T * rand() / (RAND_MAX + 1.0);
}
}
}

/* function to multiply two matrices */
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N])
{
int i, j, k;
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
for(k=0; k<N; k++)
{
result[j] = result[j] + ( a[k] * b[k][j] );
}
}
}
}


void printMatrix(double a[N][N]) /* function to display the contents
of a matrix */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
printf("%d\t", a[j]);
}
printf("\n");
}
}
 
C

CBFalconer

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

That's what we have crystal balls for. It is also the reason there
is no point in including any context in a message, so that the
crystal ball can be given a thorough exercise. Your article is a
marvel of completeness and clarity. Without such things we would
have no guessing to do, and life would be extremely boring. We
might even be able to give accurate answers, which would never do.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
F

Flash Gordon

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

That was the whole point of Chris's post. How can anyone tell you what
is wrong with your code if you don't post it?

Also, please provide context when replying, there is no guarantee that
people will have seen the post you are replying to since Google is only
one of very many ways of accessing Usenet. See
http://cfaj.freeshell.org/google/ for details on how to reply properly
through Google.
 
J

Jordan Abel

Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".

800*800 is huge - 640000 - that's a lot of memory to be allocating on
the stack [pedants: in an auto variable] and since there is no way in
the standard provided for an implementation to indicate that the
declaration of an auto variable "failed", it's undefined behavior
I need this huge size as part of my assignment.

You might have better luck with malloc().
Can anyone tell me what's going wrong?

Your arrays are too big, and you have three of them.
 
P

pemo

Im very sorry about that. Don't know what ESP means. Here's the code.

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

#define N 850
#define T 1000


void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N]);
void fillMatrix(double a[N][N]);
void prdoubleMatrix(double a[N][N]);

/* Main Method */
main()
{
printf("Hi");
clock_t now,later;
double passed;

//srand( (unsigned)time( NULL ) ); /* Set
initial seed */
printf("RAND MAX is %d\n",RAND_MAX);

double p[N][N] = { 0 }; /* Intializing p
matrix to 0 */
double q[N][N] = { 0 }; /* Intializing q
matrix to 0 */
double r[N][N]= { 0 }; /* Intializing r
matrix to 0 */

now = clock();
fillMatrix(p); /* Randomly fill
Matrix p */
printf("Hi");
printf("Matrix p \n");
printf("======== \n");
//printMatrix(p); /* Display
Matrix p */

fillMatrix(q); /* Randomly fill
Matrix q */
printf("Matrix q \n");
printf("======== \n");
//printMatrix(q); /* Display
Matrix q */

//now = clock();
multiplyMatrices(p, q, r); /* Multiply p and q and put
results in r */
later = clock();
passed = (double) (later - now) / CLOCKS_PER_SEC;

printf("Time taken for multiplication %0.20lf seconds\n", passed);
printf("Matrix r (Matrix p * Matrix q) \n");
printf("============================== \n");
//printMatrix(r); /* Display
Matrix r */

} /* end of main function */

void fillMatrix(double a[N][N]) /* function to fill a matrix with
random numbers */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
a[j] = (double) T * rand() / (RAND_MAX + 1.0);
}
}
}

/* function to multiply two matrices */
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N])
{
int i, j, k;
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
for(k=0; k<N; k++)
{
result[j] = result[j] + ( a[k] * b[k][j] );
}
}
}
}


void printMatrix(double a[N][N]) /* function to display the contents
of a matrix */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
printf("%d\t", a[j]);
}
printf("\n");
}
}



You're not trashing the stack are you? Does you code work if N is smaller?

If 'yes', perhaps try declaring these as 'static', e.g:

static double p[N][N] = { 0 };
static double q[N][N] = { 0 };
static double r[N][N]= { 0 };
 
A

amitnanda

Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was.
Thanks everyone.

The problem was my account quota in the department server.
 
V

Vladimir S. Oka

Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was.
^^^^^^^^^^^^^^^^^^^^^^^^^

And what happened to those?
The problem was my account quota in the department server.

What problem?

Cheers

Vladimir
 
I

Ico

Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was.

One more last piece of advice : please quote the message(s) you are
replying to, to provide proper context.
The problem was my account quota in the department server.

This might have triggered your problem, but this should not *be* the
problem. Your program should have detected any shortage on resources and
exited with a proper error message, instead of causing a segmentation
fault. You might want to add proper error handling where appropriate.
 
M

Mark McIntyre

Im very sorry about that. Don't know what ESP means.

telepathy.

Since you originally didn't post any code, how could anyone possibly
help solve your problem except by guessing?

For what its worth, all compilers have limits on how big a chunk of
memory they can grab. Maybe you exceeded that.

Mark McIntyre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top