Beispiel #1
0
int runKDTreePerformanceTests()
{
	std::cout << "Setup..." << std::endl; // DEBUG.

	SimpleTimer timer;

	mesh *obj_mesh = new mesh( "meshes\\bunny_small_0.obj" );

	float fovy = 45.0f;
	glm::vec2 reso( 256, 256 );
	glm::vec3 eyep( 0.5f, 0.5f, 1.0f );
	glm::vec3 vdir( 0.0f, 0.0f, -1.0f );
	glm::vec3 uvec( 0.0f, 1.0f, 0.0f );
	Camera *camera = new Camera( fovy, reso, eyep, vdir, uvec );

	BMP output_img;
	output_img.SetSize( ( int )reso.x, ( int )reso.y );
	output_img.SetBitDepth( 24 );

	float time_elapsed;
	std::string output_img_path;


	////////////////////////////////////////////////////
	// CPU brute force.
	////////////////////////////////////////////////////

	std::cout << "CPU brute force..." << std::endl; // DEBUG.

	timer.start();

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			Ray ray = camera->computeRayThroughPixel( x, y );
			glm::vec3 pixel_color = bruteForceMeshTraversal( obj_mesh, ray );

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - CPU brute force: " << time_elapsed << std::endl;

	output_img_path = "ray_casting_output\\cpu_brute_force.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// CPU stack.
	////////////////////////////////////////////////////

	std::cout << "CPU stack..." << std::endl; // DEBUG.

	timer.start();
	KDTreeCPU *kd_tree = new KDTreeCPU( obj_mesh->numTris, obj_mesh->tris, obj_mesh->numVerts, obj_mesh->verts );
	time_elapsed = timer.stop();
	std::cout << "CONSTRUCTION - CPU kd-tree: " << time_elapsed << std::endl;

	timer.start();

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			Ray ray = camera->computeRayThroughPixel( x, y );
			glm::vec3 pixel_color = kdTreeMeshTraversal( kd_tree, ray );

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - CPU stack: " << time_elapsed << std::endl;

	output_img_path = "ray_casting_output\\cpu_stack.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// CPU stackless with CPU structs.
	////////////////////////////////////////////////////

	std::cout << "CPU stackless with CPU structs..." << std::endl; // DEBUG.

	timer.start();
	kd_tree->buildRopeStructure();
	time_elapsed = timer.stop();
	std::cout << "CONSTRUCTION - CPU kd-tree rope structure: " << time_elapsed << std::endl;

	timer.start();

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			Ray ray = camera->computeRayThroughPixel( x, y );
			glm::vec3 pixel_color = kdTreeMeshStacklessTraversal( kd_tree, ray );

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - CPU stackless with CPU structs: " << time_elapsed << std::endl;

	output_img_path = "ray_casting_output\\cpu_stackless_with_cpu_structs.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// CPU stackless with GPU structs.
	////////////////////////////////////////////////////

	std::cout << "CPU stackless with GPU structs..." << std::endl; // DEBUG.

	timer.start();
	KDTreeGPU *kd_tree_gpu = new KDTreeGPU( kd_tree );

	// Create list of triangle indices for GPU kd-tree.
	std::vector<int> tri_index_vector = kd_tree_gpu->getTriIndexList();
	int *tri_index_array = new int[tri_index_vector.size()];
	for ( int i = 0; i < tri_index_vector.size(); ++i ) {
		tri_index_array[i] = tri_index_vector[i];
	}

	time_elapsed = timer.stop();
	std::cout << "CONSTRUCTION - GPU kd-tree: " << time_elapsed << std::endl;

	timer.start();

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			Ray ray = camera->computeRayThroughPixel( x, y );
			glm::vec3 pixel_color = kdTreeGPUMeshStacklessTraversal( kd_tree_gpu, tri_index_array, ray );

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - CPU stackless with GPU structs: " << time_elapsed << std::endl;

	output_img_path = "ray_casting_output\\cpu_stackless_with_gpu_structs.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// GPU brute force.
	////////////////////////////////////////////////////

	std::cout << "GPU brute force..." << std::endl; // DEBUG.

	timer.start();
	glm::vec3 *ray_cast_image_brute_force = cudaRayCastObj( camera, obj_mesh, kd_tree_gpu, true );
	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - GPU brute force: " << time_elapsed << std::endl;

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			int index = ( y * ( int )reso.x ) + x;
			glm::vec3 pixel_color = ray_cast_image_brute_force[index];;

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	output_img_path = "ray_casting_output\\gpu_brute_force.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// GPU stackless.
	////////////////////////////////////////////////////

	std::cout << "GPU stackless..." << std::endl; // DEBUG.

	timer.start();
	glm::vec3 *ray_cast_image_stackless = cudaRayCastObj( camera, obj_mesh, kd_tree_gpu, false );
	time_elapsed = timer.stop();
	std::cout << "TRAVERSAL - GPU stackless: " << time_elapsed << std::endl;

	// Iterate through all pixels.
	for ( int y = 0; y < reso.y; ++y ) {
		for ( int x = 0; x < reso.x; ++x ) {
			int index = ( y * ( int )reso.x ) + x;
			glm::vec3 pixel_color = ray_cast_image_stackless[index];;

			// Write pixel.
			output_img( x, y )->Red = ( ebmpBYTE )( pixel_color.x * 255.0f );
			output_img( x, y )->Green = ( ebmpBYTE )( pixel_color.y * 255.0f );
			output_img( x, y )->Blue = ( ebmpBYTE )( pixel_color.z * 255.0f );
		}
	}

	output_img_path = "ray_casting_output\\gpu_stackless.bmp";
	output_img.WriteToFile( output_img_path.c_str() );


	////////////////////////////////////////////////////
	// Cleanup.
	////////////////////////////////////////////////////

	delete camera;
	delete obj_mesh;
	delete kd_tree;
	delete kd_tree_gpu;
	delete[] tri_index_array;
	delete[] ray_cast_image_brute_force;
	delete[] ray_cast_image_stackless;

	return 0;
}
int  main()
   {
    SelSorter dateObjectI1;
    MrgSorter dateObjectM1;
    QkSorter dateObjectQ1;
    DateType dateValue;
    SimpleTimer timer;
    char tempString[ SMALL_STR_LEN ], insTime[ SMALL_STR_LEN ];
    char qkTime[ SMALL_STR_LEN ], mrgTime[ SMALL_STR_LEN ]; 
    bool qSortGood = false, mSortGood = false, iSortGood = false;

    // load dates ////////////////////////////////////////////////////////////
    cout << endl << "Enter list of dates: " << endl;

    while( getALine( cin, tempString ) )
       {
        dateObjectI1.add( tempString );
       }

    // assign dates to other objects /////////////////////////////////////////
    dateObjectM1 = dateObjectI1;

    dateObjectQ1 = dateObjectM1;

    // display lists, unsorted ///////////////////////////////////////////////
    displayList( dateObjectI1, 'S', UNSORTED );

    displayList( dateObjectM1, 'M', UNSORTED );

    displayList( dateObjectQ1, 'Q', UNSORTED );

    // Selection sort operation //////////////////////////////////////////////
    timer.start();

    if( dateObjectI1.sort() )
       {
        timer.stop();
        timer.getElapsedTime( insTime );
        displayList( dateObjectI1, 'S', SORTED );
        iSortGood = true;
       }

    // stop timer in case of failure
    timer.stop();

    // Merge sort operation //////////////////////////////////////////////////
    timer.start();

    if( dateObjectM1.sort() )
       {
        timer.stop();
        timer.getElapsedTime( mrgTime );
        displayList( dateObjectM1, 'M', SORTED );
        mSortGood = true;
       }

    // stop timer in case of failure
    timer.stop();

    // Quick sort operation //////////////////////////////////////////////////
    timer.start();

    if( dateObjectQ1.sort() )
       {
        timer.stop();
        timer.getElapsedTime( qkTime );
        displayList( dateObjectQ1, 'Q', SORTED );
        qSortGood = true;
       }

    // stop timer in case of failure
    timer.stop();

    // Results displayed /////////////////////////////////////////////////////
    if( iSortGood )
       {
        cout << "Elapsed Time for Selection Sort: " 
             << insTime << " seconds." << endl;
       }
    else
       {
        cout << "ERROR: Failure of Selection sort due to bad input" 
             << endl << endl;
       }

    if( mSortGood )
       {
        cout << endl << "Elapsed Time for Merge Sort: " 
             << mrgTime << " seconds." << endl;
       }
    else
       {
        cout << "ERROR: Failure of merge sort due to bad input" 
             << endl << endl;
       }


    if( qSortGood )
       {
        cout << endl << "Elapsed Time for Quick Sort: " 
             << qkTime << " seconds." << endl << endl;
       }
    else
       {
        cout << "ERROR: Failure of quick sort due to bad input" 
             << endl << endl;
       }

    return 0;
   }
