Reputation: 41
I was trying to write a program to perform matrix multiplication using OpenMP in C. I am facing a segmentation fault error whenever I am trying to pass a string literal as an argument to a function.
#include<stdio.h>
#include<time.h>
#include<omp.h>
struct matrix{
int r;
int c;
int mat[1000][1000];
};
void read_matrix(char* fname, struct matrix* m){
FILE *fp;
fp = fopen(fname,"r");
fscanf(fp, "%d %d",&m->r,&m->c);
for(int i=0;i<m->r;i++){
for(int j=0;j<m->c;j++){
fscanf(fp, "%d", &m->mat[i][j]);
}
}
fclose(fp);
}
void write_matrix(char* fname, struct matrix m){
FILE *fp;
fp = fopen(fname,"w");
fprintf(fp,"%d %d\n",m.r,m.c);
for(int i=0;i<m.r;i++){
for(int j=0;j<m.c;j++){
fprintf(fp,"%d\n",m.mat[i][j]);
}
}
fclose(fp);
}
void main(){
struct matrix m1;
struct matrix m2;
struct matrix res;
read_matrix("m1",&m1);
read_matrix("m2",&m2);
int r1 = m1.r;
int c1 = m1.c;
int c2 = m2.c;
res.r = r1;
res.c = c2;
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
res.mat[i][j] = 0;
}
}
#pragma omp parallel
{
#pragma omp for
for(int i = 0; i < r1; i++){
for(int j = 0; j < c2; j++){
for(int k = 0; k < c1; k++){
#pragma omp atomic update
res.mat[i][j] += m1.mat[i][k]*m2.mat[k][j];
}
}
}
}
write_matrix("res",res);
}
The code shows Segmentation fault (core dumped)
when run.
When run on GDB, it shows that
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400ad9 in main () at mm.c:40
40 read_matrix("m1",&m1);
Before the first read_matrix() call I added a printf statement printf("check\n");
The printf call now started throwing a segmentation fault.
I am assuming that passing a string literal is the cause of error. What could possibly be wrong with the code?
Upvotes: 0
Views: 1023
Reputation: 9173
Before the first
read_matrix()
call I added a printf statementprintf("check\n");
The printf call now started throwing a segmentation fault.
I think it may happen because you allocate struct matrix
on stack. each matrix is around 4MB, so 12MB on stack. I guess it creates site (stack overflow).
Try using static variables for matrices or dynamically allocate them. It may solve your problem. And if it doesn't, never allocate 12MB of structures on stack...
Upvotes: 6