SparseProduct.c 15.2 KB
Newer Older
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/// #include "InputOutput.h"
#include "ScalarVectors.h"
#include "hb_io.h"
#include "SparseProduct.h"

/*********************************************************************************/

// This routine creates a sparseMatrix from the next parameters
// * numR defines the number of rows
// * numC defines the number of columns
// * numE defines the number of nonzero elements
// * msr indicates if the MSR is the format used to the sparse matrix
// If msr is actived, numE doesn't include the diagonal elements
// The parameter index indicates if 0-indexing or 1-indexing is used.
void CreateSparseMatrix (ptr_SparseMatrix p_spr, int index, int numR, int numC, int numE, int msr) {
//	printf (" index = %d , numR = %d , numC = %d , numE = %d\n", index, numR, numC, numE);
	// The scalar components of the structure are initiated
	p_spr->dim1 = numR; p_spr->dim2 = numC; 
	// Only one malloc is made for the vectors of indices
	CreateInts (&(p_spr->vptr), numE+numR+1);
	// The first component of the vectors depends on the used format
	*(p_spr->vptr) = ((msr)? (numR+1): 0) + index;
	p_spr->vpos = p_spr->vptr + ((msr)? 0: (numR+1));
	// The number of nonzero elements depends on the format used
	CreateDoubles (&(p_spr->vval), numE+(numR+1)*msr);
}

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

// This routine creates the first part of a sparseMatrix from the next parameters
// * numR defines the number of rows
// * numC defines the number of columns
// * msr indicates if the MSR is the format used to the sparse matrix
// If msr is actived, numE doesn't include the diagonal elements
// The parameter index indicates if 0-indexing or 1-indexing is used.
void CreateSparseMatrixVptr (ptr_SparseMatrix spr, int numR, int numC, 
												int msr)
	{
		spr->dim1 = numR; spr->dim2 = numC; 
		CreateInts (&(spr->vptr), numR+1);
		*(spr->vptr) = ((msr)? (numR+1): 0);
	}

// This routine creates the second part of a sparseMatrix from the next parameters
// * numR defines the number of rows
// * numC defines the number of columns
// * numE defines the number of nonzero elements
// * msr indicates if the MSR is the format used to the sparse matrix
// If msr is actived, numE doesn't include the diagonal elements
// The parameter index indicates if 0-indexing or 1-indexing is used.
void CreateSparseMatrixValues (ptr_SparseMatrix spr, int numR, int numC, int numE, 
												int msr)
	{
		CreateInts (&(spr->vpos), numE+(numR+1)*msr);
		CreateDoubles (&(spr->vval), numE+(numR+1)*msr);
	}

61
62
63
64
65
66
67
68
// This routine liberates the memory related to matrix spr
void RemoveSparseMatrix (ptr_SparseMatrix spr) {
	// First the scalar are initiated
	spr->dim1 = -1; spr->dim2 = -1; 
	// The vectors are liberated
	RemoveInts (&(spr->vptr)); RemoveDoubles (&(spr->vval)); 
}

69
70
71
72
73
74
75
76
77
78
// This routine liberates the memory related to matrix spr when
// vptr and vpos have been allocated separetely
void RemoveSparseMatrix2 (ptr_SparseMatrix spr)
	{
		spr->dim1 = -1; spr->dim2 = -1; 
		RemoveInts (&(spr->vptr));
		RemoveInts (&(spr->vpos));
		RemoveDoubles (&(spr->vval)); 
	}

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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*********************************************************************************/

