 
 
 //knn code below{{{{{{{{{{{{{{{{{{{{{{{{
-int* digits=NULL;
-int* digitsDevice=NULL;
+
 float* trainDataHost=NULL;
 float* trainDataKernel=NULL;
 float* distantHost=NULL;
 float* distantKernel=NULL;
 
+int* digits=NULL;
+int* digitsDevice=NULL;
+float* EigenVectors;
 float* trainDataPCAHost=NULL;
 float* trainDataPCAKernel=NULL;
 float* distantPCAHost=NULL;
 float* distantPCAKernel=NULL;
 
+void freePCAKNN() {
+	free(digits);
+	cudaFree(digitsDevice);
+	cudaFree(EigenVectors);
+	free(trainDataPCAHost);
+	cudaFree(trainDataPCAKernel);
+	free(distantPCAHost);
+	cudaFree(distantPCAKernel);
+}
 //init the answer to the training data
 //return by pointer
 //this is because the training data only holds the data

	cudaMemcpy(distantPCAHost, distantPCAKernel, distantPCASize, cudaMemcpyDeviceToHost);
	cuCheck(__LINE__);
	for(int j=0; j<count; ++j) {
		float* curDistantHost=distantPCAKernel+j*TrainDataNum;
		mergeSortPCA(curDistantHost, TrainDataNum);
	}
}


		}
	}
	free(buffer);
	cudaMemcpy(EigenVectors, EigenVectorsHost, DataLen*Reduced_Data_Length*sizeof(float), cudaMemcpyHostToDevice);
	return;
}


void initTrainDataPCAHost(float* container) {
	FILE* ptr=fopen("explore/trainingData.csv", "r");
	char* buffer=(char*)malloc(sizeof(char));
	for(int i=0; i<TrainDataNum; i++) { 
		for(int j=0; j<Reduced_Data_Length; j++) {
			fscanf(ptr, "%f", &container[i*Reduced_Data_Length+j]);
			fscanf(ptr, "%c", buffer);
		}
	}
	free(buffer);
	return;
}

void initPCAKNN() {

	digits=(int*)malloc(TrainDataNum*sizeof(int));
	initDigits(digits);
	cudaMalloc((void **) &digitsDevice, TrainDataNum*sizeof(int));
	cudaMemcpy(digitsDevice, digits, TrainDataNum*sizeof(int), cudaMemcpyHostToDevice);
	cuCheck(__LINE__);
	int distantPCASize=sizeof(float)*TrainDataNum*15;
	distantPCAHost=(float*)malloc(distantPCASize);
	cudaMalloc((void**) &distantPCAKernel, distantPCASize);
	cuCheck(__LINE__);

	int trainDataPCASize=sizeof(float)*Reduced_Data_Length*TrainDataNum;
	trainDataPCAHost=(float*)malloc(trainDataPCASize);
	initTrainDataPCAHost(trainDataPCAHost);
	cudaMalloc((void**) &trainDataPCAKernel, trainDataPCASize);
	cuCheck(__LINE__);
	cudaMemcpy(trainDataPCAKernel, trainDataPCAHost, trainDataPCASize, cudaMemcpyHostToDevice);
	cuCheck(__LINE__);

	initEigenvectors();
}


