<commit_before>/* mandelbrot parallel fractal generator
 * Copyright (C) 2007 <NAME> (<EMAIL>)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

#include "Image.h"

/* The fractal data type */
#define FTYPE float
#define FTYPE_STR "%f "

/* The colormap */
int red[]   = {0, 0  , 0,   0  , 128, 255, 255, 255};
int green[] = {0, 0  , 128, 255, 128, 128, 255, 255};
int blue[]  = {0, 255, 255, 128, 0  , 0  , 128, 255};

/* Calculated log (2) beforehand */
double logtwo = 0.693147180559945;

FTYPE *gen_mandel (double xmin, double xmax, 
		   double ymin, double ymax,
		   double res, int it);
FTYPE get_val (double creal, double cimag, int it);
Rgb colormap (FTYPE val, int it);
void write_colormap ();
int write_data (FTYPE *img, int w, int h, int it);

/* Output options */
int write_text = 0; /* Write textfile */
int write_bmp = 1;  /* Write bitmap */
int write_pipe = 0; /* Write data through pipe to parent */
int pipe_fid = 0;   /* Pipe to parent */

int main ()
{
  write_colormap (); /* Write out colormap for viewing */

  /* Parameters */
  double xmin = -2.5;
  double xmax = 1.5;
  double ymin = -1.5;
  double ymax = 1.5;
  double res = 0.001;
  int it = 256;
  int jobs = 5;
  
  /* Image dimensions */
  int w = ceil ((xmax - xmin) / res);
  int h = ceil ((ymax - ymin) / res);
  printf ("Image size: %dx%d\n", w, h);

  /* Generate fractal */
  printf ("Generating fractal ...\n");
  int i;
  double job_y = (ymax - ymin)/jobs; /* Job image height */
  int *read_pipe = malloc ((jobs + 1) * sizeof(int));
  for (i = 0; i < jobs; i++)
    {
      /* Communication pipe */
      pipe (read_pipe + i);
      pipe_fid = read_pipe[i + 1];
      
      pid_t fork_out = fork ();
      if (fork_out < 0)
	fprintf (stderr, "Failed to fork\n");
      
      if (fork_out == 0)
	{
	  FTYPE *img = malloc (w * h/jobs * sizeof (FTYPE));
	  img = gen_mandel (xmin, xmax,
			    ymin + job_y * i, ymax + job_y * (i + 1),
			    res, it);
	  write_text = 1;
	  write_data (img, w, h/jobs, it);	  
	  
	  /* Write result to parent */
	  write_bmp = 0;
	  write_text = 0;
	  write_pipe = 1;
	  write_data (img, w, h/jobs, it);	  
	  
	  exit (EXIT_SUCCESS);<commit_msg>Fixed bug where child was doing too much.<commit_after>/* mandelbrot parallel fractal generator
 * Copyright (C) 2007 <NAME> (mos<EMAIL>)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

#include "Image.h"

/* The fractal data type */
#define FTYPE float
#define FTYPE_STR "%f "

/* The colormap */
int red[]   = {0, 0  , 0,   0  , 128, 255, 255, 255};
int green[] = {0, 0  , 128, 255, 128, 128, 255, 255};
int blue[]  = {0, 255, 255, 128, 0  , 0  , 128, 255};

/* Calculated log (2) beforehand */
double logtwo = 0.693147180559945;

FTYPE *gen_mandel (double xmin, double xmax, 
		   double ymin, double ymax,
		   double res, int it);
FTYPE get_val (double creal, double cimag, int it);
Rgb colormap (FTYPE val, int it);
void write_colormap ();
int write_data (FTYPE *img, int w, int h, int it);

/* Output options */
int write_text = 0; /* Write textfile */
int write_bmp = 1;  /* Write bitmap */
int write_pipe = 0; /* Write data through pipe to parent */
int pipe_fid = 0;   /* Pipe to parent */

int main ()
{
  write_colormap (); /* Write out colormap for viewing */

  /* Parameters */
  double xmin = -2.5;
  double xmax = 1.5;
  double ymin = -1.5;
  double ymax = 1.5;
  double res = 0.001;
  int it = 256;
  int jobs = 5;
  
  /* Image dimensions */
  int w = ceil ((xmax - xmin) / res);
  int h = ceil ((ymax - ymin) / res);
  printf ("Image size: %dx%d\n", w, h);

  /* Generate fractal */
  printf ("Generating fractal ...\n");
  int i;
  double job_y = (ymax - ymin)/jobs; /* Job image height */
  int *read_pipe = malloc ((jobs + 1) * sizeof(int));
  for (i = 0; i < jobs; i++)
    {
      /* Communication pipe */
      pipe (read_pipe + i);
      pipe_fid = read_pipe[i + 1];
      
      pid_t fork_out = fork ();
      if (fork_out < 0)
	fprintf (stderr, "Failed to fork\n");
      
      if (fork_out == 0)
	{
	  FTYPE *img = malloc (w * h/jobs * sizeof (FTYPE));
	  img = gen_mandel (xmin, xmax,
			    ymin + job_y * i, ymin + job_y * (i + 1),
			    res, it);
	  write_text = 1;
	  write_data (img, w, h/jobs, it);	  
	  
	  /* Write result to parent */
	  write_bmp = 0;
	  write_text = 0;
	  write_pipe = 1;
	  write_data (img, w, h/jobs, it);	  
	  
	  exit (EXIT_SUCCESS);
{'fraction_non_alphanumeric': 0.13603137814227134, 'fraction_numerical': 0.037796398645034764, 'mean_word_length': 2.727574750830565, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3410093', 'n_tokens_mistral': 2217, 'n_tokens_neox': 1959, 'n_words': 740}