int main(int argc, char *argv[]) { clock_t seq_clk = clock(); int numberOfTrainingPictures = atoi(argv[1]); int ***trainingSet = (int ***)malloc(sizeof(int **) * NUM_PEOPLE); for (int i=0; i<NUM_PEOPLE; i++) { trainingSet[i] = alloc_2d_matrix(numberOfTrainingPictures, MAX_DECIMAL_VALUE); } //trainingSet is a 3d matrix now (person x image x histogramIndex ) double seq_time = clock() - seq_clk; double parallel_clk = omp_get_wtime(); #pragma omp parallel { #pragma omp for for (int i=0; i<NUM_PEOPLE; i++) { char filename[256]; for (int j=0; j<numberOfTrainingPictures; j++) { sprintf(filename, "images/%d.%d.txt", i+1, j+1); int **image = read_pgm_file(filename, IMAGE_R, IMAGE_R); create_histogram(trainingSet[i][j], image, IMAGE_R, IMAGE_W); free(image); } } } int correct_answers = 0; #pragma omp parallel { #pragma omp for reduction(+:correct_answers) for (int i=0; i<NUM_PEOPLE; i++) { for (int j=numberOfTrainingPictures; j<NUM_PICS_PER_PERSON; j++) { int *test_histogram = (int *)malloc(sizeof(int) * MAX_DECIMAL_VALUE); char filename[256]; sprintf(filename, "images/%d.%d.txt", i+1, j+1); int **image = read_pgm_file(filename, IMAGE_R, IMAGE_R); create_histogram(test_histogram, image, IMAGE_R, IMAGE_W); int predicted = find_closest(trainingSet, NUM_PEOPLE, numberOfTrainingPictures, MAX_DECIMAL_VALUE, test_histogram) + 1; printf("%s %d %d\n", filename+7, predicted, i+1); if(i+1 == predicted) { correct_answers++; } } } } printf("Accuracy: %d correct answers for %d tests\n", correct_answers, NUM_PEOPLE * (NUM_PICS_PER_PERSON - numberOfTrainingPictures)); printf("Parallel time: %lf\n", omp_get_wtime() - parallel_clk); printf("Sequential time: %lf\n", seq_time/CLOCKS_PER_SEC); return 0; }
int main(int argc, char** argv){ begin = clock(); int i, j; int b; num_files = atoi(argv[1]); // 4d array to have 2 layers for height and width and 2 layers for files and people int**** pre_training_set = (int****)malloc(num_people * sizeof(int***)); for(i = 0; i < num_people; i++){ pre_training_set[i] = (int***)malloc(num_files * sizeof(int**)); } char filename[128]; // printf("Hello\n"); for(i = 0; i < num_people; i++){ for(j = 0; j < num_files; j++){ sprintf(filename, "/home/cagdas/Desktop/parallel-project-3/images/%d.%d.txt", i+1, j+1); // printf("%s\n", filename); pre_training_set[i][j] = read_pgm_file(filename, rows, cols); } } // printf("Creating the training set\n"); // the actual training set where the histogram arrays will be held for each file for each person (3 layers!) int*** training_set = (int***)malloc(num_people * sizeof(int**)); for(i = 0; i < num_people; i++){ training_set[i] = (int**)malloc(num_files * sizeof(int*)); } // create histograms for each person's each picture for(i = 0; i < num_people; i++){ for(j = 0; j < num_files; j++){ // printf("Onto it with i %d j %d\n", i, j); training_set[i][j] = (int*)malloc(sizeof(int) * 256); beginp = clock(); #pragma omp parallel for schedule(static) for(b = 0; b < 256; b++){ training_set[i][j][b] = 0; } endp = clock(); time_spentp += (double)(endp - beginp); create_histogram(training_set[i][j], pre_training_set[i][j], rows, cols); } } // printf("created the training set\n"); // test files int**** pre_test_set = (int****)malloc(num_people * sizeof(int***)); for(i = 0; i < num_people; i++){ pre_test_set[i] = (int***)malloc(sizeof(int**) * (total_num_files - num_files)); } // printf("pre test set\n"); for(i = 0; i < num_people; i++){ for(j = 0; j < (total_num_files - num_files); j++){ sprintf(filename, "/home/cagdas/Desktop/parallel-project-3/images/%d.%d.txt", i+1, j+1+num_files); // printf("Current name is %s\n", filename); pre_test_set[i][j] = read_pgm_file(filename, rows, cols); } } // printf("created pre test set\n"); int*** test_set = (int***)malloc(num_people * sizeof(int**)); for(i = 0; i < num_people; i++){ test_set[i] = (int**)malloc((total_num_files - num_files) * sizeof(int*)); for(j = 0; j < total_num_files - num_files; j++){ test_set[i][j] = (int*)malloc(sizeof(int) * 256); for(b = 0; b < 256; b++){ test_set[i][j][b] = 0; } create_histogram(test_set[i][j], pre_test_set[i][j], rows, cols); } } // printf("np abi\n"); int totalCount = 0; int falseCount = 0; for(i = 0; i < num_people; i++){ for(j = 0; j < total_num_files - num_files; j++){ int cur = find_closest(training_set, num_people, num_files, 256, test_set[i][j]); // test the file with file name "%d.%d.txt\t\t%d\t\t%d\n", i+1, j+1+num_files totalCount++; // a judgment is made if(cur != i+1){ // a false judgment is made printf("%d.%d.txt\t\t%d\t\t%d\n", i+1, j+1+num_files, i+1, cur); falseCount += 1; } } } printf("Accuracy: %d correct answers for %d tests Parallel\n", totalCount - falseCount, totalCount); // TODO:: deallocate the allocated memory space int a; beginp = clock(); #pragma omp parallel for schedule(static) private(j,a) for(i = 0; i < num_people; i++){ for(j = 0; j < num_files; j++){ for(a = 0; a < rows; a++){ free(pre_training_set[i][j][a]); } free(pre_training_set[i][j]); } free(pre_training_set[i]); } endp = clock(); time_spentp += (double)(endp - beginp); free(pre_training_set); // printf("free pre training set\n"); #pragma omp parallel for schedule(static) private(j,a) for(i = 0; i < num_people; i++){ for(j = 0; j < total_num_files - num_files; j++){ for(a = 0; a < rows; a++){ free(pre_test_set[i][j][a]); } free(pre_test_set[i][j]); } free(pre_test_set[i]); } free(pre_test_set); // printf("free pre test set\n"); beginp = clock(); #pragma omp parallel for schedule(static) private(j) for(i = 0; i < num_people; i++){ for(j = 0; j < num_files; j++){ free(training_set[i][j]); } free(training_set[i]); } endp = clock(); time_spentp += (double)(endp - beginp); free(training_set); // printf("free training set\n"); beginp = clock(); #pragma omp parallel for schedule(static) private(j) for(i = 0; i < num_people; i++){ for(j = 0; j < total_num_files - num_files; j++){ free(test_set[i][j]); } free(test_set[i]); } endp = clock(); time_spentp += (double)(endp - beginp); free(test_set); // printf("free test set\n"); end = clock(); time_spent = (double)(end-begin); printf("Total runtime: %f\n", time_spent/CLOCKS_PER_SEC); printf("Sequential runtime: %f\n", (time_spent - time_spentp)/CLOCKS_PER_SEC); return 0; }