improve performance

This commit is contained in:
Mylloon 2024-04-24 13:33:27 +02:00
parent 1f4c04541a
commit 7fabe51b4e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -7,17 +7,21 @@
#include <stdlib.h>
#include <time.h>
#define WIDTH 3840
#define HEIGHT 2160
#define ITERATIONS 1000
#define SCALE (WIDTH / 4.0)
#define DX (WIDTH / 2)
#define DY (HEIGHT / 2)
struct mandelbrot_args {
unsigned int *image;
double scale;
int x, y, dx, dy, width, height;
int x, y;
};
struct mandelbrot_args *
new_mandelbrot_args(unsigned int *image, double scale, int x, int y, int dx,
int dy, int width, int height)
new_mandelbrot_args(unsigned int *image, int x, int y)
{
struct mandelbrot_args *args;
@ -27,18 +31,13 @@ new_mandelbrot_args(unsigned int *image, double scale, int x, int y, int dx,
}
args->image = image;
args->scale = scale;
args->x = x;
args->y = y;
args->dx = dx;
args->dy = dy;
args->width = width;
args->height = height;
return args;
}
static int
int
mandel(double complex c)
{
double complex z = 0.0;
@ -50,13 +49,13 @@ mandel(double complex c)
return i;
}
static double complex
double complex
toc(int x, int y, int dx, int dy, double scale)
{
return ((x - dx) + I * (y - dy)) / scale;
}
static unsigned int
unsigned int
torgb(int n)
{
unsigned char r, g, b;
@ -100,18 +99,14 @@ draw_pixel(void *closure, struct scheduler *s)
{
struct mandelbrot_args *args = (struct mandelbrot_args *)closure;
unsigned int *image = args->image;
double scale = args->scale;
int x = args->x;
int y = args->y;
int dx = args->dx;
int dy = args->dy;
int width = args->width;
(void)s; // pas de nouvelle tâche dans le scheduler
free(closure);
pixel(image, scale, x, y, dx, dy, width);
pixel(image, SCALE, x, y, DX, DY, WIDTH);
}
void
@ -119,32 +114,24 @@ draw(void *closure, struct scheduler *s)
{
struct mandelbrot_args *args = (struct mandelbrot_args *)closure;
unsigned int *image = args->image;
double scale = args->scale;
int dx = args->dx;
int dy = args->dy;
int width = args->width;
int height = args->height;
free(closure);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
int rc = sched_spawn(
draw_pixel,
new_mandelbrot_args(image, scale, x, y, dx, dy, width, height),
s);
for(int y = args->y; y < HEIGHT; y++) {
for(int x = args->x; x < WIDTH; x++) {
int rc =
sched_spawn(draw_pixel, new_mandelbrot_args(image, x, y), s);
assert(rc >= 0);
}
}
}
void
draw_serial(unsigned int *image, double scale, int dx, int dy, int width,
int height)
draw_serial(unsigned int *image)
{
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
pixel(image, scale, x, y, dx, dy, width);
for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
pixel(image, SCALE, x, y, DX, DY, WIDTH);
}
}
}
@ -156,13 +143,9 @@ benchmark_mandelbrot(int serial, int nthreads)
struct timespec begin, end;
double delay;
int rc;
int width = 3840;
int height = 2160;
double scale = width / 4.0;
int dx = width / 2;
int dy = height / 2;
int size = WIDTH * HEIGHT;
if(!(image = malloc(width * height * sizeof(unsigned int)))) {
if(!(image = malloc(size * sizeof(unsigned int)))) {
perror("Image allocation");
return 1;
}
@ -170,11 +153,9 @@ benchmark_mandelbrot(int serial, int nthreads)
clock_gettime(CLOCK_MONOTONIC, &begin);
if(serial) {
draw_serial(image, scale, dx, dy, width, height);
draw_serial(image);
} else {
rc = sched_init(
nthreads, width * height, draw,
new_mandelbrot_args(image, scale, 0, 0, dx, dy, width, height));
rc = sched_init(nthreads, size, draw, new_mandelbrot_args(image, 0, 0));
assert(rc >= 0);
}