Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/stvk/StVKStiffnessMatrixMT.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "StVK" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code author: Jernej Barbic *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 #include <pthread.h>
30 #include "StVKStiffnessMatrixMT.h"
31
32 StVKStiffnessMatrixMT::StVKStiffnessMatrixMT(StVKInternalForces * stVKInternalForces, int numThreads_): StVKStiffnessMatrix(stVKInternalForces), numThreads(numThreads_)
33 {
34 SparseMatrix * stiffnessMatrixSkeleton;
35 GetStiffnessMatrixTopology(&stiffnessMatrixSkeleton);
36
37 // generate skeleton matrices
38 sparseMatrixBuffer = (SparseMatrix**) malloc (sizeof(SparseMatrix*) * numThreads);
39 for(int i=0; i<numThreads; i++)
40 sparseMatrixBuffer[i] = new SparseMatrix(*stiffnessMatrixSkeleton);
41
42 // split the workload
43 int numElements = volumetricMesh->getNumElements();
44 startElement = (int*) malloc (sizeof(int) * numThreads);
45 endElement = (int*) malloc (sizeof(int) * numThreads);
46
47 int remainder = numElements % numThreads;
48 // the first 'remainder' nodes will process one elements more
49 int jobSize = numElements / numThreads;
50
51 for(int rank=0; rank < numThreads; rank++)
52 {
53 if (rank < remainder)
54 {
55 startElement[rank] = rank * (jobSize+1);
56 endElement[rank] = (rank+1) * (jobSize+1);
57 }
58 else
59 {
60 startElement[rank] = remainder * (jobSize+1) + (rank-remainder) * jobSize;
61 endElement[rank] = remainder * (jobSize+1) + ((rank-remainder)+1) * jobSize;
62 }
63 }
64
65 delete(stiffnessMatrixSkeleton);
66
67 printf("Total elements: %d \n",numElements);
68 printf("Num threads: %d \n",numThreads);
69 printf("Canonical job size: %d \n",jobSize);
70 printf("Num threads with job size augmented by one elements: %d \n",remainder);
71 }
72
73 StVKStiffnessMatrixMT::~StVKStiffnessMatrixMT()
74 {
75 free(startElement);
76 free(endElement);
77 for(int i=0; i<numThreads; i++)
78 delete(sparseMatrixBuffer[i]);
79 free(sparseMatrixBuffer);
80 }
81
82 int StVKStiffnessMatrixMT::GetStartElement(int rank)
83 {
84 return startElement[rank];
85 }
86
87 int StVKStiffnessMatrixMT::GetEndElement(int rank)
88 {
89 return endElement[rank];
90 }
91
92 struct StVKStiffnessMatrixMT_threadArg
93 {
94 StVKStiffnessMatrixMT * stVKStiffnessMatrixMT;
95 const double * vertexDisplacements;
96 SparseMatrix * targetBuffer;
97 int rank;
98 };
99
100 void * StVKStiffnessMatrixMT_WorkerThread(void * arg)
101 {
102 struct StVKStiffnessMatrixMT_threadArg * threadArgp = (struct StVKStiffnessMatrixMT_threadArg*) arg;
103 StVKStiffnessMatrixMT * stVKStiffnessMatrixMT = threadArgp->stVKStiffnessMatrixMT;
104 const double * vertexDisplacements = threadArgp->vertexDisplacements;
105 SparseMatrix * targetBuffer = threadArgp->targetBuffer;
106 int rank = threadArgp->rank;
107 int startElement = stVKStiffnessMatrixMT->GetStartElement(rank);
108 int endElement = stVKStiffnessMatrixMT->GetEndElement(rank);
109
110 stVKStiffnessMatrixMT->AddLinearTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement);
111 stVKStiffnessMatrixMT->AddQuadraticTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement);
112 stVKStiffnessMatrixMT->AddCubicTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement);
113
114 return NULL;
115 }
116
117 void StVKStiffnessMatrixMT::ComputeStiffnessMatrix(const double * vertexDisplacements, SparseMatrix * sparseMatrix)
118 {
119 //PerformanceCounter stiffnessCounter;
120 // launch the threads
121 struct StVKStiffnessMatrixMT_threadArg * threadArgv = (struct StVKStiffnessMatrixMT_threadArg*) malloc (sizeof(struct StVKStiffnessMatrixMT_threadArg) * numThreads);
122
123 pthread_t * tid = (pthread_t*) malloc (sizeof(pthread_t) * numThreads);
124
125 for(int i=0; i<numThreads; i++)
126 {
127 threadArgv[i].stVKStiffnessMatrixMT = this;
128 threadArgv[i].vertexDisplacements = vertexDisplacements;
129 threadArgv[i].targetBuffer = sparseMatrixBuffer[i];
130 threadArgv[i].rank = i;
131 sparseMatrixBuffer[i]->ResetToZero();
132 }
133
134 for(int i=0; i<numThreads; i++)
135 {
136 if (pthread_create(&tid[i], NULL, StVKStiffnessMatrixMT_WorkerThread, &threadArgv[i]) != 0)
137 {
138 printf("Error: unable to launch thread %d.\n", i);
139 exit(1);
140 }
141 }
142
143 for(int i=0; i<numThreads; i++)
144 {
145 if (pthread_join(tid[i], NULL) != 0)
146 {
147 printf("Error: unable to join thread %d.\n", i);
148 exit(1);
149 }
150 }
151
152 free(threadArgv);
153 free(tid);
154
155 // assemble results
156 sparseMatrix->ResetToZero();
157 for(int i=0; i<numThreads; i++)
158 *sparseMatrix += *(sparseMatrixBuffer[i]);
159
160 //stiffnessCounter.StopCounter();
161 //printf("Stiffness matrix: %G\n", stiffnessCounter.GetElapsedTime());
162 }
163