ParallelCoarseSpaceMatVec.cc 4.16 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
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
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


#include "AMDiS.h"
#include "parallel/ParallelCoarseSpaceMatVec.h"

namespace AMDiS {

  using namespace std;

  void ParallelCoarseSpaceMatVec::create(ParallelDofMapping *iMap,
					 map<int, ParallelDofMapping*> cMap,
					 int subdomainLevel,
					 MPI::Intracomm mpiCommLocal,
					 MPI::Intracomm mpiCommGlobal)
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::update()");

    interiorMap = iMap;
    coarseSpaceMap = cMap;
    

    vector<ParallelDofMapping*> uniqueCoarseMap;
    if (coarseSpaceMap.size()) {
      std::set<ParallelDofMapping*> tmp;
      for (map<int, ParallelDofMapping*>::iterator it = coarseSpaceMap.begin();
	   it != coarseSpaceMap.end(); ++it) {
	if (tmp.count(it->second) == 0) {	  
	  tmp.insert(it->second);
	  uniqueCoarseMap.push_back(it->second);
	}
      }
    }

    int nCoarseMap = uniqueCoarseMap.size();
    mat.resize(nCoarseMap + 1);
    for (int i = 0; i < nCoarseMap + 1; i++)
      mat[i].resize(nCoarseMap + 1);

    componentIthCoarseMap.resize(coarseSpaceMap.size());
    for (unsigned int i = 0; i < componentIthCoarseMap.size(); i++) {
      bool found = false;
      for (int j = 0; j < nCoarseMap; j++) {
	if (coarseSpaceMap[i] == uniqueCoarseMap[j]) {
	  componentIthCoarseMap[i] = j;
	  found = true;
	  break;
	}
      }
      
      TEST_EXIT_DBG(found)("Should not happen!\n");
    }
	   
    // === Create PETSc matrix with the computed nnz data structure. ===
    
    int nRankRows = interiorMap->getRankDofs();
    int nOverallRows = interiorMap->getOverallDofs();

    bool localMatrix = (coarseSpaceMap.size() && subdomainLevel == 0);

    if (localMatrix) {
      MatCreateSeqAIJ(mpiCommLocal, nRankRows, nOverallRows,
		      0, PETSC_NULL, 
		      &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    } else {
      MatCreateAIJ(mpiCommGlobal, nRankRows, nRankRows, 
		   nOverallRows, nOverallRows,
		   0, PETSC_NULL,
		   0, PETSC_NULL,
		   &mat[0][0]);
      MatSetOption(mat[0][0], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
    }


    if (coarseSpaceMap.size()) {
      for (int i = 0; i < nCoarseMap; i++) {
	ParallelDofMapping* cMap = uniqueCoarseMap[i];

	int nRowsRankCoarse = cMap->getRankDofs();
	int nRowsOverallCoarse = cMap->getOverallDofs();
	
	MatCreateAIJ(mpiCommGlobal,
		     nRowsRankCoarse, nRowsRankCoarse,
		     nRowsOverallCoarse, nRowsOverallCoarse,
		     0, PETSC_NULL, 0, PETSC_NULL, 
		     &mat[i][i]);

	MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	MatSetOption(mat[i][i], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

	for (int j = 0; j < nCoarseMap + 1; j++) {
	  int nRowsRankMat = (j == 0 ? nRankRows : uniqueCoarseMap[j - 1]->getRankDofs());
	  int nRowsOverallMat = (j == 0 ? nOverallRows : uniqueCoarseMap[j - 1]->getOverallDofs());

	  MatCreateAIJ(mpiCommGlobal,
		       nRowsRankCoarse, nRowsRankMat,
		       nRowsOverallCoarse, nRowsOverallMat,
		       100, PETSC_NULL, 100, PETSC_NULL,
		       &mat[i + 1][j]);	  
	  MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	  MatSetOption(mat[i + 1][j], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

	  MatCreateAIJ(mpiCommGlobal,
		       nRowsRankMat, nRowsRankCoarse,
		       nRowsOverallMat, nRowsOverallCoarse,
		       0, PETSC_NULL, 0, PETSC_NULL,
		       &mat[j][i + 1]);
	  MSG("REMOVE THIS LINE WHEN FINISHED!\n");
	  MatSetOption(mat[j][i + 1], MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
	}
      }
    }
  }


  void ParallelCoarseSpaceMatVec::destroy()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::destroy()");

    int nMatrix = mat.size();
    for (int i = 0; i < nMatrix; i++)
      for (int j = 0; j < nMatrix; j++)
	MatDestroy(&mat[i][j]);
  }


  void ParallelCoarseSpaceMatVec::assembly()
  {
    FUNCNAME("ParallelCoarseSpaceMatVec::assembly()");

    int nMatrix = mat.size();
    for (int i = 0; i < nMatrix; i++) {
      for (int j = 0; j < nMatrix; j++) {
	MatAssemblyBegin(mat[i][j], MAT_FINAL_ASSEMBLY);
	MatAssemblyEnd(mat[i][j], MAT_FINAL_ASSEMBLY);  
      }
    }
  }

}