static double multigrid_recurse(struct element **sel, double ****f, double ****x, int var, int level, double eps, void (*opeval) ()) { if (level == (*sel)->precon->nlevels - 1) return conjugate_gradient(sel, f, x, var, opeval, jacobi_preconditioner, eps, 500); const int m = 15; const int n1 = (*sel)->basis->n; const int n2 = (*sel)->precon->nmg[level + 1]; double ****r = new_4d_array(n1, n1, n1, (*sel)->nel); double ****fn = new_4d_array(n2, n2, n2, (*sel)->nel); double ****e = new_4d_array(n2, n2, n2, (*sel)->nel); if (level == 0) compute_residual(sel, x, f, r, var, opeval); else copy_array(***f, ***r, n1 * n1 * n1 * (*sel)->nel); switch_multigrid_level(sel, level + 1); multigrid_restriction(sel, r, fn, level + 1); multigrid_recurse(sel, fn, e, var, level + 1, eps, opeval); switch_multigrid_level(sel, level); multigrid_prolongation(sel, e, r, level); add_array(***r, ***x, n1 * n1 * n1 * (*sel)->nel, 1.0); double err = conjugate_gradient(sel, f, x, var, opeval, jacobi_preconditioner, eps, m); free_4d_array(r); free_4d_array(fn); free_4d_array(e); return err; }
int main() { char file_name[100]; FILE *file; sparse_matrix A; double *b; double duration, aux; int n, i, j, k; clock_t start, end; printf("Nome do Arquivo: "); scanf("%s", file_name); file = fopen(file_name, "r"); /* Reading file */ if (file == NULL) { fprintf(stderr, "Não foi possível abrir o arquivo!\n"); return -1; } fscanf(file, "%d", &n); create_matrix(&A); b = malloc(n*sizeof(double)); for (k = 0; k < n*n; k ++) { fscanf(file, "%d %d", &i, &j); fscanf(file, "%lf", &aux); if(aux != 0) insert(A, i, j, aux); } for (k= 0; k < n; k ++) { fscanf(file, "%d", &i); fscanf(file, "%lf", &b[i]); } printf("Matriz foi lida com sucesso.\n"); start = clock(); conjugate_gradient(A, b, n); end = clock(); duration = (double)(end - start) / CLOCKS_PER_SEC; printf("Tempo de resolução por Gradientes Conjugados: %e segundos\n", duration); /* test */ for (i = 0; i < n; i ++) { if (b[i] - (1 + i%(n/100)) > 1e-5 || b[i] - (1 + i%(n/100)) < -1e-5) printf("Erro! %e %d %d\n", b[i], (1 + i%(n/100)), i); } fclose(file); printf("Fim da Análise!\n"); free_sparse_matrix(A); free(b); return 0; }
int main(){ clock_t begin, end; double time_spent; begin = clock(); matrix* Q = create_matrix(4, 4); value Q_arr[16] = {1, 0, 1, 0, 0, 2, 0, 1, 1, 0, 2, 0, 0, 1, 0, 1}; insert_array(Q_arr, Q); sparse_matrix* s_Q = create_sparse_matrix(Q, 8); matrix* q = create_matrix(4, 1); value q_arr[4] = {3, 20, 5, 15}; insert_array(q_arr, q); matrix* expected = create_matrix(4, 1); value e_arr[4] = {1, 5, 2, 10}; insert_array(e_arr, expected); matrix* x = create_zero_matrix(4, 1); conjugate_gradient(s_Q, x, q); assert(compare_matrices(x, expected)); free_matrix(Q); free_matrix(q); free_matrix(x); free_matrix(expected); free_sparse_matrix(s_Q); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("time taken was: %f \n", time_spent); }
int compute_y_coords(vtx_data * graph, int n, double *y_coords, int max_iterations) { /* Find y coords of a directed graph by solving L*x = b */ int i, j, rv = 0; double *b = N_NEW(n, double); double tol = hierarchy_cg_tol; int nedges = 0; float *uniform_weights; float *old_ewgts = graph[0].ewgts; construct_b(graph, n, b); init_vec_orth1(n, y_coords); for (i = 0; i < n; i++) { nedges += graph[i].nedges; } /* replace original edge weights (which are lengths) with uniform weights */ /* for computing the optimal arrangement */ uniform_weights = N_GNEW(nedges, float); for (i = 0; i < n; i++) { graph[i].ewgts = uniform_weights; uniform_weights[0] = (float) -(graph[i].nedges - 1); for (j = 1; j < graph[i].nedges; j++) { uniform_weights[j] = 1; } uniform_weights += graph[i].nedges; } if (conjugate_gradient(graph, y_coords, b, n, tol, max_iterations) < 0) { rv = 1; } /* restore original edge weights */ free(graph[0].ewgts); for (i = 0; i < n; i++) { graph[i].ewgts = old_ewgts; old_ewgts += graph[i].nedges; } free(b); return rv; }
real cg(Operator Ax, Operator precond, int n, int dim, real *x0, real *rhs, real tol, int maxit, int *flag){ real *x, *b, res = 0; int k, i; x = N_GNEW(n, real); b = N_GNEW(n, real); for (k = 0; k < dim; k++){ for (i = 0; i < n; i++) { x[i] = x0[i*dim+k]; b[i] = rhs[i*dim+k]; } res += conjugate_gradient(Ax, precond, n, x, b, tol, maxit, flag); for (i = 0; i < n; i++) { rhs[i*dim+k] = x[i]; } } FREE(x); FREE(b); return res; }
int main (int argc, char* argv[]) { int n; double *b; Cel *A = NULL; FILE *input; char opt; /* Tratamento da entrada */ if(argc < 2) { error(NO_OPTION); } else { opt = argv[1][0]; switch(opt) { case '1': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.01,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '2': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.05,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '3': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.1,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '4': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.3,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case 'r': if(argc < 3) error(NO_FILE); else { input = fopen(argv[2],"r"); if(input == NULL) error(OPENING_FILE); n = read_dimension(input); A = malloc_matrix(n); b = malloc_vector(n); read_matrix_system(input,n,A,b); fclose(input); conjugate_gradient(A,b,n); free_matrix(n,A); print_vector(n,b); free(b); break; } default: printf("Opcao nao reconhecida\n"); exit(EXIT_FAILURE); } } return 1; }
sipg_sem_2d ( int _order, const square_mesh<FLOAT_TYPE> & _mesh, FLOAT_TYPE (*f)(FLOAT_TYPE, FLOAT_TYPE), FLOAT_TYPE (*u_ex)(FLOAT_TYPE, FLOAT_TYPE), FLOAT_TYPE (*dx_u_ex)(FLOAT_TYPE, FLOAT_TYPE), FLOAT_TYPE (*dy_u_ex)(FLOAT_TYPE, FLOAT_TYPE), FLOAT_TYPE _pen, FLOAT_TYPE _tol ) : qt(_order+1), basis(_order), pen(_pen) { const int noe = _mesh.dim()*_mesh.dim(); order = _order; mesh = _mesh; // initialize load_Dphi_table<FLOAT_TYPE>(order); load_lgl_quadrature_table<FLOAT_TYPE>(order); const int blockD = 128; volume_gridSIZE = dim3( (noe + blockD - 1)/blockD , order+1, order+1); volume_blockSIZE = dim3(blockD, 1, 1); const int dimx = mesh.device_info.get_dimx(); const int dimy = mesh.device_info.get_dimy(); const int blockDx = 32; const int blockDy = 4; flux_gridSIZE = dim3( (dimx + blockDx - 1)/blockDx, (dimy + blockDy - 1)/blockDy, 1 ); flux_blockSIZE = dim3( blockDx, blockDy, 1 ); #ifdef USE_MODE_MATRIX host_laplacian_matrix<FLOAT_TYPE,int> h_volume_matrix(1, order); d_volume_matrix = h_volume_matrix; #endif #ifdef USE_PRECONDITIONER host_preconditioner_matrix<FLOAT_TYPE,int> h_prec_matrix(1, order, pen); d_prec_matrix = h_prec_matrix; #endif host_mode_vector<FLOAT_TYPE,int> h_xx(noe, order+1); copy(h_xx, d_u); compute_rhs(f, u_ex); system_solution_time.start(); #ifdef USE_PRECONDITIONER iterations = preconditioned_conjugate_gradient(*(this), d_u, d_rhs, _tol); #else iterations = conjugate_gradient(*(this), d_u, d_rhs, _tol); #endif system_solution_time.stop(); // copy back the solution copy(d_u, solution); err_norms(solution, f, u_ex, dx_u_ex, dy_u_ex); }
/* based on command line arguments. */ void bench_level1(char *b, unsigned int s, unsigned long r, char *o, char *dt, char *algo ){ /* BLAS operations */ if(strcmp(b, "blas_op") == 0){ if(strcmp(o, "dot_product") == 0){ if(strcmp(dt, "int") == 0) int_dot_product(s); else if(strcmp(dt, "float") == 0) float_dot_product(s); else if(strcmp(dt, "double") == 0) double_dot_product(s); else fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } else if(strcmp(o, "scalar_product") == 0){ if(strcmp(dt, "int") == 0) int_scalar_mult(s); else if(strcmp(dt, "float") == 0) float_scalar_mult(s); else if(strcmp(dt, "double") == 0) double_scalar_mult(s); else fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } else if(strcmp(o, "norm") == 0){ if(strcmp(dt, "int") == 0) int_norm(s); else if(strcmp(dt, "float") == 0) float_norm(s); else if(strcmp(dt, "double") == 0) double_norm(s); else fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } else if(strcmp(o, "axpy") == 0){ if(strcmp(dt, "int") == 0) int_axpy(s); else if(strcmp(dt, "float") == 0) float_axpy(s); else if(strcmp(dt, "double") == 0) double_axpy(s); else fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } else if(strcmp(o, "dmv") == 0){ if(strcmp(dt, "int") == 0) int_dmatvec_product(s); else if(strcmp(dt, "float") == 0) float_dmatvec_product(s); else if(strcmp(dt, "double") == 0) double_dmatvec_product(s); else fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } } /* Stencil codes */ else if (strcmp(b, "stencil") == 0){ /* o is set to "dot_product" by default. Use this to check for a default */ if( strcmp(o, "27") == 0 || strcmp(o, "dot_product") == 0){ if(strcmp(dt, "double") == 0) double_stencil27(s); else if (strcmp(dt, "float") == 0) float_stencil27(s); else { fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } } else if(strcmp(o, "19") == 0){ if(strcmp(dt, "double") == 0) double_stencil19(s); else if (strcmp(dt, "float") == 0) float_stencil19(s); else { fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } } else if(strcmp(o, "9") == 0){ if(strcmp(dt, "double") == 0) double_stencil9(s); else if (strcmp(dt, "float") == 0) float_stencil9(s); else { fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } } else if(strcmp(o, "5") == 0){ if(strcmp(dt, "double") == 0) double_stencil5(s); else if (strcmp(dt, "float") == 0) float_stencil5(s); else { fprintf(stderr, "ERROR: check you are using a valid data type...\n"); } } else fprintf(stderr, "ERROR: check you are using a valid operation type...\n"); } else if (strcmp(b, "fileparse") == 0){ fileparse(s); } else if (strcmp(b, "cg") == 0) { if (strcmp(algo, "mixed") == 0) { conjugate_gradient_mixed(s); } else if (strcmp(algo, "normal") == 0) { conjugate_gradient(s); } else fprintf(stderr, "ERROR: check you are using a valid algorithm...\n"); } else fprintf(stderr, "ERROR: check you are using a valid benchmark...\n"); }