Fast Auxiliary Space Preconditioning  1.8.4 Feb/15/2016
sparse_coo.c
Go to the documentation of this file.
1 
6 #include <math.h>
7 #include <time.h>
8 
9 #ifdef _OPENMP
10 #include <omp.h>
11 #endif
12 
13 #include "fasp.h"
14 #include "fasp_functs.h"
15 
16 /*---------------------------------*/
17 /*-- Public Functions --*/
18 /*---------------------------------*/
19 
35  const INT n,
36  const INT nnz)
37 {
38  dCOOmat A;
39 
40  A.rowind = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
41  A.colind = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
42  A.val = (REAL *)fasp_mem_calloc(nnz, sizeof(REAL));
43 
44  A.row = m; A.col = n; A.nnz = nnz;
45 
46  return A;
47 }
48 
62 void fasp_dcoo_alloc (const INT m,
63  const INT n,
64  const INT nnz,
65  dCOOmat *A)
66 {
67 
68  if ( nnz > 0 ) {
69  A->rowind = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
70  A->colind = (INT *)fasp_mem_calloc(nnz, sizeof(INT));
71  A->val = (REAL*)fasp_mem_calloc(nnz,sizeof(REAL));
72  }
73  else {
74  A->rowind = NULL;
75  A->colind = NULL;
76  A->val = NULL;
77  }
78 
79  A->row = m; A->col = n; A->nnz = nnz;
80 
81  return;
82 }
83 
95 {
96  if (A==NULL) return;
97 
98  fasp_mem_free(A->rowind); A->rowind= NULL;
99  fasp_mem_free(A->colind); A->colind = NULL;
100  fasp_mem_free(A->val); A->val = NULL;
101 }
102 
117  const INT offset)
118 {
119  const INT nnz = A->nnz;
120  INT i, *ai = A->rowind, *aj = A->colind;
121 
122  // Variables for OpenMP
123  INT nthreads = 1, use_openmp = FALSE;
124  INT myid, mybegin, myend;
125 
126 #ifdef _OPENMP
127  if (nnz > OPENMP_HOLDS) {
128  use_openmp = TRUE;
129  nthreads = FASP_GET_NUM_THREADS();
130  }
131 #endif
132 
133  if (use_openmp) {
134 #ifdef _OPENMP
135 #pragma omp parallel for private(myid, i, mybegin, myend)
136 #endif
137  for (myid=0; myid<nthreads; myid++) {
138  FASP_GET_START_END(myid, nthreads, nnz, &mybegin, &myend);
139  for (i=mybegin; i<myend; ++i) {
140  ai[i]+=offset; aj[i]+=offset;
141  }
142  }
143  }
144  else {
145  for (i=0;i<nnz;++i) {
146  ai[i]+=offset; aj[i]+=offset;
147  }
148  }
149 }
150 
151 /*---------------------------------*/
152 /*-- End of File --*/
153 /*---------------------------------*/
#define TRUE
Definition of logic type.
Definition: fasp_const.h:67
dCOOmat fasp_dcoo_create(const INT m, const INT n, const INT nnz)
Create IJ sparse matrix data memory space.
Definition: sparse_coo.c:34
INT * rowind
integer array of row indices, the size is nnz
Definition: fasp.h:221
void fasp_dcoo_shift(dCOOmat *A, const INT offset)
Re-index a REAL matrix in IJ format to make the index starting from 0 or 1.
Definition: sparse_coo.c:116
#define REAL
Definition: fasp.h:67
REAL * val
nonzero entries of A
Definition: fasp.h:227
INT col
column of matrix A, n
Definition: fasp.h:215
void * fasp_mem_calloc(LONGLONG size, INT type)
1M = 1024*1024
Definition: memory.c:60
INT nnz
number of nonzero entries
Definition: fasp.h:218
#define INT
Definition: fasp.h:64
void fasp_dcoo_alloc(const INT m, const INT n, const INT nnz, dCOOmat *A)
Allocate COO sparse matrix memory space.
Definition: sparse_coo.c:62
INT row
row number of matrix A, m
Definition: fasp.h:212
Sparse matrix of REAL type in COO (or IJ) format.
Definition: fasp.h:209
void FASP_GET_START_END(INT procid, INT nprocs, INT n, INT *start, INT *end)
Assign Load to each thread.
Definition: threads.c:83
void fasp_mem_free(void *mem)
Free up previous allocated memory body.
Definition: memory.c:150
void fasp_dcoo_free(dCOOmat *A)
Free IJ sparse matrix data memory space.
Definition: sparse_coo.c:94
#define OPENMP_HOLDS
Definition: fasp_const.h:248
Main header file for FASP.
INT * colind
integer array of column indices, the size is nnz
Definition: fasp.h:224
#define FALSE
Definition: fasp_const.h:68