Moin,
eigtl. ne billige Aufgabe, weil fast alles vorgegeben war, aber bin gerade zu blöd.
Sollen die de.wikipedia.org/wiki/Mandelbrot-Menge parallel berechnen. (in C)
Ne Funktion
Alles anzeigen
,die die komplette Berechnung vornimmt ist bereits gegeben. Einzige Aufgabe ist es noch, mit fork die Berechnung zu parallelisieren,
indem wir die HEIGHT in intervalle einteilen.
Umsetzung:
Alles anzeigen
MAX_PROCESSES ist einfache eine integer-Zahl die frei zu setzen ist für Testzwecke.
Problem ist: Die Berechnung klappt, falls sich HEIGHT ohne Rest durch MAX_PROCESSES teilen lässt.
Wenn das nicht der Fall ist, dann ist in der Datei die Reihenfolge der einzelnen Segmente ganz merkwürdig.
Er berechnet dann teilweise Dinge doppelt und fügt jeden 2 Balken einen orangenen Balken ein, der da nicht hingehört.
Das Verhalten kann ich mir gerade nicht so recht erklären. Wenn am Ende was fehlen würde, falls HEIGHT sich nicht ohne Rest durch MAX_PROCESSES teilen lässt,
wäre es für mich logisch, aber das ist gerade merkwürdig.
Ist wahrscheinlich richtig simpel, aber ich sehs gerad net.
So siehts aus für MAX_PROCESSES=9:
s14.directupload.net/file/d/3452/h6p5olyn_png.htm
für MAX_PROCESSES=64:
s14.directupload.net/file/d/3452/pbvcmfwf_png.htm
und so sollte es korrekt aussehen:
s14.directupload.net/file/d/3452/iy96qzib_png.htm
Danke schonma,
lg
eigtl. ne billige Aufgabe, weil fast alles vorgegeben war, aber bin gerade zu blöd.
Sollen die de.wikipedia.org/wiki/Mandelbrot-Menge parallel berechnen. (in C)
Ne Funktion
Quellcode
- /*
- * Writes a visual representation of the mandelbrot set into the supplied buffer.
- * Each pixel is represented by a pixel_data_t element containing the red, green and blue
- * components.
- *
- * It is possible to limit the calculation to a smaller part of the whole picture
- * with the parameters clip_x & y and clip_width and height.
- *
- * data: Pointer a clip_width * clip_height large memory area the data is written to.
- * The data is written row-wise, all pixel of the first row [x - (x + clip_width), y], followed
- * by pixel of the second row [x - (x + clip_width), y + 1], and so on
- * till [x - (x + clip_width), y + clip_height].
- * full_width: Width of the complete picture
- * full_height: Height of the complete picture
- * clip_x: x offset of the area to calculate
- * clip_y: y offset of the area to calculate
- * clip_width: Width of the area to calculate
- * clip_height: Height of the area to calculate
- */
- void calc_mandelbrot(pixel_data_t *data, int full_width, int full_height,
- int clip_x, int clip_y, int clip_width, int clip_height);
,die die komplette Berechnung vornimmt ist bereits gegeben. Einzige Aufgabe ist es noch, mit fork die Berechnung zu parallelisieren,
indem wir die HEIGHT in intervalle einteilen.
Umsetzung:
Quellcode
- int generate_mandelbrot_parallel(const char* filename)
- {
- unsigned long start, end;
- // Allocate buffer for picture data
- pixel_data_t *dataklein = calloc(WIDTH * HEIGHT / MAX_PROCESSES, sizeof(pixel_data_t));
- if (dataklein)
- {
- FILE *file = fopen(filename, "wb");
- int i;
- int child[MAX_PROCESSES];
- if(!file){
- return EXIT_FAILURE;
- }
- write_bitmap_header(file, WIDTH, HEIGHT);
- fclose(file);
- start = current_time_millis();
- for(i = 0; i<MAX_PROCESSES; i++){
- child[i] = fork();
- if(child[i] == 0){
- int my_number = i; //i sollte von dem Elternprozess übernommen worden sein
- int clip_x = 0;
- int clip_widht = WIDTH;
- int clip_y = (HEIGHT / MAX_PROCESSES) * my_number;
- int clip_height = (HEIGHT / MAX_PROCESSES);
- /*if(my_number == MAX_PROCESSES-1){
- clip_height = HEIGHT - (HEIGHT/MAX_PROCESSES)*my_number;}*/
- FILE *file = fopen(filename, "rb+");
- calc_mandelbrot(dataklein, WIDTH, HEIGHT, clip_x, clip_y, clip_widht, clip_height);//dieses mal x nach y
- fseek(file, ((WIDTH*HEIGHT) / MAX_PROCESSES) * sizeof(pixel_data_t) * my_number + BITMAP_HEADER_SIZE, SEEK_SET); //open File an der Stelle (WIDTH * HEIGHT / MAX_PROCESSES) * my_number und dataklein dazuschreiben
- fwrite(dataklein, sizeof(pixel_data_t), ((WIDTH*HEIGHT) / MAX_PROCESSES), file);
- fclose(file);
- free(dataklein);
- exit(0);
- }
- }
- /*for(i = 0; i<MAX_PROCESSES;i++){
- waitpid(child[i], NULL, 0);
- }*/
- free(dataklein);
- end = current_time_millis();
- printf("%.2f seconds\n", (double) (end - start) / 1000);
- return EXIT_SUCCESS;
- }
- return EXIT_FAILURE;
- }
MAX_PROCESSES ist einfache eine integer-Zahl die frei zu setzen ist für Testzwecke.
Problem ist: Die Berechnung klappt, falls sich HEIGHT ohne Rest durch MAX_PROCESSES teilen lässt.
Wenn das nicht der Fall ist, dann ist in der Datei die Reihenfolge der einzelnen Segmente ganz merkwürdig.
Er berechnet dann teilweise Dinge doppelt und fügt jeden 2 Balken einen orangenen Balken ein, der da nicht hingehört.
Das Verhalten kann ich mir gerade nicht so recht erklären. Wenn am Ende was fehlen würde, falls HEIGHT sich nicht ohne Rest durch MAX_PROCESSES teilen lässt,
wäre es für mich logisch, aber das ist gerade merkwürdig.
Ist wahrscheinlich richtig simpel, aber ich sehs gerad net.
So siehts aus für MAX_PROCESSES=9:
s14.directupload.net/file/d/3452/h6p5olyn_png.htm
für MAX_PROCESSES=64:
s14.directupload.net/file/d/3452/pbvcmfwf_png.htm
und so sollte es korrekt aussehen:
s14.directupload.net/file/d/3452/iy96qzib_png.htm
Danke schonma,
lg