Mandelbrotmenge parallel berechnen(C)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Mandelbrotmenge parallel berechnen(C)

    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

    Quellcode

    1. /*
    2. * Writes a visual representation of the mandelbrot set into the supplied buffer.
    3. * Each pixel is represented by a pixel_data_t element containing the red, green and blue
    4. * components.
    5. *
    6. * It is possible to limit the calculation to a smaller part of the whole picture
    7. * with the parameters clip_x & y and clip_width and height.
    8. *
    9. * data: Pointer a clip_width * clip_height large memory area the data is written to.
    10. * The data is written row-wise, all pixel of the first row [x - (x + clip_width), y], followed
    11. * by pixel of the second row [x - (x + clip_width), y + 1], and so on
    12. * till [x - (x + clip_width), y + clip_height].
    13. * full_width: Width of the complete picture
    14. * full_height: Height of the complete picture
    15. * clip_x: x offset of the area to calculate
    16. * clip_y: y offset of the area to calculate
    17. * clip_width: Width of the area to calculate
    18. * clip_height: Height of the area to calculate
    19. */
    20. void calc_mandelbrot(pixel_data_t *data, int full_width, int full_height,
    21. int clip_x, int clip_y, int clip_width, int clip_height);
    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:

    Quellcode

    1. int generate_mandelbrot_parallel(const char* filename)
    2. {
    3. unsigned long start, end;
    4. // Allocate buffer for picture data
    5. pixel_data_t *dataklein = calloc(WIDTH * HEIGHT / MAX_PROCESSES, sizeof(pixel_data_t));
    6. if (dataklein)
    7. {
    8. FILE *file = fopen(filename, "wb");
    9. int i;
    10. int child[MAX_PROCESSES];
    11. if(!file){
    12. return EXIT_FAILURE;
    13. }
    14. write_bitmap_header(file, WIDTH, HEIGHT);
    15. fclose(file);
    16. start = current_time_millis();
    17. for(i = 0; i<MAX_PROCESSES; i++){
    18. child[i] = fork();
    19. if(child[i] == 0){
    20. int my_number = i; //i sollte von dem Elternprozess übernommen worden sein
    21. int clip_x = 0;
    22. int clip_widht = WIDTH;
    23. int clip_y = (HEIGHT / MAX_PROCESSES) * my_number;
    24. int clip_height = (HEIGHT / MAX_PROCESSES);
    25. /*if(my_number == MAX_PROCESSES-1){
    26. clip_height = HEIGHT - (HEIGHT/MAX_PROCESSES)*my_number;}*/
    27. FILE *file = fopen(filename, "rb+");
    28. calc_mandelbrot(dataklein, WIDTH, HEIGHT, clip_x, clip_y, clip_widht, clip_height);//dieses mal x nach y
    29. 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
    30. fwrite(dataklein, sizeof(pixel_data_t), ((WIDTH*HEIGHT) / MAX_PROCESSES), file);
    31. fclose(file);
    32. free(dataklein);
    33. exit(0);
    34. }
    35. }
    36. /*for(i = 0; i<MAX_PROCESSES;i++){
    37. waitpid(child[i], NULL, 0);
    38. }*/
    39. free(dataklein);
    40. end = current_time_millis();
    41. printf("%.2f seconds\n", (double) (end - start) / 1000);
    42. return EXIT_SUCCESS;
    43. }
    44. return EXIT_FAILURE;
    45. }
    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