static void assert_linearity(linear_map_t A, int n, void *e, int ntrials)
{
	int sn = n * sizeof(double);
	double *x = xmalloc(sn);
	double *y = xmalloc(sn);
	double *Ax = xmalloc(sn);
	double *Ay = xmalloc(sn);
	double *xpy = xmalloc(sn);
	double *AxpAy = xmalloc(sn);
	double *Axpy = xmalloc(sn);
	for (int i = 0; i < ntrials; i++) {
		fill_random_vector(x, n);
		fill_random_vector(y, n);
		vector_sum(xpy, x, y, n);
		A(Ax, x, n, e);
		A(Ay, y, n, e);
		A(Axpy, xpy, n, e);
		vector_sum(AxpAy, Ax, Ay, n);
		double error = 0;
		for (int k = 0; k < n; k++)
			error = hypot(error, fabs(AxpAy[k]-Axpy[k]));
		fprintf(stderr, "error linearity = %g\n", error);
		if (error > 1e-12)
			fprintf(stderr, "WARNING: non linear!\n");

	}
	free(x);
	free(y    );
	free(Ax   );
	free(Ay   );
	free(xpy  );
	free(AxpAy);
	free(Axpy );
}
Пример #2
0
int main(int argc, char** argv) {
	double *v, *ws, *wp, no;
	int l;
	int n = atoi(argv[1]);
        int num_th = atoi(argv[2]);
	// check whether command line arguments are correct
        if (n < num_th){
                printf("num_th has to be smaller or equal than n\n");
                return 1;
        }
        if (n % num_th != 0){
                printf("n has to be a multiple of num_th\n");
                return 1;
        }
	l = n/num_th;
 	// allocate memory for A according to C99 standard (variabel size array)
        double (*A)[n] = malloc(n*n*sizeof(double));
        v = (double*) malloc(n*sizeof(double));
        wp = (double*) malloc(n*sizeof(double));
        ws = (double*) malloc(n*sizeof(double));
        // fill A and v with random numbers
        fill_random_matrix(n, n, A);
        fill_random_vector(v, n);
        multiply_matrix_vector(n, A, v, ws);
#	pragma omp parallel num_threads(num_th)	
	multiply_matrix_vector_para(n, A, v, wp, l);
	no = norm(ws,wp,n);
	printf("norm=%f\n",no);
	free(v);
        free(ws);
        free(wp);
        free(A);
	return 0;
}
Пример #3
0
int main(int argc, char** argv) {
	int n=5;
	double *v, *ws, *wp, no;
/*	Matrix A and vector v are to be muliplied
	The solution is ws for sequential multiplication and
	wp for parallel multiplication 
*/
	// allocate memory for A according to C99 standard (variabel size array)
	double (*A)[n] = malloc(n*n*sizeof(double));
	v = (double*) malloc(n*sizeof(double));
	wp = (double*) malloc(n*sizeof(double));
	ws = (double*) malloc(n*sizeof(double));
	// fill A and v with random numbers
	fill_random_matrix(n, n, A);
	fill_random_vector(v, n);
	multiply_matrix_vector(n, A, v, ws);
	// calculate the norm of ws - wp
	// The parallel algorithm works correct if no == 0;
	no = norm(ws,wp,n);
	printf("norm=%f\n",no);
	free(v);
	free(ws);
	free(wp);
	free(A);

	return 0;
}
Пример #4
0
int main(int argc, char** argv) {
	int i;
	int n = atoi(argv[1]);
	int num_th = atoi(argv[2]);
	double *v, *ws, *wp, no;
	// check whether input is correct
	if (n < num_th){
		printf("num_th has to be smaller or equal than n\n");
		return 1;
	}

	if (n % num_th != 0){
		printf("n has to be a multiple of num_th\n");
		return 1;
	}

	// lines to calculate per thread
	int l = n / num_th;
	// allocate memory for A according to C99 standard (variabel size array)
        double (*A)[n] = malloc(n*n*sizeof(double));
        v = (double*) malloc(n*sizeof(double));
        wp = (double*) malloc(n*sizeof(double));
        ws = (double*) malloc(n*sizeof(double));

	thread_parm_t *parm[n];
	pthread_t thread[n];
	fill_random_matrix(n, n, A);
	fill_random_vector(v, n);
	// calculate A * v sequentially as a reference
	multiply_matrix_vector(n, A, v, ws);
	// for each thread one parameter list is creadted
	for (i=0; i<num_th; i++){
	parm[i] = malloc(sizeof(thread_parm_t));
	parm[i]->matrix=A;
	parm[i]->vec=v;
	parm[i]->result=wp;
	parm[i]->dimension=n;
	parm[i]->row=i;
	parm[i]->lines=l;
	}
	// create all threads
	for (i=0; i<num_th; i++){
	pthread_create(&thread[i], NULL, multiply_matrix_vector_para, (void *)parm[i]);
	}
	//wait for all threads to complete	
	for (i=0; i<num_th; i++){
	pthread_join(thread[i],NULL);
	}
	// calcualte norm of wp - ws
	no = norm(wp,ws,n);
	printf("norm=%f\n",no);
	free(v);
	free(ws);
	free(wp);
	free(A);

	return 0;
}
static void assert_symmetry(linear_map_t A, int n, void *e, int ntrials)
{
	int sn = n * sizeof(double);
	double *x = xmalloc(sn);
	double *y = xmalloc(sn);
	double *Ax = xmalloc(sn);
	double *Ay = xmalloc(sn);
	for (int i = 0; i < ntrials; i++) {
		fill_random_vector(x, n);
		fill_random_vector(y, n);
		A(Ax, x, n, e);
		A(Ay, y, n, e);
		double Axy = scalar_product(Ax, y, n);
		double xAy = scalar_product(x, Ay, n);
		double error = fabs(Axy-xAy);
		fprintf(stderr, "error symmetry = %g\n", error);
		if (error > 1e-12)
			fprintf(stderr, "WARNING: non symmetric!\n");
	}
	free(x);
	free(y);
	free(Ax);
	free(Ay);
}
static void assert_positivity(linear_map_t A, int n, void *e, int ntrials)
{
	int sn = n * sizeof(double);
	double *x = xmalloc(sn);
	double *Ax = xmalloc(sn);
	for (int i = 0; i < ntrials; i++) {
		fill_random_vector(x, n);
		A(Ax, x, n, e);
		double Axx = scalar_product(Ax, x, n);
		double pos = Axx;
		fprintf(stderr, "positivity = %g\n", pos);
		if (pos < -1e-12)
			fprintf(stderr, "WARNING: non positive!\n");
	}
	free(x);
	free(Ax);
}
Пример #7
0
int main(int argc, char** argv) {
	int n, m;
	double **A, *v, *ws, *wp, no;

	n = 5;
	m= 5;

	A=make_matrix(n, m);
	v=make_vector(n);
	wp=make_vector(n);
	ws=make_vector(n);
	A=fill_random_matrix(A, n, m);
	v=fill_random_vector(v, n);
	ws=multiply_matrix_vector(A, v, ws, n, m);
	wp=mulitply_matrix_vector_para(A, v, wp, n, m);
	no = norm(ws,wp,n);
	printf("norm=%f\n",no);

	return 0;
}