Sphere :: Sphere(float r, int vs, int rs) { // set radius radius = r; // set stack interval float stack = M_PI / vs; // set slice interval float slice = (2 * M_PI) / rs; // define the number of vertices verts_size = (vs - 1) * (rs) + 2; verts = new GLfloat*[verts_size]; // define the number of faces faces_size = (vs - 1) * rs * 2; faces = new int*[faces_size]; // define arrays for normals v_norms = new GLfloat*[verts_size]; f_norms = new GLfloat*[faces_size]; // define to keep track of faces // for vertex normal calculation int vert_faces[verts_size]; // VERTICES ///////////////////////////////////////////// GLfloat* v; // current angle of rotation for stack float curr_stack; // current angle of rotation for slice float curr_slice; // current positions in the array int pos, pos2, pos3; // loop to set "body" vertices for( int i = 0; i < vs - 1; i ++ ) { // calculate stack angle curr_stack = -(i+1) * stack; for( int j = 0; j < rs; j++ ) { // calculate slice angle curr_slice = j * slice; // calculate position in the array pos = i * rs + j; // allocate a new vertex v = new GLfloat[4]; // set vertex at north pole v[0] = 0; v[1] = r; v[2] = 0; v[3] = 1; // rotate by stack angle v_rotate_z(v, curr_stack); // rotate by slice angle v_rotate_y(v, curr_slice); // store in verts array verts[pos] = v; // initialize vertex normal v_norms[pos] = new GLfloat[4]; init_vector(v_norms[pos]); // initalize vertex face count vert_faces[pos] = 0; } } pos = verts_size - 2; // allocate a new vertex v = new GLfloat[4]; // set vertex at north pole v[0] = 0; v[1] = r; v[2] = 0; v[3] = 1; // store in verts array verts[pos] = v; // initialize vertex normal v_norms[pos] = new GLfloat[4]; init_vector(v_norms[pos]); // initalize vertex face count vert_faces[pos] = 0; pos = verts_size - 1; // allocate a new vertex v = new GLfloat[4]; // set vertex at south pole v[0] = 0; v[1] = -r; v[2] = 0; v[3] = 1; // store in verts array verts[pos] = v; // initialize vertex normal v_norms[pos] = new GLfloat[4]; init_vector(v_norms[pos]); // initalize vertex face count vert_faces[pos] = 0; // FACES + NORMALS ///////////////////////////////////////////// // position of face in array int f_pos; // first loop sets the body of the sphere for( int i = 0; i < vs - 2; i++ ) { for( int j = 0; j < 2*(rs-1); j+= 2 ) { // calculate position in the array pos = i * rs + j/2; pos2 = (i + 1) * rs +j/2; pos3 = pos2 + 1; f_pos = i * 2 * rs + j; set_face(f_pos, pos, pos2, pos3, vert_faces); f_pos++; pos2++; pos3 = pos + 1; set_face(f_pos, pos, pos2, pos3, vert_faces); } // calculate position in the array pos = (i * rs)+ rs - 1; pos2 = (i + 1) * rs + rs - 1; pos3 = (i + 1) * rs; f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); f_pos++; pos2 = (i+1) * rs; pos3 = i * rs; set_face(f_pos, pos, pos2, pos3, vert_faces); } // second loop sets the top endcap //////////////////////////////////////// for( int i = 0; i < rs - 1; i++ ) { // set positions in arrays pos = verts_size-2; pos2 = i; pos3 = i + 1; f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); } // set positions in arrays pos = verts_size-2; pos2 = pos3; pos3 = 0; f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); // third loop sets the bottom endcap //////////////////////////////////////// for( int i = 0; i < rs - 1; i++ ) { // set positions in arrays pos = verts_size-1; pos3 = (vs - 2) * rs + i; pos2 = pos3 + 1; f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); } // set positions in arrays pos = verts_size-1; pos2 = pos3; pos3 = (vs - 2) * rs; f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); // VERTEX NORMALS ///////////////////////////////////////////// // loop through vertices for( int i = 0; i < verts_size; i++ ) { v_norms[i][0] = v_norms[i][0] / vert_faces[i]; v_norms[i][1] = v_norms[i][1] / vert_faces[i]; v_norms[i][2] = v_norms[i][2] / vert_faces[i]; v_norms[i][3] = 1.0; normalize(v_norms[i]); } v_norms[verts_size-2][0] = 0.0; v_norms[verts_size-2][1] = 1.0; v_norms[verts_size-2][2] = 0.0; v_norms[verts_size-2][3] = 1.0; v_norms[verts_size-1][0] = 0.0; v_norms[verts_size-1][1] = -1.0; v_norms[verts_size-1][2] = 0.0; v_norms[verts_size-1][3] = 1.0; }
int main(int argc, char** argv) { int rank, size; int N; char opt; int nt = -1; int max_threads = 16; // on jupiter bool id = false; algo_t algo = reduce_scatter; FILE *f = NULL; static const char optstring[] = "n:a:f:i:p:"; static const struct option long_options[] = { {"n", 1, NULL, 'n'}, {"file", 1, NULL, 'f'}, {"i", 1, NULL, 'i'}, {NULL, 0, NULL, 0} }; MPI_Init(&argc,&argv); // get rank and size from communicator MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&rank); while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != EOF) { switch(opt) { case 'i': if (strcmp("procs", optarg) == 0) { id = true; } break; case 'p': nt = atoi(optarg); if (nt > max_threads) { printf("Using too much procs %d, use max %d", nt, max_threads); return EXIT_FAILURE; } else { printf("Using %d procs.", nt); } case 'n': N = atoi(optarg); break; case 'f': f = fopen(optarg,"a"); if (f == NULL) { mpi_printf(root, "Could not open log file '%s': %s\n", optarg, strerror(errno)); MPI_Finalize(); return EXIT_FAILURE; } break; case 'a': if (strcmp("ref", optarg) == 0) { mpi_printf(root, "Using reference implementation \n"); algo = ref; } else if ((strcmp("reduce_scatter", optarg) == 0)) { mpi_printf(root, "Using MPI_Allgather implementation \n"); algo = reduce_scatter; } break; default: MPI_Finalize(); return EXIT_FAILURE; } } if(N == 0) { if ( rank == root ){ printf("Usage: mpirun -nn nodecount p3-reduce_scatter.exe -n N\n"); printf("N is the the matrix size. \n\n"); } return 1; } /* ======================================================== */ /* Initialisation matrix & vector */ ATYPE *matrix = NULL; ATYPE *vector = NULL; if (rank == root) { debug("Setting up root data structures"); matrix = init_matrix(N,1); vector = init_vector(N,1); } int colcnt = N - (N/size ) * (size - 1 ); int partition = N/size; ATYPE *local_matrix = NULL; local_matrix = (ATYPE*) malloc (sizeof(ATYPE) * N * colcnt); ATYPE *local_vector = NULL; local_vector = (ATYPE*) malloc (sizeof(ATYPE) * partition) ; ATYPE *reference = NULL; reference = init_vector(N,1); ATYPE *result = NULL; result = init_vector(N,1); double inittime,totaltime; if( algo == ref) { if (rank == root) { inittime = MPI_Wtime(); matrix_vector_mult_ref(matrix, vector, N, reference); totaltime = MPI_Wtime() - inittime; } } else if (algo == reduce_scatter) { if(rank == root){ debug("Comptuting reference"); matrix_vector_mult_ref(matrix, vector, N, reference); } MPI_Barrier(MPI_COMM_WORLD); /* ======================================================== */ /* distributing matrix and vector */ distribute_vector(vector, local_vector, rank, size, partition, N); distribute_matrix(matrix, local_matrix, rank, size, partition, N); debug("begin MPI_Reduce_scatter"); MPI_Barrier(MPI_COMM_WORLD); inittime = MPI_Wtime(); compute_reduce_scatter(local_matrix, local_vector, result, rank, size, N, partition); MPI_Barrier(MPI_COMM_WORLD); totaltime = MPI_Wtime() - inittime; double localtime = totaltime; MPI_Reduce(&localtime, &totaltime, 1, MPI_DOUBLE, MPI_MAX, root, MPI_COMM_WORLD); debug("after MPI_Reduce_scatter"); /* TODO: fix test so it uses vector idea */ /* debug("Testing result"); */ /* if (test_vector_part(result, local_vector, (rank * partition) , partition)) { */ /* debug("testresult: OK"); */ /* } else { */ /* debug("testresult: FAILURE"); */ /* debug("Result:"); */ /* printArray(recvbuff, N); */ /* debug("Reference:"); */ /* printArray(reference,N); */ /* } */ MPI_Barrier(MPI_COMM_WORLD); } if (rank == 0) { if (f != NULL) { if (id) { fprintf(f,"%d,%lf\n",nt, totaltime); } else { fprintf(f,"%d,%lf\n",N, totaltime); } } if (id) { printf("%d,%lf\n",nt , totaltime); } else { printf("%d,%lf\n",N , totaltime); } } debug("cleaning up"); free(vector); free(matrix); MPI_Finalize(); if ( f != NULL) { fclose(f); } return 0; }
Torus :: Torus(float r, float r2, int vs, int rs) { // set radii radius = r; radius2 = r2; // set stack interval float stack = (2 * M_PI) / vs; // set slice interval float slice = (2 * M_PI) / rs; // define the number of vertices verts_size = vs * rs; verts = new GLfloat*[verts_size]; // define the number of faces faces_size = verts_size * 2; faces = new int*[faces_size]; // define arrays for normals v_norms = new GLfloat*[verts_size]; f_norms = new GLfloat*[faces_size]; // define to keep track of faces // for vertex normal calculation int vert_faces[verts_size]; // VERTICES ///////////////////////////////////////////// GLfloat* v; // current angle of rotation for stack float curr_stack; // current angle of rotation for slice float curr_slice; // current positions in the array int pos, pos2, pos3; // loop to set vertices for( int i = 0; i < vs; i++ ) { // calculate stack angle curr_stack = -i * stack; for( int j = 0; j < rs; j++ ) { // calculate slice angle curr_slice = j * slice; // calculate position in the array pos = i * rs + j; // allocate new vertex v = new GLfloat[4]; v[0] = 0; v[1] = r2; v[2] = 0; v[3] = 1; // rotate by stack angle v_rotate_z(v, curr_stack); // translate to edge of the ring v_translate(v, r, 0.0, 0.0); // rotate by slice angle v_rotate_y(v, curr_slice); // store in verts array verts[pos] = v; // initialize vertex normal v_norms[pos] = new GLfloat[4]; init_vector(v_norms[pos]); // initalize vertex face count vert_faces[pos] = 0; } } // FACES + NORMALS ///////////////////////////////////////////// // position of face in array int f_pos; // first loop sets the body for( int i = 0; i < vs; i++ ) { for( int j = 0; j < 2*(rs-1); j+= 2 ) { // calculate position in the array pos = i * rs + j/2; if(i == vs-1) { pos2 = j/2; } else { pos2 = (i + 1) * rs +j/2; } pos3 = pos2 + 1; f_pos = i * 2 * rs + j; set_face(f_pos, pos, pos2, pos3, vert_faces); f_pos++; pos2++; pos3 = pos + 1; set_face(f_pos, pos, pos2, pos3, vert_faces); } // calculate position in the array pos = (i * rs)+ rs - 1; if(i == vs-1) { pos2 = rs - 1; pos3 = 0; } else { pos2 = (i + 1) * rs + rs - 1; pos3 = (i + 1) * rs; } f_pos++; set_face(f_pos, pos, pos2, pos3, vert_faces); f_pos++; if(i == vs-1) { pos2 = 0; } else { pos2 = (i+1) * rs; } pos3 = i * rs; set_face(f_pos, pos, pos2, pos3, vert_faces); } // VERTEX NORMALS ///////////////////////////////////////////// // loop through vertices for( int i = 0; i < verts_size; i++ ) { v_norms[i][0] = v_norms[i][0] / vert_faces[i]; v_norms[i][1] = v_norms[i][1] / vert_faces[i]; v_norms[i][2] = v_norms[i][2] / vert_faces[i]; v_norms[i][3] = 1.0; normalize(v_norms[i]); } }
int tfel_deserialize_ciphertext_kp(attribute_set *Delta, basis *c_i, unsigned char *bin_ct_buf, size_t max_len, EC_PAIRING p) { int i, j, t; unsigned char *buf_ptr = NULL; size_t buf_len; size_t *result_len = NULL; result_len = &buf_len; *result_len = 0; buf_ptr = bin_ct_buf; size_t length; /* KPでやること Deltaの初期化→Delta->valueを作るためにDelta.numが必要なので最初にdを引き出す その後、Delta->valueをmallocしvalueを引き出す */ //dをつくる v_vector *v_ptr = NULL; v_ptr = (v_vector*)malloc(sizeof(v_vector)); if (v_ptr == NULL) return -1; Delta->value = v_ptr; Delta->num = *((int*)buf_ptr); *result_len += sizeof(int); //x_tを引き出す buf_ptr = (unsigned char*)(bin_ct_buf + *result_len); for (i = 0; i < Delta->num; i++) { init_vector(v_ptr, i); v_ptr->x_t[1] = *((int*)buf_ptr); *result_len += sizeof(int); buf_ptr = (unsigned char*)(bin_ct_buf + *result_len); v_ptr->next = (v_vector*)malloc(sizeof(v_vector)); if (v_ptr->next == NULL) { clear_att_set_value(Delta->value); return -1; } if (i == Delta->num-1) { free(v_ptr->next); v_ptr->next = NULL; } else v_ptr = v_ptr->next; } //c(i)のdeserialize c_i->dim = Delta->num+1; c_i->M = (EC_POINT**)malloc(sizeof(EC_POINT*)*c_i->dim); if (c_i->M == NULL) { //error処理 } for(i = 0; i < c_i->dim; i++) { //printf("i = %d\n", i); if(*(int*)result_len > max_len) { printf("import c(i) error\n"); exit(1); } if(i == 0) t = 5; else t = 7; c_i->M[i] = (EC_POINT*)malloc(sizeof(EC_POINT)*t); for(j = 0; j < t; j++) { point_init(c_i->M[i][j], p->g1); //printf("j = %d\n", j); buf_ptr = (unsigned char*)(bin_ct_buf + *result_len); length = *(int*)buf_ptr; //printf("length = %zd\n", length); *result_len += sizeof(int); buf_ptr = (unsigned char*)(bin_ct_buf + *result_len); point_from_oct(c_i->M[i][j], buf_ptr, length); *result_len += length; } } return 0; }
/** * coffman_algorithm * * Función principal para calcular el algoritmo de * coffman sobre las matrices A y M y el vector RE * El funcionamiento está explicando en la implementación */ void coffman_algorithm(COFFMAN_DATA* data, FILE* target) { // primero necesitamos calcular RA int i,j; // shorcuts int p = data->num_proccess; int r = data->num_resources; // vector RA inicial que necesitamos int RA[r]; // vector RD (alias X), que iremos actualizando // conforme progrese el algoritmo, necesitamos // una copia puesto que sólo tenemos que actualizarlo // en una iteración de todos los procesos sin marcar // y vamos a usar el vector para comprobar ciertas condiciones int RD[r]; int RD_copy[r]; // procesos marcados, también necesitamos una copia // puesto que necesitamos comprobar a posteriori (una vez marcados) // si coincide con el estado anterior, y si es el mismo estamos // ante un interbloqueo int marked[p]; int marked_copy[p]; int previous=-1; int all_marked = 0, deadlock = 0; // inicializamos los vectores init_vector(RA, r); init_vector(RD, r); init_vector(marked, p); // calculamos el vector RA sumando // las filas de la matriz A for(i=0; i<p; i++) { for(j=0; j<r; j++) { RA[j] += data->A[i][j]; } } // calculamos RD(X) restando a RE-A for(i=0; i<r; i++) { RD[i] = data->RE[i] - RA[i]; } fprintf(target, COFFMAN_RD); print_vector(RD, r, target); fprintf(target, EOL); fprintf(target, COFFMAN_INIT_X); print_vector(RD, r, target); fprintf(target, EOL); // ver si hay algun proceso para marcar en A for(i=0; i<p; i++) { // recorremos las columnas for(j=0; j<r; j++) { previous = data->A[i][j]; // si algún elemento no es 0, salimos if(data->A[i][j] != 0) break; } // no ha habido elementos distintos de 0 // el proceso se marca para no procesarlo if(previous == 0) { marked[i] = 1; fprintf(target, COFFMAN_INIT_MARK, i+1); } } // hacemos un bucle infinito, hasta que encontremos // un interbloqueo o todos los procesos se marquen for(;;) { // comprobamos si ya están todos marcados if(all_proccess_marked(marked, p)) { all_marked = 1; break; } fprintf(target, COFFMAN_WITHOUT_MARK); without_mark(marked, p, target); fprintf(target, EOL); fprintf(target, EOL); // hacemos las copias de marked y RD, puesto que // usaremos esto para comprobar el estado interno copy_vector(marked_copy, marked, p); copy_vector(RD_copy, RD, p); // un bloque por procesos, solo cogiendo aquellos sin marcar for(i=0; i<p; i++) { // proceso no marcado, comprobamos ciertas cosas if(!marked[i]) { // si la comparación es 0, necesitamos marcar el proceso // y actualizar el vector RD, pero usamos RD_copy para los // otros procesos de esta interación if(compare_resource_vector(data->M[i], RD_copy, r)) { fprintf(target, COFFMAN_PROCCESS_AGAIN, i+1); // se marca el proceso marked[i] = 1; // actualizamos los recoursos en RD for(j=0; j<r; j++) { RD[j] += data->A[i][j]; } fprintf(target, COFFMAN_X); print_vector(RD, r, target); fprintf(target, EOL); } } } // si despues del algoritmo los procesos marcados // son iguales a los de la fase anterior, deadlock if(compare_status(marked, marked_copy, p) == 1) { deadlock = 1; break; } } fprintf(target, EOL); // todos los procesos marcados if(all_marked) fprintf(target, COFFMAN_NOT_DEADLOCK); // hemos llegado a un interbloqueo if(deadlock) { fprintf(target, COFFMAN_DEADLOCK); without_mark(marked, p, target); } }
int main(int argc, char * argv[]) { pthread_t *tid;//number of thread args *arg; int total_processes; double *a, *b, *x; int res1, res2; long int t; int n; int N; int i; char * filename = 0; const char * name = "c.txt"; if( argc != 3 && argc != 4 ) { printf("Usage : %s <n> <total_processes> <filename>\n", argv[0]); return 0; } n = atoi(argv[1]);//from number to string total_processes = atoi (argv[2]); if(!n || !total_processes) { printf("Usage : %s <n> <total_processes> <filename>\n", argv[0]); return 0; } a = new double[n*n]; b = new double[n]; x = new double[n]; tid = new pthread_t[total_processes]; arg = new args[total_processes]; if(argc > 3) filename = argv[3]; if(filename) { res1 = read_matrix(a, n, "a.txt"); res2 = read_vector(b, n, "b.txt"); if(res1 || res2) { printf("cannot read from file\n"); delete [] tid; delete [] arg; delete [] a; delete [] b; delete [] x; return 1; } } else { init_matrix(a, n); init_vector(b, a, n); } printf("matrix A:\n"); print_matrix(a, n); printf("vector b:\n"); print_vector(b, n); for (i = 0; i < total_processes; i++) { arg[i].a = a; arg[i].b = b; arg[i].n = n; arg[i].total_processes = total_processes; arg[i].num_process = i; arg[i].error = 0; } t = get_full_time (); for (i = 0; i < total_processes; i++) { if (pthread_create (tid + i, 0, &thread_method_of_reflections, arg + i)) { printf ("Cannot create thread %d\n", i); return 2; } } for (i = 0; i < total_processes; i++) pthread_join (tid[i], 0); back_hod(a, b, x, n); t = get_full_time () - t; N = (n < MAX_N) ? n : MAX_N; printf("result : "); for(i = 0; i < N; i++) printf("%lg ", x[i]); printvectorfile(x,n,name); if(filename) { read_matrix(a, n, "a.txt"); read_vector(b, n, "b.txt"); printf("\nResidual = %le\nElapsed time = %Lg\n",SolutionError(n,a,b,x),(long double)t/(CLOCKS_PER_SEC)); } else { init_matrix(a, n); init_vector(b, a, n); printf("\nResidual = %le\nError = %le\nElapsed time = %Lg\n",SolutionError(n,a,b,x), SolutionAccuracy(n,x),(long double)t/(CLOCKS_PER_SEC)); } delete [] tid; delete [] arg; delete [] a; delete [] b; delete [] x; return 0; }
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLtrsv(ViennaCLMatrix A, ViennaCLVector x, ViennaCLUplo uplo) { viennacl::backend::mem_handle v1_handle; viennacl::backend::mem_handle A_handle; if (init_vector(v1_handle, x) != ViennaCLSuccess) return ViennaCLGenericFailure; if (init_matrix(A_handle, A) != ViennaCLSuccess) return ViennaCLGenericFailure; switch (x->precision) { case ViennaCLFloat: { viennacl::vector_base<float> v1(v1_handle, x->size, x->offset, x->inc); viennacl::matrix_base<float> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2, A->order == ViennaCLRowMajor); if (A->trans == ViennaCLTrans) { if (uplo == ViennaCLUpper) viennacl::linalg::inplace_solve(viennacl::trans(mat), v1, viennacl::linalg::upper_tag()); else viennacl::linalg::inplace_solve(viennacl::trans(mat), v1, viennacl::linalg::lower_tag()); } else { if (uplo == ViennaCLUpper) viennacl::linalg::inplace_solve(mat, v1, viennacl::linalg::upper_tag()); else viennacl::linalg::inplace_solve(mat, v1, viennacl::linalg::lower_tag()); } return ViennaCLSuccess; } case ViennaCLDouble: { viennacl::vector_base<double> v1(v1_handle, x->size, x->offset, x->inc); viennacl::matrix_base<double> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2, A->order == ViennaCLRowMajor); if (A->trans == ViennaCLTrans) { if (uplo == ViennaCLUpper) viennacl::linalg::inplace_solve(viennacl::trans(mat), v1, viennacl::linalg::upper_tag()); else viennacl::linalg::inplace_solve(viennacl::trans(mat), v1, viennacl::linalg::lower_tag()); } else { if (uplo == ViennaCLUpper) viennacl::linalg::inplace_solve(mat, v1, viennacl::linalg::upper_tag()); else viennacl::linalg::inplace_solve(mat, v1, viennacl::linalg::lower_tag()); } return ViennaCLSuccess; } default: return ViennaCLGenericFailure; } }
ViennaCLStatus ViennaCLgemv(ViennaCLHostScalar alpha, ViennaCLMatrix A, ViennaCLVector x, ViennaCLHostScalar beta, ViennaCLVector y) { viennacl::backend::mem_handle v1_handle; viennacl::backend::mem_handle v2_handle; viennacl::backend::mem_handle A_handle; if (init_vector(v1_handle, x) != ViennaCLSuccess) return ViennaCLGenericFailure; if (init_vector(v2_handle, y) != ViennaCLSuccess) return ViennaCLGenericFailure; if (init_matrix(A_handle, A) != ViennaCLSuccess) return ViennaCLGenericFailure; switch (x->precision) { case ViennaCLFloat: { viennacl::vector_base<float> v1(v1_handle, x->size, x->offset, x->inc); viennacl::vector_base<float> v2(v2_handle, y->size, y->offset, y->inc); if (A->order == ViennaCLRowMajor) { viennacl::matrix_base<float> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2); v2 *= beta->value_float; if (A->trans == ViennaCLTrans) v2 += alpha->value_float * viennacl::linalg::prod(viennacl::trans(mat), v1); else v2 += alpha->value_float * viennacl::linalg::prod(mat, v1); } else { viennacl::matrix_base<float, viennacl::column_major> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2); v2 *= beta->value_float; if (A->trans == ViennaCLTrans) v2 += alpha->value_float * viennacl::linalg::prod(viennacl::trans(mat), v1); else v2 += alpha->value_float * viennacl::linalg::prod(mat, v1); } return ViennaCLSuccess; } case ViennaCLDouble: { viennacl::vector_base<double> v1(v1_handle, x->size, x->offset, x->inc); viennacl::vector_base<double> v2(v2_handle, y->size, y->offset, y->inc); if (A->order == ViennaCLRowMajor) { viennacl::matrix_base<double> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2); v2 *= beta->value_double; if (A->trans == ViennaCLTrans) v2 += alpha->value_double * viennacl::linalg::prod(viennacl::trans(mat), v1); else v2 += alpha->value_double * viennacl::linalg::prod(mat, v1); } else { viennacl::matrix_base<double, viennacl::column_major> mat(A_handle, A->size1, A->start1, A->stride1, A->internal_size1, A->size2, A->start2, A->stride2, A->internal_size2); v2 *= beta->value_double; if (A->trans == ViennaCLTrans) v2 += alpha->value_double * viennacl::linalg::prod(viennacl::trans(mat), v1); else v2 += alpha->value_double * viennacl::linalg::prod(mat, v1); } return ViennaCLSuccess; } default: return ViennaCLGenericFailure; } }
unsigned int master(unsigned int base_dim, unsigned int max_fact, unsigned int** exponents, mpz_t * As, int comm_size, unsigned int print_fact) { unsigned int fact_count = 0; MPI_Status status; int count; int source; /* Buffer per ricevere gli esponenti */ unsigned int* buffer_exp; /* Buffer per ricevere (A + s) */ unsigned char buffer_As[BUFFER_DIM]; init_vector(& buffer_exp, base_dim); double t1 = MPI_Wtime(); double t2; int fact_per_rank[comm_size]; for(int i = 0; i < comm_size; ++i) fact_per_rank[i] = 0; while(fact_count < max_fact + base_dim) { /* Ricevo il vettore di esponenti */ MPI_Recv(buffer_exp, base_dim, MPI_UNSIGNED, MPI_ANY_SOURCE, ROW_TAG, MPI_COMM_WORLD, &status); source = status.MPI_SOURCE; for(unsigned int i = 0; i < base_dim; ++i) set_matrix(exponents, fact_count, i, buffer_exp[i]); /* Ricevo l'mpz contenente (A + s) */ MPI_Recv(buffer_As, BUFFER_DIM, MPI_UNSIGNED_CHAR, source, AS_TAG, MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_UNSIGNED_CHAR, &count); mpz_import(As[fact_count], count, 1, 1, 1, 0, buffer_As); ++fact_count; ++fact_per_rank[source]; if(fact_count % print_fact == 0) { t2 = MPI_Wtime() - t1; printf("#%d/%d in %.6f seconds\n", fact_count, max_fact + base_dim, t2); } } /* Spedisco '1' agli slave per indicare la terminazione */ char stop_signal = '1'; for(unsigned int i = 1; i < comm_size; ++i) MPI_Send(&stop_signal, 1, MPI_CHAR, i, 0, MPI_COMM_WORLD); printf("#Sending stop_signal\n"); printf("#Fattorizzazioni per ranks:\n#"); for(int i = 1; i < comm_size; ++i) printf("%d \t", i); printf("\n#"); for(int i = 1; i < comm_size; ++i) printf("%d \t", fact_per_rank[i]); printf("\n"); return fact_count; }
int main(int argc, char**argv){ char * ficheiroIn; char * connectivity = malloc(100*sizeof(char)); node * list; int option; int initial_node, final_node; if(argc<2){ printf("TOO FEW ARGUMENTS\n"); exit(-1); } printf("PLEASE CHOOSE AN OPTION \n"); printf("1. CALCULATE THE NODES SEPARATING NODE A FROM B\n"); printf("2. CALCULATE STATISTICS AND CONNECTIVITY OF THE GRAPH\n"); if(scanf("%d", &option)!=1){ printf("ERROR: SPECIFY A VALID SOURCE AND DESTINATION\n"); exit(0); } ficheiroIn = argv[1]; int size = Read_file(ficheiroIn, &list); /*********************VECTOR DE ESTATISTICAS***************************/ int * node_statistics = malloc(size*sizeof(int)); init_vector(&node_statistics, size, 0); int * parent = malloc(size*sizeof(int)); /**********************************************************************/ int min = 100; if(option == 1){ printf("PLEASE CHOOSE A SOURCE AND DESTINATION.\n"); if(scanf("%d %d", &initial_node, &final_node)!=2){ printf("ERROR: SPECIFY A VALID SOURCE AND DESTINATION\n"); exit(0); } if(contiguous(list, initial_node, final_node)!=0) printf("THERE IS NO WAY OF SEPARATING NODE %d FROM NODE %d BECAUSE THEY ARE CONTIGUOUS\n", initial_node, final_node); else printf("FOR SPECIFIED SET OF NODES, IS NECESSARY TO TAKE %d NODE(S) WHICH IS/ARE:%s\n", ford_fulkerson(&list, size, &parent, initial_node, final_node, &connectivity, min), connectivity); }else{ int colum; int row; for(colum=0; colum<size; colum++){ for(row=0; row<size; row++){ if(row!=colum){ if(contiguous(list, colum, row)!=0){ (node_statistics[0]) ++; }else{ (node_statistics[ford_fulkerson(&list, size, &parent, colum, row, &connectivity, min)]) ++; Read_file(ficheiroIn, &list); } } } } cumulative_statistics(node_statistics, size); printf("IF YOU TAKE THE NODE(S)%s THE GRAPH WILL SPLIT\n", connectivity); } exit(0); }