mandelbrot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2019 Mael Keryell
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <starpu.h>
  20. #include "cpu_mandelbrot.h"
  21. void cpu_mandelbrot(void **, void *);
  22. void gpu_mandelbrot(void **, void *);
  23. static struct starpu_perfmodel model =
  24. {
  25. .type = STARPU_HISTORY_BASED,
  26. .symbol = "history_perf"
  27. };
  28. static struct starpu_codelet cl =
  29. {
  30. .cpu_funcs = {cpu_mandelbrot},
  31. //.cuda_funcs = {gpu_mandelbrot},
  32. .nbuffers = 1,
  33. .modes = {STARPU_W},
  34. .model = &model
  35. };
  36. void mandelbrot_with_starpu(long long *pixels, struct params *p, long long dim, long long nslicesx)
  37. {
  38. starpu_data_handle_t pixels_handle;
  39. starpu_matrix_data_register(&pixels_handle, STARPU_MAIN_RAM, (uintptr_t)pixels, dim, dim, dim, sizeof(long long));
  40. struct starpu_data_filter horiz =
  41. {
  42. .filter_func = starpu_matrix_filter_block,
  43. .nchildren = nslicesx
  44. };
  45. starpu_data_partition(pixels_handle, &horiz);
  46. long long taskx;
  47. for (taskx = 0; taskx < nslicesx; taskx++)
  48. {
  49. struct starpu_task *task = starpu_task_create();
  50. task->cl = &cl;
  51. task->handles[0] = starpu_data_get_child(pixels_handle, taskx);
  52. task->cl_arg = p;
  53. task->cl_arg_size = sizeof(*p);
  54. if (starpu_task_submit(task)!=0) fprintf(stderr,"submit task error\n");
  55. }
  56. starpu_task_wait_for_all();
  57. starpu_data_unpartition(pixels_handle, STARPU_MAIN_RAM);
  58. starpu_data_unregister(pixels_handle);
  59. }
  60. void pixels2img(long long *pixels, long long width, long long height, const char *filename)
  61. {
  62. FILE *fp = fopen(filename, "w");
  63. if (!fp)
  64. return;
  65. 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}};
  66. fprintf(fp, "P3\n%lld %lld\n255\n", width, height);
  67. long long i, j;
  68. for (i = 0; i < height; ++i)
  69. {
  70. for (j = 0; j < width; ++j)
  71. {
  72. fprintf(fp, "%d %d %d ", MAPPING[pixels[j*width+i]][0], MAPPING[pixels[j*width+i]][1], MAPPING[pixels[j*width+i]][2]);
  73. }
  74. }
  75. fclose(fp);
  76. }
  77. double min_times(double cr, double ci, long long dim, long long nslices, int gen_images)
  78. {
  79. long long *pixels = calloc(dim*dim, sizeof(long long));
  80. struct params *p = calloc(nslices, sizeof(struct params));
  81. double t_min = 0;
  82. long long i;
  83. for (i=0; i<nslices; i++)
  84. {
  85. p[i].centerr = cr;
  86. p[i].centeri = ci;
  87. p[i].offset = i*dim/nslices;
  88. p[i].dim = dim;
  89. }
  90. double start, stop, exec_t;
  91. for (i = 0; i < 10; i++)
  92. {
  93. start = starpu_timing_now(); // starpu_timing_now() gives the time in microseconds.
  94. mandelbrot_with_starpu(pixels, &p[i], dim, nslices);
  95. stop = starpu_timing_now();
  96. exec_t = (stop-start)*1.e3;
  97. if (t_min==0 || t_min>exec_t)
  98. t_min = exec_t;
  99. }
  100. if (gen_images == 1)
  101. {
  102. char filename[64];
  103. snprintf(filename, 64, "out%lld.ppm", dim);
  104. pixels2img(pixels,dim,dim,filename);
  105. }
  106. free(pixels);
  107. free(p);
  108. return t_min;
  109. }
  110. void display_times(double cr, double ci, long long start_dim, long long step_dim, long long stop_dim, long long nslices, int gen_images)
  111. {
  112. long long dim;
  113. for (dim = start_dim; dim <= stop_dim; dim += step_dim)
  114. {
  115. printf("Dimension: %lld...\n", dim);
  116. double res = min_times(cr, ci, dim, nslices, gen_images);
  117. res = res / dim / dim; // time per pixel
  118. printf("%lld %lf\n", dim, res);
  119. }
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. double cr, ci;
  124. long long start_dim, step_dim, stop_dim, nslices;
  125. int gen_images;
  126. if (argc != 8)
  127. {
  128. printf("Usage: %s cr ci start_dim step_dim stop_dim nslices(must divide dims) gen_images. Using default parameters\n", argv[0]);
  129. cr = -0.800671;
  130. ci = -0.158392;
  131. start_dim = 32;
  132. step_dim = 32;
  133. stop_dim = 512;
  134. nslices = 4;
  135. gen_images = 0;
  136. }
  137. else
  138. {
  139. cr = (float) atof(argv[1]);
  140. ci = (float) atof(argv[2]);
  141. start_dim = atoll(argv[3]);
  142. step_dim = atoll(argv[4]);
  143. stop_dim = atoll(argv[5]);
  144. nslices = atoll(argv[6]);
  145. gen_images = atoi(argv[7]);
  146. }
  147. if (starpu_init(NULL) != EXIT_SUCCESS)
  148. {
  149. fprintf(stderr, "ERROR\n");
  150. return 77;
  151. }
  152. display_times(cr, ci, start_dim, step_dim, stop_dim, nslices, gen_images);
  153. starpu_shutdown();
  154. return 0;
  155. }