Przeglądaj źródła

julia/examples/mandelbrot: fix starpu version

Nathalie Furmento 5 lat temu
rodzic
commit
00a7023c57

+ 9 - 12
julia/examples/mandelbrot/cpu_mandelbrot.c

@@ -1,14 +1,7 @@
 #include <stdio.h>
 #include <starpu.h>
 #include <math.h>
-
-struct params {
-        double centerr;
-        double centeri;
-        long offset;
-        long dim;
-};
-
+#include "cpu_mandelbrot.h"
 
 void cpu_mandelbrot(void *descr[], void *cl_arg)
 {
@@ -38,14 +31,17 @@ void cpu_mandelbrot(void *descr[], void *cl_arg)
 
         long long x,y;
 
-        for (y = 0; y < height; y++){
-                for (x = 0; x < width; x++){
+        for (y = 0; y < height; y++)
+	{
+                for (x = 0; x < width; x++)
+		{
                         cr = centerr + (x - (dim/2)) * iz;
 			zr = cr;
                         ci = centeri + (y+offset - (dim/2)) * iz;
                         zi = ci;
 
-                        for (n = 0; n <= max_iterations; n++) {
+                        for (n = 0; n <= max_iterations; n++)
+			{
 				if (zr*zr + zi*zi>diverge) break;
                                 tmp = zr*zr - zi*zi + cr;
                                 zi = 2*zr*zi + ci;
@@ -61,7 +57,8 @@ void cpu_mandelbrot(void *descr[], void *cl_arg)
 
 char* CPU = "cpu_mandelbrot";
 char* GPU = "gpu_mandelbrot";
-extern char *starpu_find_function(char *name, char *device) {
+extern char *starpu_find_function(char *name, char *device)
+{
 	if (!strcmp(device,"gpu")) return GPU;
 	return CPU;
 }

+ 8 - 0
julia/examples/mandelbrot/cpu_mandelbrot.h

@@ -0,0 +1,8 @@
+struct params {
+        double centerr;
+        double centeri;
+        long offset;
+        long dim;
+};
+
+

+ 42 - 40
julia/examples/mandelbrot/mandelbrot.c

@@ -16,33 +16,31 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <starpu.h>
+#include "cpu_mandelbrot.h"
 
 void cpu_mandelbrot(void **, void *);
 void gpu_mandelbrot(void **, void *);
 
 static struct starpu_perfmodel model =
 {
-		.type = STARPU_HISTORY_BASED,
-		.symbol = "history_perf"
+	.type = STARPU_HISTORY_BASED,
+	.symbol = "history_perf"
 };
 
 static struct starpu_codelet cl =
 {
-	.cpu_funcs = {cpu_mandelbrot},
+ 	.cpu_funcs = {cpu_mandelbrot},
 	//.cuda_funcs = {gpu_mandelbrot},
-	.nbuffers = 2,
-	.modes = {STARPU_W, STARPU_R},
+	.nbuffers = 1,
+	.modes = {STARPU_W},
 	.model = &model
 };
 
-
-void mandelbrot_with_starpu(long long *pixels, float *params, long long dim, long long nslicesx)
+void mandelbrot_with_starpu(long long *pixels, struct params *p, long long dim, long long nslicesx)
 {
 	starpu_data_handle_t pixels_handle;
-	starpu_data_handle_t params_handle;
 
 	starpu_matrix_data_register(&pixels_handle, STARPU_MAIN_RAM, (uintptr_t)pixels, dim, dim, dim, sizeof(long long));
-	starpu_matrix_data_register(&params_handle, STARPU_MAIN_RAM, (uintptr_t)params, 4*nslicesx, 4*nslicesx, 1, sizeof(float));
 
 	struct starpu_data_filter horiz =
 	{
@@ -51,66 +49,68 @@ void mandelbrot_with_starpu(long long *pixels, float *params, long long dim, lon
 	};
 
 	starpu_data_partition(pixels_handle, &horiz);
-	starpu_data_partition(params_handle, &horiz);
 
 	long long taskx;
 
-	for (taskx = 0; taskx < nslicesx; taskx++){
+	for (taskx = 0; taskx < nslicesx; taskx++)
+	{
 		struct starpu_task *task = starpu_task_create();
 
 		task->cl = &cl;
 		task->handles[0] = starpu_data_get_child(pixels_handle, taskx);
-		task->handles[1] = starpu_data_get_child(params_handle, taskx);
+		task->cl_arg = p;
+		task->cl_arg_size = sizeof(*p);
 		if (starpu_task_submit(task)!=0) fprintf(stderr,"submit task error\n");
 	}
 
 	starpu_task_wait_for_all();
 
 	starpu_data_unpartition(pixels_handle, STARPU_MAIN_RAM);
-	starpu_data_unpartition(params_handle, STARPU_MAIN_RAM);
-
 	starpu_data_unregister(pixels_handle);
-	starpu_data_unregister(params_handle);
 }
 
 void pixels2img(long long *pixels, long long width, long long height, const char *filename)
 {
-  FILE *fp = fopen(filename, "w");
-  if (!fp)
-    return;
+	FILE *fp = fopen(filename, "w");
+	if (!fp)
+		return;
 
-  int MAPPING[16][3] = {{66,30,15},{25,7,26},{9,1,47},{4,4,73},{0,7,100},{12,44,138},{24,82,177},{57,125,209},{134,181,229},{211,236,248},{241,233,191},{248,201,95},{255,170,0},{204,128,0},{153,87,0},{106,52,3}};
+	int MAPPING[16][3] = {{66,30,15},{25,7,26},{9,1,47},{4,4,73},{0,7,100},{12,44,138},{24,82,177},{57,125,209},{134,181,229},{211,236,248},{241,233,191},{248,201,95},{255,170,0},{204,128,0},{153,87,0},{106,52,3}};
 
-  fprintf(fp, "P3\n%lld %lld\n255\n", width, height);
-  long long i, j;
-  for (i = 0; i < height; ++i) {
-    for (j = 0; j < width; ++j) {
-      fprintf(fp, "%d %d %d ", MAPPING[pixels[j*width+i]][0], MAPPING[pixels[j*width+i]][1], MAPPING[pixels[j*width+i]][2]);
-    }
-  }
+	fprintf(fp, "P3\n%lld %lld\n255\n", width, height);
+	long long i, j;
+	for (i = 0; i < height; ++i)
+	{
+		for (j = 0; j < width; ++j)
+		{
+			fprintf(fp, "%d %d %d ", MAPPING[pixels[j*width+i]][0], MAPPING[pixels[j*width+i]][1], MAPPING[pixels[j*width+i]][2]);
+		}
+	}
 
-  fclose(fp);
+	fclose(fp);
 }
 
 double min_times(double cr, double ci, long long dim, long long nslices)
 {
 	long long *pixels = calloc(dim*dim, sizeof(long long));
-	float *params = calloc(4*nslices, sizeof(float));
+	struct params *p = calloc(nslices, sizeof(struct params));
 
 	double t_min = 0;
 	long long i;
 
-	for (i=0; i<nslices; i++) {
-		params[4*i+0] = cr;
-		params[4*i+1] = ci;
-		params[4*i+2] = i*dim/nslices;
-		params[4*i+3] = dim;
+	for (i=0; i<nslices; i++)
+	{
+		p[i].centerr = cr;
+		p[i].centeri = ci;
+		p[i].offset = i*dim/nslices;
+		p[i].dim = dim;
 	}
 
 	double start, stop, exec_t;
-	for (i = 0; i < 10; i++){
+	for (i = 0; i < 10; i++)
+	{
 		start = starpu_timing_now(); // starpu_timing_now() gives the time in microseconds.
-		mandelbrot_with_starpu(pixels, params, dim, nslices);
+		mandelbrot_with_starpu(pixels, &p[i], dim, nslices);
 		stop = starpu_timing_now();
 		exec_t = (stop-start)*1.e3;
 		if (t_min==0 || t_min>exec_t)
@@ -122,17 +122,17 @@ double min_times(double cr, double ci, long long dim, long long nslices)
 	pixels2img(pixels,dim,dim,filename);
 
 	free(pixels);
-	free(params);
+	free(p);
 
 	return t_min;
 }
 
 void display_times(double cr, double ci, long long start_dim, long long step_dim, long long stop_dim, long long nslices)
 {
-
 	long long dim;
 
-	for (dim = start_dim; dim <= stop_dim; dim += step_dim) {
+	for (dim = start_dim; dim <= stop_dim; dim += step_dim)
+	{
 		printf("Dimension: %lld...\n", dim);
 		double res = min_times(cr, ci, dim, nslices);
 		res = res / dim / dim; // time per pixel
@@ -142,11 +142,13 @@ void display_times(double cr, double ci, long long start_dim, long long step_dim
 
 int main(int argc, char **argv)
 {
-	if (argc != 7){
+	if (argc != 7)
+	{
 		printf("Usage: %s cr ci start_dim step_dim stop_dim nslices(must divide dims)\n", argv[0]);
 		return 1;
 	}
-	if (starpu_init(NULL) != EXIT_SUCCESS){
+	if (starpu_init(NULL) != EXIT_SUCCESS)
+	{
 		fprintf(stderr, "ERROR\n");
 		return 77;
 	}

+ 2 - 3
julia/examples/mandelbrot/mandelbrot.jl

@@ -34,7 +34,7 @@ using LinearAlgebra
                 zi = 2*zr*zi + ci
                 zr = tmp
             end
-            
+
             if (n < max_iterations)
                 pixels[y,x] = round(15 * n * imi)
             else
@@ -75,7 +75,7 @@ end
 
 function min_times(cr ::Float64, ci ::Float64, dim ::Int64, nslices ::Int64)
     tmin=0;
-    
+
     pixels ::Matrix{Int64} = zeros(dim, dim)
     for i = 1:10
         t = time_ns();
@@ -101,4 +101,3 @@ end
 display_time(-0.800671,-0.158392,32,32,4096,4)
 
 starpu_shutdown()
-