-
Notifications
You must be signed in to change notification settings - Fork 1
/
spa.cpp
85 lines (76 loc) · 1.58 KB
/
spa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "spa.h"
#include "utility.h"
#include <cassert>
// copy constructor
template <class T, class ITYPE>
Spa<T, ITYPE>::Spa (const Spa<T,ITYPE> & rhs)
: length(rhs.length)
{
if(length > 0)
{
indices = rhs.indices;
bitmap = new bool[length];
values = new T[length];
for(ITYPE i=0; i< length; ++i)
bitmap[i]= rhs.bitmap[i];
for(ITYPE i=0; i< length; ++i)
values[i]= rhs.values[i];
}
}
template <class T, class ITYPE>
Spa<T, ITYPE> & Spa<T, ITYPE>::operator= (const Spa<T, ITYPE> & rhs)
{
if(this != &rhs)
{
if(length > 0) // if the existing object is not empty, make it empty
{
delete [] bitmap;
delete [] values;
}
length = rhs.length;
if(length > 0)
{
indices = rhs.indices;
bitmap = new bool[length];
values = new T[length];
for(ITYPE i=0; i< length; ++i)
bitmap[i]= rhs.bitmap[i];
for(ITYPE i=0; i< length; ++i)
values[i]= rhs.values[i];
}
}
return *this;
}
template <class T, class ITYPE>
Spa<T, ITYPE>::~Spa()
{
if( length > 0)
{
delete [] bitmap;
delete [] bitmap;
}
}
template <class T, class ITYPE>
Spa<T, ITYPE>::Scatter(T value, ITYPE pos)
{
if(bitmap[pos])
values[pos] += value;
else
{
bitmap[pos] = true;
values[pos] = value;
indices.push_back(pos);
}
}
template <class T, class ITYPE>
Spa<T, ITYPE>::Gather(T * y)
{
ITYPE nnz = indices.size();
for(ITYPE =0; i<nnz; ++i)
{
y[indices[i]] += values[indices[i]];
values[indices[i]] = 0;
bitmap[indices[i]] = false;
}
indices.clear(); // free vector, and set size() to 0
}