COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
csc.cpp
Go to the documentation of this file.
1/****************************************************************/
2/* Parallel Combinatorial BLAS Library (for Graph Computations) */
3/* version 1.6 -------------------------------------------------*/
4/* date: 11/15/2016 --------------------------------------------*/
5/* authors: Ariful Azad, Aydin Buluc, Adam Lugowski ------------*/
6/****************************************************************/
7/*
8 Copyright (c) 2010-2016, The Regents of the University of California
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27 */
28
29
30#include "csc.h"
31#include <cassert>
32
33namespace combblas {
34
35// Constructing empty Csc objects (size = 0) are not allowed.
36template <class IT, class NT>
38 jc(NULL), ir(NULL), num(NULL), n(0), nz(0)
39{
40}
41
42
43template <class IT, class NT>
45{
46 assert(size != 0 && n != 0);
47 num = new NT[nz];
48 ir = new IT[nz];
49 jc = new IT[n+1];
50}
51
52template <class IT, class NT>
53Csc<IT,NT>::Csc (const Csc<IT,NT> & rhs): n(rhs.n), nz(rhs.nz)
54{
55 if(nz > 0)
56 {
57 ir = new IT[nz];
58 num = new NT[nz];
59 std::copy(rhs.ir, rhs.ir+nz, ir); // copy(first, last, result)
60 std::copy(rhs.num, rhs.num+nz, num);
61 }
62 jc = new IT[n+1];
63 std::copy(rhs.jc, rhs.jc+n+1, jc);
64}
65
66template <class IT, class NT>
68{
69 if(this != &rhs)
70 {
71 if(nz > 0) // if the existing object is not empty
72 {
73 // make it empty
74 delete [] num;
75 delete [] ir;
76 }
77 delete [] jc;
78
79 nz = rhs.nz;
80 n = rhs.n;
81 if(nz > 0) // if the copied object is not empty
82 {
83 ir = new IT[nz];
84 num = new NT[nz];
85 std::copy(rhs.ir, rhs.ir+nz, ir);
86 std::copy(rhs.num, rhs.num+nz, num);
87 }
88 jc = new IT[n+1];
89 std::copy(rhs.jc, rhs.jc+n+1, jc);
90 }
91 return *this;
92}
93
94template <class IT, class NT>
96{
97 if( nz > 0)
98 {
99 delete [] num;
100 delete [] ir;
101 }
102 delete [] jc;
103}
104
106template <class IT, class NT>
108{
109 if(nsize == nz)
110 {
111 // No need to do anything!
112 return;
113 }
114 else if(nsize == 0)
115 {
116 delete [] num;
117 delete [] ir;
118 nz = 0;
119 return;
120 }
121
122 NT * tmpnum = num;
123 IT * tmpir = ir;
124 num = new NT[nsize];
125 ir = new IT[nsize];
126
127 if(nsize > nz) // Grow it
128 {
129 std::copy(tmpir, tmpir + nz, ir); //copy all old elements
130 std::copy(tmpnum, tmpnum + nz, num);
131 }
132 else // Shrink it
133 {
134 std::copy(tmpir, tmpir + nsize, ir); // copy only a portion of the old elements
135 std::copy(tmpnum, tmpnum + nsize, num);
136 }
137 delete [] tmpnum; // delete the memory pointed by previous pointers
138 delete [] tmpir;
139 nz = nsize;
140}
141
142
143
144template <class IT, class NT>
145template <typename UnaryOperation, typename GlobalIT>
148 bool inPlace,
149 GlobalIT rowOffset,
150 GlobalIT colOffset
151 )
152{
153 IT prunednnz = 0;
154 for (IT i = 0; i < n; ++i)
155 {
156 for (IT j = jc[i]; j < jc[i+1]; ++j)
157 {
158 if (!(unary_op(std::make_tuple(rowOffset + ir[j],
159 colOffset + i, num[j]))))
160 ++prunednnz;
161 }
162 }
163
164 IT *oldjc = jc;
165 IT *oldir = ir;
166 NT *oldnum = num;
167 jc = new IT[n + 1];
168 ir = new IT[prunednnz];
169 num = new NT[prunednnz];
170
171 IT cnnz = 0;
172 jc[0] = 0;
173 for (IT i = 0; i < n; ++i)
174 {
175 for (IT j = oldjc[i]; j < oldjc[i+1]; ++j)
176 {
177 if (!(unary_op(std::make_tuple(rowOffset + oldir[j],
178 colOffset + i, oldnum[j]))))
179 {
180 ir[cnnz] = oldir[j];
181 num[cnnz++] = oldnum[j];
182 }
183 }
184 jc[i+1] = cnnz;
185 }
186
187 assert (cnnz == prunednnz);
188
190 if (inPlace)
191 {
193 nz = cnnz;
194 }
195 else
196 {
197 ret = new Csc<IT, NT>();
198 ret->jc = jc;
199 ret->ir = ir;
200 ret->num = num;
201 ret->n = n;
202 ret->nz = cnnz;
203
204 jc = oldjc;
205 ir = oldir;
206 num = oldnum;
207 }
208
209 return ret;
210}
211
212
213
214template <class IT, class NT>
215void
217 Csc<IT, NT> * &B,
218 IT cut
219 )
220{
221 // left
222 if (jc[cut] == 0)
223 A = NULL;
224 else
225 {
226 A = new Csc<IT, NT>(jc[cut], cut);
227 std::copy(jc, jc + cut + 1, A->jc);
228 std::copy(ir, ir + jc[cut], A->ir);
229 std::copy(num, num + jc[cut], A->num);
230 }
231
232 // right
233 if (nz - jc[cut] == 0)
234 B = NULL;
235 else
236 {
237 B = new Csc<IT, NT>(nz - jc[cut], n - cut);
238 std::copy(jc + cut, jc + n + 1, B->jc);
239 transform(B->jc, B->jc + (n - cut + 1), B->jc,
240 bind2nd(std::minus<IT>(), jc[cut]));
241 std::copy(ir + jc[cut], ir + nz, B->ir);
242 std::copy(num + jc[cut], num + nz, B->num);
243 }
244}
245
246
247
248template <class IT, class NT>
249void
251 const Csc<IT, NT> *B,
252 IT cut
253 )
254{
255 assert (A != NULL && B != NULL);
256
257 IT cnz = A->nz + B->nz;
258 IT cn = A->n + B->n;
259 if (cnz > 0)
260 {
261 *this = Csc<IT, NT>(cnz, cn);
262
263 std::copy(A->jc, A->jc + A->n, jc);
264 std::copy(B->jc, B->jc + B->n + 1, jc + A->n);
265 transform(jc + A->n, jc + cn + 1, jc + A->n,
266 bind2nd(std::plus<IT>(), A->jc[A->n]));
267
268 std::copy(A->ir, A->ir + A->nz, ir);
269 std::copy(B->ir, B->ir + B->nz, ir + A->nz);
270
271 std::copy(A->num, A->num + A->nz, num);
272 std::copy(B->num, B->num + B->nz, num + A->nz);
273 }
274}
275
276
277
278
279}
int64_t IT
double NT
Definition test.cpp:53
void Merge(const Csc< IT, NT > *A, const Csc< IT, NT > *B, IT cut)
Definition csc.cpp:250
IT * jc
Definition csc.h:70
Csc< IT, NT > * PruneI(UnaryOperation unary_op, bool inPlace, GlobalIT rowOffset, GlobalIT colOffset)
Definition csc.cpp:147
Csc< IT, NT > & operator=(const Csc< IT, NT > &rhs)
Definition csc.cpp:67
void Resize(IT nsize)
Does not change the dimension.
Definition csc.cpp:107
void Split(Csc< IT, NT > *&A, Csc< IT, NT > *&B, IT cut)
Definition csc.cpp:216
IT * ir
Definition csc.h:71
NT * num
Definition csc.h:72
int size
Definition common.h:20
void DeleteAll(A arr1)
Definition Deleter.h:48
double A