next up previous contents
Next: B.2.1 Izracun pasu Up: B Algoritem distribuiranega programa Previous: B.1.6 Shranjevanje rezultatov Vsebina: contents

B.2 Delavec

Program delavca je locen od izvrsnega programa upravitelja. Najvec procesorskega casa se v navideznem racunalniku porabi prav na delavcih.

extern unsigned int Options;
* extern int First_Line, Last_Line;
* extern int First_Column, Last_Column;

MAIN_RETURN_TYPE alt_main PARAMS((int argc, char **argv));

static char *calc_tile PARAMS((int y1, int y2));
* static int width;
* char *PixelMap; /* izracunana slika */
* int PixelMapSize;

int main(argc, argv)
* int argc;
* char **argv;
* {
* int mytid; /* ID delavca */
* int mastertid; /* ID upravitelja */
* FILE *f;
* char *buffer;
* long file_size;

static char filename[MAXFILENAME];

PixelMapSize = 0;
* PixelMap = NULL;

mytid = pvm_mytid();

mastertid = pvm_parent();

/* Sprejem podatkov */
* pvm_recv(mastertid, MSGSCENE);
* pvm_upkstr(filename);
* pvm_upklong(&file_size, 1, 1);
* buffer = malloc(file_size);
* pvm_upkbyte(buffer, file_size, 1);

#if COMPRESSED
* {
* char command[MAXFILENAME + 20];
* sprintf(command, "%s -d -c > %s", COMPRESSPRG, filename);
* f = popen(command, "w");
* }
* #else
* f = fopen(filename, "w");
* #endif

if (f == NULL)
* {
* pvm_perror("Writing scene file");
* pvm_exit();
* return -1;
* }

fwrite(buffer, file_size, 1, f);

fflush(f);

#if COMPRESSED
* pclose (f);
* #else
* fclose (f);
* #endif

#ifdef DEBUG
* fputs("Scene file successfuly received, waiting ...\n", stderr);
* #endif

Klic glavnega programa, ki interpretira vhodno datoteko in se pripravi na izvrsevanje nalog.

alt_main(argc, argv);

width = Last_Column - First_Column;

while (pvm_recv(mastertid, MSGTILE) > 0)
* {
* int job, y1, y2;

pvm_unpackf("%d %d %d", &job, &y1, &y2);
* #ifdef DEBUG
* fprintf(stderr, "Received job #%d (%d-%d)\n", job, y1, y2);
* #endif
* calc_tile(y1, y2);

#ifdef DEBUG
* fprintf(stderr, "tile #%d calculated (%d-%d)\n", job, y1, y2);
* #endif

Zapakiramo podatke in posljemo rezultat upravitelju. pvm_initsend(PvmDataDefault);
* pvm_pkint(&job, 1, 1);
* pvm_pkint(&y1, 1, 1);
* pvm_pkint(&y2, 1, 1);
* pvm_pkbyte(PixelMap, (y2 - y1) * width * 3, 1);
* if (pvm_send(mastertid, MSGDATA))
* {
* fprintf(stderr, "error sending image back\n");
* }
* free (PixelMap);
* PixelMapSize = 0;
* #ifdef DEBUG
* fprintf(stderr, "result #%d (%d-%d) sended back\n", job, y1, y2);
* #endif

}
* fprintf(stderr, "error receiving work to do\n");
* pvm_exit();
* exit(1);
* }




Copyright © 1995 Leon Kos, Univerza v Ljubljani