NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparsematrix.h
Go to the documentation of this file.
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  *
30  * Code and text by Sean Baxter, NVIDIA Research
31  * See http://nvlabs.github.io/moderngpu for repository and documentation.
32  *
33  ******************************************************************************/
34 
35 #pragma once
36 
37 #include "util/static.h"
38 
39 namespace mgpu {
40 
41 struct SparseMatrix {
42  int height, width, nz;
43  std::vector<int> csr; // height
44  std::vector<int> cols; // nz
45  std::vector<double> matrix; // nz
46 };
47 
48 bool ReadSparseMatrix(FILE* f, std::auto_ptr<SparseMatrix>* ppMatrix,
49  std::string& err);
50 
51 bool ReadSparseMatrix(const char* filename,
52  std::auto_ptr<SparseMatrix>* ppMatrix, std::string& err);
53 
54 bool LoadBinaryMatrix(const char* filename,
55  std::auto_ptr<SparseMatrix>* ppMatrix);
56 
57 bool StoreBinaryMatrix(const char* filename, const SparseMatrix& matrix);
58 
59 bool LoadCachedMatrix(const char* filename,
60  std::auto_ptr<SparseMatrix>* ppMatrix, std::string& err);
61 
62 // Multiply the matrix by a vector of 1s.
63 template<typename T>
64 void SpmvTest(const SparseMatrix& m, T* results) {
65  memset(results, 0, sizeof(T) * m.height);
66  for(int row = 0; row < m.height; ++row) {
67  T product = 0;
68  int begin = m.csr[row];
69  int end = (row + 1 < m.height) ? m.csr[row + 1] : m.nz;
70  for(int i = begin; i < end; ++i)
71  product += (T)m.matrix[i];
72 
73  results[row] = product;
74  }
75 }
76 
77 template<typename T>
78 void CompareVecs(const T* test, const T* ref, int count) {
79  for(int i = 0; i < count; ++i) {
80  double x = ref[i];
81  double y = test[i];
82  double diff = fabs(x - y);
83 
84  if(diff > 1.0e-5) {
85  if(y > 0) {
86  if(1.01 * x < y || 0.99 * x > y) {
87  printf("BAD OUTPUT AT COMPONENT %d: %8.5e vs %8.5e\n", i,
88  x, y);
89  // exit(0);
90  return;
91  }
92  } else {
93  if(1.01 * x > y || 0.99 * x < y) {
94  printf("BAD OUTPUT AT COMPONENT %d: %8.5e vs %8.5e\n", i,
95  x, y);
96  // exit(0);
97  return;
98  }
99  }
100  }
101  }
102 }
103 
104 struct MatrixStats {
105  int height, width, nz;
106 
107  // Row density moments:
108  double mean;
109  double stddev;
110  double skewness;
111 };
112 
114 
116  std::auto_ptr<SparseMatrix>* ppC);
117 
118 
120 
121 void ComputeColRanges(const SparseMatrix& A, const SparseMatrix& B,
122  int* colMin, int* colMax);
123 
124 } // namespace mgpu