// This routine creates de sparse matrix dst from the symmetric matrix spr.
// The parameters indexS and indexD indicate, respectivaly, if 0-indexing or 1-indexing is used
// to store the sparse matrices.
void DesymmetrizeSparseMatrices (SparseMatrix src, int indexS, ptr_SparseMatrix dst, int indexD) {
	int n = src.dim1, nnz = 0;
	int *sizes = NULL;
	int *pp1 = NULL, *pp2 = NULL, *pp3 = NULL, *pp4 = NULL, *pp5 = NULL;
	int i, j, dim, indexDS = indexD - indexS;
	double *pd3 = NULL, *pd4 = NULL;

	// The vector sizes is created and initiated
	CreateInts (&sizes, n); InitInts (sizes, n, 0, 0);
	// This loop counts the number of elements in each row
	pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS;
	pp2 = pp1 + 1 ; pp4 = sizes - indexS;
	for (i=indexS; i<(n+indexS); i++) {
		// The size of the corresponding row is accumulated
		dim = (*pp2 - *pp1); pp4[i] += dim;
		// Now each component of the row is analyzed
		for (j=0; j<dim; j++) {
			// The nondiagonals elements define another element in the graph
			if (*pp3 != i) pp4[*pp3]++;
			pp3++;
		}
		pp1 = pp2++; 
	}
	
	// Compute the number of nonzeros of the new sparse matrix
	nnz = AddInts (sizes, n);
	// Create the new sparse matrix
	CreateSparseMatrix (dst, indexD, n, n, nnz, 0);
	// Fill the vector of pointers
	CopyInts (sizes, (dst->vptr) + 1, n);
	dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n);
	// The vector sizes is initiated with the beginning of each row
	CopyInts (dst->vptr, sizes, n);
	// This loop fills the contents of vector vpos
	pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; 
	pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS;
	pd3 = src.vval  + *pp1 - indexS; pd4 = dst->vval - indexD;
	for (i=indexS; i<(n+indexS); i++) {
		dim = (*pp2 - *pp1);
		for (j=0; j<dim; j++) {
			// The elements in the i-th row
			pp4[pp5[i]  ] = *pp3+indexDS; 
			pd4[pp5[i]++] = *pd3; 
			if (*pp3 != i) {
				// The nondiagonals elements define another element in the graph
				pp4[pp5[*pp3]  ] = i+indexDS;
				pd4[pp5[*pp3]++] = *pd3;
			}
			pp3++; pd3++;
		}
		pp1 = pp2++;
	}
	// The memory related to the vector sizes is liberated
	RemoveInts (&sizes);
}

/*********************************************************************************/

// This routine creates de sparse matrix dst from the matrix spr.
// The parameters indexS and indexD indicate, respectivaly, if 0-indexing or 1-indexing is used
// to store the sparse matrices.
void TransposeSparseMatrices (SparseMatrix src, int indexS, ptr_SparseMatrix dst, int indexD) {
    int n = src.dim1, nnz = 0;
    int *sizes = NULL;
    int *pp1 = NULL, *pp2 = NULL, *pp3 = NULL, *pp4 = NULL, *pp5 = NULL;
    int i, j, dim, indexDS = indexD - indexS;
    double *pd3 = NULL, *pd4 = NULL;

    // The vector sizes is created and initiated
    CreateInts (&sizes, n); InitInts (sizes, n, 0, 0);
    // This loop counts the number of elements in each row
    pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS;
    pp2 = pp1 + 1 ; pp4 = sizes - indexS;
    for (i=indexS; i<(n+indexS); i++) {
        // The size of the corresponding row is accumulated
        dim = (*pp2 - *pp1); 
        // Now each component of the row is analyzed
        for (j=0; j<dim; j++) {
            pp4[*pp3]++;
            pp3++;
        }
        pp1 = pp2++; 
    }

    // Compute the number of nonzeros of the new sparse matrix
    nnz = AddInts (sizes, n); 
    // Create the new sparse matrix
    CreateSparseMatrix (dst, indexD, n, n, nnz, 0);
    // Fill the vector of pointers
    CopyInts (sizes, (dst->vptr) + 1, n);
    dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n);
    // The vector sizes is initiated with the beginning of each row
    CopyInts (dst->vptr, sizes, n);
    // This loop fills the contents of vector vpos
    pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; 
    pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS;
    pd3 = src.vval  + *pp1 - indexS; pd4 = dst->vval - indexD;
    for (i=indexS; i<(n+indexS); i++) {
        dim = (*pp2 - *pp1);
        for (j=0; j<dim; j++) {
            // The elements in the i-th column
            pp4[pp5[*pp3]  ] = i+indexDS;
            pd4[pp5[*pp3]++] = *pd3;
            pp3++; pd3++;
        }
        pp1 = pp2++;
    }
    // The memory related to the vector sizes is liberated
    RemoveInts (&sizes);
}

/*********************************************************************************/