Beispiel #3
0
int main (int argc, char *argv[]){
	srand(time(NULL));		//seeding rand with time
	time_t t;				
	time(&t);
	const float systemSize = 16.0;		
	const int particles = 128;
	const int nPred = 1;
	const int wait = 1;
	const float maxeta = 5;		//maximum noise parameter
	float predNoise = 0.0;
	const int realisations = 1;	//number of realisations
	const int iterations = 2000000;	//number of time steps
	const int last = 100;			//number of last steps over which order parameter would be averaged
	int c;
	//int *gsd;						//pointer to initialise array that stores different group size 
	float timeElapsed;
	Store store(particles);			//Store class object 
	store.fileOpen();
	Swarm swarm(particles, systemSize, nPred);
	swarm.allocate();
	swarm.launchRandInit((unsigned long) t);
	SimpleTimer time; time.reset();
	time.start();
	float avgEta = 0.0;
	for (float eta = maxeta; eta <= maxeta; eta = eta + 0.2){		//loop to iterate over different noise values
		store.orientationParam = 0.0;				//initialize OP to zero before each round of replication
		for (int rep = 0; rep < realisations; rep++){		//loop to perform more number of realizations
			swarm.init(eta);
			swarm.initPredator(predNoise);
			swarm.initid();
			swarm.initAttack();
			swarm.cudaCopy();
			Screen screen;
			if (screen.init() == false){
				cout << "error initialising SDL." << endl;
			}
			for (int i = 0; i < iterations; i++){		//loop to run the simulations for number of timesteps
				screen.clear();
				swarm.update();
				const Particle * const pParticles = swarm.returnParticles();	//store the particle
				for (int p = 0; p < particles; p++){
					Particle particle = pParticles[p];
					avgEta = avgEta + particle.eta;
					}
				avgEta = avgEta / particles;
				for (int p = 0; p < particles; p++){
					Particle particle = pParticles[p];

					int x = particle.coord.x * Screen::SCREEN_WIDTH / systemSize;
					int y = particle.coord.y * Screen::SCREEN_HEIGHT / systemSize;
					//store.printCoord(x,y);
					screen.setPixel(x, y, int(255 * particle.eta / maxeta), 0, int(255 * abs(maxeta - particle.eta) / maxeta));
					}
				const Predator * const pPredators = swarm.returnPredators();
				for (int p = 0; p < nPred; p++){
					Predator predator = pPredators[p];

					int x = predator.coord.x * Screen::SCREEN_WIDTH / systemSize;
					int y = predator.coord.y * Screen::SCREEN_HEIGHT / systemSize;
					//store.printCoord(x,y);
					screen.setPixel(x, y, 0, 0, 0);
					}
				screen.update();	
				if (i >= iterations - last){
					swarm.cudaBackCopy();
					store.orientationParam += swarm.calcOrderparam();
				}
				if (screen.processEvents() == false){
					break;
				}
				if (i%10000 == 0){
					for (int p = 0; p < particles; p++){
						Particle particle = pParticles[p];
						store.eta[p] = particle.eta;
						store.print(p);
					}
					store.endl();
				}
				if (i%wait == 0) swarm.initAttack();
			}
			screen.close();
			/*if (cudaDeviceSynchronize() != cudaSuccess)
				cout << "Device synchronisation failed \n";
			swarm.cudaUniteIdBackCopy();
			swarm.grouping();
			c = swarm.findgroups();
			cout << "number of independent groups are " << c << "\n";
			gsd = new int[c];
			swarm.calcgsd(gsd);
			for (int i = 0; i < c; i++){
				store.printGroupSize(gsd[i]);
			}
			store.endl();*/	
		}
		/*store.endl();
		store.orientationParam = store.orientationParam / realisations / last;
		//cout << store.orientationParam << "\n";
		store.print(eta);
		store.endl();*/
	}
	time.stop();
	timeElapsed = time.printTime();
	store.printTime(timeElapsed);
	store.fileClose();
	
	//delete []gsd;
	return 0;
}