Commit e481b3bc authored by Charron Maxime's avatar Charron Maxime
Browse files

Ajout version tableau

parent 46f84fab
......@@ -103,6 +103,6 @@ int* CPU_findBestCompability (unsigned char* diff, int inputImgWidth, int inputI
int* ret = (int*) malloc( 2 * sizeof(int));
ret[0] = minX;
ret[1] = minY;
printf("Best compatibily find at x=%d and y=%d\n",minX, minY);
printf("Best compatibily find at x=%d and y=%d (%d) with a difference of %d\n",minX, minY, minY * inputImgWidth + minX, diff[minY * (inputImgWidth - searchImgWidth) + minX]);
return ret;
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include "func_all.h"
unsigned char* GPU_task_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight) {
double compute_diff = omp_get_wtime();
unsigned char *diff = (unsigned char *) malloc( (inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight) * sizeof(unsigned char));
#pragma omp parallel
#pragma omp single
{
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("%.2f%% (%.0f seconds)\r",(float) ((y * (inputImgWidth-searchImgWidth) + x) *100) / ((inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight)), (float) (omp_get_wtime() - compute_diff));
fflush(stdout);
#pragma omp task
{
int sum = 0;
for ( int yBox = 0; yBox < searchImgHeight; yBox++ ) {
for ( int xBox = 0; xBox < searchImgWidth; xBox++ ) {
sum += pow(abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]),2);
}
}
double som_moy = sum / 1387200;
diff[y * (inputImgWidth - searchImgWidth) + x] = round(som_moy);
}
}
}
}
return diff;
}
\ No newline at end of file
unsigned char* GPU_task_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
......@@ -9,105 +9,47 @@
unsigned char* GPU_v2_rgb2grey (unsigned char *image, int imgWidth, int imgHeight) {
unsigned char *outputGrey = (unsigned char *) malloc(imgWidth * imgHeight * sizeof(unsigned char));
#pragma omp parallel for
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
outputGrey[index] = 0.299*image[index*3] + 0.587*image[index*3 + 1] + 0.114*image[index*3 + 2];
}
return outputGrey;
}
void GPU_v2_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight) {
unsigned char *imageFinal = (unsigned char *)malloc(imgWidth* imgHeight * 3 * sizeof(unsigned char));
#pragma omp parallel for
for ( int index = 0; index < imgHeight*imgWidth; index++ ){
imageFinal[index*3] = image[index];
imageFinal[index*3 + 1] = image[index];
imageFinal[index*3 + 2] = image[index];
}
savePicture(imageFinal, filename, imgWidth, imgHeight);
}
unsigned char* GPU_v2_generateRedBox(int x, int y, unsigned char *image, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight){
unsigned char *ret = (unsigned char *)malloc(inputImgWidth * inputImgHeight * 3 * sizeof(unsigned char));
memcpy( ret, image, inputImgWidth * inputImgHeight * 3 * sizeof(unsigned char) );
#pragma omp parallel for
for ( int i = 0; i < searchImgWidth; i++ ){
ret[y*inputImgWidth*3 + (x+i)*3] = 255;
ret[y*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[y*inputImgWidth*3 + (x+i)*3 + 2] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3] = 255;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 1] = 0;
ret[(y+searchImgHeight)*inputImgWidth*3 + (x+i)*3 + 2] = 0;
}
#pragma omp parallel for
for ( int i = 0; i < searchImgHeight; i++ ){
ret[(y+i)*inputImgWidth*3 + x*3] = 255;
ret[(y+i)*inputImgWidth*3 + x*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + x*3 + 2] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3] = 255;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 1] = 0;
ret[(y+i)*inputImgWidth*3 + (x+searchImgWidth)*3 + 2] = 0;
}
return ret;
}
unsigned char* GPU_v2_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight) {
int GPU_v2_calculateIndex(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight) {
double compute_diff = omp_get_wtime();
unsigned char *diff = (unsigned char *) malloc( (inputImgWidth-searchImgWidth) * (inputImgHeight-searchImgHeight) * sizeof(unsigned char));
#pragma omp parallel for
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("(%.0f seconds)\r", (float) (omp_get_wtime() - compute_diff));
fflush(stdout);
int sum = 0;
for ( int yBox = 0; yBox < searchImgHeight; yBox++ ) {
for ( int xBox = 0; xBox < searchImgWidth; xBox++ ) {
//if ( search[ (yBox) * inputImgWidth + xBox] > 0 ) {
// Methode 1 : Avec le carré des différences
//if ( methode_one )
sum += pow(abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]),2);
int *min_val;
int *min_index;
int num_thread = 0;
#pragma omp parallel shared(min_val, min_index, num_thread)
{
#pragma omp single
{
num_thread = omp_get_num_threads();
min_val = (int*) malloc( sizeof(int) * num_thread);
min_index = (int*) malloc( sizeof(int) * num_thread);
}
// Methode 2 : Sans le carré des différences
//else
// sum += abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]);
//}
min_index[omp_get_thread_num()] = 0;
min_val[omp_get_thread_num()] = 10000*10000;
#pragma omp for
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
// print the process
printf("(%.0f seconds)\r", (float) (omp_get_wtime() - compute_diff));
fflush(stdout);
int sum = 0;
for ( int yBox = 0; yBox < searchImgHeight; yBox++ ) {
for ( int xBox = 0; xBox < searchImgWidth; xBox++ ) {
sum += pow(abs(input[ (y+yBox) * inputImgWidth + x + xBox] - search[ yBox * searchImgWidth + xBox]),2);
}
}
}
//if ( methode_one ) {
// Methode 1
// rapporter la somme entre 0 et 255 ( 255^2 * largeur_goat * hauteur_goat / 255)
double som_moy = sum / 1387200;
diff[y * (inputImgWidth - searchImgWidth) + x] = round(som_moy);
//}else{
// Methode 2
//diff[y * (inputImgWidth - searchImgWidth) + x] = sum / 5440;
//}
}
}
return diff;
}
int* GPU_v2_findBestCompability (unsigned char* diff, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight){
int minY = 0;
int minX = 0;
for ( int y = 0; y < inputImgHeight - searchImgHeight; y ++ ) {
for ( int x = 0; x < inputImgWidth - searchImgWidth; x++ ) {
if (diff[y * (inputImgWidth - searchImgWidth) + x] < diff[minY * (inputImgWidth - searchImgWidth) + minX]){
minY = y;
minX = x;
int tid = omp_get_thread_num();
if ( min_val[tid] > sum ) {
min_index[tid] = y * inputImgWidth + x;
min_val[tid] = sum;
}
}
}
}
int* ret = (int*) malloc( 2 * sizeof(int));
ret[0] = minX;
ret[1] = minY;
printf("Best compatibily find at x=%d and y=%d\n",minX, minY);
return ret;
}
\ No newline at end of file
int final_min_val = min_val[0];
int index = min_index[0];
for ( int i = 1; i < num_thread; i++ ) {
if ( final_min_val > min_val[i] )
index = min_index[i];
}
return index;
}
unsigned char* GPU_v2_rgb2grey (unsigned char *image, int imgWidth, int imgHeight);
void GPU_v2_saveGreyPicture (unsigned char *image, char* filename, int imgWidth, int imgHeight);
unsigned char* GPU_v2_generateRedBox(int x, int y, unsigned char *image, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
unsigned char* GPU_v2_calculateDiff(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
int* GPU_v2_findBestCompability (unsigned char* diff, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
\ No newline at end of file
int GPU_v2_calculateIndex(unsigned char* input, unsigned char* search, int inputImgWidth, int inputImgHeight, int searchImgWidth, int searchImgHeight);
\ No newline at end of file
img/output/final_cpu.png

89.8 KB | W: | H:

img/output/final_cpu.png

1.19 MB | W: | H:

img/output/final_cpu.png
img/output/final_cpu.png
img/output/final_cpu.png
img/output/final_cpu.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -6,6 +6,8 @@
#include "functions/func_all.h"
#include "functions/func_CPU.h"
#include "functions/func_OPEN_simple_v1.h"
#include "functions/func_OPEN_v2.h"
#include "functions/func_OPEN_task.h"
static const int IS_INPUT = 1;
static const int IS_SEARCH = 0;
......@@ -118,50 +120,50 @@ int main (int argc, char *argv[])
printf("\n\n============================================\n");
printf("\tOPEN MP (version simple)\n");
printf("============================================\n");
double gpu_time = omp_get_wtime();
double gpu_v1_time = omp_get_wtime();
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
double gpu_input_to_grey_time = omp_get_wtime();
double gpu_v1_input_to_grey_time = omp_get_wtime();
inputGrey = GPU_SIMPLE_rgb2grey(inputImg, inputImgWidth, inputImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "input");
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_input_to_grey_time);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v1_input_to_grey_time);
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
double gpu_search_to_grey_time = omp_get_wtime();
double gpu_v1_search_to_grey_time = omp_get_wtime();
searchGrey = GPU_SIMPLE_rgb2grey(searchImg, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_search_to_grey_time);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v1_search_to_grey_time);
// ==================================== Calculate compability
printf("Calculate difference\n");
// time execution managed in the function
double gpu_compute_diff_time = omp_get_wtime();
double gpu_v1_compute_diff_time = omp_get_wtime();
diff = GPU_SIMPLE_calculateDiff(inputGrey,searchGrey, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
time = convert((float) (omp_get_wtime() - gpu_compute_diff_time));
time = convert((float) (omp_get_wtime() - gpu_v1_compute_diff_time));
fflush(stdout);
printf("\rDone in \033[0;32m %d minutes %f seconds \033[0m\n",(int) time[0], time[1]);
// ==================================== Search max compability
double gpu_find_compa_time = omp_get_wtime();
double gpu_v1_find_compa_time = omp_get_wtime();
coord = GPU_SIMPLE_findBestCompability(diff, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_find_compa_time);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v1_find_compa_time);
free(diff);
// ==================================== Save a copy of 'inputImg' with the red box
printf("Save picture with red box\n");
double gpu_box_time = omp_get_wtime();
double gpu_v1_box_time = omp_get_wtime();
final_m1 = GPU_SIMPLE_generateRedBox(coord[0], coord[1], inputImg, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_box_time);
savePicture(final_m1, (char*)"final_gpu", inputImgWidth, inputImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v1_box_time);
savePicture(final_m1, (char*)"final_gpu_v1", inputImgWidth, inputImgHeight);
free(final_m1);
printf("======================================================\n");
time = convert((float)(omp_get_wtime() - gpu_time));
time = convert((float)(omp_get_wtime() - gpu_v1_time));
printf("The OPEN MP part took \033[0;32m %d minutes %f seconds \033[0m to execute\n",(int) time[0], time[1]);
......@@ -171,59 +173,53 @@ int main (int argc, char *argv[])
/*
// ============================================================================================================
printf("\n\n============================================\n");
printf(" OPEN MP (version légérement plus performante)\n");
printf("OPEN MP (version légérement plus performante)\n");
printf("============================================\n");
double gpu_time = omp_get_wtime();
double gpu_v2_time = omp_get_wtime();
// ==================================== Convert input image to grey scale
printf("Convert input to grey\n");
double gpu_input_to_grey_time = omp_get_wtime();
inputGrey = GPU_rgb2grey(inputImg, inputImgWidth, inputImgHeight);
double gpu_v2_input_to_grey_time = omp_get_wtime();
inputGrey = GPU_SIMPLE_rgb2grey(inputImg, inputImgWidth, inputImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "input");
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_input_to_grey_time);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v2_input_to_grey_time);
// ==================================== Convert search image to grey scale
printf("Convert search to grey\n");
double gpu_search_to_grey_time = omp_get_wtime();
searchGrey = GPU_rgb2grey(searchImg, searchImgWidth, searchImgHeight);
double gpu_v2_search_to_grey_time = omp_get_wtime();
searchGrey = GPU_SIMPLE_rgb2grey(searchImg, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight, inputImgWidth, inputGrey, "search");
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_search_to_grey_time);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v2_search_to_grey_time);
// ==================================== Calculate compability
printf("Calculate difference\n");
// time execution managed in the function
double gpu_compute_diff_time = omp_get_wtime();
diff = GPU_calculateDiff(inputGrey,searchGrey, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
//saveGreyPicture(inputImgHeight-searchImgHeight, inputImgWidth-searchImgWidth, diff, (char*)"debug_m1");
time = convert((float) (omp_get_wtime() - gpu_compute_diff_time));
// ==================================== Calculate difference and search max compability
double gpu_v2_find_compa_time = omp_get_wtime();
int index = GPU_v2_calculateIndex(inputGrey,searchGrey, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
time = convert((float) (omp_get_wtime() - gpu_v2_find_compa_time));
fflush(stdout);
printf("\rDone in \033[0;32m %d minutes %d seconds \033[0m\n", time[0], time[1]);
printf("\rDone in \033[0;32m %d minutes %f seconds \033[0m\n",(int) time[0], time[1]);
printf("Best compatibily find at x=%d and y=%d (index : %d)\n", index%inputImgWidth,(int)index/inputImgWidth, index);
// ==================================== Search max compability
double gpu_find_compa_time = omp_get_wtime();
coord = GPU_findBestCompability(diff, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_find_compa_time);
free(diff);
// ==================================== Save a copy of 'inputImg' with the red box
printf("Save picture with red box\n");
double gpu_box_time = omp_get_wtime();
final_m1 = GPU_generateRedBox(coord[0], coord[1], inputImg, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_box_time);
savePicture(final_m1, (char*)"final_gpu", inputImgWidth, inputImgHeight);
free(final_m1);
double gpu_v2_box_time = omp_get_wtime();
final_m1 = GPU_SIMPLE_generateRedBox(index%inputImgWidth, (int)index/inputImgWidth, inputImg, inputImgWidth, inputImgHeight, searchImgWidth, searchImgHeight);
printf("Done in \033[0;32m %f \033[0m seconds\n",omp_get_wtime() - gpu_v2_box_time);
savePicture(final_m1, (char*)"final_gpu_v2", inputImgWidth, inputImgHeight);
free(final_m1);
printf("======================================================\n");
time = convert((float)(omp_get_wtime() - temps_initial));
printf("The OPEN MP part took \033[0;32m %d minutes %d seconds \033[0m to execute\n", time[0], time[1]);
*/
time = convert((float)(omp_get_wtime() - gpu_v2_time));
printf("The OPEN MP part took \033[0;32m %d minutes %f seconds \033[0m to execute\n", (int)time[0], time[1]);
......@@ -232,7 +228,7 @@ int main (int argc, char *argv[])
free_img(inputImg);
free_img(searchImg);
time = convert((float)(omp_get_wtime() - temps_initial));
time = convert((float)(omp_get_wtime() - gpu_v2_time));
printf("\n\nThe program took \033[0;32m %d minutes %f seconds \033[0m to execute\n",(int) time[0], time[1]);
printf("Good bye!\n");
......
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment