Ejemplo n.º 1
0
    void test_with (VP &v1, VP &v2, MP &m1) const {
        {
            // Rows and columns
            initialize_matrix (m1);
            for (int i = 0; i < N; ++ i) {
                v1 = ublas::row (m1, i);
                std::cout << "row (m, " << i << ") = " << v1 << std::endl;
                v1 = ublas::column (m1, i);
                std::cout << "column (m, " << i << ") = " << v1 << std::endl;
            }

            // Outer product
            initialize_vector (v1);
            initialize_vector (v2);
            m1 = ublas::outer_prod (v1, v2);
            std::cout << "outer_prod (v1, v2) = " << m1 << std::endl;

            // Matrix vector product
            initialize_matrix (m1);
            initialize_vector (v1);
            v2 = ublas::prod (m1, v1);
            std::cout << "prod (m1, v1) = " << v2 << std::endl;
            v2 = ublas::prod (v1, m1);
            std::cout << "prod (v1, m1) = " << v2 << std::endl;
        }
    }
Ejemplo n.º 2
0
    void operator () (int runs) const {
        try {
            static M m1 (N * N), m2 (N * N), m3 (N * N);
            initialize_vector (m1);
            initialize_vector (m2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                for (int j = 0; j < N; ++ j) {
                    std::valarray<value_type> row (m1 [std::slice (N * j, N, 1)]);
                    for (int k = 0; k < N; ++ k) {
                        std::valarray<value_type> column (m2 [std::slice (k, N, N)]);
                        m3 [N * j + k] = (row * column).sum ();
                    }
                }
//                sink_vector (m3);
            }
            footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
        catch (...) {
            std::cout << "unknown exception" << std::endl;
        }
    }
Ejemplo n.º 3
0
    void operator () (int runs) const {
        try {
            static V v1 (N), v2 (N), v3 (N);
            initialize_vector (v1);
            initialize_vector (v2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                v3 = - (v1 + v2);
//                sink_vector (v3);
            }
            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 4
0
int main(int argc, char* argv[]) {
    int i = 0;
    int err;
    clock_t t0, t1, t2;

    if (argc == 2) {
        max_threads = atoi(argv[1]);
    } else {
        max_threads = MAX_THREADS;
    }
    
    t0 = clock();
    printf("Running 3s-00");
    // random seed
    // http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c
    srand(time(NULL));
    printf("*** 3s-00 ***\n");
    printf("Initializing vector... ");
    fflush(stdout);
    initialize_vector();
    printf("Vector initialized!\n");
    fflush(stdout);
    t1 = clock();
    count_3s();
    t2 = clock();
    printf("Count by threads %d\n", count);
    printf("Double check %d\n", double_count);
    printf("Time: Vector de Inicialización %f\n", (((float) t1 - (float) t0) / 1000000.0F) * 1000);
    printf("Time: Contar El Numero de 3s en vector un dado %f\n", (((float) t2 - (float) t1) / 1000000.0F) * 1000);
    printf("Finishing 3s-00\n");
    return 0;
}
Ejemplo n.º 5
0
int main(int argc, char* argv[]) {
	int i = 0;
	int err;
	clock_t t1, t2, t0,t3;

	if (argc == 2) {
		max_threads = atoi(argv[1]);
	} else {
		max_threads = MAX_THREADS;
	}
	printf("Running 3s-00");
	// random seed
	// http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c
	srand(time(NULL));
	printf("*** 3s-00 ***\n");
	printf("Initializing vector... ");
	fflush(stdout);
	t0 = clock();
	initialize_vector();
	t3 = clock();
	printf("Vector initialized!\n");
	fflush(stdout);
	t1 = clock();
	count_3s();
	t2 = clock();
	printf("Count by threads %d\n", count);
	printf("Double check %d\n", double_count);
	printf("[[3s-00] Elapsed time %f\n", (((float)t2 - (float)t1) / 1000000.0F ) * 1000);
	printf("[[3s-00] Initialize vector time %f\n", (((float)t3 - (float)t0) / 1000000.0F ) * 1000);
printf("[[3s-00] Count 3s time %f\n", (((float)t2 - (float)t1) / 1000000.0F ) * 1000);
	printf("Finishing 3s-00");
	return 0;
}
Ejemplo n.º 6
0
    void operator () (int runs) const {
        try {
            static M m1 (N * N), m2 (N * N), m3 (N * N);
            initialize_vector (m1);
            initialize_vector (m2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                m3 = - (m1 + m2);
//                sink_vector (m3);
            }
            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 7
0
    void operator () (int runs) const {
        try {
            static V v1 (N), v2 (N);
            initialize_vector (v1);
            initialize_vector (v2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                static value_type s (0);
                s = (v1 * v2).sum ();
//                sink_scalar (s);
            }
            footer<value_type> () (N, N - 1, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 8
0
    void operator () (int runs, fast_tag) const {
        try {
            static M m (N, N);
            static V v1 (N), v2 (N);
            initialize_vector (v1);
            initialize_vector (v2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                m.assign (- ublas::outer_prod (v1, v2));
//                sink_matrix (m);
            }
            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 9
0
vector_t *create_vector() {
	vector_t * v = (vector_t *) calloc(1, sizeof(vector_t));

	if (v != NULL) {
		initialize_vector(v);
	}

	return v;
}
Ejemplo n.º 10
0
int main(int argc, char* argv[]) {
	int i = 0;
	int err;
	clock_t t1, t2, tt1, tt2;

	if (argc == 2) {
		max_threads = atoi(argv[1]);
		if (max_threads > MAX_THREADS)
			max_threads = MAX_THREADS;
	} else {
		max_threads = MAX_THREADS;
	}
	printf("[3s-02] Using %d threads\n",max_threads);
	// random seed
	// http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c
	printf("*** 3s-02 ***\n");
	srand(time(NULL));
	printf("Initializing vector... ");
	fflush(stdout);
	initialize_vector();
	printf("Vector initialized!\n");
	fflush(stdout);
	t1 = clock();
	while (i < max_threads) {
		err = pthread_create(&tid[i], NULL, &count3s_thread, (void*)i);
		if (err != 0) 
			printf("[3s-02] Can't create a thread: [%d]\n", i);
		else
			printf("[3s-02] Thread created!\n");
		i++;
	}
	// https://computing.llnl.gov/tutorials/pthreads/#Joining
	i = 0;
	for (; i < max_threads; i++) {
		tt1 = clock();
		void *status;
		int rc;
		rc = pthread_join(tid[i], &status);
		if (rc) {
			printf("ERROR; retrun code from pthread_join() is %d\n", rc);
			exit(-1);
		}  else {
			printf("Thread [%d] exited with status [%ld]\n", i, (long)status);
		}

		tt2 = clock();
		printf("[3s-01] Elapsed time %f\n", (((float)tt2 - (float)tt1) / 1000000.0F ) * 1000);
		printf(" for thread: [%d]\n", i);
	}
	t2 = clock();
	printf("[3s-02] Count by threads %d\n", count);
	printf("[3s-02] Double check %d\n", double_count);
	//printf("[[3s-02] Elapsed time %ld ms\n", ((double)t2 - t1) / CLOCKS_PER_SEC * 1000);
	printf("[3s-02] Elapsed full time %f\n", (((float)t2 - (float)t1) / 1000000.0F ) * 1000);
	return 0;
}
Ejemplo n.º 11
0
    void operator () (int runs) const {
        try {
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_vector (vr1);
            initialize_vector (vr2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                static value_type s (0);
                s = ublas::inner_prod (vr1, vr2);
//                sink_scalar (s);
            }
            footer<value_type> () (N, N - 1, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 12
0
    void operator () (int runs, fast_tag) const {
        try {
            static V v1 (N), v2 (N), v3 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N)),
                                   vr3 (v2, ublas::range (0, N));
            initialize_vector (vr1);
            initialize_vector (vr2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                vr3.assign (- (vr1 + vr2));
//                sink_vector (vr3);
            }
            footer<value_type> () (0, 2 * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 13
0
void initialize_point_struct(point_struct *Pt, int numVariables)
/***************************************************************\
* USAGE: initialize the memory                                  *
\***************************************************************/
{ // initialize the memory
  initialize_vector(Pt->origX, numVariables);
  initialize_vector(Pt->x, numVariables);
  initialize_vector(Pt->Nx, numVariables);
  mpf_init(Pt->norm_x);
  initialize_number(Pt->origAlpha);
  initialize_number(Pt->origBeta);
  initialize_number(Pt->origGamma);
  initialize_number(Pt->alpha);
  initialize_number(Pt->beta);
  initialize_number(Pt->gamma);
  Pt->isApproxSoln = Pt->isActive = Pt->isReal = 0;

  return;
}
Ejemplo n.º 14
0
vector_t *create_vector2(void *data) {
	vector_t * v = (vector_t *) calloc(1, sizeof(vector_t));

	if (v != NULL) {
		initialize_vector(v);
		add_element(v, data);
	}

	return v;
}
Ejemplo n.º 15
0
    void operator () (int runs) const {
        try {
            static M m (N * N);
            static V v1 (N), v2 (N);
            initialize_vector (m);
            initialize_vector (v1);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                for (int j = 0; j < N; ++ j) {
                    std::valarray<value_type> row (m [std::slice (N * j, N, 1)]);
                    v2 [j] = (row * v1).sum ();
                }
//                sink_vector (v2);
            }
            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 16
0
    void operator () (int runs, safe_tag) const {
        try {
            static M m (N, N);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_vector (vr1);
            initialize_vector (vr2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                mr = - ublas::outer_prod (vr1, vr2);
//                sink_matrix (mr);
            }
            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 17
0
    void operator () (int runs) const {
        try {
            static M m (N * N);
            static V v1 (N), v2 (N);
            initialize_vector (v1);
            initialize_vector (v2);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                for (int j = 0; j < N; ++ j) {
                    for (int k = 0; k < N; ++ k) {
                        m [N * j + k] = - v1 [j] * v2 [k];
                    }
                }
//                sink_vector (m);
            }
            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 18
0
    void operator () (VP &v1, VP &v2, MP &m1) const {
        try {
            // Rows and columns
            initialize_matrix (m1, ublas::lower_tag ());
            for (int i = 0; i < N; ++ i) {
                v2 = ublas::row (m1, i);
                std::cout << "row (m, " << i << ") = " << v2 << std::endl;
                v2 = ublas::column (m1, i);
                std::cout << "column (m, " << i << ") = " << v2 << std::endl;
            }

            // Outer product
            initialize_vector (v1);
            initialize_vector (v2);
            v1 (0) = 0;
            v1 (N - 1) = 0;
            v2 (0) = 0;
            v2 (N - 1) = 0;
            m1 = ublas::outer_prod (v1, v2);
            std::cout << "outer_prod (v1, v2) = " << m1 << std::endl;

            // Matrix vector product
            initialize_matrix (m1, ublas::lower_tag ());
            initialize_vector (v1);
            v2 = ublas::prod (m1, v1);
            std::cout << "prod (m1, v1) = " << v2 << std::endl;
            v2 = ublas::prod (v1, m1);
            std::cout << "prod (v1, m1) = " << v2 << std::endl;
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
        catch (...) {
            std::cout << "unknown exception" << std::endl;
        }
    }
Ejemplo n.º 19
0
void DifferentialEvolution::initialize_population(RunManagerAbstract &run_manager, int d)
{
	ostream &fout_restart = file_manager.get_ofstream("rst");
	int iter = 0;

	RestartController::write_start_iteration(fout_restart, solver_type_name, iter, iter);
	Parameters ctl_pars;
	for (int i = 0; i < d; ++i)
	{
		ctl_pars.clear();
		initialize_vector(ctl_pars);
		par_transform.ctl2model_ip(ctl_pars);
		run_manager.add_run(ctl_pars);
	}
	RestartController::write_upgrade_runs_built(fout_restart);
	// make innitial population vector model runs
	cout << endl;
	cout << "  performing initial population model runs... ";
	cout.flush();
	run_manager.run();
	gen_1.copy(run_manager.get_runstorage_ref());

	// get the best_run to track phi
	int r_status;
	int n_par = par_list.size();
	best_phi = std::numeric_limits<double>::max();

	Parameters tmp_pars;
	Observations tmp_obs;
	ModelRun tmp_run(obj_func_ptr);
	for (int i_run = 0; i_run < d; ++i_run)
	{
		bool r_status = gen_1.get_run(i_run, tmp_pars, tmp_obs);
		if (r_status > 0)
		{
			par_transform.model2ctl_ip(tmp_pars);
			tmp_run.update_ctl(tmp_pars, tmp_obs);
			double tmp_phi = tmp_run.get_phi(DynamicRegularization::get_unit_reg_instance());
			if (tmp_phi < best_phi)
			{
				best_phi = tmp_phi;
				best_run_idx = i_run;
			}
		}
	}

}
Ejemplo n.º 20
0
    void operator () (int runs, safe_tag) const {
        try {
            static M m (N, N, N * N);
            static V v1 (N, N), v2 (N, N);
            initialize_matrix (m);
            initialize_vector (v1);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                v2 = ublas::prod (m, v1);
//                sink_vector (v2);
            }
            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
    }
Ejemplo n.º 21
0
int main(int argc, char* argv[]) {
  int i = 0;
  int err;
  clock_t t1, t2, t1ini, t2ini,t1th, t2th;
  if (argc == 2) {
    max_threads = atoi(argv[1]);
    if (max_threads > MAX_THREADS)
      max_threads = MAX_THREADS;
  } else {
    max_threads = MAX_THREADS;
  }
  printf("[3s-01] Using %d threads\n",max_threads);
  // random seed
  // http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c
  srand(time(NULL));
  printf("*** 3s-01 ***\n");
  printf("Initializing vector... ");
  fflush(stdout);
  t1ini= clock();
  initialize_vector();
  t2ini= clock();
  printf("Vector initialized!\n");
  fflush(stdout);
  t1 = clock();
  while (i < max_threads) {
   
    err = pthread_create(&tid[i], NULL, &count3s_thread, (void*)i);
    if (err != 0) 
      printf("[3s-01] Can't create a thread: [%d]\n", i);
    else
      printf("[3s-01] Thread created!\n");
      i++;
  }
  t2 = clock();
  printf("[3s-01] Count by threads %d\n", count);
  printf("[3s-01] Double check %d\n", double_count);
  printf("[[3s-01] Initializing time %f\n", (((float)t2ini - (float)t1ini) / 1000000.0F ) * 1000);
  //printf("[[3s-01] Elapsed time %ld ms\n", (ti2 - t1) / CLOCKS_PER_SEC * 1000);
  printf("[[3s-01] Elapsed time %f\n", (((float)t2 - (float)t1) / 1000000.0F ) * 1000);
  for(i=0; i<MAX_THREADS; i++){
    printf("[[3s-01] Elapsed time thread [%d] = %f\n",i, arraytime[i]);
  }
  return 0;
}
Ejemplo n.º 22
0
    void operator () (int runs, fast_tag) const {
        try {
            static M m (N, N);
            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
            static V v1 (N), v2 (N);
            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
                                   vr2 (v2, ublas::range (0, N));
            initialize_matrix (mr);
            initialize_vector (vr1);
            boost::timer t;
            for (int i = 0; i < runs; ++ i) {
                vr2.assign (ublas::prod (mr, vr1));
//                sink_vector (vr2);
            }
            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
        catch (...) {
            std::cout << "unknown exception" << std::endl;
        }
    }
Ejemplo n.º 23
0
void load_floating_points(int *numPoints, complex_vector **points, int numVars, char *PtsFile)
/***************************************************************\
* USAGE: load points from PtsFile                               *
\***************************************************************/
{
  int i, j, rV, base = 10;
  FILE *IN = fopen(PtsFile, "r");

  // error checking - file must exist
  if (IN == NULL)
  {
    printf("\nERROR: '%s' does not exist!\n", PtsFile);
    errExit(ERROR_FILE_NOT_EXIST); 
  }

  // read in the number of points
  rV = fscanf(IN, "%d", numPoints);

  // error checking
  if (rV != 1)
  { 
    printf("\nERROR: Unable to read the number of points stored in '%s'.\n", PtsFile);
    errExit(ERROR_FILE_NOT_EXIST); 
  }
  else if (*numPoints <= 0)
  {
    printf("ERROR: The number of points in '%s' must be positive!\n", PtsFile);
    errExit(ERROR_CONFIGURATION);
  }

  // allocate points
  *points = (complex_vector *)errMalloc((*numPoints) * sizeof(complex_vector));
  
  // read in the points
  for (i = 0; i < *numPoints; i++)
  { // setup points[i]
    initialize_vector((*points)[i], numVars);
  
    for (j = 0; j < numVars; j++)
    { // setup real part
      rV = mpf_inp_str((*points)[i]->coord[j]->re, IN, base);

      // error checking
      if (rV == 0)
      { 
        printf("\nERROR: Unable to read in %d floating point vectors from '%s'.\n", *numPoints, PtsFile);
        errExit(ERROR_FILE_NOT_EXIST); 
      }

      // setup imag part
      rV = mpf_inp_str((*points)[i]->coord[j]->im, IN, base);

      // error checking
      if (rV == 0)
      { 
        printf("\nERROR: Unable to read in %d floating point vectors from '%s'.\n", *numPoints, PtsFile);
        errExit(ERROR_FILE_NOT_EXIST); 
      }
    }  
  }

  // close file
  fclose(IN);

  return;
}
Ejemplo n.º 24
0
    void test_with (VP &v1, VP &v2, VP &v3) const {
        {
            value_type t;
            size_type i;
            real_type n;

            // Default Construct
            default_construct<VP>::test ();
            
            // Copy and swap
            initialize_vector (v1);
            initialize_vector (v2);
            v1 = v2;
            std::cout << "v1 = v2 = " << v1 << std::endl;
            v1.assign_temporary (v2);
            std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
            v1.swap (v2);
            std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;

#ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
            // Project range and slice
            initialize_vector (v1);
            initialize_vector (v2);
            project (v1, ublas::range(0,1)) = project (v2, ublas::range(0,1));
            project (v1, ublas::range(0,1)) = project (v2, ublas::slice(0,1,1));
            project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::slice(0,1,2));
            project (v1, ublas::slice(2,-1,2)) = project (v2, ublas::range(0,2));
            std::cout << "v1 = range/slice " << v1 << std::endl;
#endif

            // Unary vector operations resulting in a vector
            initialize_vector (v1);
            v2 = - v1;
            std::cout << "- v1 = " << v2 << std::endl;
            v2 = ublas::conj (v1);
            std::cout << "conj (v1) = " << v2 << std::endl;

            // Binary vector operations resulting in a vector
            initialize_vector (v1);
            initialize_vector (v2);
            initialize_vector (v3);
            v3 = v1 + v2;
            std::cout << "v1 + v2 = " << v3 << std::endl;

            v3 = v1 - v2;
            std::cout << "v1 - v2 = " << v3 << std::endl;

            // Scaling a vector
            t = N;
            initialize_vector (v1);
            v2 = value_type (1.) * v1;
            std::cout << "1. * v1 = " << v2 << std::endl;
            v2 = t * v1;
            std::cout << "N * v1 = " << v2 << std::endl;
            initialize_vector (v1);
            v2 = v1 * value_type (1.);
            std::cout << "v1 * 1. = " << v2 << std::endl;
            v2 = v1 * t;
            std::cout << "v1 * N = " << v2 << std::endl;

            // Some assignments
            initialize_vector (v1);
            initialize_vector (v2);
            v2 += v1;
            std::cout << "v2 += v1 = " << v2 << std::endl;
            v2 -= v1;
            std::cout << "v2 -= v1 = " << v2 << std::endl;
            v2 = v2 + v1;
            std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
            v2 = v2 - v1;
            std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
            v1 *= value_type (1.);
            std::cout << "v1 *= 1. = " << v1 << std::endl;
            v1 *= t;
            std::cout << "v1 *= N = " << v1 << std::endl;

            // Unary vector operations resulting in a scalar
            initialize_vector (v1);
            t = ublas::sum (v1);
            std::cout << "sum (v1) = " << t << std::endl;
            n = ublas::norm_1 (v1);
            std::cout << "norm_1 (v1) = " << n << std::endl;
            n = ublas::norm_2 (v1);
            std::cout << "norm_2 (v1) = " << n << std::endl;
            n = ublas::norm_inf (v1);
            std::cout << "norm_inf (v1) = " << n << std::endl;

            i = ublas::index_norm_inf (v1);
            std::cout << "index_norm_inf (v1) = " << i << std::endl;

            // Binary vector operations resulting in a scalar
            initialize_vector (v1);
            initialize_vector (v2);
            t = ublas::inner_prod (v1, v2);
            std::cout << "inner_prod (v1, v2) = " << t << std::endl;
        }
    }
Ejemplo n.º 25
0
    void test_with (VP &v1, VP &v2, VP &v3) const {
        {
            value_type t;
            size_type i;
            real_type n;

            // Copy and swap
            initialize_vector (v1);
            initialize_vector (v2);
            v1 = v2;
            std::cout << "v1 = v2 = " << v1 << std::endl;
            v1.assign_temporary (v2);
            std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
            v1.swap (v2);
            std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;

            // Zero assignment
            v1 = ublas::zero_vector<value_type> (v1.size ());
            std::cout << "v1.zero_vector = " << v1 << std::endl;
            v1 = v2;

            // Unary vector operations resulting in a vector
            initialize_vector (v1);
            v2 = - v1;
            std::cout << "- v1 = " << v2 << std::endl;
            v2 = ublas::conj (v1);
            std::cout << "conj (v1) = " << v2 << std::endl;

            // Binary vector operations resulting in a vector
            initialize_vector (v1);
            initialize_vector (v2);
            v3 = v1 + v2;
            std::cout << "v1 + v2 = " << v3 << std::endl;

            v3 = v1 - v2;
            std::cout << "v1 - v2 = " << v3 << std::endl;

            // Scaling a vector
            t = value_type (N);
            initialize_vector (v1);
            v2 = value_type (1.) * v1;
            std::cout << "1. * v1 = " << v2 << std::endl;
//            v2 = t * v1;
            std::cout << "N * v1 = " << v2 << std::endl;
            initialize_vector (v1);
//            v2 = v1 * value_type (1.);
            std::cout << "v1 * 1. = " << v2 << std::endl;
//            v2 = v1 * t;
            std::cout << "v1 * N = " << v2 << std::endl;

            // Some assignments
            initialize_vector (v1);
            initialize_vector (v2);
            v2 += v1;
            std::cout << "v2 += v1 = " << v2 << std::endl;
            v2 -= v1;
            std::cout << "v2 -= v1 = " << v2 << std::endl;
            v2 = v2 + v1;
            std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
            v2 = v2 - v1;
            std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
            v1 *= value_type (1.);
            std::cout << "v1 *= 1. = " << v1 << std::endl;
            v1 *= t;
            std::cout << "v1 *= N = " << v1 << std::endl;

            // Unary vector operations resulting in a scalar
            initialize_vector (v1);
            t = ublas::sum (v1);
            std::cout << "sum (v1) = " << t << std::endl;
            n = ublas::norm_1 (v1);
            std::cout << "norm_1 (v1) = " << n << std::endl;
            n = ublas::norm_2 (v1);
            std::cout << "norm_2 (v1) = " << n << std::endl;
            n = ublas::norm_inf (v1);
            std::cout << "norm_inf (v1) = " << n << std::endl;

            i = ublas::index_norm_inf (v1);
            std::cout << "index_norm_inf (v1) = " << i << std::endl;

            // Binary vector operations resulting in a scalar
            initialize_vector (v1);
            initialize_vector (v2);
            t = ublas::inner_prod (v1, v2);
            std::cout << "inner_prod (v1, v2) = " << t << std::endl;
        }
    }
Ejemplo n.º 26
0
 static void test ()
 {
     MC default_constuct;
     initialize_vector (default_constuct);
     std::cout << "default construct = " << default_constuct << std::endl;
 }
Ejemplo n.º 27
0
int main() {
  vector x,b;
  vector r,p,Ap;
  matrix A;
  
  double one=1.0, zero=0.0;
  double normr, rtrans, oldtrans, p_ap_dot , alpha, beta;
  int iter=0;

  //create matrix
  allocate_3d_poission_matrix(A,N);
    
  printf("Rows: %d, nnz: %d\n", A.num_rows, A.row_offsets[A.num_rows]);

  allocate_vector(x,A.num_rows);
  allocate_vector(Ap,A.num_rows);
  allocate_vector(r,A.num_rows);
  allocate_vector(p,A.num_rows);
  allocate_vector(b,A.num_rows);

  initialize_vector(x,100000);
  initialize_vector(b,1);
 

  waxpby(one, x, zero, x, p);
  matvec(A,p,Ap);
  waxpby(one, b, -one, Ap, r);
  
  rtrans=dot(r,r);
  normr=sqrt(rtrans);
  
  double st = omp_get_wtime();
  do {
    if(iter==0) {
      waxpby(one,r,zero,r,p);
    } else {
      oldtrans=rtrans;
      rtrans = dot(r,r);
      beta = rtrans/oldtrans;
      waxpby(one,r,beta,p,p);
    }
    
    normr=sqrt(rtrans);
  
    matvec(A,p,Ap);
    p_ap_dot = dot(Ap,p);

    alpha = rtrans/p_ap_dot;

    waxpby(one,x,alpha,p,x);
    waxpby(one,r,-alpha,Ap,r);

    if(iter%10==0)
      printf("Iteration: %d, Tolerance: %.4e\n", iter, normr);
    iter++;
  } while(iter<MAX_ITERS && normr>TOL);
  double et = omp_get_wtime();

  printf("Total Iterations: %d\n", iter);
  printf("Total Time: %lf s\n", (et-st));

  free_vector(x);
  free_vector(r);
  free_vector(p);
  free_vector(Ap);
  free_matrix(A);

  return 0;
}
Ejemplo n.º 28
0
void test_blas_1<V, N>::test () {
    {
        value_type t;
        real_type n;
        V v1 (N), v2 (N);

        // _asum
        initialize_vector (v1);
        n = ublas::blas_1::asum (v1);
        std::cout << "asum (v1) = " << n << std::endl;

        // _amax
        initialize_vector (v1);
        n = ublas::blas_1::amax (v1);
        std::cout << "amax (v1) = " << n << std::endl;

        // _nrm2
        initialize_vector (v1);
        n = ublas::blas_1::nrm2 (v1);
        std::cout << "nrm2 (v1) = " << n << std::endl;

        // _dot
        // _dotu
        // _dotc
        initialize_vector (v1);
        initialize_vector (v2);
        t = ublas::blas_1::dot (v1, v2);
        std::cout << "dot (v1, v2) = " << t << std::endl;
        t = ublas::blas_1::dot (ublas::conj (v1), v2);
        std::cout << "dot (conj (v1), v2) = " << t << std::endl;

        // _copy
        initialize_vector (v2);
        ublas::blas_1::copy (v1, v2);
        std::cout << "copy (v1, v2) = " << v1 << std::endl;

        // _swap
        initialize_vector (v1);
        initialize_vector (v2);
        ublas::blas_1::swap (v1, v2);
        std::cout << "swap (v1, v2) = " << v1 << " " << v2 << std::endl;

        // _scal
        // csscal
        // zdscal
        initialize_vector (v1);
        ublas::blas_1::scal (v1, value_type (1));
        std::cout << "scal (v1, 1) = " << v1 << std::endl;

        // _axpy
        initialize_vector (v1);
        initialize_vector (v2);
        ublas::blas_1::axpy (v1, value_type (1), v2);
        std::cout << "axpy (v1, 1, v2) = " << v1 << std::endl;

        // _rot
        initialize_vector (v1);
        initialize_vector (v2);
        ublas::blas_1::rot (value_type (1), v1, value_type (1), v2);
        std::cout << "rot (1, v1, 1, v2) = " << v1 << " " << v2 << std::endl;
    }
}
Ejemplo n.º 29
0
void test_blas_2<V, M, N>::test () {
    {
        V v1 (N), v2 (N);
        M m (N, N);

        // _t_mv
        initialize_vector (v1);
        initialize_matrix (m);
        ublas::blas_2::tmv (v1, m);
        std::cout << "tmv (v1, m) = " << v1 << std::endl;
        initialize_vector (v1);
        initialize_matrix (m);
        ublas::blas_2::tmv (v1, ublas::trans (m));
        std::cout << "tmv (v1, trans (m)) = " << v1 << std::endl;
#ifdef USE_STD_COMPLEX
        initialize_vector (v1);
        initialize_matrix (m);
        ublas::blas_2::tmv (v1, ublas::herm (m));
        std::cout << "tmv (v1, herm (m)) = " << v1 << std::endl;
#endif

        // _t_sv
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::lower_tag ());
        ublas::blas_2::tsv (v1, m, ublas::lower_tag ());
        std::cout << "tsv (v1, m) = " << v1 << " " << ublas::prod (m, v1) - v2 << std::endl;
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::upper_tag ());
        ublas::blas_2::tsv (v1, ublas::trans (m), ublas::lower_tag ());
        std::cout << "tsv (v1, trans (m)) = " << v1 << " " << ublas::prod (ublas::trans (m), v1) - v2 << std::endl;
#ifdef USE_STD_COMPLEX
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::upper_tag ());
        ublas::blas_2::tsv (v1, ublas::herm (m), ublas::lower_tag ());
        std::cout << "tsv (v1, herm (m)) = " << v1 << " " << ublas::prod (ublas::herm (m), v1) - v2 << std::endl;
#endif
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::upper_tag ());
        ublas::blas_2::tsv (v1, m, ublas::upper_tag ());
        std::cout << "tsv (v1, m) = " << v1 << " " << ublas::prod (m, v1) - v2 << std::endl;
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::lower_tag ());
        ublas::blas_2::tsv (v1, ublas::trans (m), ublas::upper_tag ());
        std::cout << "tsv (v1, trans (m)) = " << v1 << " " << ublas::prod (ublas::trans (m), v1) - v2 << std::endl;
#ifdef USE_STD_COMPLEX
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m, ublas::lower_tag ());
        ublas::blas_2::tsv (v1, ublas::herm (m), ublas::upper_tag ());
        std::cout << "tsv (v1, herm (m)) = " << v1 << " " << ublas::prod (ublas::herm (m), v1) - v2 << std::endl;
#endif

        // _g_mv
        // _s_mv
        // _h_mv
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m);
        ublas::blas_2::gmv (v1, value_type (1), value_type (1), m, v2);
        std::cout << "gmv (v1, 1, 1, m, v2) = " << v1 << std::endl;
        ublas::blas_2::gmv (v1, value_type (1), value_type (1), ublas::trans (m), v2);
        std::cout << "gmv (v1, 1, 1, trans (m), v2) = " << v1 << std::endl;
#ifdef USE_STD_COMPLEX
        ublas::blas_2::gmv (v1, value_type (1), value_type (1), ublas::herm (m), v2);
        std::cout << "gmv (v1, 1, 1, herm (m), v2) = " << v1 << std::endl;
#endif

        // _g_r
        // _g_ru
        // _g_rc
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m);
        ublas::blas_2::gr (m, value_type (1), v1, v2);
        std::cout << "gr (m, 1, v1, v2) = " << m << std::endl;
        ublas::blas_2::gr (m, value_type (1), v1, ublas::conj (v2));
        std::cout << "gr (m, 1, v1, conj (v2)) = " << m << std::endl;

        // _s_r
        initialize_vector (v1);
        initialize_matrix (m);
        ublas::blas_2::sr (m, value_type (1), v1);
        std::cout << "sr (m, 1, v1) = " << m << std::endl;

#ifdef USE_STD_COMPLEX
        // _h_r
        initialize_vector (v1);
        initialize_matrix (m);
        ublas::blas_2::hr (m, value_type (1), v1);
        std::cout << "hr (m, 1, v1) = " << m << std::endl;
#endif

        // _s_r2
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m);
        ublas::blas_2::sr2 (m, value_type (1), v1, v2);
        std::cout << "sr2 (m, 1, v1, v2) = " << m << std::endl;

#ifdef USE_STD_COMPLEX
        // _h_r2
        initialize_vector (v1);
        initialize_vector (v2);
        initialize_matrix (m);
        ublas::blas_2::hr2 (m, value_type (1), v1, v2);
        std::cout << "hr2 (m, 1, v1, v2) = " << m << std::endl;
#endif
    }
}
Ejemplo n.º 30
0
    void operator () (VP &v1, VP &v2, VP &v3) const {
        try {
            value_type t;
            size_type i;
            real_type n;

            // Copy and swap
            initialize_vector (v1);
            initialize_vector (v2);
            v1 = v2;
            std::cout << "v1 = v2 = " << v1 << std::endl;
            v1.assign_temporary (v2);
            std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
            v1.swap (v2);
            std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;

            // Unary vector operations resulting in a vector
            initialize_vector (v1);
            v2 = - v1;
            std::cout << "- v1 = " << v2 << std::endl;
            v2 = ublas::conj (v1);
            std::cout << "conj (v1) = " << v2 << std::endl;

            // Binary vector operations resulting in a vector
            initialize_vector (v1);
            initialize_vector (v2);
            v3 = v1 + v2;
            std::cout << "v1 + v2 = " << v3 << std::endl;

            v3 = v1 - v2;
            std::cout << "v1 - v2 = " << v3 << std::endl;

            // Scaling a vector
            t = N;
            initialize_vector (v1);
            v2 = value_type (1.) * v1;
            std::cout << "1. * v1 = " << v2 << std::endl;
            v2 = t * v1;
            std::cout << "N * v1 = " << v2 << std::endl;
            initialize_vector (v1);
            v2 = v1 * value_type (1.);
            std::cout << "v1 * 1. = " << v2 << std::endl;
            v2 = v1 * t;
            std::cout << "v1 * N = " << v2 << std::endl;

            // Some assignments
            initialize_vector (v1);
            initialize_vector (v2);
#ifdef BOOST_UBLAS_USE_ET
            v2 += v1;
            std::cout << "v2 += v1 = " << v2 << std::endl;
            v2 -= v1;
            std::cout << "v2 -= v1 = " << v2 << std::endl;
#else
            v2 = v2 + v1;
            std::cout << "v2 += v1 = " << v2 << std::endl;
            v2 = v2 - v1;
            std::cout << "v2 -= v1 = " << v2 << std::endl;
#endif
            v1 *= value_type (1.);
            std::cout << "v1 *= 1. = " << v1 << std::endl;
            v1 *= t;
            std::cout << "v1 *= N = " << v1 << std::endl;

            // Unary vector operations resulting in a scalar
            initialize_vector (v1);
            t = ublas::sum (v1);
            std::cout << "sum (v1) = " << t << std::endl;
            n = ublas::norm_1 (v1);
            std::cout << "norm_1 (v1) = " << n << std::endl;
            n = ublas::norm_2 (v1);
            std::cout << "norm_2 (v1) = " << n << std::endl;
            n = ublas::norm_inf (v1);
            std::cout << "norm_inf (v1) = " << n << std::endl;

            i = ublas::index_norm_inf (v1);
            std::cout << "index_norm_inf (v1) = " << i << std::endl;

            // Binary vector operations resulting in a scalar
            initialize_vector (v1);
            initialize_vector (v2);
            t = ublas::inner_prod (v1, v2);
            std::cout << "inner_prod (v1, v2) = " << t << std::endl;
        }
        catch (std::exception &e) {
            std::cout << e.what () << std::endl;
        }
        catch (...) {
            std::cout << "unknown exception" << std::endl;
        }
    }