void testSub() { printf("=== Test sub ===\n"); uint32_t a[] = { 0x01234567, 0x89abcdef }; uint32_t b[] = { 0x00200000, 0x8a001001 }; uint32_t c[2]; sub_array(2, a, b, c); uint32_t expected1[] = { 0x1034566, 0xffabbdee }; assertArrayEquals(2, expected1, c); sub_array(2, b, a, c); uint32_t expected2[] = { 0xfefcba99u, 0x00544212u }; assertArrayEquals(2, expected2, c); uint32_t aa[] = { 0, 0x01234567, 0x89abcdef }; uint32_t bb[] = { 0, 0x00200000, 0x8a001001 }; uint32_t cc[3]; sub_array(3, aa, bb, cc); uint32_t expected3[] = { 0, 0x1034566, 0xffabbdee }; assertArrayEquals(2, expected3, cc); sub_array(3, bb, aa, cc); uint32_t expected4[] = { 0xffffffff, 0xfefcba99u, 0x00544212u }; assertArrayEquals(3, expected4, cc); }
int main(int argc, char *argv[]) { int node_index, node_count; int size; int block_size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &node_index); MPI_Comm_size(MPI_COMM_WORLD, &node_count); if (node_index == 0) { sprintf(buf, "result%d.txt", node_count); freopen(buf, "w", stdout); } // Timer started MPI_Barrier(MPI_COMM_WORLD); if (node_index == 0) timer_start(); if (argc != 3 && argc != 4) { if (node_index == 0) printf("Usage: %s matrix_size block_size [input_file]\n", argv[0]); MPI_Finalize(); return -1; } else { size = atoi(argv[1]); block_size = atoi(argv[2]); if ((size < 2) || (block_size < 1) || (block_size > size)) { if (node_index == 0) { printf("Wrong format matrix_size > 1, matrix_size >= block_size > 0\n"); printf("Usage: %s matrix_size block_size [input_file]\n", argv[0]); } MPI_Finalize(); return -2; } } int memlen = size + node_count * block_size - 1; memlen /= node_count * block_size; memlen *= block_size * size; double *data = new double [memlen]; memset(data, 0, memlen * sizeof(double)); if (argc == 3) { generate_matrix(MATRIX_INDEX, size, block_size, node_index, node_count, data); } else { if (read_matrix_file(argv[3], size, block_size, node_index, node_count, data)) { if (node_index == 0) printf("Cannot open(read from) file: %s\n", argv[3]); delete[] data; MPI_Finalize(); return -3; } } print_matrix(size, block_size, node_index, node_count, data); // Initialization done! MPI_Barrier(MPI_COMM_WORLD); if (node_index == 0) print_full_time("on init"); if (spd_inverse(size, block_size, node_index, node_count, data)) { if (node_index == 0) printf("Method cannot be applied!\n"); delete[] data; MPI_Finalize(); return -4; } // Algorithm done! MPI_Barrier(MPI_COMM_WORLD); if (node_index == 0) print_full_time("on algorithm"); // Printing result on console (and in file) downtr_to_symm(size, block_size, node_index, node_count, data); print_matrix(size, block_size, node_index, node_count, data); double *workspace = new double[memlen]; memset(workspace, 0, memlen * sizeof(double)); // For this two matrices inverse is known if (MATRIX_INDEX == 1 || MATRIX_INDEX == 2) { if (MATRIX_INDEX == 1) generate_matrix(2, size, block_size, node_index, node_count, workspace); else generate_matrix(1, size, block_size, node_index, node_count, workspace); sub_array(memlen, data, workspace); double err_norm = inf_norm_matrix(size, block_size, node_index, node_count, workspace); err_norm = 0; // if (node_index == 0) // printf("Error = %11.5le\n", err_norm); } // Restore input matrix to calculate residual if (argc == 3) { generate_matrix(MATRIX_INDEX, size, block_size, node_index, node_count, workspace); } else { if (read_matrix_file(argv[3], size, block_size, node_index, node_count, workspace)) { if (node_index == 0) printf("Cannot open(read from) file: %s\n", argv[3]); delete[] data; delete[] workspace; MPI_Finalize(); return -5; } } #ifdef PRINT_RESULT_TO_FILE if (print_matrix_file(output, size, block_size, node_index, node_count, data)) { if (node_index == 0) printf("Cannot open(print to) file: %s\n", output); delete[] data; delete[] workspace; MPI_Finalize(); return -6; } #endif double *residual = new double[memlen]; tmatrix_mul(size, block_size, node_index, node_count, workspace, data, residual); generate_matrix(-2, size, block_size, node_index, node_count, residual); generate_matrix(0, size, block_size, node_index, node_count, workspace); sub_array(memlen, workspace, residual); downtr_to_symm(size, block_size, node_index, node_count, residual); double res_norm = inf_norm_matrix(size, block_size, node_index, node_count, residual); if (node_index == 0) printf("Residual = %11.5le\n", res_norm); delete[] data; delete[] workspace; delete[] residual; MPI_Finalize(); return 0; }
params* staff_segment(const image_t *img, staff_info *staff){ int8_t range_f; float thrsh; uint16_t height; uint16_t width; uint16_t l; uint16_t i; uint16_t staffCounter,staffX,staffY; uint16_t length_staff_lines; uint16_t s_begin,s_end,fudge; int16_t addNum; projection_t* proj_onto_y; flex_array_t* crude_lines; flex_array_t* line_w; flex_array_t* diff_array; flex_array_t* minus_array_var; flex_array_t* compare_array; flex_array_t* kill_array; flex_array_t* less_crude_lines; flex_array_t* diff_lines; flex_array_t* test_lines; flex_array_t* top; flex_array_t* middle; flex_array_t* bottom; params* new_param; range_f=5; /*this code uses a projection to determine cuts for segmenting a music page into individual lines of music*/ height=img->height; width=img->width; /*projection on to vertical axis:*/ proj_onto_y = project(img , 2); /*calculate threshold:*/ thrsh = 0.42 * max_array(proj_onto_y); crude_lines = find(proj_onto_y,thrsh,greater); delete_flex_array(proj_onto_y); /*create array holding y values of all stafflines:*/ l = crude_lines->length; i = 0; length_staff_lines=0; /*find length of staff line array*/ while (i < l){ /*next staffline must be at least two pixels away:*/ while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){ i = i + 1; } length_staff_lines++; i = i + 1; } less_crude_lines=make_flex_array(length_staff_lines); line_w=make_flex_array(length_staff_lines); i=0; staffCounter=0; while (i < l){ s_begin = crude_lines->data[i]; /*next staffline must be at least two pixels away:*/ while ( ((i+1)<l) && ((crude_lines->data[i]+1)==crude_lines->data[i+1]||((crude_lines->data[i]+2)==crude_lines->data[i+1]))){ i = i + 1; } s_end = crude_lines->data[i]; /*add staffline to array:*/ less_crude_lines->data[staffCounter]=(s_begin+s_end+1)/2;/*round((s_begin + s_end)/2) works the same*/ line_w->data[staffCounter]=(s_end - s_begin+1);/*think this is necessary the +1 is new*/ i = i + 1; staffCounter++; } delete_flex_array(crude_lines); /*search for any incorrect lines*/ /*(check against others):*/ diff_lines=diff(less_crude_lines); fudge=median(diff_lines); delete_flex_array(diff_lines); l = less_crude_lines->length; kill_array=make_flex_array(l); for (i=0;i<l;i++){ kill_array->data[i]=0; } i = 0; while (i<=(l-5)){ test_lines = sub_array(less_crude_lines,i,i+4);/*staff_lines(i:i+4);*/ diff_array=abs_diff(test_lines); delete_flex_array(test_lines); minus_array_var=minus(diff_array,fudge); delete_flex_array(diff_array); compare_array=find(minus_array_var,fudge/5,greater); delete_flex_array(minus_array_var); if (compare_array){ kill_array->data[i]=1; i = i + 1; delete_flex_array(compare_array); } i = i + 5; } while (i<l){ kill_array->data[i]=1; i++; } /*for (i=0;i<l;i++){ if(kill_array->data[i]==0) printf("%d\n",less_crude_lines->data[i]); } system("PAUSE");*/ /*kill bad stafflines:*/ less_crude_lines=kill_array_indices(less_crude_lines,kill_array); delete_flex_array(kill_array); l=less_crude_lines->length; if(l%5){ fprintf(stderr,"Error, found stafflines not a multiple of 5"); exit(1); } staff->staff_lines=(uint16_t**)multialloc(sizeof(uint16_t),2,5,l/5); staff->staff_bounds=(uint16_t**)multialloc(sizeof(uint16_t),2,l/5,2); staff->number_staffs=l/5; staffX=0; staffY=0; for (i=0;i<l;i++){ staff->staff_lines[staffX][staffY]=less_crude_lines->data[i]; staffX++; if (staffX==5){ staffX=0; staffY++; } } delete_flex_array(less_crude_lines); /*calculate a good place to cut stafflines*/ top=get_line_at_index(staff,0); middle=get_line_at_index(staff,2); bottom=get_line_at_index(staff,4); diff_array=minus_array(bottom,top); delete_flex_array(top); delete_flex_array(bottom); new_param=malloc(sizeof(params)); new_param->staff_h=rounded_mean(diff_array); delete_flex_array(diff_array); range_f=((new_param->staff_h)*range_f+2)/4;/*should be same as round(range_f/2*mean(staff_lines(5, :) - staff_lines(1, :))); where range_f was 2.5*/ for (i=0;i<l/5;i++){ addNum=middle->data[i]-range_f; if(addNum<0) addNum=0; staff->staff_bounds[i][0]=addNum; addNum=middle->data[i]+range_f; if(addNum>=height) addNum=height-1; staff->staff_bounds[i][1]=addNum; } delete_flex_array(middle); /*music parameters */ new_param->thickness=rounded_mean(line_w); delete_flex_array(line_w); addNum=0; for (i=1;i<5;i++){ top=get_line_at_index(staff,i); bottom=get_line_at_index(staff,i-1); diff_array=minus_array(top,bottom); delete_flex_array(top); delete_flex_array(bottom); addNum+=sum(diff_array); delete_flex_array(diff_array); } addNum+=2*(staff->number_staffs); new_param->spacing=addNum/(4*(staff->number_staffs))-new_param->thickness;/*may introduce rounding errors*/ new_param->ks=0; new_param->ks_x=0; return new_param; }
/*PARSE RADAR DATA - traverse the two data array's and * use the start_array to determine * the start of a pulse. Keep track of the pulse, * and log the pulse data based on the * pulse count into a 2 dimensional array, response_parsed. * NOTE: intensity_time must be of size NUM_TRIGGERS*SAMPLES_PER_PULSE*/ void process_radar_data(char* intensity_time, rdata_t* trigger, rdata_t* response, int buf_size){ int count = 0; int i, j; rdata_t average, max; assert(intensity_time != NULL); assert(trigger != NULL); assert(response != NULL); /*for size of radar data to be correct, init_processing must be *called before this function */ rdata_t response_parsed[NUM_TRIGGERS][size_of_sendarray]; /*create a simplied edge trigger based off the transmit signal*/ memset(start, 0, buf_size); find_trigger_start(trigger, start, buf_size); #ifdef PRINT_TRIGGERING for (i = 0; i < buf_size; i++) { printf("%d: %d %f %f\n", i, start[i], trigger[i], response[i]); } #endif for(i = 13; i < buf_size-SAMPLES_PER_PULSE; i++){ /*find the trigger and if found, load data into the 2-d array, while keeping track of the time of the pulse*/ if (start[i] == 1 && none(start,i-11,i-1) == 0){ rdata_t_cpy(response_parsed[count], &response[i], SAMPLES_PER_PULSE); count = count + 1; } /*only record for a preset amount of triggers*/ if (count == NUM_TRIGGERS) break; } #ifdef PRINT_PARSED for (i = 0; i < NUM_TRIGGERS; i++){ for (j = 0; j < SAMPLES_PER_PULSE/10; j++) { printf("%d ", (int)floorf(response_parsed[i][j])); } printf("\n"); } #endif /*subtract the avarage value of each sub array!*/ for(i = 0; i < NUM_TRIGGERS; i++){ average = mean(response_parsed[i], 0, SAMPLES_PER_PULSE-1); rdata_t_addi(response_parsed[i], -1.0*average, SAMPLES_PER_PULSE); } #ifdef PRINT_AVERAGED for (i = 0; i < NUM_TRIGGERS; i++){ for (j = 0; j < SAMPLES_PER_PULSE/10; j++) { printf("%d ", (int)floorf(response_parsed[i][j])); } printf("\n"); } #endif /*create 2-pulse cancelor*/ for (i = 1; i < NUM_TRIGGERS; i++) sub_array(response_parsed[i], response_parsed[i-1], SAMPLES_PER_PULSE); #ifdef PRINT_CANCELOR for (i = 0; i < NUM_TRIGGERS; i++){ for (j = 0; j < SAMPLES_PER_PULSE/10; j++) { printf("%d ", (int)floorf(response_parsed[i][j])); } printf("\n"); } #endif /*ifft and convert to intensity values*/ for(i = 0; i < NUM_TRIGGERS; i++){ ifft(ifft_array,response_parsed[i]); rdata_t_cpy(response_parsed[i], ifft_array, size_of_sendarray); dbv(response_parsed[i], size_of_sendarray); } #ifdef PRINT_IFFT for (i = 0; i < NUM_TRIGGERS; i++){ for (j = 0; j < SAMPLES_PER_PULSE/10; j++) { printf("%d ", (int)floor(response_parsed[i][j])); } printf("\n"); } #endif max = find_max(&response_parsed[0][0], NUM_TRIGGERS*size_of_sendarray); rdata_t_addi(&response_parsed[0][0], -1.0*max, NUM_TRIGGERS*size_of_sendarray); intensify(intensity_time, &(response_parsed[0][0]), size_of_sendarray*NUM_TRIGGERS); #ifdef PRINT_FINAL for (i = 0; i < NUM_TRIGGERS; i++){ for (j = 0; j < SAMPLES_PER_PULSE/10; j++) { printf("%d ", (unsigned int)(unsigned char)floor(intensity_time[NUM_TRIGGERS*i+j])); } printf("\n"); } #endif }