Esempio n. 1
0
int
main( int argc, char **argv )
{
    nthreads = argc == 2 ? atoi( argv[1] ) : 0;

    if( nthreads < 1 ){
      fprintf( stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0] );
      return 1;
    }

    scene = create_sphereflake_scene( sphereflake_recursion );

    /* Write the image format header */
    /* P3 is an ASCII-formatted, color, PPM file */
    printf( "P3\n%d %d\n%d\n", width, height, max_color );
    printf( "# Rendering scene with %d spheres and %d lights\n",
            scene.sphere_count,
            scene.light_count );

    /* Create an array of pthread_t objects based on specified number of threads */
    pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * nthreads);
    if(threads == NULL){
        fprintf(stderr, "An error occurred while allocating memory for threads");
        exit(1);
    }

    /* Created an array of thread ID's represented as integers */
    int *threadNum = (int *)malloc(sizeof(int) * nthreads);
    if(threadNum == NULL){
        fprintf(stderr, "An error occurred while allocating memory for thread IDs");
        exit(1);
    }

    /* Create threads that each calculate the color of specific pixels */
    for(int i = 0; i < nthreads; i++){
	threadNum[i] = i;
	pthread_create(&threads[i], NULL, threadFunction, &threadNum[i]);
    }

    /* Wait for all threads to complete before continuing */
    for(int i = 0; i < nthreads; i++) {
	pthread_join(threads[i], NULL); 
    }

    /* write each pixel out to disk. ppm is forgiving about whitespace,
     * but has a maximum of 70 chars/line, so use one line per pixel
     */
    for(int i = 0; i < width; i++){
        for(int j = 0; j < height; j++){
            printf( "%.0f %.0f %.0f\n", scaled_color[i][j][0], scaled_color[i][j][1], scaled_color[i][j][2] );
        }
        printf( "\n" );
    }
 
    /* Free dynamically allocated memory */
    free_scene( &scene );
    free(threads);
    free(threadNum);

    if( ferror( stdout ) || fclose( stdout ) != 0 )
    {
        fprintf( stderr, "Output error\n" );
	return 1;
    }

    return 0;
}
int main(int argc, char **argv)
{
	nthreads = argc == 2 ? atoi(argv[1]) : 0;

	if (nthreads < 1)
	{
		fprintf(stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0]);
		return 1;
	}

	if (nthreads > width)
	{
		nthreads = width;
	}

	scene = create_sphereflake_scene(sphereflake_recursion);

	/* Write the image format header */
	/* P3 is an ASCII-formatted, color, PPM file */
	printf("P3\n%d %d\n%d\n", width, height, max_color);
	printf("# Rendering scene with %d spheres and %d lights\n",
		scene.sphere_count,
		scene.light_count);


	//create threads
	pthread_t tids[nthreads];
	int threadnum[nthreads];
	int t;
	int ret;
	for (t = 0; t < nthreads; t++)
	{
		threadnum[t] = t;
		ret = pthread_create(&tids[t], NULL, threadFunction, (void *)&threadnum[t]);
		if (ret)
		{
			printf("Error creating thread. Error code is %d\n", ret);
			exit(-1);
		}
	}

	//join 
	for (t = 0; t < nthreads; t++)
	{
		ret = pthread_join(tids[t], NULL);
		if (ret)
		{
			printf("Error joining thread. Error code is %d\n", ret);
			exit(-2);
		}
	}

	//PRINT THE PICTURE
	int index;
	for (index = 0; index < height * width; index++)
	{
		COLORS_thread current = gimpCity[index];
		//print extra new line for the next line
		if (index % height == 0 && index != 0)
		{
			printf("\n");
		}

		/* write this pixel out to disk. ppm is forgiving about whitespace,
		* but has a maximum of 70 chars/line, so use one line per pixel
		*/
		printf("%.0f %.0f %.0f\n",
			current.red, current.green, current.blue);
	}
	printf("\n");

	free_scene(&scene);

	if (ferror(stdout) || fclose(stdout) != 0)
	{
		fprintf(stderr, "Output error\n");
		return 1;
	}

	return 0;
}
Esempio n. 3
0
int
main( int argc, char **argv )
{
    nthreads = argc == 2 ? atoi( argv[1] ) : 0;

    if( nthreads < 1 )
    {
      fprintf( stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0] );
      return 1;
    }
