sparsematrix.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /******************************************************************************
  2. * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the NVIDIA CORPORATION nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. ******************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Code and text by Sean Baxter, NVIDIA Research
  30. * See http://nvlabs.github.io/moderngpu for repository and documentation.
  31. *
  32. ******************************************************************************/
  33. #pragma once
  34. #include "util/static.h"
  35. namespace mgpu {
  36. struct SparseMatrix {
  37. int height, width, nz;
  38. std::vector<int> csr; // height
  39. std::vector<int> cols; // nz
  40. std::vector<double> matrix; // nz
  41. };
  42. bool ReadSparseMatrix(FILE* f, std::auto_ptr<SparseMatrix>* ppMatrix,
  43. std::string& err);
  44. bool ReadSparseMatrix(const char* filename,
  45. std::auto_ptr<SparseMatrix>* ppMatrix, std::string& err);
  46. bool LoadBinaryMatrix(const char* filename,
  47. std::auto_ptr<SparseMatrix>* ppMatrix);
  48. bool StoreBinaryMatrix(const char* filename, const SparseMatrix& matrix);
  49. bool LoadCachedMatrix(const char* filename,
  50. std::auto_ptr<SparseMatrix>* ppMatrix, std::string& err);
  51. // Multiply the matrix by a vector of 1s.
  52. template<typename T>
  53. void SpmvTest(const SparseMatrix& m, T* results) {
  54. memset(results, 0, sizeof(T) * m.height);
  55. for(int row = 0; row < m.height; ++row) {
  56. T product = 0;
  57. int begin = m.csr[row];
  58. int end = (row + 1 < m.height) ? m.csr[row + 1] : m.nz;
  59. for(int i = begin; i < end; ++i)
  60. product += (T)m.matrix[i];
  61. results[row] = product;
  62. }
  63. }
  64. template<typename T>
  65. void CompareVecs(const T* test, const T* ref, int count) {
  66. for(int i = 0; i < count; ++i) {
  67. double x = ref[i];
  68. double y = test[i];
  69. double diff = fabs(x - y);
  70. if(diff > 1.0e-5) {
  71. if(y > 0) {
  72. if(1.01 * x < y || 0.99 * x > y) {
  73. printf("BAD OUTPUT AT COMPONENT %d: %8.5e vs %8.5e\n", i,
  74. x, y);
  75. // exit(0);
  76. return;
  77. }
  78. } else {
  79. if(1.01 * x > y || 0.99 * x < y) {
  80. printf("BAD OUTPUT AT COMPONENT %d: %8.5e vs %8.5e\n", i,
  81. x, y);
  82. // exit(0);
  83. return;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. struct MatrixStats {
  90. int height, width, nz;
  91. // Row density moments:
  92. double mean;
  93. double stddev;
  94. double skewness;
  95. };
  96. MatrixStats ComputeMatrixStats(const SparseMatrix& m);
  97. int64 MulSparseMatrices(const SparseMatrix& A, const SparseMatrix& B,
  98. std::auto_ptr<SparseMatrix>* ppC);
  99. int64 ComputeProductCount(const SparseMatrix& A, const SparseMatrix& B);
  100. void ComputeColRanges(const SparseMatrix& A, const SparseMatrix& B,
  101. int* colMin, int* colMax);
  102. } // namespace mgpu