Skip to content

Commit

Permalink
Tried to address #4, but the bug is still in place
Browse files Browse the repository at this point in the history
  • Loading branch information
programagor committed Jun 1, 2018
1 parent bdfa59a commit aa90c02
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Binary file modified bin/rainbrot-gen
Binary file not shown.
15 changes: 9 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ int main (int argc,char** argv)
off_t fsize = (off_t)(sizeof(uint64_t))*args.re_size*args.im_size;
int files[l];
uint64_t *maps[l];

uint64_t *zeros=calloc(args.re_size,sizeof(uint64_t)); /* String full of zeros, to write into new files in batches */
if(!zeros)
{
fprintf(stderr,"Can't create a string full of zeros, quitting\n");
return(1);
}
for(int i=0; i<l; i++)
{
char fname[STR_MAXLEN];
Expand All @@ -120,7 +125,6 @@ int main (int argc,char** argv)
{
/* Create file */
files[i]=open(fpath,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
uint64_t *zeros=calloc(args.re_size,sizeof(uint64_t)); /* Get string full of zeros, to write into file in batches */
for(uint64_t x=0;x<args.re_size;x++)
{
if(-write(files[i],zeros,args.im_size*sizeof(uint64_t))==-1) /* Create file of the right size, full of zeros */
Expand All @@ -131,7 +135,6 @@ int main (int argc,char** argv)
return(1);
}
}
free(zeros);
if(v)
fprintf(stdout,"created.\n");
close(files[i]);
Expand Down Expand Up @@ -165,14 +168,14 @@ int main (int argc,char** argv)
return(1);
}
/* Finally, when the file is ready, map it to memory */
if(!(maps[i]=mmap(NULL,fsize,PROT_READ|PROT_WRITE,MAP_SHARED,files[i],0)))
if((maps[i]=mmap(NULL,fsize,PROT_READ|PROT_WRITE,MAP_SHARED,files[i],0))==MAP_FAILED)
{
fprintf(stderr,"Can't create mapping\n");
fprintf(stderr,"Can't create mapping, quitting\n");
return(1);
}
madvise(maps[i],fsize,MADV_RANDOM);
}

free(zeros);
/* Now, everything is ready. Let's roll! */

/* Create threads */
Expand Down
31 changes: 25 additions & 6 deletions src/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ void* worker(void *arg_v)

/* Create thread-local buffer */
uint64_t **buff=malloc(arg->re_size*sizeof(uint64_t*));
if(!buff)
{
fprintf(stderr,"Can't allocate space for buffer array\n");
exit(1);
}
for(uint32_t x=0;x<arg->re_size;x++)
{
buff[x]=calloc(arg->im_size,sizeof(uint64_t));
Expand All @@ -29,6 +34,14 @@ void* worker(void *arg_v)
exit(1);
}
}

/* Keep track of which rows were written to */
uint8_t *dirty_rows=calloc(arg->re_size,sizeof(uint8_t));
if(!dirty_rows)
{
fprintf(stderr,"Can't allocate space for dirty rows marker, quitting\n");
exit(1);
}

/* Run until number of runs is reached */
while(1)
Expand Down Expand Up @@ -90,8 +103,7 @@ void* worker(void *arg_v)
while(inside==1);

int64_t idx_x,idx_y; /* buff (2D array) coords */
uint8_t dirty_rows[arg->re_size];
for(uint32_t i=0;i< arg->re_size ;dirty_rows[i++]=0); /*zero dirty rows */

/* Now we have a point which is outside, can iterate and draw */
Z=c;
uint64_t i;
Expand Down Expand Up @@ -132,10 +144,17 @@ void* worker(void *arg_v)
}
pthread_mutex_unlock(&arg->locks[k]);

for(uint32_t x=0;x<arg->re_size;x++)for(uint32_t y=0;y<arg->im_size;y++)
{
buff[x][y]=0;
}
for(uint32_t x=0;x< arg->re_size;x++)
{
if(dirty_rows[x])
{
for(uint32_t y=0;y< arg->im_size;y++)
{
buff[x][y]=0;
}
dirty_rows[x]=0;
}
}

}
}
Expand Down

0 comments on commit aa90c02

Please sign in to comment.