Project4-CUDA-ICP icon indicating copy to clipboard operation
Project4-CUDA-ICP copied to clipboard

ubuntu

Open yangninghua opened this issue 3 years ago • 0 comments

/**
* @file      main.cpp
* @brief     Example Points flocking simulation for CIS 565
* @authors   Liam Boone, Kai Ninomiya, Kangning (Gary) Li
* @date      2013-2017
* @copyright University of Pennsylvania
*/

#include "main.hpp"
#include <time.h>  

// ================
// Configuration
// ================

//toggles 
#define GPUNAIVE 1
#define GPUKDTREE 0

#define dims 3

// LOOK-1.2 - change this to adjust particle count in the simulation
int N_FOR_VIS = 5000; // number of points // reset after reading file
float DT = 0.2f;

std::vector<glm::vec3> Ybuffer;
std::vector<glm::vec3> Xbuffer;

#if GPUKDTREE
glm::vec4 *YbufferTree;
glm::ivec3 *track;
int treesize;
int tracksize;
# endif


// Data read function 
void read_data(std::vector<glm::vec3> &buffer, std::string filename, float offset, bool flag) {

	std::ifstream filein(filename);
	int count = 0;
	int vertices_cnt;
	std::vector<std::string> header_info;

	// ply files have vertex count on line 21
	for (std::string line; std::getline(filein, line); )
	{	// get number of vertices
		if (count == 17) {
			std::istringstream iss(line);
			for (std::string s; iss >> s; )
				header_info.push_back(s);
			vertices_cnt = std::stoi(header_info[2]);
			break;
		}
		count++;
	}
	std::cout << "vertex count :" << vertices_cnt << std::endl;
	filein.clear();
	filein.seekg(0, std::ios::beg);
	count = 0;

	//vertices are stored from line 24 onwards until vcount
	for (std::string line; std::getline(filein, line); )
	{  //24
		if (count >24 + vertices_cnt) break;

		if (count >= 24) {
			std::istringstream is(line);

			float x = 0.0f, y = 0.0f, z = 0.0f;
			is >> x >> y >> z;

			if (flag) {
				//glm::vec3 pt(x+10 , y+5, z+5 );
				//glm::vec3 moved_point = pt;//* glm::mat3( 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
				glm::vec3 tmp(x + offset, y + offset, z + offset);
				//std::cout<<x<<" "<<y<<" "<<z<<std::endl;
				buffer.push_back(tmp);
				//buffer.push_back(moved_point);
			}
			else {

				glm::vec3 tmp(x, y, z);
				buffer.push_back(tmp);
			}


			//int idx = (count - 24)*dims;
			//is >> buffer[idx] >> buffer[idx + 1] >> buffer[idx + 2];
			//cout << buffer[idx] << " " << buffer[idx + 1] << " " << buffer[idx + 2] << endl;
		}
		count++;
	}
	std::cout << "data load completed :" << (count - 24 - 1) << std::endl;
	return;
}

/*
*C main function.
*/
int main(int argc, char* argv[]) {
	const char *projectName;
	projectName = "Project4 PICP";

	// Load data into cpu buffers
	printf("** Read Point Cloud Data **\n");

	std::cout << "Data File Y(target): " << argv[1] << std::endl;
	read_data(Ybuffer, argv[1], 0.0f, false);

	std::cout << "Data File X(source): " << argv[2] << std::endl;
	read_data(Xbuffer, argv[2], 0.0f, false);

	// Initialize drawing state
	N_FOR_VIS = Ybuffer.size() + Xbuffer.size();

	std::cout << Ybuffer[0].x << " " << Ybuffer[0].y << " " << Ybuffer[0].z << std::endl;
	std::cout << Xbuffer[0].x << " " << Xbuffer[0].y << " " << Xbuffer[0].z << std::endl;
	std::cout << "total points = " << N_FOR_VIS << std::endl;

#if GPUKDTREE
	std::cout << "Building KDsearch tree for Y" << std::endl;

	//std::vector<glm::vec3> ybuff ={ // test data
	//						glm::vec3(1,7,5),
	//						glm::vec3(2,6,6),
	//						glm::vec3(3,5,7),
	//						glm::vec3(4,4,1),
	//						glm::vec3(5,3,2),
	//						glm::vec3(6,2,3),
	//						glm::vec3(7,1,4)
	//						};

	int size = KDTree::nextPowerOf2(2 * (Ybuffer.size() + 1)); // to store nulls for leaf nodes
	std::vector<glm::vec4> YbuffTree(size, glm::vec4(0.0f));

	// Init mystack
	int sz = (int)log2(size);
	int X = (int)Xbuffer.size();
	std::vector<glm::ivec3> tk(X*sz, glm::ivec3(0, 0, 0));
	track = &tk[0];

	KDTree::initCpuKDTree(Ybuffer, YbuffTree);
	YbufferTree = &YbuffTree[0];

	tracksize = X * sz;
	treesize = size;

# endif

	if (init(argc, argv)) {
		mainLoop();
		Points::endSimulation();
		return 0;
	}
	else {
		return 1;
	}


	return 0;
}

//-------------------------------
//---------RUNTIME STUFF---------
//-------------------------------

std::string deviceName;

/**
* Initialization of CUDA and GLFW.
*/
bool init(int argc, char **argv) {

	// Set window title to "Student Name: [SM 2.0] GPU Name"

	cudaDeviceProp deviceProp;
	int gpuDevice = 1;
	int device_count = 0;

	cudaGetDeviceCount(&device_count);

	if (gpuDevice > device_count) {
		std::cout
			<< "Error: GPU device number is greater than the number of devices!"
			<< " Perhaps a CUDA-capable GPU is not installed?"
			<< std::endl;
		return false;
	}
	cudaGetDeviceProperties(&deviceProp, gpuDevice);
	//int major = deviceProp.major;
	//int minor = deviceProp.minor;

	//std::ostringstream ss;
	//ss << projectName << " [SM " << major << "." << minor << " " << deviceProp.name << "]";
	//deviceName = ss.str();

	Points::initCpuICP(Ybuffer, Xbuffer);
#if GPUKDTREE
	Points::initGPUKD(Ybuffer, Xbuffer, YbufferTree, track, treesize, tracksize);
#elif GPUNAIVE
	Points::initGPU(Ybuffer, Xbuffer);
#endif
	return true;
}

//====================================
// Main loop
//====================================

void runCUDA() {

#if GPUKDTREE
	Points::stepSimulationGPUKD(Ybuffer, Xbuffer, treesize, tracksize, DT);
#elif GPUNAIVE
	Points::stepSimulationGPUNaive(Ybuffer, Xbuffer, DT);
#else
	Points::stepSimulationICPNaive(Ybuffer, Xbuffer, DT);
#endif

}

void mainLoop() {
	Points::unitTest(); // LOOK-1.2 We run some basic example code to make sure
						// your CUDA development setup is ready to go.

	double start, end, cost;
	start = clock();
	for (size_t i = 0; i < 50; i++)
	{
		runCUDA();
	}
	end = clock();
	cost = end - start;
	std::cout << "s:" << cost/ CLOCKS_PER_SEC << std::endl;
}

yangninghua avatar Sep 15 '20 08:09 yangninghua