29 double alpha, beta, r_r, r_r_new, p_Ap;
32 double *Ap =
gv_calloc(n,
sizeof(
double));
33 double *Ax =
gv_calloc(n,
sizeof(
double));
34 double *alphap =
gv_calloc(n,
sizeof(
double));
36 double *orth_b =
gv_calloc(n,
sizeof(
double));
45 for (i = 0; i < max_iterations &&
max_abs(n, r) >
tol; i++) {
57 if (i < max_iterations - 1) {
63 agerrorf(
"conjugate_gradient: unexpected length 0 vector\n");
91 (
float **
A,
double *x,
double *b,
int n,
double tol,
92 int max_iterations,
bool ortho1) {
98 double alpha, beta, r_r, r_r_new, p_Ap;
100 double *p =
gv_calloc(n,
sizeof(
double));
101 double *Ap =
gv_calloc(n,
sizeof(
double));
102 double *Ax =
gv_calloc(n,
sizeof(
double));
103 double *alphap =
gv_calloc(n,
sizeof(
double));
105 double *orth_b =
gv_calloc(n,
sizeof(
double));
116 for (i = 0; i < max_iterations &&
max_abs(n, r) >
tol; i++) {
128 if (i < max_iterations - 1) {
139 agerrorf(
"conjugate_gradient: unexpected length 0 vector\n");
142 beta = r_r_new / r_r;
161 double tol,
int max_iterations)
169 double alpha, beta, r_r, r_r_new, p_Ap;
189 for (i = 0; i < max_iterations &&
max_absf(n, r) >
tol; i++) {
207 if (i < max_iterations - 1) {
215 agerrorf(
"conjugate_gradient: unexpected length 0 vector\n");
218 beta = r_r_new / r_r;
221 for (
size_t j = 0; j < (size_t)n; ++j) {
222 p[j] = (float)beta * p[j] + r[j];
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
int conjugate_gradient(vtx_data *A, double *x, double *b, int n, double tol, int max_iterations)
int conjugate_gradient_mkernel(float *A, float *x, float *b, int n, double tol, int max_iterations)
int conjugate_gradient_f(float **A, double *x, double *b, int n, double tol, int max_iterations, bool ortho1)
void agerrorf(const char *fmt,...)
double vectors_inner_product(int n, const double *vector1, const double *vector2)
void orthog1f(int n, float *vec)
void right_mult_with_vector(vtx_data *matrix, int n, double *vector, double *result)
void right_mult_with_vector_f(float **matrix, int n, double *vector, double *result)
double max_absf(int n, float *vector)
void orthog1(int n, double *vec)
void copy_vectorf(int n, float *source, float *dest)
void right_mult_with_vector_ff(float *packed_matrix, int n, float *vector, float *result)
void vectors_mult_additionf(int n, float *vector1, float alpha, float *vector2)
double max_abs(int n, double *vector)
void vectors_subtraction(int n, double *vector1, double *vector2, double *result)
void vectors_addition(int n, double *vector1, double *vector2, double *result)
void copy_vector(int n, const double *source, double *dest)
void vectors_scalar_mult(int n, const double *vector, double alpha, double *result)
void vectors_subtractionf(int n, float *vector1, float *vector2, float *result)
double vectors_inner_productf(int n, float *vector1, float *vector2)
static void cleanup2(graph_t *g, int64_t nc)
static void cleanup1(graph_t *g)