// load a .mat matlab matrix // will load the first matrix (sparse or dense) in the file // returns 0 on success static int read_mat(char const *const filename, matrix_t *const A) { #ifndef HAVE_MATIO return 1; #else const int LOCAL_DEBUG = 0; mat_t* matfp; matfp = Mat_Open(filename, MAT_ACC_RDONLY); if(matfp == NULL) return 1; // failed to open file matvar_t* t; int more_data = 1; while (more_data) { t = Mat_VarReadNextInfo(matfp); if(t == NULL) { return 2; // no suitable variable found } // TODO decide if this is the one: if(t-> if(1) { more_data = 0; } else { // keep going Mat_VarFree(t); t = NULL; } } { // load the selected variable, including data Mat_Rewind(matfp); matvar_t* tt = Mat_VarRead(matfp, t->name); Mat_VarFree(t); t = tt; if(LOCAL_DEBUG) Mat_VarPrint(tt, 1); } // debug info if(LOCAL_DEBUG && t->name != NULL) printf("%s: loaded variable %s\n", filename, t->name); // checks and data handling int ret = 0; if(t->rank > 2 || t->rank <= 0) { // number of dimensions ret = 2; } else if(t->data_type != MAT_T_DOUBLE) { if(LOCAL_DEBUG) printf("data_type=%d\n",t->data_type); ret = 3; } else if(t->isComplex) { ret = 10; // TODO can't handle complex matrices yet } else if(t->isLogical) { ret = 11; // TODO can't handle logicals yet } else { if(t->rank == 1) { A->m = t->dims[0]; // rows A->n = 1; // cols } else { A->m = t->dims[0]; // rows A->n = t->dims[1]; // cols } A->sym = SM_UNSYMMETRIC; //if(t->data_type == MAT_T_DOUBLE) { A->data_type = REAL_DOUBLE; //} // TODO complex, single precision, various sized integers if(t->class_type == MAT_C_SPARSE) { // t.data = sparse_t in CSC format // Note that Matlab will save('-v4'...) a sparse matrix // to version 4 format without complaint but it appears // to be gibberish as far as MatIO is concerned sparse_t* st = t->data; A->nz = st->ndata; //st->nzmax has the actual size of the allocated st->data A->format = SM_CSC; // transfer the data pointer into our strucut // TODO check for negative values in ir/jc before throwing away their signs A->ii = (unsigned int*) st->ir; st->ir = NULL; A->jj = (unsigned int*) st->jc; st->jc = NULL; A->dd = st->data; st->data = NULL; } else if(t->class_type == MAT_C_DOUBLE) { A->nz = A->m * A->n; A->format = DCOL; // transfer the data pointer into our struct A->dd = t->data; t->data = NULL; } else { ret = 4; // unknown class of data structure } } // DEBUG if(LOCAL_DEBUG && validate_matrix(A) != 0) { ret = 50; fprintf(stderr, "problem loading .mat matrix file\n"); printf_matrix(" ", A); } // sanity checks // t.dims[] is don't-care // t.isGlobal is don't-care Mat_Close(matfp); Mat_VarFree(t); return ret; // success? #endif }
int main(int argc, char *argv[]) { position_vector * j_field [MAX_THREADS]; //imported j field position_vector * j_field_total = NULL; position_vector * j_field_last = NULL; position_vector * b_field[MAX_THREADS]; //calculated b_field position_vector * b_field_total; vect_list * calc_position = NULL; //position for b_field to be calculated vect_list * calc_position_dup[MAX_THREADS]; vect_list * calc_position_dup_last = NULL; vect_list * calc_position_temp = calc_position; vect_list * dimensions_total = NULL; //description of j field segments vect_list * dimensions[MAX_THREADS]; vect_list * dimensions_last = NULL; char * filename; char flag = 0; char isvalidfile = 0; double coords[9]; int coordcounter = 0; char * inputvalid; int num_threads = 2; int threadcounter = 0; int sizecounter = 0; int input_length = 0; //initialize j_field and b_fields to NULL for (coordcounter = 0; coordcounter < MAX_THREADS; coordcounter++) { j_field[coordcounter] = NULL; b_field[coordcounter] = NULL; dimensions[coordcounter] = NULL; } if (!(argc == 2 || argc == 3 || argc == 4 || argc == 12 || argc == 6)) // argc should be 2 or 4 or 9 for correct execution { // We print argv[0] assuming it is the program name printf("Incorrect Syntax Error.\n"); printf("Usable Syntax: %s (current density filename) ([-p] [-m] [-h]) (arg1 arg2 arg3 ...)", argv[0]); printf("\n\texample: %s ./currdens.txt\nFor more information use %s -h", argv[0], argv[0]); return -1; } else { // We assume argv[1] is a filename to open filename = argv[1]; if (argc > 2) { if (argv[2][0] == ('-') && argv[2][2] == (int) (NULL)) { //printf("\ninside switch if\n"); switch (argv[2][1]) { case 'p': if (argc == 4) { flag = 0; } else { printf("Usable Syntax: %s (current density filename) ([-p] [-m] [-b] [-help]) (arg1 arg2 arg3 ...)", argv[0]); printf("\n\texample: %s ./currdens.txt\nFor more information use %s -help", argv[0], argv[0]); return -2; } break; case 'm': if (argc == 12) { flag = 1; } else if (argc == 6) { flag = 2; } else if (argc == 3) { flag = 3; } else { printf("\nIncorrect parameters, please use one of the following:"); print_help(); return -2; } break; default: printf("\nIncorrect parameters, please use one of the following:"); print_help(); return -2; } } else { printf("\nIncorrect parameters, please use one of the following:"); print_help(); return -1; } } else { //printf("else"); if (argv[1][0] == ('-') && (int) (NULL)) { switch (argv[1][1]) { case 'h': printf("\nWelcome to Magnetic field solver help. The program is used as follows:"); printf("\nUsable Syntax: %s (current density filename) ([-p] [-m] [-h]) (arg1 arg2 arg3 ...)", argv[0]); printf("\n\texample: %s ./currdens.txt\nFor more information use %s -h", argv[0], argv[0]); print_help(); return 0; default: printf("\nIncorrect parameters, please use one of the following:"); print_help(); return -2; } } else { printf("\nIncorrect parameters, please use one of the following:"); print_help(); return -2; } } } if (get_data(&j_field_total, &dimensions_total, filename) == 1) { isvalidfile = 1; //printf("isvalid"); } if (flag == 0 && isvalidfile) { if (get_vect_list(argv[3], &calc_position) == 1) { } else { isvalidfile = 0; return -3; } } else if (flag == 1) { for (coordcounter = 0; coordcounter < 9; coordcounter++) { coords[coordcounter] = strtod(argv[3 + coordcounter], &inputvalid); if (inputvalid == argv[3]) { printf("\nCould not interpret number %s", inputvalid); return -3; } } if(validate_matrix(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5], coords[6], coords[7], coords[8]) == 0){ printf("\n\nError: Cannot create a matrix with these bounds,\nplease specify valid minimum and maximum values,\nas well as valid intervals."); return -3; } calc_position = create_calc_position(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5], coords[6], coords[7], coords[8]); } else if (flag == 2 && isvalidfile) { for (coordcounter = 0; coordcounter < 3; coordcounter++) { coords[coordcounter] = strtod(argv[3 + coordcounter], &inputvalid); if (inputvalid == argv[3]) { printf("\nCould not interpret number %s", inputvalid); return -3; } } if (coords[0] <= 0 || coords[1] <= 0 || coords[2] <= 0) { printf("\nImpossible to have a interval size of zero or less."); return -4; } calc_position = bounded_position_interval(j_field_total, coords[0], coords[1], coords[2]); } else if (flag == 3 && isvalidfile)calc_position = bounded_position(j_field_total); if (isvalidfile) { printf("\n\nUsing position with data in index 1: %lf %lf %lf", calc_position->vector_element->x, calc_position->vector_element->y, calc_position->vector_element->z); printf("\nUsing dim with data in index 1: %lf %lf %lf\n\n", dimensions_total->vector_element->x, dimensions_total->vector_element->y, dimensions_total->vector_element->z); /******************************************************************************************************** * Added threaded functionality: * ********************************************************************************************************/ input_length = get_length(j_field_total); for (threadcounter = 0; threadcounter < num_threads; threadcounter++) { dimensions_last = dimensions[threadcounter]; j_field_last = j_field[threadcounter]; for (sizecounter = 0; sizecounter < (input_length / num_threads); sizecounter++) { if ((j_field_total != NULL) && (dimensions_total != NULL)) { add_position_vector_pointer(&(j_field[threadcounter]), &j_field_last, j_field_total); add_vector(&(dimensions[threadcounter]), &dimensions_last, dimensions_total->vector_element); j_field_total = j_field_total->next; dimensions_total = dimensions_total->next; } } } printf("Calculating B field with %d thread(s)",num_threads); #pragma omp parallel for for (threadcounter = 0; threadcounter < num_threads; threadcounter++) {//A,B,C such that total iterations known at start of loop //<your code here> //<force ordered execution of part of the code. A=C willbe guaranteed to execute //before A=C+1> b_field[threadcounter] = calculateBfield(calc_position, j_field[threadcounter], dimensions[threadcounter]); } b_field_total = concatanate_b_field(b_field, num_threads); printf("\ncompleted calculating B field.\nSaving data to disk."); filename = strcat(filename, "BF"); /* Put b_field into b_field total. */ save_position_vector(b_field_total, filename); } printf("\n\tPress enter to exit."); scanf("%c", &inputvalid); return 0; }