int ReadMatrixHB (char *filename, ptr_SparseMatrix p_spr) {
  int *colptr = NULL;
  double *exact = NULL;
  double *guess = NULL;
  int indcrd;
  char *indfmt = NULL;
  FILE *input;
  char *key = NULL;
  char *mxtype = NULL;
  int ncol;
  int neltvl;
  int nnzero;
  int nrhs;
  int nrhsix;
  int nrow;
  int ptrcrd;
  char *ptrfmt = NULL;
  int rhscrd;
  char *rhsfmt = NULL;
  int *rhsind = NULL;
  int *rhsptr = NULL;
  char *rhstyp = NULL;
  double *rhsval = NULL;
  double *rhsvec = NULL;
  int *rowind = NULL;
  char *title = NULL;
  int totcrd;
  int valcrd;
  char *valfmt = NULL;
  double *values = NULL;

	printf ("\nTEST09\n");
	printf ("  HB_FILE_READ reads all the data in an HB file.\n");
	printf ("  HB_FILE_MODULE is the module that stores the data.\n");

	input = fopen (filename, "r");
	if ( !input ) {
		printf ("\n TEST09 - Warning!\n Error opening the file %s .\n", filename);
		return -1;
	}

	hb_file_read ( input, &title, &key, &totcrd, &ptrcrd, &indcrd,
									&valcrd, &rhscrd, &mxtype, &nrow, &ncol, &nnzero, &neltvl,
									&ptrfmt, &indfmt, &valfmt, &rhsfmt, &rhstyp, &nrhs, &nrhsix,
									&colptr, &rowind, &values, &rhsval, &rhsptr, &rhsind, &rhsvec,
									&guess, &exact );
	fclose (input);

	// Conversion Fortran to C
	CopyShiftInts (colptr, colptr, nrow+1, -1);
	CopyShiftInts (rowind, rowind, nnzero, -1);

	//  Data assignment
	p_spr->dim1 = nrow  ; p_spr->dim2 = ncol  ; 
	p_spr->vptr = colptr; p_spr->vpos = rowind; p_spr->vval = values; 

	//  Memory liberation
	free (exact ); free (guess ); free (indfmt);
	free (key   ); free (mxtype); free (ptrfmt);
	free (rhsfmt); free (rhsind); free (rhsptr);
	free (rhstyp); free (rhsval); free (rhsvec);
	free (title ); free (valfmt);
	
	return 0;
}

/*********************************************************************************/

// This routine computes the product { res += spr * vec }.
// The parameter index indicates if 0-indexing or 1-indexing is used,
void ProdSparseMatrixVector2 (SparseMatrix spr, int index, double *vec, double *res) {
	int i, j;
	int *pp1 = spr.vptr, *pp2 = pp1+1, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pvec = vec - index, *pd2 = res;
	double *pd1 = spr.vval + *pp1 - index;

	// If the MSR format is used, first the diagonal has to be processed
	if (spr.vptr == spr.vpos)
		VvecDoubles (1.0, spr.vval, vec, 1.0, res, spr.dim1);

	for (i=0; i<spr.dim1; i++) {
		// The dot product between the row i and the vector vec is computed
		aux = 0.0;
		for (j=*pp1; j<*pp2; j++)
			aux += *(pd1++) * pvec[*(pi1++)];
//		for (j=spr.vptr[i]; j<spr.vptr[i+1]; j++)
//			aux += spr.vval[j] * pvec[spr.vpos[j]];
		// Accumulate the obtained value on the result
		*(pd2++) += aux; pp1 = pp2++;
	}
}

// This routine computes the product { res += spr * vec }.
// The parameter index indicates if 0-indexing or 1-indexing is used,
void ProdSparseMatrixVectorByRows (SparseMatrix spr, int index, double *vec, double *res) {
	int i, j, dim = spr.dim1;
	int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pvec = vec + *pp1 - index;
	double *pd1 = spr.vval + *pp1 - index;

	// Process all the rows of the matrix
	for (i=0; i<dim; i++) {
		// The dot product between the row i and the vector vec is computed
		aux = 0.0;
		for (j=pp1[i]; j<pp1[i+1]; j++)
			aux = fma(pd1[j], pvec[pi1[j]], aux);
		// Accumulate the obtained value on the result
		res[i] += aux; 
	}
}

// This routine computes the product { res += spr * vec }.
// The parameter index indicates if 0-indexing or 1-indexing is used,
void ProdSparseMatrixVectorByRows_OMP (SparseMatrix spr, int index, double *vec, double *res) {
	int i, j, dim = spr.dim1;
	int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pvec = vec + *pp1 - index;
	double *pd1 = spr.vval + *pp1 - index;

	// Process all the rows of the matrix
	#pragma omp parallel for private(j, aux)
	for (i=0; i<dim; i++) {
		// The dot product between the row i and the vector vec is computed
		aux = 0.0;
		for (j=pp1[i]; j<pp1[i+1]; j++)
			aux += pd1[j] * pvec[pi1[j]];
		// Accumulate the obtained value on the result
		res[i] += aux; 
	}
}