/*
    if( nthreads != 1 )
    {
      fprintf( stderr, "%s: Multithreading is not supported yet.\n", argv[0] );
      return 1;
    }
*/
    scene = create_sphereflake_scene( sphereflake_recursion );

    /* Write the image format header */
    /* P3 is an ASCII-formatted, color, PPM file */
    printf( "P3\n%d %d\n%d\n", width, height, max_color );
    printf( "# Rendering scene with %d spheres and %d lights\n",
            scene.sphere_count,
            scene.light_count );


// MY CODE//
    //int extras = width % nthreads;
    //int min = width / nthreads;

    pthread_t *list = malloc(nthreads * sizeof(pthread_t));
    //int *index = malloc (nthreads * sizeof(int));

    information* info = malloc(nthreads*sizeof(information*));
    //info->id = 0;
    //initiate structs
    for (int a = 0; a < nthreads; a++)
    {
	(info+a)->id = 0;
    }
    for (int a = 0; a < nthreads; a++)
    {
	//count++;
	//index[a] = a;
	pthread_create(list + a, NULL, threadfunc, (info+a));
	(info+a+1)->id = (info+a)->id + 1;
    }

    for( int b=0; b < nthreads; b++)
    	pthread_join(list[b], NULL);

    for (int x = 0; x < width; x++)
    {
	for (int y=0; y < height; y++)
	{
		 /* write this pixel out to disk. ppm is forgiving about whitespace,
             	* but has a maximum of 70 chars/line, so use one line per pixel
             		*/
              printf( "%.0f %.0f %.0f\n",
              scaled_color[x][y][0], scaled_color[x][y][1], scaled_color[x][y][2] );
	}
    }

//END MY CODE//

    free_scene( &scene );

    if( ferror( stdout ) || fclose( stdout ) != 0 )
    {
        fprintf( stderr, "Output error\n" );
	return 1;
    }

    return 0;
}
Esempio n. 4
0
File: main.c Progetto: kkodani/cs35L
int
main( int argc, char **argv )
{
    int nthreads = argc == 2 ? atoi( argv[1] ) : 0;

    if( nthreads < 1 || nthreads > MAX_NTHREAD)
    {
      fprintf( stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0] );
      return 1;
    }
