Example #1
0
int main(int argc, char *argv[]) {
	int n, i, diag_size, mid_size, short_size, execs = 0;
	int *diag, *l_upper, *l_lower, *u_upper, *u_lower;
	double *x, *newx, *b, *true_x;
	double residual = 10000;
	struct timeb start, stop;
	int time_diff;
	
	if (argc != 2) {
		printf("Format: jacobi <n>\n");
		return 1;
	}
	
	n = atoi(argv[1]);
	
	//start timing here
	ftime(&start);
	
	gen_laplace_mat(n, &diag, &l_upper, &l_lower, &u_upper, &u_lower);
	
	diag_size = n * n;
	mid_size = n * n - 1;
	short_size = n * n - n;
	
	x = malloc(diag_size * sizeof(double));
	newx = malloc(diag_size * sizeof(double));
	true_x = malloc(diag_size * sizeof(double));
	b = malloc(diag_size * sizeof(double));
	
	init_x(diag_size, x);
	init_true_x(diag_size, true_x);
	init_b(n, diag_size, mid_size, short_size, diag, l_upper, l_lower, u_upper, u_lower, true_x, b);
	
	while (residual > 0.001 && execs < 100000) {
		calc_newx(n, diag_size, mid_size, short_size, diag, l_upper, l_lower, u_upper, u_lower, x, b, newx);
		for (i = 0; i < diag_size; i++) {
			x[i] = newx[i];
		}
		residual = calc_resid(n, diag_size, mid_size, short_size, diag, l_upper, l_lower, u_upper, u_lower, x, b);
		execs++;
	}
	
	// end timing here
	ftime(&stop);
	
	time_diff = (int)(1000.0 * (stop.time - start.time) + (stop.millitm - start.millitm));
	
	printf("Finished in %u milliseconds.\n", time_diff);
	
	printf("In %d iterations, got residual of %f\n", execs, residual);
	
	return 0;
}
Example #2
0
int main(int argc, char *argv[])
{

  if (argc < 3){
    printf("Arguments required, Quitting...\n");
    return 1;
  }
  int N = atoi(argv[1]);
  int max_iter = atoi(argv[2]);
  double h2 = 1.0/(N+1)/(N+1);
  double *u, *uc, *f;

  // allocate arrays
  int n_per_proc =  N + 2;
  
  u = (double *) malloc(n_per_proc*sizeof (double));
  uc = (double *) malloc(n_per_proc*sizeof (double));

  f = (double *) malloc(n_per_proc*sizeof (double));

  // initialize f and u
  int i;
  for (i = 0; i < n_per_proc-1; i++) {
    f[i] = 1.0;
    u[i] = 0.0;
  }

  // Begin iterations
  double resid_init, resid_cur;
  resid_init = calc_resid(n_per_proc, h2, f, u);
  resid_cur = resid_init;
  printf("%f\n", resid_init);
  
  int iter = 0;

  timestamp_type t1, t2;

  get_timestamp(&t1);
  while (resid_cur / resid_init > STOP_ITER_RAT){
    
    /* resid_cur = 0.0; */
    u[0] = 0.0;
    u[n_per_proc - 1] = 0.0;
    jacobi_laplace(n_per_proc, h2, f, u, uc);
    
    resid_cur = calc_resid(n_per_proc, h2, f, u);
    printf("Resid is %f\n", resid_cur );
    
    if (++iter > max_iter) break;

  }
  get_timestamp(&t2);


  printf("Total time: %f\n", timestamp_diff_in_seconds(t1,t2));
  // deallocate
  free(f);
  free(u);
  free(uc);
  
  return 0;
}