/*void ProdSparseMatrixVectorByRows_OMPTasks (SparseMatrix spr, int index, double *vec, double *res, int bm) {
	int i, dim = spr.dim1;

	// Process all the rows of the matrix
	//#pragma omp taskloop grainsize(bm) 
	for ( i=0; i<dim; i+=bm) {
		int cs = dim - i;
		int c = cs < bm ? cs : bm;
//	for (i=0; i<dim; i++) {
	  #pragma omp task depend(inout:res[i:i+c-1]) //shared(c)
		{
//	printf("Task SPMV ---- i: %d, c: %d \n", i, c);
		  int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	    double aux, *pvec = vec + *pp1 - index;
	    double *pd1 = spr.vval + *pp1 - index;
	  	// The dot product between the row i and the vector vec is computed
		  aux = 0.0;
			for(int idx=i; idx < i+c; idx++){
			//	printf("Task SPMV ---- idx: %d\n", idx);
		  	for (int j=pp1[idx]; j<pp1[idx+1]; j++)
			  	aux += pd1[j] * pvec[pi1[j]];
		  	// Accumulate the obtained value on the result
		  	res[idx] += aux; 
	  	}
		}
	}
}
*/

void ProdSparseMatrixVectorByRows_OMPTasks (SparseMatrix spr, int index, double *vec, double *res, int bm) {
	int i, j, idx, dim = spr.dim1;
	int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pvec = vec + *pp1 - index;
	double *pd1 = spr.vval + *pp1 - index;

	// Process all the rows of the matrix
	#pragma omp taskloop grainsize(bm) 
	for ( i=0; i<dim; i++ ) {
	  	// The dot product between the row i and the vector vec is computed
		  aux = 0.0;
		  for (j=pp1[i]; j<pp1[i+1]; j++)
			  aux += pd1[j] * pvec[pi1[j]];
		  // Accumulate the obtained value on the result
		  res[i] += aux; 
	}
}


/*********************************************************************************/

// This routine computes the product { res += spr * vec }.
// The parameter index indicates if 0-indexing or 1-indexing is used,
void ProdSparseMatrixVectorByCols (SparseMatrix spr, int index, double *vec, double *res) {
	int i, j, dim = spr.dim1;
	int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pres = res + *pp1 - index;
	double *pd1 = spr.vval + *pp1 - index;

	// Process all the columns of the matrix
	for (i=0; i<dim; i++) {
		// The result is scaled by the column i and the scalar vec[i]
		aux = vec[i];
		for (j=pp1[i]; j<pp1[i+1]; j++)
			pres[pi1[j]] += pd1[j] * aux;
	}
}

// This routine computes the product { res += spr * vec }.
// The parameter index indicates if 0-indexing or 1-indexing is used,
void ProdSparseMatrixVectorByCols_OMP (SparseMatrix spr, int index, double *vec, double *res) {
	int i, j, dim = spr.dim1;
	int *pp1 = spr.vptr, *pi1 = spr.vpos + *pp1 - index;
	double aux, *pres = res + *pp1 - index;
	double *pd1 = spr.vval + *pp1 - index;

	// Process all the columns of the matrix
	#pragma omp parallel for private(j, aux)
	for (i=0; i<dim; i++) {
		// The result is scaled by the column i and the scalar vec[i]
		for (j=pp1[i]; j<pp1[i+1]; j++) {
			aux = vec[i] * pd1[j];
			#pragma omp atomic
				pres[pi1[j]] += aux;
		}
	}
}

/*********************************************************************************/

void GetDiagonalSparseMatrix2 (SparseMatrix spr, int shft, double *diag, int *posd) {
    int i, j, dim = (spr.dim1 < spr.dim2) ? spr.dim1 : spr.dim2;
    int *pp1 = NULL, *pp2 = NULL, *pi1 = NULL, *pi2 = posd; 
    double *pd1 = NULL, *pd2 = diag;

    if (spr.vptr == spr.vpos)
        CopyDoubles (spr.vval, diag, spr.dim1);
    else {
        pp1 = spr.vptr; pp2 = pp1+1; j = (*pp2-*pp1);
        pi1 = spr.vpos+*pp1; pd1 = spr.vval+*pp1; 
        for (i=0; i<dim; i++) {
            while ((j > 0) && (*pi1 < (i+shft))) {
                pi1++; pd1++; j--;
            }
            *(pd2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pd1: 0.0;
            //*(pi2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pp2-j: -1;
            pi1 += j; pd1 += j; pp1 = (pp2++); j = (*pp2-*pp1);
        }
    }
}

/*********************************************************************************/