|
7 | 7 |
|
8 | 8 |
|
9 | 9 | #include "baseHessian.h" |
| 10 | +#include <helper_cuda.h> |
10 | 11 | #include <sys/time.h> |
11 | 12 | #include <sys/sysinfo.h> |
12 | 13 | #include "../gpu_global_utility.h" |
@@ -400,3 +401,67 @@ void BaseHessian::PrintHessianInfo() |
400 | 401 | cout << "part2_end=" << m_nRowEndPos2 << endl; |
401 | 402 | } |
402 | 403 |
|
| 404 | + |
| 405 | +/** |
| 406 | + * @brief: kernel matrix precomputation |
| 407 | + */ |
| 408 | +void BaseHessian::PrecomputeKernelMatrix(vector<vector<float_point> > &v_vDocVector, BaseHessian *hessianIOOps) |
| 409 | +{ |
| 410 | + //compute Hessian Matrix |
| 411 | + string strHessianMatrixFileName = HESSIAN_FILE; |
| 412 | + string strDiagHessianFileName = HESSIAN_DIAG_FILE; |
| 413 | + |
| 414 | + int nNumofSample = v_vDocVector.size(); |
| 415 | + |
| 416 | + //initialize Hessian IO operator |
| 417 | + //CLinearKernel RBF(pfGamma[j]); |
| 418 | + |
| 419 | + int nNumofRowsOfHessianMatrix = v_vDocVector.size(); |
| 420 | + //space of row-index-in-file is for improving reading performace |
| 421 | + BaseHessian::m_nNumofDim = v_vDocVector.front().size(); |
| 422 | + BaseHessian::m_nTotalNumofInstance = nNumofRowsOfHessianMatrix; |
| 423 | + |
| 424 | + StorageManager *manager = StorageManager::getManager(); |
| 425 | + int nNumofHessianRow = manager->RowInRAM(BaseHessian::m_nNumofDim, BaseHessian::m_nTotalNumofInstance, nNumofSample); |
| 426 | + |
| 427 | + cout << nNumofHessianRow << " rows cached in RAM" << endl; |
| 428 | + long long lSizeofCachedHessian = sizeof(float_point) * (long long)nNumofHessianRow * nNumofSample; |
| 429 | + |
| 430 | + |
| 431 | + cout << "numRow " << nNumofHessianRow << "; numIns " << nNumofSample << "; numBytes " << lSizeofCachedHessian << endl; |
| 432 | + if(lSizeofCachedHessian < 0) |
| 433 | + { |
| 434 | + cerr << "locate negative amount of host memory" << endl; |
| 435 | + exit(-1); |
| 436 | + } |
| 437 | + |
| 438 | + checkCudaErrors(cudaMallocHost((void**)&(BaseHessian::m_pfHessianRowsInHostMem), lSizeofCachedHessian)); |
| 439 | + |
| 440 | + memset(BaseHessian::m_pfHessianRowsInHostMem, 0, lSizeofCachedHessian); |
| 441 | + BaseHessian::m_nNumofCachedHessianRow = nNumofHessianRow; |
| 442 | + BaseHessian::m_pfHessianDiag = new float_point[hessianIOOps->m_nTotalNumofInstance]; |
| 443 | + //hessianIOOps->m_pfHessianDiagTest = new float_point[hessianIOOps->m_nTotalNumofInstance]; |
| 444 | + |
| 445 | + //pre-compute Hessian Matrix and store the result into a file |
| 446 | + cout << "precomputing kernel matrix..."; |
| 447 | + cout.flush(); |
| 448 | + |
| 449 | + timeval t1, t2; |
| 450 | + float_point elapsedTime; |
| 451 | + gettimeofday(&t1, NULL); |
| 452 | + bool bWriteHessian = hessianIOOps->PrecomputeHessian(strHessianMatrixFileName, strDiagHessianFileName, v_vDocVector); |
| 453 | + hessianIOOps->ReadDiagFromHessianMatrix(); |
| 454 | + |
| 455 | + gettimeofday(&t2, NULL); |
| 456 | + elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; |
| 457 | + elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; |
| 458 | + //cout << "Done" << elapsedTime << " ms.\n"; |
| 459 | + cout << " Done" << endl; |
| 460 | + |
| 461 | + if(bWriteHessian == false) |
| 462 | + { |
| 463 | + cerr << "write matrix to file failed" << endl; |
| 464 | + exit(0); |
| 465 | + } |
| 466 | +} |
| 467 | + |
0 commit comments