void populate_matrix(TreeNode<T>* root, bool* matrix, int matrix_size)
{
	if(!root) return;

	if(root->left)
	{
		populate_matrix(root->left, matrix, matrix_size);

		matrix[root->data * matrix_size + root->left->data] = 1;
		for(int i = 0; i < matrix_size; ++i)
		{
			if(matrix[root->left->data * matrix_size + i] == 1)
				matrix[root->data * matrix_size + i] = 1;
		}
	}

	if(root->right)
	{
		populate_matrix(root->right, matrix, matrix_size);
	
		matrix[root->data * matrix_size + root->right->data] = 1;
		for(int i = 0; i < matrix_size; ++i)
		{
			if(matrix[root->right->data * matrix_size + i] == 1)
				matrix[root->data * matrix_size + i] = 1;
		}
	}
}
예제 #2
0
int main(void){
    int tid; /*this task id */
    int bufid, master, bytes, msgtag, source;
    char * graph_text = NULL;
    int **matrix;
    int degree, edges;
    int len;
    int i;
    int greycodeLength;
    
    tid = pvm_mytid();          /*enroll in pvm */
    master = pvm_parent();      /*get the master id */

    while (1){    
        /*Get the first graph */
        pvm_initsend(PvmDataDefault);
        pvm_pkstr("");                                        /*Just put something in the buffer */
        pvm_send(master, MSGREQTASK);                         /* Request a task */
        bufid = pvm_recv(master, MSGTASK);                    /* Get the task */
        pvm_bufinfo(bufid, &bytes, &msgtag, &source);  /* Get the size of the text */
        graph_text = malloc(bytes);                           
        pvm_upkstr(graph_text);                               /* Unpack the text */

        len = strlen(graph_text);
        if(graph_text[len - 1] == '\n')
            graph_text[len - 1] = '\0';
            

        matrix = create_matrix(graph_text, &degree);          /*Generate the appropriate matrix */
        populate_matrix(graph_text, matrix, &edges, degree);

        greycodeLength = 1 << degree;
        // Initialize code (where the greycode is stored)
        int * vals = calloc(2 * greycodeLength, sizeof(int));
        int ** code = malloc(greycodeLength * sizeof(int *)); 

       for(i = 0; i < greycodeLength; i++){                     /*assign rows within the allocated space */ 
          code[i] = vals + i*2;
        }
	
        /*** DO GREYCODE ALGORITHM ON THE MATRIX ***/
        if(greycode(degree, matrix, code) == 1){                      /*If we found a code */
            pvm_initsend(PvmDataDefault);
            pvm_pkstr(graph_text);
            pvm_send(master, MSGCODE);
        } else{                                               /*Didnt find a code */
            pvm_initsend(PvmDataDefault);
            pvm_pkstr(graph_text);
            pvm_send(master,MSGNOCODE);
        }

        destroy_matrix(matrix);                               /* Free the memory associated with the matrix */
        destroy_matrix(code);
	    free(graph_text);
    }
 
    pvm_exit();
    exit(0);

}
예제 #3
0
int calculated_weight(int values[], int rows, int columns)
{
  int collection[rows][columns];

  populate_matrix(rows, columns, collection, values);
  int result = shortest_path(rows, columns, collection);  

  return result;
}
예제 #4
0
//convert the ray to the matrix visualisation
void ray_of_ints_to_matrix_picture(struct ray* r){
    int mtx_rows = r->length;
    // TODO: change mtx_cols to max int in array (within reason ...)
    int mtx_cols = r->length;
    printf("%i\n", r->length);
    char m[mtx_rows][mtx_cols];
    populate_matrix(mtx_rows,mtx_cols, m, r->array, UNIT, BLANK);
    display_matrix(mtx_rows,mtx_cols, m);
    
}
bool* solve(TreeNode<T>* root)
{
	int nodes_in_tree = num_nodes_in_tree(root);
	bool* matrix = new bool[nodes_in_tree * nodes_in_tree];

	populate_matrix(root, matrix, nodes_in_tree);

	for(int i = 0; i < nodes_in_tree; ++i)
	{
		for(int j = 0; j < nodes_in_tree; ++j)
		{
			std::cout << matrix[i*nodes_in_tree + j] << " ";
		}
		std::cout << std::endl;
	}

	return matrix;
} 
int main(int argc, char *argv[]){
	double t1, t2; 
	int my_rank;
	int proc_n;
	int omp_rank;
	int i;
	int work_sent = 0;
	int work_received = 0;
	run_quick = atoi(argv[1]);
	t1 = MPI_Wtime(); 
	MPI_Status status;

	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
	MPI_Comm_size(MPI_COMM_WORLD, &proc_n);

	if (my_rank == 0){
		populate_matrix();
		while(work_sent < ROWS) {
				for (i = 0; i < proc_n - 1 && work_sent < ROWS; ++i){
						MPI_Send(matrix[work_sent], THREADS*COLUMNS,
										MPI_INT, i+1, WORK_TAG,
										MPI_COMM_WORLD);
						work_sent+=THREADS;
				}
				for (i = 0; i < proc_n - 1 && work_received < ROWS; ++i) {
						MPI_Recv(matrix[work_received], THREADS*COLUMNS,
										MPI_INT, i+1, WORK_TAG,
										MPI_COMM_WORLD, &status);
						work_received+=THREADS;
				}
		}	
		int terminator = proc_n;
		while (--terminator)
			MPI_Send(0, 0, MPI_INT, terminator, SUICIDE_TAG, MPI_COMM_WORLD);

		print_matrix();
	}
	else{
		while(1) {
				int work_pool[THREADS][COLUMNS];
				MPI_Recv(work_pool, THREADS*COLUMNS,
								MPI_INT, 0, MPI_ANY_TAG,
								MPI_COMM_WORLD, &status);

				if (status.MPI_TAG == SUICIDE_TAG) {
						MPI_Finalize();
						return 0;
				}
				#pragma omp parallel for
				for (i = 0; i < THREADS; ++i) {
					if(run_quick){
						printf("quick\n");
						qsort(work_pool[i], COLUMNS, sizeof(int), compare);
					}
					else{
						printf("bubble\n");
						bubble_sort(COLUMNS, work_pool[i]);
					}
				}
				#pragma omp barrier
				MPI_Send(work_pool, THREADS*COLUMNS,
								MPI_INT, 0, WORK_TAG,
								MPI_COMM_WORLD);
		}
	}
	t2 = MPI_Wtime(); 
	MPI_Finalize();
	printf( "Elapsed time is %f\n", t2 - t1 ); 
}
예제 #7
0
int main() {
    int id, sarn, sacn, sbcn, sbrn, LCM, t, i, j, l, jp, ip, local_block;
    double t1, t2;
    Matrix A, B, C, // global matrices
           sa, sb, sc, //local matrices of the process
           subsa, subsb, subsc; //local submatrices of the process
    
    create_matrix(&A, A_RN, A_CN);
    create_matrix(&B, B_RN, B_CN);
    create_matrix(&C, A_RN, B_CN);

    populate_matrix(&A);
    populate_matrix(&B);

    //printf("\nMatrices generadas\n");
    //print_matrix(&A, 'A');
    //printf("\n");
    //print_matrix(&B, 'B');

    //submatrices sizes
    sarn = (A_RN / M) * (M / P);
    sacn = (A_CN / K) * (K / Q);
    sbrn = (B_RN / K) * (K / P);
    sbcn = (B_CN / N) * (N / Q);
    
    local_block =  BLOCK_SZ;
    
    LCM = lcm(P, Q);
    shift_matrix_left(&A, BLOCK_SZ, 1);
    shift_matrix_up(&B, BLOCK_SZ, 1);

    t1 = omp_get_wtime();
    #pragma omp parallel default(none) shared(A, B, C, sarn, sacn, sbrn, sbcn, LCM, local_block) \
                                       private(sa, sb, sc, id, t, i, j, l, jp, ip, subsa, subsb, subsc) num_threads(P * Q)
    {
        id = omp_get_thread_num();

        create_matrix(&sa, sarn, sacn);
        create_matrix(&sb, sbrn, sbcn);
        create_matrix(&sc, sarn, sbcn);
        
        for(t = 0; t < LCM; t++){
            
            create_matrix(&subsa, local_block, local_block);
            create_matrix(&subsb, local_block, local_block);
            create_matrix(&subsc, local_block, local_block);
            
            rsync_process_blocks(&A, &sa, id, FALSE);
            rsync_process_blocks(&B, &sb, id, FALSE);
            
            for(i = 0; i < (M / P); i++){
                for(j = 0; j < (N / Q); j++){
                    for(l = 0; l < (K / LCM); l++){
                        jp = (j % K + l * LCM / Q) % (K / Q);
                        ip = (i % K + l * LCM / P) % (K / P);

                        rsync_process_submatrix(&sc, &subsc, i, j, FALSE);
                        rsync_process_submatrix(&sa, &subsa, i, jp, FALSE);
                        rsync_process_submatrix(&sb, &subsb, ip, j, FALSE);
                        
                        matrix_product(&subsc, &subsa, &subsb);

                        rsync_process_submatrix(&sc, &subsc, i, j, TRUE);
                    }
                }
            }
            
            rsync_process_blocks(&C, &sc, id, TRUE);
            
            #pragma omp barrier

            #pragma omp single
            {
                shift_matrix_left(&A, BLOCK_SZ, 0);
                shift_matrix_up(&B, BLOCK_SZ, 0);
            }
        }
    }
    t2 = omp_get_wtime();

    printf("\nResultado\n");
    //print_matrix(&C, 'C');
    printf("\nTiempo: %.4f segundos\n", (t2 - t1));

    return 0;
}