gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
sparsityPattern.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributor(s):
7 // Jonathan Lambrechts
8 //
9 
10 #include "sparsityPattern.h"
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 // this class has been optimized, please before changing anything, check twice :
16 // the impact on the performance to assemble typical High Order FE problems
17 // and the impact on the memory usage for this operation
18 
20 
22 {
23  for(int i = 0; i < _nRows; i++) {
24  if(_rowsj[i]) free(_rowsj[i]);
25  }
26  if(_nByRow) free(_nByRow);
27  if(_nAllocByRow) free(_nAllocByRow);
28  if(_rowsj) free(_rowsj);
29  _nByRow = nullptr;
30  _rowsj = nullptr;
31  _nAllocByRow = nullptr;
32  _nRows = 0;
33  _nRowsAlloc = 0;
34 }
35 
36 void sparsityPattern::insertEntry(int i, int j)
37 {
38  if(i >= _nRows) {
39  if(i >= _nRowsAlloc) {
40  _nRowsAlloc = (i + 1) * 3 / 2;
41  _rowsj = (int **)realloc(_rowsj, sizeof(int *) * _nRowsAlloc);
42  _nByRow = (int *)realloc(_nByRow, sizeof(int) * _nRowsAlloc);
43  _nAllocByRow = (int *)realloc(_nAllocByRow, sizeof(int) * _nRowsAlloc);
44  }
45  for(int k = _nRows; k <= i; k++) {
46  _nByRow[k] = 0;
47  _nAllocByRow[k] = 0;
48  _rowsj[k] = nullptr;
49  }
50  _nRows = i + 1;
51  }
52 
53  int n = _nByRow[i];
54  int *rowj = _rowsj[i];
55  int k = 0;
56  int k0 = 0, k1 = n;
57  if(n > 20) {
58  while(k1 - k0 > 20) {
59  int k2 = ((k0 + k1) / 2);
60  if(rowj[k2] > j)
61  k1 = k2;
62  else if(rowj[k2] < j)
63  k0 = k2 + 1;
64  else
65  return;
66  }
67  for(k = k0; k < k1; k++) {
68  if(rowj[k] >= j) {
69  if(rowj[k] == j) return;
70  break;
71  }
72  }
73  }
74  else { // this "if() else" is not strictly necessary but with it, gcc unroll
75  // the for(k) loop
76  for(k = 0; k < n; k++) {
77  if(rowj[k] >= j) {
78  if(rowj[k] == j) return;
79  break;
80  }
81  }
82  }
83  _nByRow[i] = n + 1;
84  if(_nByRow[i] > _nAllocByRow[i]) {
85  int na = (n + 1) * 3 / 2;
86  _rowsj[i] = (int *)realloc(_rowsj[i], (na * sizeof(int)));
87  _nAllocByRow[i] = na;
88  }
89  memmove(&_rowsj[i][k + 1], &_rowsj[i][k], (n - k) * sizeof(int));
90  _rowsj[i][k] = j;
91 }
92 
94 {
95  _nRows = 0;
96  _nRowsAlloc = 0;
97  _rowsj = nullptr;
98  _nByRow = nullptr;
99  _nAllocByRow = nullptr;
100 }
101 
102 const int *sparsityPattern::getRow(int i, int &size) const
103 {
104  if(i >= _nRows) {
105  size = 0;
106  return nullptr;
107  }
108  size = _nByRow[i];
109  return _rowsj[i];
110 }
sparsityPattern::~sparsityPattern
~sparsityPattern()
Definition: sparsityPattern.cpp:19
sparsityPattern::_nByRow
int * _nByRow
Definition: sparsityPattern.h:14
sparsityPattern::insertEntry
void insertEntry(int i, int j)
Definition: sparsityPattern.cpp:36
sparsityPattern::_nAllocByRow
int * _nAllocByRow
Definition: sparsityPattern.h:14
sparsityPattern::_nRowsAlloc
int _nRowsAlloc
Definition: sparsityPattern.h:16
sparsityPattern::getRow
const int * getRow(int line, int &size) const
Definition: sparsityPattern.cpp:102
sparsityPattern::_nRows
int _nRows
Definition: sparsityPattern.h:16
sparsityPattern.h
sparsityPattern::_rowsj
int ** _rowsj
Definition: sparsityPattern.h:15
sparsityPattern::sparsityPattern
sparsityPattern()
Definition: sparsityPattern.cpp:93
sparsityPattern::clear
void clear()
Definition: sparsityPattern.cpp:21