Created
September 18, 2012 16:30
-
-
Save marcel-valdez/3744123 to your computer and use it in GitHub Desktop.
Pseudocodigo de ejercicio 5 de capitulo 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void translacion_master(int* image_data_ptr, int height, int width, int proc_count, int deltaX) { | |
/* El proceso maestro no es un 'worker' */ | |
int worker_count = proc_count - 1; | |
/* Se calcula el numero de renglones por 'worker' */ | |
int rows_slice = height / worker_count; | |
/* Se calcular el remanente para el ultimo proceso */ | |
int remainder = height % worker_count; | |
/* Se aloja memoria para la imagen 'trasladada' */ | |
int* new_image_data_ptr; | |
/* Inicializar datos */ | |
int opData[3]; | |
opData[0] = width; | |
opData[1] = height; | |
opData[2] = deltaX; | |
/* Enviar renglones a cada proceso */ | |
int proc; | |
for(proc = 1; proc <= worker_count; proc++) { | |
/* Enviar primero los datos sobre los renglones a enviar*/ | |
send(opData, 3, proc); | |
/* Obtener la direccion de memoria de los renglones que le corresponden al esclavo */ | |
int* row_data = &image_data_ptr[(proc - 1) * rows_slice * width]; | |
/* Obtener el tamano de los datos a enviar al esclavo */ | |
int current_rows_slice = (proc == worker_count) ? rows_slice + remainder : rows_slice; | |
int slave_data_size = current_rows_slice * width; | |
/* Enviar los renglones tal cual.*/ | |
send(row_data, slave_data_size, Pproc); | |
} | |
/* Recibir datos de los esclavos */ | |
int i; | |
for(i = 1; i <= worker_count; i++) { | |
int* rows_to_receive_ptr = &image_data_ptr[rows_slice * width * (i - 1)]; | |
int current_rows_slice = (i == worker_count) ? rows_slice + remainder : rows_slice; | |
recv(rows_to_receive_ptr, current_rows_slice * width, Pi); | |
} | |
display_image(new_image_data_ptr); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void translacion_slave(int my_proc_idx, int total_procs) { | |
/* numero total de esclavos */ | |
int total_workers = total_procs - 1; | |
/* direccion de memoria de los datos de los renglones */ | |
int* rows_ptr; | |
/* obtener los datos que se recibiran del maestro: altura, anchura y delta X */ | |
int opData[3]; | |
recv(opData, 3, Pmaster); | |
int width = opData[0], height = opData[1], deltaX = opData[2]; | |
/* cantidad de renglones que me toca calcular */ | |
int rows_slice = (height / total_workers); | |
rows_slice += my_proc_idx == total_workers ? (height % total_workers) : 0; | |
/* obtener renglones del maestro */ | |
recv(rows_ptr, rows_slice * width, Pmaster); | |
/* calcular nuevos pixeles */ | |
move_section(rows_ptr, rows_slice, width, deltaX); | |
/* regresar pixeles al proceso maestro */ | |
send(rows_ptr, rows_slice * width, Pmaster); | |
} | |
void move_section(int* rows_ptr, int count, int width, int deltaX) { | |
int row; | |
for(row = 0; row < count; row++) { | |
int* row_ptr = move_pointer(rows_ptr, row * width); | |
move_row(row_ptr, width, deltaX); | |
} | |
} | |
void move_row(int* row_ptr, int width, int deltaX) { | |
int pending_pixel = *row_ptr; | |
int x, x2, i; | |
for(i = 0; i < width; i++) { | |
/* Calcular nuevo x2, y del pixel */ | |
x2 = x + deltaX; | |
/* Si x2 > width, entonces x2 = x2 - width */ | |
if (x2 >= width) { x2 -= width; } | |
else if (x2 < 0) { x2 += width; } | |
/* Asignar pixel en nueva direccion */ | |
int replaced_pixel = row_ptr[x2]; | |
row_ptr[x2] = pending_pixel; | |
pending_pixel = replaced_pixel; | |
/* Pasar al siguiente pixel a mover. */ | |
x = x2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment