File: rsb_csr2coo.c

package info (click to toggle)
librsb 1.2.0.9%2Breal%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 33,844 kB
  • sloc: ansic: 426,131; f90: 84,225; sh: 5,806; makefile: 698; objc: 686; awk: 18; sed: 1
file content (240 lines) | stat: -rw-r--r-- 6,788 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*                                                                                                                            

Copyright (C) 2008-2015 Michele Martone

This file is part of librsb.

librsb is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

librsb is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public
License along with librsb; see the file COPYING.
If not, see <http://www.gnu.org/licenses/>.

*/
/* @cond INNERDOC  */
 /**
 * @file
 * @brief CSR to COO conversion code
 * @author Michele Martone
 * */
#include "rsb_common.h"

void rsb__do_prefix_sum_coo_idx_t(rsb_nnz_idx_t *IA, rsb_nnz_idx_t nnz)
{
	/* FIXME: shall optimize */
	rsb_nnz_idx_t i;
	for(i=1;RSB_LIKELY(i<nnz);++i)
		IA[i] += IA[i-1];
}

rsb_err_t rsb__do_switch_fullword_array_to_compressed(rsb_nnz_idx_t *IA, rsb_nnz_idx_t nnz, rsb_nnz_idx_t m)
{
		/**
 			FIXME: no test case
			see rsb__do_switch_compressed_array_to_fullword_coo
 			FIXME: need a no-calloc version
	 		TODO: rsb__do_switch_fullword_array_to_compressed -> rsb__idx_fia2fpa
  		*/
		rsb_err_t errval = RSB_ERR_NO_ERROR;
		rsb_nnz_idx_t i;
		rsb_coo_idx_t * IP = NULL;
		IP = rsb__calloc(sizeof(rsb_coo_idx_t)*(m+1));
		if(!IP)
		{
			errval = RSB_ERR_ENOMEM;
			RSB_PERR_GOTO(err,RSB_ERRM_ES);
		}
#if 0
		for(i=0;RSB_LIKELY(i<nnz);++i)
			if(IA[i]>=m || IA[i]<0)
			{
				errval = RSB_ERR_BADARGS;
				RSB_PERR_GOTO(err,"0 <= IA[%d]=%d < m=%d  ?\n",i,IA[i],m);
			}
#endif
		for(i=0;RSB_LIKELY(i<nnz);++i)
			IP[IA[i]+1]++;
		for(i=0;RSB_LIKELY(i<m);++i)
			IP[i+1] += IP[i];
		RSB_COA_MEMCPY(IA,IP,0,0,m+1);
err:
		RSB_CONDITIONAL_FREE(IP);
	RSB_DO_ERR_RETURN(errval)
}

rsb_err_t rsb__do_switch_compressed_array_to_fullword_coo(rsb_nnz_idx_t *RSB_RESTRICT IP, rsb_nnz_idx_t m, rsb_coo_idx_t off, rsb_coo_idx_t *RSB_RESTRICT TA)
{
		/**
 			FIXME: no test case
	 		Requires m+1 temporary space.
			see rsb__do_switch_fullword_array_to_compressed
	 		TODO: rsb__do_switch_compressed_array_to_fullword_coo -> rsb__idx_fpa2fia
  		*/
		rsb_err_t errval = RSB_ERR_NO_ERROR;
		rsb_nnz_idx_t /*k,*/li,ri;
		//rsb_nnz_idx_t nnz = IP[m+1];
		rsb_coo_idx_t i;
		rsb_coo_idx_t * RSB_RESTRICT IA = TA;

		if(!IA)
			IA = rsb__malloc(sizeof(rsb_coo_idx_t)*(m+1));
		if(!IA)
		{
			errval = RSB_ERR_ENOMEM;
			RSB_PERR_GOTO(err,RSB_ERRM_ES);
		}
		RSB_COA_MEMCPY(IA,IP,0,0,m+1);
		for(i=0;RSB_LIKELY(i<m);++i)
		{
			ri = IA[i+1];
			li = IA[i];
			rsb__util_coo_array_set(IP+li,ri-li,i+off);
		}
err:
		if(IA!=TA)
			RSB_CONDITIONAL_FREE(IA);
	RSB_DO_ERR_RETURN(errval)
}

rsb_err_t rsb_do_switch_in_place_csr_to_in_place_coo(struct rsb_mtx_t * mtxAp, rsb_bool_t do_shift)
{
	/**
		\ingroup gr_internals
	 */
	rsb_err_t errval = RSB_ERR_NO_ERROR;
	rsb_nnz_idx_t li,ri;
	rsb_coo_idx_t i;
	// IA needs expansion
	rsb_coo_idx_t * IA = NULL;

	if( mtxAp->flags & RSB_FLAG_USE_HALFWORD_INDICES)
	{
		rsb__do_switch_array_to_fullword_coo((rsb_half_idx_t*)(mtxAp->bindx),mtxAp->nnz,0);
	}
	else
	{
	}

	if(rsb__is_coo_matrix(mtxAp))
	{
		// FIXME: TODO (nothing todo)
		goto err;
	}
	IA = rsb__malloc(sizeof(rsb_coo_idx_t)*(mtxAp->Mdim+1));
	if(!IA)
	{
		RSB_PERR_GOTO(err,RSB_ERRM_ES);
		errval = RSB_ERR_ENOMEM;
	}
	RSB_COA_MEMCPY(IA,mtxAp->bpntr,0,0,mtxAp->Mdim+1);
	for(i=0;RSB_LIKELY(i<mtxAp->Mdim);++i)
	{
		ri = IA[i+1];
		li = IA[i];
		rsb__util_coo_array_set(mtxAp->bpntr+li,ri-li,i);
	}
	if(do_shift)
	{
		// JA needs displacement of mtxAp->coff
		rsb__util_coo_array_add(mtxAp->bindx,mtxAp->nnz,mtxAp->coff);
		// IA needs displacement of mtxAp->coff
		rsb__util_coo_array_add(mtxAp->bpntr,mtxAp->nnz,mtxAp->roff);
	}
	// VA is opaque to us: no processing is needed
	RSB_CONDITIONAL_FREE(IA);
err:
	RSB_DO_ERR_RETURN(errval)
}

rsb_nnz_idx_t rsb_do_count_lowtri_in_csr(const struct rsb_coo_matrix_t *csrp)
{
	register rsb_coo_idx_t i;
	register rsb_nnz_idx_t lnz = 0;
	const rsb_coo_idx_t *IA = csrp->IA;
	const rsb_coo_idx_t *JA = csrp->JA;
	for(i=0;i<csrp->nr;++i)
	{
		register rsb_nnz_idx_t nnz0 = IA[i+0];
		register rsb_nnz_idx_t nnz1 = IA[i+1];
		lnz += rsb__nnz_split_coo_bsearch(JA+nnz0,i+1,nnz1-nnz0);
	}
	return lnz;
}

rsb_nnz_idx_t rsb__do_count_upptri_in_csr(const struct rsb_coo_matrix_t *csrp)
{
	register rsb_coo_idx_t i;
	register rsb_nnz_idx_t unz = 0;
	const rsb_coo_idx_t *IA = csrp->IA;
	const rsb_coo_idx_t *JA = csrp->JA;
	for(i=0;i<csrp->nr;++i)
	{
		register rsb_nnz_idx_t nnz0 = IA[i+0];
		register rsb_nnz_idx_t nnz1 = IA[i+1];
		unz += nnz1-nnz0-rsb__nnz_split_coo_bsearch(JA+nnz0,i,nnz1-nnz0);
	}
	return unz;
}

rsb_nnz_idx_t rsb__do_copy_lowtri_from_csr_to_coo(const struct rsb_coo_matrix_t *csrp, struct rsb_coo_matrix_t *coop)
{
	register rsb_coo_idx_t i;
	register rsb_nnz_idx_t lnz = 0;
	const rsb_coo_idx_t *IA = csrp->IA;
	const rsb_coo_idx_t *JA = csrp->JA;
	const rsb_coo_idx_t *VA = csrp->VA;
	size_t el_size = RSB_SIZEOF(csrp->typecode);
	for(i=0;i<csrp->nr;++i)
	{
		register rsb_nnz_idx_t nnz0 = IA[i+0];
		register rsb_nnz_idx_t nnz1 = IA[i+1];
		nnz1 = nnz0+rsb__nnz_split_coo_bsearch(JA+nnz0,i+1,nnz1-nnz0);
		RSB_CSR2COO_MEMCPY(coop->VA,coop->IA,coop->JA,VA,i,JA,lnz,nnz0,nnz1-nnz0,el_size);
		lnz += nnz1-nnz0;
	}
	return lnz;
}

rsb_nnz_idx_t rsb__do_copy_upptri_from_csr_to_coo(const struct rsb_coo_matrix_t *csrp, struct rsb_coo_matrix_t *coop)
{
	register rsb_coo_idx_t i;
	register rsb_nnz_idx_t unz = 0;
	const rsb_coo_idx_t *IA = csrp->IA;
	const rsb_coo_idx_t *JA = csrp->JA;
	const rsb_coo_idx_t *VA = csrp->VA;
	size_t el_size = RSB_SIZEOF(csrp->typecode);
	for(i=0;i<csrp->nr;++i)
	{
		register rsb_nnz_idx_t nnz0 = IA[i+0];
		register rsb_nnz_idx_t nnz1 = IA[i+1];
		nnz0 = nnz0+rsb__nnz_split_coo_bsearch(JA+nnz0,i,nnz1-nnz0);
		RSB_CSR2COO_MEMCPY(coop->VA,coop->IA,coop->JA,VA,i,JA,unz,nnz0,nnz1-nnz0,el_size);
		unz += nnz1-nnz0;
	}
	return unz;
}

rsb_nnz_idx_t rsb__do_count_tri_in_csr(const struct rsb_coo_matrix_t *csrp, rsb_nnz_idx_t *lnzp, rsb_nnz_idx_t *unzp)
{
	/* FIXME: should optimize */
	if(lnzp)
		*lnzp = rsb_do_count_lowtri_in_csr(csrp);
	if(unzp)
		*unzp = rsb__do_count_upptri_in_csr(csrp);
	return (lnzp?*lnzp:0)+(unzp?*unzp:0);
}
rsb_nnz_idx_t rsb__do_copy_tri_from_csr_to_coo(const struct rsb_coo_matrix_t *csrp, struct rsb_coo_matrix_t *lcoop, struct rsb_coo_matrix_t *ucoop)
{
	/* FIXME: should optimize */
	return rsb__do_copy_lowtri_from_csr_to_coo(csrp,lcoop)+rsb__do_copy_upptri_from_csr_to_coo(csrp,ucoop);
}

/* @endcond */