/*
    if( nthreads != 1 )
    {
      fprintf( stderr, "%s: Multithreading is not supported yet.\n", argv[0] );
      return 1;
    }
*/
    scene_t scene = create_sphereflake_scene( sphereflake_recursion );

    /* Write the image format header */
    /* P3 is an ASCII-formatted, color, PPM file */
    printf( "P3\n%d %d\n%d\n", width, height, max_color );
    printf( "# Rendering scene with %d spheres and %d lights\n",
            scene.sphere_count,
            scene.light_count );

    Vec3 camera_pos;
    set( camera_pos, 0., 0., -4. );
    Vec3 camera_dir;
    set( camera_dir, 0., 0., 1. );
    const double camera_fov = 75.0 * (PI/180.0);
    Vec3 bg_color;
    set( bg_color, 0.8, 0.8, 1 );

    const double pixel_dx = tan( 0.5*camera_fov ) / ((double)width*0.5);
    const double pixel_dy = tan( 0.5*camera_fov ) / ((double)height*0.5);
    const double subsample_dx
        = halfSamples  ? pixel_dx / ((double)halfSamples*2.0)
                       : pixel_dx;
    const double subsample_dy
        = halfSamples ? pixel_dy / ((double)halfSamples*2.0)
                      : pixel_dy;


    pthread_t threads[128];
	struct Arguments arguments[MAX_NTHREAD];
    int image_size = width*height*3;
	float *image = (float *)malloc(image_size*sizeof(float));
	
	for (int i = 0; i < nthreads; i++)
	{
		arguments[i].nthreads = nthreads;
		arguments[i].width = width;
		arguments[i].pixel_dx = pixel_dx;
		arguments[i].height = height;
		arguments[i].pixel_dy = pixel_dy;
		arguments[i].halfSamples = halfSamples;
		arguments[i].subsample_dx = subsample_dx;
		arguments[i].subsample_dy = subsample_dy;
		arguments[i].bg_color = &bg_color;
		arguments[i].scene = &scene;
		arguments[i].camera_pos = &camera_pos;
		arguments[i].image = image;
		arguments[i].thread_num = i;
	}
	
	for (int i = 0; i < nthreads; i++)
	{	
		if (pthread_create(&threads[i],NULL,&single_thread,(void *)&arguments[i]))
        {
			printf("Cannot create thread!!!\n");
			return 1;
        }
	}
	
	for (int i = 0; i < nthreads; i++)
	{
		if (pthread_join(threads[i],NULL))
		{
			printf("Cannot join thread!!!\n");
			return 1;
		}
	}
	
	/* print out whole array*/
	for (int i = 0;i < image_size;i+=3)
	{
		if ((i)%(height) == 0 && i != 0)
			printf("\n");
		printf( "%.0f %.0f %.0f\n",image[i],image[i+1],image[i+2]);
	}
	printf("\n");
	
	free(image);
	
	
	
    free_scene( &scene );

    if( ferror( stdout ) || fclose( stdout ) != 0 )
    {
        fprintf( stderr, "Output error\n" );
	return 1;
    }

    return 0;
}
Esempio n. 5
0
int
main( int argc, char **argv )
{
    struct storage info;
    info.nthreads = argc == 2 ? atoi( argv[1] ) : 0;

    if( info.nthreads < 1 )
    {
      fprintf( stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0] );
      return 1;
    }

    /*
    if( nthreads != 1 )
    {
      fprintf( stderr, "%s: Multithreading is not supported yet.\n", argv[0] );
      return 1;
    }
    */

    info.scene = create_sphereflake_scene( sphereflake_recursion );

    /* Write the image format header */
    /* P3 is an ASCII-formatted, color, PPM file */
    printf( "P3\n%d %d\n%d\n", width, height, max_color );
    printf( "# Rendering scene with %d spheres and %d lights\n",
            info.scene.sphere_count,
            info.scene.light_count );

    set( info.camera_pos, 0., 0., -4. );

    set( info.camera_dir, 0., 0., 1. );
    info.camera_fov = 75.0 * (PI/180.0);

    set( info.bg_color, 0.8, 0.8, 1 );

    info.pixel_dx = tan( 0.5*info.camera_fov ) / ((double)width*0.5);
    info.pixel_dy = tan( 0.5*info.camera_fov ) / ((double)height*0.5);
    info.subsample_dx
        = halfSamples  ? info.pixel_dx / ((double)halfSamples*2.0)
                       : info.pixel_dx;
    info.subsample_dy
        = halfSamples ? info.pixel_dy / ((double)halfSamples*2.0)
                      : info.pixel_dy;


    pthread_t* tids = (pthread_t*)malloc(info.nthreads*sizeof(pthread_t));

    int ret;

    struct thread allthreads[8]; //each thread needs its own t_num but can share other data
    //store ptr to shared data
    for (int k = 0; k < info.nthreads; k++)
    {
        allthreads[k].m_stor = &info;
    }

    int i, j;
    for (i = 0; i < info.nthreads; i++)
    {
        allthreads[i].t_num = i;
        struct thread* ptr = &allthreads[i];
        ret=pthread_create(&tids[i], NULL, &processColumn, (void*) ptr);
        if (ret)
        {
            printf("Error creating thread. Error code is %d\n", ret);
            exit(-1);
        }
    }

    for (j = 0; j < info.nthreads; j++)
    {
        ret = pthread_join(tids[j], NULL);
        if (ret)
        {
            printf("Error pthread_join. Error code is %d\n", ret);
            exit(-1);
        }
    }

    /*print*/
    for (i = 0; i < width; i++)
    {
        for (j = 0; j < height; j++)
        {
            printf( "%.0f %.0f %.0f\n",
                info.scaled_color[i][j][0], info.scaled_color[i][j][1], info.scaled_color[i][j][2]);
        }
        printf( "\n" );
    }

    free_scene( &(info.scene) );
    free(tids);

    if( ferror( stdout ) || fclose( stdout ) != 0 )
    {
        fprintf( stderr, "Output error\n" );
	   return 1;
    }

    return 0;
}
Esempio n. 6
0
int
main( int argc, char **argv )
{
    int nthreads = argc == 2 ? atoi( argv[1] ) : 0;

    if( nthreads < 1 )
    {
      fprintf( stderr, "%s: usage: %s NTHREADS\n", argv[0], argv[0] );
      return 1;
    }

    scene = create_sphereflake_scene( sphereflake_recursion );

    /* Write the image format header */
    /* P3 is an ASCII-formatted, color, PPM file */
    printf( "P3\n%d %d\n%d\n", width, height, max_color );
    printf( "# Rendering scene with %d spheres and %d lights\n",
            scene.sphere_count,
            scene.light_count );

    //Vec3 camera_pos;
    set( camera_pos, 0., 0., -4. );
    //Vec3 camera_dir;
    set( camera_dir, 0., 0., 1. );
    camera_fov = 75.0 * (PI/180.0);
    //Vec3 bg_color;
    set( bg_color, 0.8, 0.8, 1 );

    pixel_dx = tan( 0.5*camera_fov ) / ((double)width*0.5);
    pixel_dy = tan( 0.5*camera_fov ) / ((double)height*0.5);
    subsample_dx
        = halfSamples  ? pixel_dx / ((double)halfSamples*2.0)
                       : pixel_dx;
    subsample_dy
        = halfSamples ? pixel_dy / ((double)halfSamples*2.0)
                      : pixel_dy;

    // thread setup
	// store the threads in threads[]
	// store the structs containing the start/end in thread_info[]
	pthread_t threads[nthreads];
	struct information thread_info[nthreads];

	for (int k = 0; k < nthreads; k++) {
		thread_info[k].start = (k)*(width/nthreads);
		thread_info[k].end = (k+1)*(width/nthreads);
	}
	thread_info[nthreads-1].end = width;

	// spawn the threads
	int rc;
	for (int k = 0; k < nthreads; k++) {
		rc = pthread_create(&threads[k], NULL, render_pixel, &thread_info[k]);
		if (rc) {
			printf("Error: unable to create thread");
			exit(-1);
      	}
	}

	for (int k = 0; k < nthreads; k++) {
		rc = pthread_join(threads[k], NULL);
		if (rc) {
			printf("Error: unable to join thread");
			exit(-1);
		}
	}

	for (int j = 0; j < width; j++) {
		for (int k = 0; k < height; k++) {
			/* write this pixel out to disk. ppm is forgiving about whitespace,
			 * but has a maximum of 70 chars/line, so use one line per pixel
			 */
			printf( "%.0f %.0f %.0f\n",
			s_color[j][k][0], s_color[j][k][1], s_color[j][k][2] );

		}
		printf("\n");
	}

	// for each pixel

    free_scene( &scene );

    if( ferror( stdout ) || fclose( stdout ) != 0 )
    {
        fprintf( stderr, "Output error\n" );
	return 1;
    }

    return 0;
}