int main(int argc, char** argv){ // Number of threads to use int nThreads = 1; // Matrix sizes int m = 2; int n = 2; int k = 2; // Reading command line arguments if(argc != 5){ printf("useage: gemm nThreads m n k\n"); exit(-1); } else{ nThreads = atoi(argv[1]); m = atoi(argv[2]); n = atoi(argv[3]); k = atoi(argv[4]); } pthread_t threads[nThreads]; // Initializing matrices float alpha = -2; float beta = 1; float* A = create_random_matrix(m,k); float* B = create_random_matrix(k,n); float* C = create_random_matrix(m,n); for (int t = 0; t < nThreads; t++) { int x_min = t * n / nThreads; int x_max = x_min + n / nThreads; sub_matrix_args *args = create_args(A, B, C, alpha, beta, x_min, x_max, 0, m, n, k); pthread_create(&threads[t], NULL, calculate_sub_matrix, (void *) args); } // Printing result print_matrix(A, m,k); print_matrix(B, k,n); print_matrix(C, m,n); }
int main(int argc, char** argv){ // Number of threads to use int nThreads = 1; // Matrix sizes int m = 2; int n = 2; int k = 2; // Reading command line arguments if(argc != 5){ printf("useage: gemm nThreads m n k\n"); exit(-1); } else{ nThreads = atoi(argv[1]); m = atoi(argv[2]); n = atoi(argv[3]); k = atoi(argv[4]); } // Initializing matrices float alpha = -2; float beta = 1; float* A = create_random_matrix(m,k); float* B = create_random_matrix(k,n); float* C = create_random_matrix(m,n); // Performing computation for(int x = 0; x < n; x++){ for(int y = 0; y < m; y++){ C[y*n + x] *= beta; for(int z = 0; z < k; z++){ C[y*n + x] += alpha*A[y*k+z]*B[z*n + x]; } } } //Printing result // print_matrix(A, m,k); // print_matrix(B, k,n); // print_matrix(C, m,n); }
int main(int argc, char *argv[]) { pclu_context *pclu = pclu_create_context(); printf("\n%s\n", pclu_context_info(pclu)); matrix *aa = create_random_matrix(SIZE, SIZE); matrix *bb = create_identity_matrix(SIZE, SIZE); #if PRINT_DATA printf("Matrix aa:\n"); print_matrix(aa); printf("\n"); printf("Matrix bb:\n"); print_matrix(bb); printf("\n"); #endif timer* tt1 = timer_alloc(); matrix *cc = create_matrix(SIZE, SIZE); matrix_multiply_cl(pclu, cc, aa, bb); double tm1 = timer_read(tt1); timer_free(tt1); printf("Matrix_multiply_cl took %.04f seconds.\n", tm1); #if PRINT_DATA printf("Matrix cc:\n"); print_matrix(cc); printf("\n"); #endif timer* tt = timer_alloc(); check_result(aa, cc); double ct = timer_read(tt); timer_free(tt); printf("Check result took %.04f seconds.\n", ct); destroy_matrix(aa); destroy_matrix(bb); pclu_destroy_context(pclu); return 0; }
// O primeiro parâmetro é o tamanho do vetor, e o segundo é o número de threads. int main(int argc, char *argv[]) { if (argc!=3) { printf("Número incorreto de parâmetros\n"); printf("Uso: ./<nome do programa> <ordem da matriz> <qtd. de threads>\n"); return 0; } int thread_quantity, sum, sum_thread=0, counter; int offset=0, partition_size; long int **matrix_copy; int_tuple *offset_vector; double time_main, time_thread; // Um vetor de pthreads permite a criação dinâmica de threads. pthread_t *thread_vector; sscanf(argv[1],"%d",&matrix_size); sscanf(argv[2],"%d",&thread_quantity); printf("Criando dados para as matrizes... "); thread_vector=(pthread_t*)malloc(sizeof(pthread_t)*thread_quantity); offset_vector=(int_tuple*)malloc(sizeof(int_tuple)*thread_quantity); partition_size=matrix_size/thread_quantity; partition_size--; for(counter=0; counter<thread_quantity-1; counter++) { offset_vector[counter].start=offset; offset=offset+partition_size; offset_vector[counter].end=offset; offset++; } offset_vector[thread_quantity-1].start=offset; offset_vector[thread_quantity-1].end=matrix_size-1; matrix_a=create_random_matrix(); matrix_b=create_random_matrix(); printf("feito.\n\n"); matrix_result=(long int**)malloc(sizeof(long int*)*matrix_size); for(counter=0; counter<matrix_size; counter++) matrix_result[counter]=(long int*)malloc(sizeof(long int)*matrix_size); printf("Iniciando as contas na função main().\n"); set_start_time(); multiply_matrix(); set_end_time(); time_main=end_time-start_time; printf("As contas na main() foram finalizadas.\n"); /* Aloca uma nova matriz para fazer uma cópia do resultado. * Isso é feito para saber se a multiplicação está igual. */ matrix_copy=(long int**)malloc(sizeof(long int*)*matrix_size); for(counter=0; counter<matrix_size; counter++) matrix_copy[counter]=(long int*)malloc(sizeof(long int)*matrix_size); for(counter=0; counter<matrix_size; counter++) { for(offset=0; offset<matrix_size; offset++) matrix_copy[counter][offset]=matrix_result[counter][offset]; } printf("\nIniciando as contas com %d threads.\n",thread_quantity); // Inicia as contas com as threads, assim como a marcação de tempo. set_start_time(); for(counter=0; counter<thread_quantity; counter++) pthread_create(thread_vector+counter,NULL,threaded_matrix_multiplier,(void*)(offset_vector+counter)); for(counter=0; counter<thread_quantity; counter++) pthread_join(thread_vector[counter],NULL); set_end_time(); time_thread=end_time-start_time; printf("As threads foram finalizadas.\n\n"); check_matrix_match(matrix_copy); printf("\n"); printf("Tempo na main(): %f segundos\n",time_main); printf("Tempo com threads: %f segundos\n",time_thread); //Libera tudo que foi alocado. free(thread_vector); free(offset_vector); for(counter=0; counter<matrix_size; counter++) { free(matrix_a[counter]); free(matrix_b[counter]); free(matrix_result[counter]); free(matrix_copy[counter]); } free(matrix_a); free(matrix_b); free(matrix_result); free(matrix_copy); return 0; }
int main() { // set a new seed for the random number generator // at each start (system time) srand(time(NULL)); ////////////////////////// // // assignment code starts here :) // ////////////////////////// int n = 4096; int N = 20; // create random matrix and measure the time elapsed for // creation tic(); float** matrix = create_random_matrix(n); double elapsed_time = toc(); printf("Elapsed time creating a random matrix: %f seconds\n",elapsed_time); double sum = 0; elapsed_time = 0; for (int m = 0; m < N; m++) { tic(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sum += matrix[i][j]; } } elapsed_time += toc(); } sum /= N; elapsed_time /= N; printf("row-wise %f seconds -> %f\n", elapsed_time, sum); sum = 0; elapsed_time = 0; for (int m = 0; m < N; m++) { tic(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sum += matrix[j][i]; } } elapsed_time += toc(); } sum /= N; elapsed_time /= N; printf("column-wise %f seconds -> %f\n", elapsed_time, sum); int n2 = n * n; int* indices = malloc(n2 * sizeof(int)); for (int i = 0; i < n2; i++) { indices[i] = (rand() / RAND_MAX) * n; }; sum = 0; elapsed_time = 0; for (int m = 0; m < N; m++) { tic(); for (int i = 0; i < n2; i++) { sum += matrix[indices[i]][indices[n2 - i - 1]]; } elapsed_time += toc(); } sum /= N; elapsed_time /= N; printf("random %f seconds -> %f\n", elapsed_time, sum); free(indices); free_matrix(matrix, n); return 0; }
int main(){ printf("Let's test some linear algebra functions....\n\n"); // create a random nxn matrix for later use matrix_t A = create_random_matrix(DIM,DIM); printf("New Random Matrix A:\n"); print_matrix(A); // also create random vector vector_t b = create_random_vector(DIM); printf("\nNew Random Vector b:\n"); print_vector(b); // do an LUP decomposition on A matrix_t L,U,P; LUP_decomposition(A,&L,&U,&P); printf("\nL:\n"); print_matrix(L); printf("U:\n"); print_matrix(U); printf("P:\n"); print_matrix(P); // do a QR decomposition on A matrix_t Q,R; QR_decomposition(A,&Q,&R); printf("\nQR Decomposition of A\n"); printf("Q:\n"); print_matrix(Q); printf("R:\n"); print_matrix(R); // get determinant of A float det = matrix_determinant(A); printf("\nDeterminant of A : %8.4f\n", det); // get an inverse for A matrix_t Ainv = invert_matrix(A); if(A.initialized != 1) return -1; printf("\nAinverse\n"); print_matrix(Ainv); // multiply A times A inverse matrix_t AA = multiply_matrices(A,Ainv); if(AA.initialized!=1) return -1; printf("\nA * Ainverse:\n"); print_matrix(AA); // solve a square linear system vector_t x = lin_system_solve(A, b); printf("\nGaussian Elimination solution x to the equation Ax=b:\n"); print_vector(x); // now do again but with qr decomposition method vector_t xqr = lin_system_solve_qr(A, b); printf("\nQR solution x to the equation Ax=b:\n"); print_vector(xqr); // If b are the coefficients of a polynomial, get the coefficients of the // new polynomial b^2 vector_t bb = poly_power(b,2); printf("\nCoefficients of polynomial b times itself\n"); print_vector(bb); // clean up all the allocated memory. This isn't strictly necessary since // we are already at the end of the program, but good practice to do. destroy_matrix(&A); destroy_matrix(&AA); destroy_vector(&b); destroy_vector(&bb); destroy_vector(&x); destroy_vector(&xqr); destroy_matrix(&Q); destroy_matrix(&R); printf("DONE\n"); return 0; }