| 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 "StVKInternalForcesMT.h" |
| 31 | |
| 32 | StVKInternalForcesMT::StVKInternalForcesMT(VolumetricMesh * volumetricMesh, StVKElementABCD * precomputedABCDIntegrals, bool addGravity_, double g_, int numThreads_): StVKInternalForces(volumetricMesh, precomputedABCDIntegrals, addGravity_, g_), numThreads(numThreads_) |
| 33 | { |
| 34 | internalForceBuffer = (double*) malloc (sizeof(double) * numThreads * 3 * volumetricMesh->getNumVertices()); |
| 35 | energyBuffer = (double*) malloc (sizeof(double) * numThreads); |
| 36 | energyAuxBuffer = (double*) malloc (sizeof(double) * numThreads * 3 * volumetricMesh->getNumVertices()); |
| 37 | |
| 38 | // split the workload |
| 39 | int numElements = volumetricMesh->getNumElements(); |
| 40 | startElement = (int*) malloc (sizeof(int) * numThreads); |
| 41 | endElement = (int*) malloc (sizeof(int) * numThreads); |
| 42 | |
| 43 | int remainder = numElements % numThreads; |
| 44 | // the first 'remainder' nodes will process one element more |
| 45 | int jobSize = numElements / numThreads; |
| 46 | |
| 47 | for(int rank=0; rank < numThreads; rank++) |
| 48 | { |
| 49 | if (rank < remainder) |
| 50 | { |
| 51 | startElement[rank] = rank * (jobSize+1); |
| 52 | endElement[rank] = (rank+1) * (jobSize+1); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | startElement[rank] = remainder * (jobSize+1) + (rank-remainder) * jobSize; |
| 57 | endElement[rank] = remainder * (jobSize+1) + ((rank-remainder)+1) * jobSize; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | printf("Total elements: %d \n",numElements); |
| 62 | printf("Num threads: %d \n",numThreads); |
| 63 | printf("Canonical job size: %d \n",jobSize); |
| 64 | printf("Num threads with job size augmented by one element: %d \n",remainder); |
| 65 | } |
| 66 | |
| 67 | StVKInternalForcesMT::~StVKInternalForcesMT() |
| 68 | { |
| 69 | free(startElement); |
| 70 | free(endElement); |
| 71 | free(energyBuffer); |
| 72 | free(energyAuxBuffer); |
| 73 | free(internalForceBuffer); |
| 74 | } |
| 75 | |
| 76 | int StVKInternalForcesMT::GetStartElement(int rank) |
| 77 | { |
| 78 | return startElement[rank]; |
| 79 | } |
| 80 | |
| 81 | int StVKInternalForcesMT::GetEndElement(int rank) |
| 82 | { |
| 83 | return endElement[rank]; |
| 84 | } |
| 85 | |
| 86 | struct StVKInternalForcesMT_threadArg |
| 87 | { |
| 88 | StVKInternalForcesMT * stVKInternalForcesMT; |
| 89 | const double * vertexDisplacements; |
| 90 | double * targetBuffer; |
| 91 | int rank; |
| 92 | int computationTarget; // 0 = force, 1 = energy |
| 93 | double * auxBuffer; // for energy computations |
| 94 | }; |
| 95 | |
| 96 | void * StVKInternalForcesMT_WorkerThread(void * arg) |
| 97 | { |
| 98 | struct StVKInternalForcesMT_threadArg * threadArgp = (struct StVKInternalForcesMT_threadArg*) arg; |
| 99 | StVKInternalForcesMT * stVKInternalForcesMT = threadArgp->stVKInternalForcesMT; |
| 100 | const double * vertexDisplacements = threadArgp->vertexDisplacements; |
| 101 | double * targetBuffer = threadArgp->targetBuffer; |
| 102 | int rank = threadArgp->rank; |
| 103 | int startElement = stVKInternalForcesMT->GetStartElement(rank); |
| 104 | int endElement = stVKInternalForcesMT->GetEndElement(rank); |
| 105 | |
| 106 | if (threadArgp->computationTarget == 0) |
| 107 | { |
| 108 | stVKInternalForcesMT->AddLinearTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement); |
| 109 | stVKInternalForcesMT->AddQuadraticTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement); |
| 110 | stVKInternalForcesMT->AddCubicTermsContribution(vertexDisplacements, targetBuffer, startElement, endElement); |
| 111 | } |
| 112 | |
| 113 | if (threadArgp->computationTarget == 1) |
| 114 | { |
| 115 | *targetBuffer = stVKInternalForcesMT->ComputeEnergyContribution(vertexDisplacements, startElement, endElement, threadArgp->auxBuffer); |
| 116 | } |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | void StVKInternalForcesMT::ComputeForces(const double * vertexDisplacements, double * internalForces) |
| 122 | { |
| 123 | //PerformanceCounter forceCounter; |
| 124 | Compute(0, vertexDisplacements, internalForces); |
| 125 | |
| 126 | //forceCounter.StopCounter(); |
| 127 | //printf("Internal forces: %G\n", forceCounter.GetElapsedTime()); |
| 128 | } |
| 129 | |
| 130 | double StVKInternalForcesMT::ComputeEnergy(const double * vertexDisplacements) |
| 131 | { |
| 132 | double result; |
| 133 | Compute(1, vertexDisplacements, &result); |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | void StVKInternalForcesMT::Compute(int computationTarget, const double * vertexDisplacements, double * target) |
| 138 | { |
| 139 | int numVertices3 = 3 * volumetricMesh->getNumVertices(); |
| 140 | struct StVKInternalForcesMT_threadArg * threadArgv = (struct StVKInternalForcesMT_threadArg*) malloc (sizeof(struct StVKInternalForcesMT_threadArg) * numThreads); |
| 141 | |
| 142 | pthread_t * tid = (pthread_t*) malloc (sizeof(pthread_t) * numThreads); |
| 143 | |
| 144 | for(int i=0; i<numThreads; i++) |
| 145 | { |
| 146 | threadArgv[i].stVKInternalForcesMT = this; |
| 147 | threadArgv[i].vertexDisplacements = vertexDisplacements; |
| 148 | if (computationTarget == 0) |
| 149 | threadArgv[i].targetBuffer = &internalForceBuffer[i * numVertices3]; |
| 150 | if (computationTarget == 1) |
| 151 | { |
| 152 | threadArgv[i].targetBuffer = &energyBuffer[i]; |
| 153 | threadArgv[i].auxBuffer = &energyAuxBuffer[i * numVertices3]; |
| 154 | } |
| 155 | threadArgv[i].rank = i; |
| 156 | threadArgv[i].computationTarget = computationTarget; |
| 157 | } |
| 158 | |
| 159 | if (computationTarget == 0) |
| 160 | memset(internalForceBuffer, 0, sizeof(double) * numVertices3 * numThreads); |
| 161 | if (computationTarget == 1) |
| 162 | memset(energyBuffer, 0, sizeof(double) * numThreads); |
| 163 | |
| 164 | for(int i=0; i<numThreads; i++) |
| 165 | { |
| 166 | if (pthread_create(&tid[i], NULL, StVKInternalForcesMT_WorkerThread, &threadArgv[i]) != 0) |
| 167 | { |
| 168 | printf("Error: unable to launch thread %d.\n", i); |
| 169 | exit(1); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | for(int i=0; i<numThreads; i++) |
| 174 | { |
| 175 | if (pthread_join(tid[i], NULL) != 0) |
| 176 | { |
| 177 | printf("Error: unable to join thread %d.\n", i); |
| 178 | exit(1); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | free(threadArgv); |
| 183 | free(tid); |
| 184 | |
| 185 | // assemble |
| 186 | if (computationTarget == 0) |
| 187 | { |
| 188 | memset(target, 0, sizeof(double) * numVertices3); |
| 189 | for(int i=0; i<numThreads; i++) |
| 190 | { |
| 191 | double * source = &internalForceBuffer[i * numVertices3]; |
| 192 | for(int j=0; j<numVertices3; j++) |
| 193 | target[j] += source[j]; |
| 194 | } |
| 195 | |
| 196 | if (addGravity) |
| 197 | { |
| 198 | int n = volumetricMesh->getNumVertices(); |
| 199 | for(int i=0; i<3*n; i++) |
| 200 | { |
| 201 | target[i] -= gravityForce[i]; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (computationTarget == 1) |
| 207 | { |
| 208 | *target = 0; |
| 209 | for(int i=0; i<numThreads; i++) |
| 210 | *target += energyBuffer[i]; |
| 211 | } |
| 212 | } |
| 213 |