Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. //! Calculates one or more than one coVarianceMatrix given data.
  2. // There can be many classes since many covariance matrixes.
  3. /*!
  4. param inMatrix This vector contains matrix data in major storage.
  5. Forexample if inMatrix=[1 2 3 4 5 6] and trialSizes=[2] this means matrix we will work on a matrix like :
  6. |1 4 |
  7. |2 5 |
  8. |3 6 | -> 2 Trials, 3 Features. Columns contains feature rows contains trials (samples)
  9. param trialSizes There can be many classes since many covariance matrixes. Samples from all classes will be given with inMatrix.
  10. But we need to know how many trials(samples) we have for each class.
  11. For example if inMatrix=[1 2 3 4 5 6 7 8 9 10 11 12] and trialSizes=[2,2]
  12. this means matrix we will work on a matrix like :
  13. |1 4 | |7 10 |
  14. |2 5 | |8 11 |
  15. |3 6 | |9 12 | --> Total number of trials(samples which is total rowCount) 2 + 2 = 4 ,
  16. So colSize = inMatrix.size()/4 = 3(feature vector size)
  17. --> There is two element in trialSize vec so each vector has to samples
  18. */
  19. void multiQDACovianceCalculator(std::vector<float>& inMatrix, std::vector<int>& trialSizes)
  20. {
  21. cublasHandle_t handle; // CUBLAS context
  22. int classCount = trialSizes.size();
  23. int rowSize = std::accumulate(trialSizes.begin(), trialSizes.end(), 0);
  24. int dimensionSize = inMatrix.size() / rowSize;
  25. float alpha = 1.0f;
  26. float beta = 0.0f; // bet =1
  27.  
  28. thrust::device_vector<float> d_cov1(dimensionSize * dimensionSize);
  29. thrust::device_vector<float> d_cov2(dimensionSize * dimensionSize);
  30. thrust::device_vector<float> d_covResult(dimensionSize * dimensionSize);
  31.  
  32. thrust::device_vector<float> d_wholeMatrix(inMatrix);
  33. thrust::device_vector<float> d_meansVec(dimensionSize); // rowVec of means of trials
  34. float *meanVecPtr = thrust::raw_pointer_cast(d_meansVec.data());
  35. float *device2DMatrixPtr = thrust::raw_pointer_cast(d_wholeMatrix.data());
  36. auto maxTrialNumber = *std::max_element(trialSizes.begin(), trialSizes.end());
  37. thrust::device_vector<float> deviceVector(maxTrialNumber, 1.0f);
  38.  
  39. cublasCreate(&handle);
  40. // Inside of for loop one covariance matrix calculated each time
  41. for (int i = 0; i < trialSizes.size(); i++)
  42. {
  43. // X*transpose(X) / N
  44. alpha = 1.0f / trialSizes[i];
  45. cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_T, dimensionSize, dimensionSize, trialSizes[i], &alpha,
  46. device2DMatrixPtr, dimensionSize, device2DMatrixPtr, dimensionSize, &beta,
  47. thrust::raw_pointer_cast(d_cov1.data()), dimensionSize);
  48.  
  49. // Mean vector of each column
  50. alpha = 1.0f;
  51. cublasSgemv(handle, CUBLAS_OP_N, dimensionSize, trialSizes[i], &alpha, device2DMatrixPtr,
  52. dimensionSize, thrust::raw_pointer_cast(deviceVector.data()), 1, &beta, meanVecPtr, 1);
  53.  
  54. // MeanVec * transpose(MeanVec) / N*N
  55. alpha = 1.0f / (trialSizes[i] * trialSizes[i]);
  56. cublasSgemm(handle, CUBLAS_OP_T, CUBLAS_OP_N, dimensionSize, dimensionSize, 1, &alpha,
  57. meanVecPtr, 1, meanVecPtr, 1, &beta,
  58. thrust::raw_pointer_cast(d_cov2.data()), dimensionSize);
  59.  
  60. alpha = 1.0f;
  61. beta = -1.0f;
  62. // (X*transpose(X) / N) - (MeanVec * transpose(MeanVec) / N*N)
  63. cublasSgeam(handle, CUBLAS_OP_N, CUBLAS_OP_N, dimensionSize, dimensionSize, &alpha,
  64. thrust::raw_pointer_cast(d_cov1.data()), dimensionSize, &beta, thrust::raw_pointer_cast(d_cov2.data()),
  65. dimensionSize, thrust::raw_pointer_cast(d_covResult.data()), dimensionSize);
  66.  
  67. // Go to other class and calculate its covarianceMatrix
  68. device2DMatrixPtr += trialSizes[i] * dimensionSize;
  69. }
  70. printVector(d_covResult);
  71. cublasDestroy(handle);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement