void keyboard (unsigned char key, int x, int y) { vector *vec = camera_direction(-state.camera.pitch, state.camera.yaw, state.camera.roll); vector *move = malloc_vector(); copy_vector(move, vec); scale(vec, 10); rotate(move, 0, 90, 0); printf("pitch: %f, yaw: %f\n", state.camera.pitch, state.camera.yaw); switch(key) { case 'q': exit(0); break; case 'w': state.camera.x += vec->data[0]; state.camera.y += vec->data[1]; state.camera.z += vec->data[2]; break; case 'a': state.camera.x += 10*move->data[0]; state.camera.z += 10*move->data[2]; break; case 's': state.camera.x -= vec->data[0]; state.camera.y -= vec->data[1]; state.camera.z -= vec->data[2]; break; case 'd': state.camera.x -= 10*move->data[0]; state.camera.z -= 10*move->data[2]; break; case 'r': case 'R': if(state.filling) { glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); } else { glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); } state.filling = !state.filling; break; default: break; } free_vector(vec); printf("x: %f, y: %f, z: %f\n", state.camera.x, state.camera.y, state.camera.z); }
int conjugate_gradient(Cel *A, double *b, int n){ double *p,*r,*aux; /* aux Guarda o vetor resultado do produto A*(pn-1)*/ double alpha, beta, temp; /* temp guarda o produto do os vetores rn-1t*rn-1*/ int i,k; p = malloc_vector(n); r = malloc_vector(n); aux = malloc_vector(n); /* Incializacao */ for(i = 0; i < n; i++){ r[i] = b[i]; p[i] = b[i]; b[i] = 0; /* x0 */ } /* Metodo */ for(k = 1; ; k++){ /* Alpha */ temp = calculate_vv_product(r,r,n); /* rn-1t*rn-1t */ calculate_mv_product(A,p,aux,n); alpha = temp/calculate_vv_product(p,aux,n); /* Residuo */ calculate_ve_product(aux,aux,alpha,n); calculate_vv_difference(r,aux,n); /* X */ calculate_ve_product(aux,p,alpha,n); calculate_vv_sum(b,aux,n); if(calculate_vector_norm2(r,n) < ERROR) break; /* Beta */ beta = calculate_vv_product(r,r,n)/temp; /* p */ calculate_ve_product(p,p,beta,n); calculate_vv_sum(p,r,n); } free(aux); return 0; }
int main(int argc, char *argv[]){ int i, j, N, oc, pid, nprocs; FILE *in, *out = stdout; char input[50]; double *b, *A, *u, *l, *result; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &pid); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); while ((oc = getopt(argc, argv, "s:i:")) != -1) { switch(oc) { case 's': N = atoi(optarg); break; case 'i': strcpy(input, optarg); break; } } if (MASTER == pid) { double stime, ftime, ssend, fsend, srecv, frecv; malloc_all(&A, &l, &u, &b, N); malloc_vector(&result, N); open_file(&in, input); read_transpose(A, l, u, b, N, in); stime = MPI_Wtime(); ssend = MPI_Wtime(); for (i=0; i<N; i++) MPI_Send(&u[i*N], N, MPI_DOUBLE, i%(nprocs-1)+1, TAG, MPI_COMM_WORLD); for (i=0; i<N-1; i++) MPI_Bcast(&l[i *N+ i+1], N-(i+1), MPI_DOUBLE, i%(nprocs-1)+1, MPI_COMM_WORLD); fsend = MPI_Wtime(); srecv = MPI_Wtime(); for (i=0; i<N; i++) MPI_Recv(&u[i*N], N, MPI_DOUBLE, i%(nprocs-1)+1, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); frecv = MPI_Wtime(); ftime = MPI_Wtime(); if (check_result(A, result, l, u, b, N)) fprintf(out, "( %s, %d ): %lf %lf %lf\n", input, nprocs, ftime-stime, fsend-ssend, frecv-srecv); else fprintf(out, "( %s, %d ): ERRO NA VERIFICAÇÃO\n", input, nprocs); free_all(A, l, u, b); } else { decomposeMPI(N, nprocs, pid); } MPI_Finalize(); return 0; }
int main (int argc, char* argv[]) { int n; double *b; Cel *A = NULL; FILE *input; char opt; /* Tratamento da entrada */ if(argc < 2) { error(NO_OPTION); } else { opt = argv[1][0]; switch(opt) { case '1': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.01,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '2': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.05,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '3': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.1,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case '4': n = MATRIX_TEST_SIZE; A = malloc_matrix(n); b = malloc_vector(n); build_test_matrix(A,0.3,n); build_test_vector(b,n); conjugate_gradient(A,b,n); free_matrix(n,A); free(b); break; case 'r': if(argc < 3) error(NO_FILE); else { input = fopen(argv[2],"r"); if(input == NULL) error(OPENING_FILE); n = read_dimension(input); A = malloc_matrix(n); b = malloc_vector(n); read_matrix_system(input,n,A,b); fclose(input); conjugate_gradient(A,b,n); free_matrix(n,A); print_vector(n,b); free(b); break; } default: printf("Opcao nao reconhecida\n"); exit(EXIT_FAILURE); } } return 1; }