forked from smdogroup/tacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTACSAssembler_thread.cpp
300 lines (244 loc) · 9.73 KB
/
TACSAssembler_thread.cpp
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
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
/*
This file is part of TACS: The Toolkit for the Analysis of Composite
Structures, a parallel finite-element code for structural and
multidisciplinary design optimization.
Copyright (C) 2010 University of Toronto
Copyright (C) 2012 University of Michigan
Copyright (C) 2014 Georgia Tech Research Corporation
Additional copyright (C) 2010 Graeme J. Kennedy and Joaquim
R.R.A. Martins All rights reserved.
TACS is licensed under the Apache License, Version 2.0 (the
"License"); you may not use this software except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
*/
#include "TACSAssembler.h"
#include "tacslapack.h"
/*!
Schedule the parts of the matrix/residual to assemble
*/
static pthread_mutex_t sched_mutex = PTHREAD_MUTEX_INITIALIZER;
void TACSAssembler::schedPthreadJob( TACSAssembler * tacs,
int * index, int total_size ){
pthread_mutex_lock(&sched_mutex);
if (tacs->numCompletedElements < total_size){
*index = tacs->numCompletedElements;
tacs->numCompletedElements += 1;
}
else {
*index = -1;
}
pthread_mutex_unlock(&sched_mutex);
}
/*!
The threaded-implementation of the residual assembly
Note that the input must be the TACSAssemblerPthreadInfo data type.
This function only uses the following data members:
tacs: the pointer to the TACSAssembler object
*/
void *TACSAssembler::assembleRes_thread( void *t ){
TACSAssemblerPthreadInfo *pinfo =
static_cast<TACSAssemblerPthreadInfo*>(t);
// Un-pack information for this computation
TACSAssembler *tacs = pinfo->tacs;
TACSBVec *res = pinfo->res;
// Allocate a temporary array large enough to store everything required
int s = tacs->maxElementSize;
int sx = 3*tacs->maxElementNodes;
int dataSize = 4*s + sx;
TacsScalar *data = new TacsScalar[ dataSize ];
// Set pointers to the allocate memory
TacsScalar *vars = &data[0];
TacsScalar *dvars = &data[s];
TacsScalar *ddvars = &data[2*s];
TacsScalar *elemRes = &data[3*s];
TacsScalar *elemXpts = &data[4*s];
// Set the data for the auxiliary elements - if there are any
int naux = 0, aux_count = 0;
TACSAuxElem *aux = NULL;
if (tacs->auxElements){
naux = tacs->auxElements->getAuxElements(&aux);
}
while (tacs->numCompletedElements < tacs->numElements){
int elemIndex = -1;
TACSAssembler::schedPthreadJob(tacs, &elemIndex, tacs->numElements);
if (elemIndex >= 0){
// Get the element object
TACSElement *element = tacs->elements[elemIndex];
// Retrieve the variable values
int ptr = tacs->elementNodeIndex[elemIndex];
int len = tacs->elementNodeIndex[elemIndex+1] - ptr;
const int *nodes = &tacs->elementTacsNodes[ptr];
tacs->xptVec->getValues(len, nodes, elemXpts);
tacs->varsVec->getValues(len, nodes, vars);
tacs->dvarsVec->getValues(len, nodes, dvars);
tacs->ddvarsVec->getValues(len, nodes, ddvars);
// Generate the Jacobian of the element
element->addResidual(tacs->time, elemRes, elemXpts,
vars, dvars, ddvars);
// Increment the aux counter until we possibly have
// aux[aux_count].num == elemIndex
while (aux_count < naux && aux[aux_count].num < elemIndex){
aux_count++;
}
// Add the residual from the auxiliary elements
while (aux_count < naux && aux[aux_count].num == elemIndex){
aux[aux_count].elem->addResidual(tacs->time, elemRes, elemXpts,
vars, dvars, ddvars);
aux_count++;
}
// Add the values to the residual when the memory unlocks
pthread_mutex_lock(&tacs->tacs_mutex);
res->setValues(len, nodes, elemRes, TACS_ADD_VALUES);
pthread_mutex_unlock(&tacs->tacs_mutex);
}
}
delete [] data;
pthread_exit(NULL);
}
/*!
The threaded-implementation of the matrix assembly
Note that the input must be the TACSAssemblerPthreadInfo data type.
This function only uses the following data members:
tacs: the pointer to the TACSAssembler object
A: the generic TACSMat base class
*/
void *TACSAssembler::assembleJacobian_thread( void *t ){
TACSAssemblerPthreadInfo *pinfo =
static_cast<TACSAssemblerPthreadInfo*>(t);
// Un-pack information for this computation
TACSAssembler *tacs = pinfo->tacs;
TACSBVec *res = pinfo->res;
TACSMat *A = pinfo->mat;
double alpha = pinfo->alpha;
double beta = pinfo->beta;
double gamma = pinfo->gamma;
MatrixOrientation matOr = pinfo->matOr;
// Allocate a temporary array large enough to store everything
// required
int s = tacs->maxElementSize;
int sx = 3*tacs->maxElementNodes;
int sw = tacs->maxElementIndepNodes;
int dataSize = 4*s + sx + s*s + sw;
TacsScalar *data = new TacsScalar[ dataSize ];
int *idata = new int[ sw + tacs->maxElementNodes + 1 ];
// Set pointers to the allocate memory
TacsScalar *vars = &data[0];
TacsScalar *dvars = &data[s];
TacsScalar *ddvars = &data[2*s];
TacsScalar *elemRes = &data[3*s];
TacsScalar *elemXpts = &data[4*s];
TacsScalar *elemWeights = &data[4*s + sx];
TacsScalar *elemMat = &data[4*s + sx + sw];
// Set the data for the auxiliary elements - if there are any
int naux = 0, aux_count = 0;
TACSAuxElem *aux = NULL;
if (tacs->auxElements){
naux = tacs->auxElements->getAuxElements(&aux);
}
while (tacs->numCompletedElements < tacs->numElements){
int elemIndex = -1;
TACSAssembler::schedPthreadJob(tacs, &elemIndex, tacs->numElements);
if (elemIndex >= 0){
// Get the element object
TACSElement *element = tacs->elements[elemIndex];
// Retrieve the variable values
int ptr = tacs->elementNodeIndex[elemIndex];
int len = tacs->elementNodeIndex[elemIndex+1] - ptr;
const int *nodes = &tacs->elementTacsNodes[ptr];
tacs->xptVec->getValues(len, nodes, elemXpts);
tacs->varsVec->getValues(len, nodes, vars);
tacs->dvarsVec->getValues(len, nodes, dvars);
tacs->ddvarsVec->getValues(len, nodes, ddvars);
// Retrieve the number of element variables
int nvars = element->numVariables();
memset(elemRes, 0, nvars*sizeof(TacsScalar));
memset(elemMat, 0, nvars*nvars*sizeof(TacsScalar));
// Generate the Jacobian of the element
if (res){
element->addResidual(tacs->time, elemRes, elemXpts,
vars, dvars, ddvars);
}
element->addJacobian(tacs->time, elemMat,
alpha, beta, gamma,
elemXpts, vars, dvars, ddvars);
// Increment the aux counter until we possibly have
// aux[aux_count].num == elemIndex
while (aux_count < naux && aux[aux_count].num < elemIndex){
aux_count++;
}
// Add the residual from the auxiliary elements
while (aux_count < naux && aux[aux_count].num == elemIndex){
if (res){
aux[aux_count].elem->addResidual(tacs->time, elemRes, elemXpts,
vars, dvars, ddvars);
}
aux[aux_count].elem->addJacobian(tacs->time, elemMat,
alpha, beta, gamma,
elemXpts, vars, dvars, ddvars);
aux_count++;
}
pthread_mutex_lock(&tacs->tacs_mutex);
// Add values to the residual
if (res){ res->setValues(len, nodes, elemRes, TACS_ADD_VALUES); }
// Add values to the matrix
tacs->addMatValues(A, elemIndex, elemMat, idata, elemWeights, matOr);
pthread_mutex_unlock(&tacs->tacs_mutex);
}
}
delete [] data;
delete [] idata;
pthread_exit(NULL);
}
/*!
The threaded-implementation of the matrix-type assembly
This function uses the following information from the
TACSAssemblerPthreadInfo class:
A: the matrix to assemble (output)
scaleFactor: scaling factor applied to the matrix
matType: the matrix type defined in Element.h
matOr: the matrix orientation: NORMAL or TRANSPOSE
*/
void *TACSAssembler::assembleMatType_thread( void *t ){
TACSAssemblerPthreadInfo *pinfo =
static_cast<TACSAssemblerPthreadInfo*>(t);
// Un-pack information for this computation
TACSAssembler *tacs = pinfo->tacs;
TACSMat *A = pinfo->mat;
ElementMatrixType matType = pinfo->matType;
MatrixOrientation matOr = pinfo->matOr;
// Allocate a temporary array large enough to store everything required
int s = tacs->maxElementSize;
int sx = 3*tacs->maxElementNodes;
int sw = tacs->maxElementIndepNodes;
int dataSize = s + sx + s*s + sw;
TacsScalar *data = new TacsScalar[ dataSize ];
int *idata = new int[ sw + tacs->maxElementNodes + 1 ];
TacsScalar *vars = &data[0];
TacsScalar *elemXpts = &data[s];
TacsScalar *elemWeights = &data[s + sx];
TacsScalar *elemMat = &data[s + sx + sw];
while (tacs->numCompletedElements < tacs->numElements){
int elemIndex = -1;
TACSAssembler::schedPthreadJob(tacs, &elemIndex, tacs->numElements);
if (elemIndex >= 0){
// Get the element
TACSElement *element = tacs->elements[elemIndex];
// Retrieve the variable values
// Retrieve the variable values
int ptr = tacs->elementNodeIndex[elemIndex];
int len = tacs->elementNodeIndex[elemIndex+1] - ptr;
const int *nodes = &tacs->elementTacsNodes[ptr];
tacs->xptVec->getValues(len, nodes, elemXpts);
tacs->varsVec->getValues(len, nodes, vars);
// Retrieve the type of the matrix
element->getMatType(matType, elemMat, elemXpts, vars);
pthread_mutex_lock(&tacs->tacs_mutex);
// Add values to the matrix
tacs->addMatValues(A, elemIndex, elemMat, idata, elemWeights, matOr);
pthread_mutex_unlock(&tacs->tacs_mutex);
}
}
delete [] data;
pthread_exit(NULL);
}