Hi I have a big problem. When we compile serial.c with gcc, I get this error
program is generating the sparse matrix
Segmentation fault
I think ı have to use malloc() but I don't know how to use and add this function in my program. Could you help me please? Thank you for your help... - #include <stdio.h>
-
#include <math.h>
-
-
#define MAX_DIM 100000
-
-
typedef float MATRIX_T[MAX_DIM][MAX_DIM];
-
-
int Jacobi(MATRIX_T A, float x[], float b[], int n,
-
float tol, int max_iter);
-
void Read_matrix(char* prompt, MATRIX_T A, int n);
-
void Read_vector(char* prompt, float x[], int n);
-
void Print_vector(char* title, float x[], int n);
-
void Print_matrix(char* title, MATRIX_T A, int n);
-
-
main(int argc, char* argv) {
-
MATRIX_T A;
-
float x[MAX_DIM];
-
float b[MAX_DIM];
-
int n;
-
float tol;
-
int max_iter;
-
int converged;
-
n=100000;
-
tol=0.0000001;
-
max_iter=10000;
-
Read_matrix("program is generating the sparse matrix", A, n);
-
Read_vector("program is generating the sparse vector", b, n);
-
-
converged = Jacobi(A, x, b, n, tol, max_iter);
-
-
if (converged)
-
Print_vector("The solution is", x, n);
-
else
-
printf("Failed to converge in %d iterations\n", max_iter);
-
} /* main */
-
-
-
/*********************************************************************/
-
/* Return 1 if iteration converged, 0 otherwise */
-
/* MATRIX_T is just a 2-dimensional array */
-
int Jacobi(
-
MATRIX_T A /* in */,
-
float x[] /* out */,
-
float b[] /* in */,
-
int n /* in */,
-
float tol /* in */,
-
int max_iter /* in */) {
-
int i, j;
-
int iter_num;
-
float x_old[MAX_DIM];
-
-
float Distance(float x[], float y[], int n);
-
-
/* Initialize x */
-
for (i = 0; i < n; i++)
-
x[i] = b[i];
-
-
iter_num = 0;
-
do {
-
iter_num++;
-
-
for (i = 0; i < n; i++)
-
x_old[i] = x[i];
-
-
for (i = 0; i < n; i++){
-
x[i] = b[i];
-
for (j = 0; j < i; j++)
-
x[i] = x[i] - A[i][j]*x_old[j];
-
for (j = i+1; j < n; j++)
-
x[i] = x[i] - A[i][j]*x_old[j];
-
x[i] = x[i]/A[i][i];
-
}
-
} while ((iter_num < max_iter) &&
-
(Distance(x,x_old,n) >= tol));
-
-
if (Distance(x,x_old,n) < tol)
-
return 1;
-
else
-
return 0;
-
} /* Jacobi */
-
-
-
/*********************************************************************/
-
float Distance(float x[], float y[], int n) {
-
int i;
-
float sum = 0.0;
-
-
for (i = 0; i < n; i++) {
-
sum = sum + (x[i] - y[i])*(x[i] - y[i]);
-
}
-
return sqrt(sum);
-
} /* Distance */
-
-
-
/*********************************************************************/
-
void Read_matrix(
-
char* prompt /* in */,
-
MATRIX_T A /* out */,
-
int n /* in */) {
-
int i, j;
-
printf("%s\n", prompt);
-
for (i = 0; i < n; i++)
-
for (j = 0; j < n; j++){
-
if( rand()/2147483647.0 > 0.7 )
-
A[i][j] = rand() /2147483647.0;
-
else A[i][j] = 0;
-
}
-
-
} /* Read_matrix */
-
-
-
/*********************************************************************/
-
void Read_vector(
-
char* prompt /* in */,
-
float x[] /* out */,
-
int n /* in */) {
-
int i;
-
-
printf("%s\n", prompt);
-
for (i = 0; i < n; i++){
-
if( rand()/2147483647.0 > 0.7 )
-
x[i] = rand() /2147483647.0;
-
else x[i] = 0;
-
}
-
} /* Read_vector */
-
-
-
/*********************************************************************/
-
void Print_vector(
-
char* title /* in */,
-
float x[] /* in */,
-
int n /* in */) {
-
int i;
-
-
printf("%s\n", title);
-
for (i = 0; i < n; i++)
-
printf("%4.1f ", x[i]);
-
printf("\n");
-
} /* Print_vector */
-
-
/*********************************************************************/
-
void Print_matrix(
-
char* title /* in */,
-
MATRIX_T A /* in */,
-
int n /* in */) {
-
int i,j;
-
-
printf("%s\n", title);
-
for (i = 0; i < n; i++) {
-
for (j = 0; j < n; j++)
-
printf("%f", A[i][j]);
-
printf("\n");
-
}
-
} /* Print_matrix */
6 3848
As far as I can tell there's nothing sparse about that 100,000x100,000 matrix,
i.e. every element is there and it's taking up too much space no matter what
you want to do about it as it is now.
kind regards,
Jos
Er, your stack has exploded. ;) You created an array that is about 4.6 TiB in size. That is over 4768 GiB!!! Good luck with that. :D
You defiantly need to implement a sparse array type. And you probably want to not pass it by value but by indirection (pass a pointer to the type).
Adrian
100000 x 100000 is probably too big for a stack variable. You will need to allocate this on the heap using malloc(). Check your documentation on how to use this function.
Oh, my bad. My calculation is wrong. Your actually allocating a little over 37 GiB. Not as bad as TiB ;).
Adrian
Oh, my bad. My calculation is wrong. Your actually allocating a little over 37 GiB. Not as bad as TiB ;).
Adrian
But still,very very bad.
Savage
Jacobi Iteration with that size of Matrix could take about a week to converge especially since you're using sparse matrices. Get yourself a better solver or you will be waiting a long long time for a solution.
Another thing you could also do, if you insist using archaic numerical methods, is to start with a small example that you know the solution of, say 2*2, or 3*3, and then work your way up to 100000*100000.
If you know the sturcture of your matrices, tri-diagonal, band-diagonal etc you SHOULD find a more suitable solver than Jacobi Iteration.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: George Sakkis |
last post by:
Is there any sparse matrix package compatible with Numeric/Numarray ? Ideally, the implementation of
a matrix (dense/sparse) should be transparent to the application. However the APIs of the only...
|
by: friends |
last post by:
I am new in C language and want to be good in it. I am trying to write
the program for Spare matrix operations using linked list. I got some
success but in many scenarios it fails to give proper...
|
by: mariaczi |
last post by:
Hi,
I code class to storage sparse matrix row compressed and i have a
problem with implements method to setVal and addVal.
I will replace later this methods overloaded operator().
Please, can You...
|
by: deLenn |
last post by:
Hi,
Does scipy have an equivalent to Matlab's 'find' function, to list the
indices of all nonzero elements in a sparse matrix?
Cheers.
|
by: dolcetheking |
last post by:
I have this sparse matrix
|5| | | | |
| |4| |7| |
| | | | | |
|1| |3| | |
| | | | |2|
and I need to make a vector V(6)={5,4,7,1,3,2}
I DONT KNOW HOW TO DO THIS IN C++
|
by: mediratta |
last post by:
Hi,
I want to allocate memory for a large matrix, whose size will be
around 2.5 million x 17000. Three fourth of its rows will have all
zeroes, but it is not known which will be those rows. If I...
|
by: adam.kleinbaum |
last post by:
Hi there,
I'm a novice C programmer working with a series of large (30,000 x
30,000) sparse matrices on a Linux system using the GCC compiler. To
represent and store these matrices, I'd like to...
|
by: nivedita prasad |
last post by:
Program for addition of two sparse matrix using link list..
Regards
Nivedita Prasad
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |