// build the acceleration structure void Bvh::Build() { // malloc memory mallocMemory(); // build bounding box computeBBox(); // generate bvh primitives for( auto primitive : *m_primitives ) SORT_MALLOC_ID(Bvh_Primitive,BVH_LEAF_PRILIST_MEMID)(primitive); // recursively split node m_root = new Bvh_Node(); splitNode( m_root , 0 , m_primitives->size() , 0 ); }
void sort(){ mallocMemory();// malloc memory for each temporary file MinHeap<MergeSortInfo<DataType> > mh(tempFileNumber); // write the sorted file in file. int fd = open(gfn.sortedFileName().c_str(),O_CREAT|O_RDWR,0777); int sortedIndex = 0;// edge index in buffer int sortedBytes = 0;// sorted bytes int writeDataSize = sizeof(DataType); DataType *sortedArray= (DataType *)malloc(WRITE_BUFFER_NUMBER * writeDataSize); int sourceNumber = tempFileNumber; //the index of each file int *positions = (int *)malloc(sourceNumber * sizeof(int)); for(int i = 0; i < sourceNumber; i ++){ mh.insert(MergeSortInfo<DataType>(i,loadedData[i][0])); positions[i] = 0; } while(sourceNumber > 0 || mh.heapSize() != 0){ MergeSortInfo<DataType> temp = mh.removeMin(); sortedArray[sortedIndex] = temp.element; sortedBytes += writeDataSize; if(++ sortedIndex == WRITE_BUFFER_NUMBER){ writeFile<DataType>(fd,sortedArray,sortedBytes); sortedBytes = 0; sortedIndex = 0; } int minIndex = temp.arrayIndex; -- loadedDataNumber[minIndex]; if(loadedDataNumber[minIndex] == 0){ int leftDataNumber = eachFileDataNumber[minIndex]; if(leftDataNumber > 0){ int loadNumber = (leftDataNumber < loadedBudgetNumber)?leftDataNumber:loadedBudgetNumber; int fd = open(gfn.binaryTempFileName(minIndex).c_str(),O_RDONLY); int offset = eachFileSize[minIndex] - eachFileDataNumber[minIndex] * sizeof(DataType); readFile<DataType>(fd,loadedData[minIndex],loadNumber * sizeof(DataType),offset); close(fd); loadedDataNumber[minIndex] = loadNumber; eachFileDataNumber[minIndex] -= loadNumber; positions[minIndex] = 0; mh.insert(MergeSortInfo<DataType>(minIndex,loadedData[minIndex][positions[minIndex]])); } else{ -- sourceNumber; remove(gfn.binaryTempFileName(minIndex).c_str()); } } else{ mh.insert(MergeSortInfo<DataType>(minIndex,loadedData[minIndex][++ positions[minIndex]])); } } if(sortedBytes > 0){ writeFile<DataType>(fd,sortedArray,sortedBytes); } close(fd); free(sortedArray); mh.release(); free(positions); std::cout << "Finish sort graph." << std::endl; releaseMemory(); }