/**
 * test first strategy: explorer_strategy() + print generated matrix
 * @strategy
 */
void test_explorer_strategy(mpi::communicator world, int limit) {
	// primes number data structure
	ms_vector primes;
	// vector to collecting all generated matrix
	vector<ms_matrix> list;
	// my rank
	int rank = world.rank();

	if (rank == 0) {
		cout << "Test the Explorer strategy...\n";
	}

	// generate primes numbers
	find_prime_numbers(world, limit, &primes);

	// send to all the prime numbers
	mpi::broadcast(world, primes, 0);

	int length = 3;
	ms_matrix matrix(length, ms_vector(length));

	fill_random_matrix(&primes, &matrix, rank);

	// receive all generated matrix
	mpi::gather(world, matrix, list, 0);

	if (rank == 0) {
		// print all generated matrix
		cout << "Print all generated matrix:\n";
		print_list_matrix(list);
	}
}
Ejemplo n.º 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;
}
Ejemplo n.º 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;
}
Ejemplo n.º 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;
}
Ejemplo n.º 5
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;
}