![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsElasticRod_CompByCUDA.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (09/02/2009) 00009 ******************************************************************************/ 00010 #include "TAPsElasticRod_CompByCUDA.hpp" 00011 // Using Inclusion Model (i.e. definitions are included in declarations) 00012 // (this name.cpp is included in name.hpp) 00013 // Each friend is defined directly inside its declaration. 00014 00015 BEGIN_NAMESPACE_TAPs 00016 //============================================================================= 00017 //----------------------------------------------------------------------------- 00018 00019 // Constructor 00020 template <typename T> 00021 ElasticRod_CompByCUDA<T>::ElasticRod_CompByCUDA () : 00022 m_pIdxCurr( 0 ), 00023 m_pIdxPrev( 0 ), 00024 m_pIdxNext( 0 ), 00025 m_pParameters( NULL ), 00026 m_pNodeList( NULL ), 00027 m_cudaPosList( NULL ), 00028 m_cudaPrevPosList( NULL ), 00029 m_cudaOriList( NULL ), 00030 m_cudaPrevOriList( NULL ), 00031 m_cudaIntForceList( NULL ), 00032 m_cudaExtForceList( NULL ), 00033 m_cudaID( 0 ), 00034 m_bUsePLHM( false ) 00035 {} 00036 00037 00038 // Destructor 00039 template <typename T> 00040 ElasticRod_CompByCUDA<T>::~ElasticRod_CompByCUDA () 00041 { 00042 DeleteCUDA(); 00043 } 00044 00045 00046 // Create CUDA 00047 template <typename T> 00048 void ElasticRod_CompByCUDA<T>::CreateCUDA ( 00049 int * pIdxCurr, 00050 int * pIdxPrev, 00051 int * pIdxNext, 00052 ElasticRodParameters<T> * pParameters, 00053 SetOfElasticRodNodes * pNodeList, 00054 bool bUsePLHM 00055 ) 00056 { 00057 m_pIdxCurr = pIdxCurr; 00058 m_pIdxPrev = pIdxPrev; 00059 m_pIdxNext = pIdxNext; 00060 m_pParameters = pParameters; 00061 m_pNodeList = pNodeList; 00062 00063 // Setting utilizing CUDA's page-locked host memory 00064 m_bUsePLHM = bUsePLHM; 00065 00066 //#if CUDART_VERSION >= 2020 00067 if ( m_bUsePLHM ) { 00068 int device; 00069 TAPs::CUDA::GetDevice( device ); 00070 m_bUsePLHM = TAPs::CUDA::CanDeviceMapHostMemory( device ); 00071 std::cout << "m_bUsePLHM: " << m_bUsePLHM << " printed from by File: " << __FILE__ << "; Line: " << __LINE__ << "\n"; 00072 } 00073 //#else 00074 // m_bUsePLHM = false; 00075 //#endif 00076 00077 try { 00078 if ( m_bUsePLHM ) InitPLHM(); 00079 else Init(); 00080 } 00081 catch ( char * e ) { 00082 throw e; 00083 } 00084 } 00085 00086 00087 // Delete CUDA 00088 template <typename T> 00089 void ElasticRod_CompByCUDA<T>::DeleteCUDA () 00090 { 00091 if ( m_bUsePLHM ) CleanPLHM(); 00092 else Clean(); 00093 } 00094 00095 00096 // Advance simulation by time step 00097 template <typename T> 00098 void ElasticRod_CompByCUDA<T>::AdvanceSimulation () 00099 { 00100 if ( m_bUsePLHM ) AdvSimPLHM(); 00101 else AdvSim(); 00102 } 00103 00104 00105 // Advance simulation by time step 00106 template <typename T> 00107 void ElasticRod_CompByCUDA<T>::AdvSim () 00108 { 00109 // Copy the (current) vertex positions of the object to memory for transfering to CUDA. 00110 CopyPosToMem(); 00111 CopyIntForceToMem(); 00112 CopyExtForceToMem(); 00113 CopyOriToMem(); 00114 #ifdef TAPs_ADVANCED_SIMULATION 00115 //CopySimFlagsToMem(); 00116 //CopyPosConstraintToMem(); 00117 #endif//TAPs_ADVANCED_SIMULATION 00118 00119 // Call the wrapper function 00120 TAPs::CUDA::Global__ModelElasticRod_AdvSim( 00121 m_cudaID, 00122 m_pParameters->NumOfNodes, 00123 64, 00124 0, 00125 m_pParameters->TimeStep, 00126 1, 00127 m_pParameters->Radius, 00128 m_pParameters->LinkLength, 00129 m_pParameters->LinkLength, 00130 m_pParameters->MassOfPoint, 00131 m_pParameters->MaterialDensity, 00132 //m_pParameters->Kt, //!< kinetic translational constant 00133 //m_pParameters->Kr[0], //!< kinetic rotational constant -- x 00134 //m_pParameters->Kr[1], //!< kinetic rotational constant -- y 00135 //m_pParameters->Kr[2], //!< kinetic rotational constant -- z 00136 //m_pParameters->Dt, //!< translational constant 00137 //m_pParameters->Dr[0], //!< rotational dissipation constant -- x 00138 //m_pParameters->Dr[1], //!< rotational dissipation constant -- y 00139 //m_pParameters->Dr[2], //!< rotational dissipation constant -- z 00140 m_pParameters->Kconstraint_3rdDirAlignTangent, 00141 m_pParameters->Kvdamping, 00142 m_pParameters->Ps, 00143 m_pParameters->Pb[0], 00144 m_pParameters->Pb[1], 00145 m_pParameters->Pb[2], 00146 m_cudaPosList, 00147 m_cudaPrevPosList, 00148 m_cudaOriList, 00149 m_cudaPrevOriList, 00150 m_cudaIntForceList, 00151 m_cudaExtForceList 00152 ); 00153 00154 00155 //* 00156 // The new vertex positions are returned by CUDA to memory for previous vertex positions. 00157 // So the new vertex positions must be copied to the vertex positions of the object. 00158 CopyMemToPrevPos(); 00159 CopyMemToPrevOri(); 00160 // Then the previous and current vertex pointers (that point to memory for transfering to CUDA) are swapped. 00161 // So that now the previous becomes current and the current becomes previous. 00162 SwapBuffers(); 00163 //*/ 00164 00165 /* 00166 // DEBUG -- PRINTOUT 00167 { 00168 std::cout << "CUDA\n"; 00169 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00170 int i = 0; 00171 int p = 0; 00172 int P = *m_pIdxNext; 00173 while ( node != m_pNodeList->end() ) { 00174 std::cout << "C" << i << ": " << m_cudaPrevPosList[p+0] << " " << m_cudaPrevPosList[p+1] << " " << m_cudaPrevPosList[p+2] << "\n"; 00175 p += 4; 00176 ++i; 00177 ++node; 00178 } 00179 } 00180 { 00181 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00182 int i = 0; 00183 int p = 0; 00184 int P = *m_pIdxNext; 00185 while ( node != m_pNodeList->end() ) { 00186 std::cout << "O" << i << ": " << m_cudaPrevOriList[p+0] << " " << m_cudaPrevOriList[p+1] << " " << m_cudaPrevOriList[p+2] << " " << m_cudaPrevOriList[p+3] << "\n"; 00187 p += 4; 00188 ++i; 00189 ++node; 00190 } 00191 } 00192 //*/ 00193 00194 00195 #ifdef TAPs_ADVANCED_SIMULATION 00196 // The new strand's simulation flags list is returned by CUDA. 00197 // It must be copied to the simulation flags list of the strand. 00198 //CUDA_CopyMemToSimFlags(); 00199 //CUDA_CopyMemToPosConstraint(); 00200 00201 // NEED TO CHANGE THE VALUES STORED IN m_pAdvSimData 00202 00203 #endif//TAPs_ADVANCED_SIMULATION 00204 } 00205 00206 00207 // Advance simulation by time step 00208 template <typename T> 00209 void ElasticRod_CompByCUDA<T>::AdvSimPLHM () 00210 { 00211 // Copy the (current) vertex positions of the object to memory for transfering to CUDA. 00212 CopyPosToMem(); 00213 CopyIntForceToMem(); 00214 CopyExtForceToMem(); 00215 CopyOriToMem(); 00216 #ifdef TAPs_ADVANCED_SIMULATION 00217 //CopySimFlagsToMem(); 00218 //CopyPosConstraintToMem(); 00219 #endif//TAPs_ADVANCED_SIMULATION 00220 00221 // DEBUG 00222 //printf( "BEFORE SIM -- HOST PTRs %x %x; %x %x \n", m_cudaPosList, m_cudaPrevPosList, m_cudaOriList, m_cudaPrevOriList ); 00223 00224 // Call the wrapper function 00225 TAPs::CUDA::Global__PLHMModelElasticRod_AdvSim( 00226 m_cudaID, 00227 m_pParameters->NumOfNodes, 00228 64, 00229 0, 00230 m_pParameters->TimeStep, 00231 1, 00232 m_pParameters->Radius, 00233 m_pParameters->LinkLength, 00234 m_pParameters->LinkLength, 00235 m_pParameters->MassOfPoint, 00236 m_pParameters->MaterialDensity, 00237 //m_pParameters->Kt, //!< kinetic translational constant 00238 //m_pParameters->Kr[0], //!< kinetic rotational constant -- x 00239 //m_pParameters->Kr[1], //!< kinetic rotational constant -- y 00240 //m_pParameters->Kr[2], //!< kinetic rotational constant -- z 00241 //m_pParameters->Dt, //!< translational constant 00242 //m_pParameters->Dr[0], //!< rotational dissipation constant -- x 00243 //m_pParameters->Dr[1], //!< rotational dissipation constant -- y 00244 //m_pParameters->Dr[2], //!< rotational dissipation constant -- z 00245 m_pParameters->Kconstraint_3rdDirAlignTangent, 00246 m_pParameters->Kvdamping, 00247 m_pParameters->Ps, 00248 m_pParameters->Pb[0], 00249 m_pParameters->Pb[1], 00250 m_pParameters->Pb[2], 00251 m_cudaPosList, 00252 m_cudaPrevPosList, 00253 m_cudaOriList, 00254 m_cudaPrevOriList, 00255 m_cudaIntForceList, 00256 m_cudaExtForceList 00257 ); 00258 00259 // DEBUG 00260 //printf( "AFTER SIM -- HOST PTRs %x %x; %x %x \n\n", m_cudaPosList, m_cudaPrevPosList, m_cudaOriList, m_cudaPrevOriList ); 00261 00262 // The new vertex positions are returned by CUDA to memory for previous vertex positions. 00263 // So the new vertex positions must be copied to the vertex positions of the object. 00264 //CopyMemToPrevPos(); 00265 //CopyMemToPrevOri(); 00266 // The swapping is done in Global__PLHMModelElasticRod_AdvSim fn 00267 //SwapBuffers(); 00268 00269 CopyMemPLHMToPos(); 00270 CopyMemPLHMToOri(); 00271 //SwapBuffers(); 00272 00273 #ifdef TAPs_ADVANCED_SIMULATION 00274 // The new strand's simulation flags list is returned by CUDA. 00275 // It must be copied to the simulation flags list of the strand. 00276 //CUDA_CopyMemToSimFlags(); 00277 //CUDA_CopyMemToPosConstraint(); 00278 00279 // NEED TO CHANGE THE VALUES STORED IN m_pAdvSimData 00280 00281 #endif//TAPs_ADVANCED_SIMULATION 00282 } 00283 00284 00285 #if defined(__gl_h_) || defined(__GL_H__) 00286 // Generate generalized cylinder vertex data for drawing 00287 template <typename T> 00288 void ElasticRod_CompByCUDA<T>::GenCylinderVertexDataForDrawing ( GLuint vbo_GL, unsigned int numOfCrossSectionVertices ) 00289 { 00290 if ( m_bUsePLHM ) { 00291 TAPs::CUDA::GL__GenCylinderForElasticRodModel_PLHM( 00292 m_cudaID, 00293 m_pParameters->NumOfNodes, // number of vertices 00294 64, // number of threads per block 00295 vbo_GL, // OpenGL vertex buffer object 00296 numOfCrossSectionVertices, // number of cross section's vertices 00297 m_pParameters->Radius 00298 ); 00299 } 00300 else { 00301 TAPs::CUDA::GL__GenCylinderForElasticRodModel( 00302 m_cudaID, 00303 m_pParameters->NumOfNodes, // number of vertices 00304 64, // number of threads per block 00305 vbo_GL, // OpenGL vertex buffer object 00306 numOfCrossSectionVertices, // number of cross section's vertices 00307 m_pParameters->Radius 00308 ); 00309 } 00310 } 00311 #endif//#if defined(__gl_h_) || defined(__GL_H__) 00312 00313 00314 template <typename T> 00315 void ElasticRod_CompByCUDA<T>::Init () throw (...) 00316 { 00317 Clean(); 00318 00319 unsigned int size = sizeof(float) * (m_pParameters->NumOfNodes) * 4; 00320 00321 // Allocate memory for the centerline's position list 00322 m_cudaPosList = (float *)malloc( size ); 00323 if ( !m_cudaPosList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaPosList!"; 00324 00325 // Allocate memory for the centerline's previous position list 00326 m_cudaPrevPosList = (float *)malloc( size ); 00327 if ( !m_cudaPrevPosList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaPrevPosList!"; 00328 00329 // Allocate memory for the orientation list 00330 m_cudaOriList = (float *)malloc( size ); 00331 if ( !m_cudaOriList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaOriList!"; 00332 00333 // Allocate memory for the previous orientation list 00334 m_cudaPrevOriList = (float *)malloc( size ); 00335 if ( !m_cudaPrevOriList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaPreOriList!"; 00336 00337 // Allocate memory for the internal force list 00338 m_cudaIntForceList = (float *)malloc( size ); 00339 if ( !m_cudaIntForceList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaIntForceList!"; 00340 00341 // Allocate memory for the external force list 00342 m_cudaExtForceList = (float *)malloc( size ); 00343 if ( !m_cudaExtForceList ) throw "Couldn't allocate memory for ModelElasticRod's m_cudaExtForceList!"; 00344 00345 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00346 int p = 0; 00347 int C = *m_pIdxCurr; 00348 int P = *m_pIdxNext; 00349 while ( node != m_pNodeList->end() ) { 00350 // x 00351 m_cudaPosList[p] = node->Centerline[C].GetPosition()[0]; 00352 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[0]; 00353 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[0]; 00354 m_cudaExtForceList[p] = node->ExternalForce[0]; 00355 m_cudaOriList[p] = node->Orientation[C].GetR(); 00356 m_cudaPrevOriList[p++] = node->Orientation[P].GetR(); 00357 // y 00358 m_cudaPosList[p] = node->Centerline[C].GetPosition()[1]; 00359 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[1]; 00360 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[1]; 00361 m_cudaExtForceList[p] = node->ExternalForce[1]; 00362 m_cudaOriList[p] = node->Orientation[C].GetI(); 00363 m_cudaPrevOriList[p++] = node->Orientation[P].GetI(); 00364 // z 00365 m_cudaPosList[p] = node->Centerline[C].GetPosition()[2]; 00366 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[2]; 00367 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[2]; 00368 m_cudaExtForceList[p] = node->ExternalForce[2]; 00369 m_cudaOriList[p] = node->Orientation[C].GetJ(); 00370 m_cudaPrevOriList[p++] = node->Orientation[P].GetJ(); 00371 // w 00372 m_cudaPosList[p] = 1; 00373 m_cudaPrevPosList[p] = 1; 00374 m_cudaIntForceList[p] = 1; 00375 m_cudaExtForceList[p] = 1; 00376 m_cudaOriList[p] = node->Orientation[C].GetK(); 00377 m_cudaPrevOriList[p++] = node->Orientation[P].GetK(); 00378 ++node; 00379 } 00380 00381 TAPs::CUDA::InitailizeDataForElasticRodModel( 00382 m_cudaID, 00383 m_pParameters->NumOfNodes, 00384 m_cudaPosList, 00385 m_cudaPrevPosList, 00386 m_cudaOriList, 00387 m_cudaPrevOriList, 00388 m_cudaIntForceList, 00389 m_cudaExtForceList 00390 ); 00391 } 00392 00393 00394 template <typename T> 00395 void ElasticRod_CompByCUDA<T>::Clean () 00396 { 00397 if ( m_cudaPosList ) { 00398 free( m_cudaPosList ); 00399 m_cudaPosList = NULL; 00400 } 00401 if ( m_cudaPrevPosList ) { 00402 free( m_cudaPrevPosList ); 00403 m_cudaPrevPosList = NULL; 00404 } 00405 if ( m_cudaOriList ) { 00406 free( m_cudaOriList ); 00407 m_cudaOriList = NULL; 00408 } 00409 if ( m_cudaPrevOriList ) { 00410 free( m_cudaPrevOriList ); 00411 m_cudaPrevOriList = NULL; 00412 } 00413 if ( m_cudaIntForceList ) { 00414 free( m_cudaIntForceList ); 00415 m_cudaIntForceList = NULL; 00416 } 00417 if ( m_cudaExtForceList ) { 00418 free( m_cudaExtForceList ); 00419 m_cudaExtForceList = NULL; 00420 } 00421 00422 if ( m_cudaID != 0 ) { 00423 TAPs::CUDA::ClearDataForElasticRodModel( m_cudaID ); 00424 } 00425 } 00426 00427 00428 template <typename T> 00429 void ElasticRod_CompByCUDA<T>::CopyPosToMem () 00430 { 00431 if ( m_cudaPosList ) { 00432 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00433 int p = 0; 00434 int C = *m_pIdxCurr; 00435 while ( node != m_pNodeList->end() ) { 00436 m_cudaPosList[p++] = node->Centerline[C].GetPosition()[0]; 00437 m_cudaPosList[p++] = node->Centerline[C].GetPosition()[1]; 00438 m_cudaPosList[p++] = node->Centerline[C].GetPosition()[2]; 00439 //if ( p != 0 ) { 00440 m_cudaPosList[p++] = 1; 00441 //} 00442 //else { 00443 // m_cudaPosList[p++] = 0; 00444 //} 00445 ++node; 00446 } 00447 } 00448 } 00449 00450 00451 template <typename T> 00452 void ElasticRod_CompByCUDA<T>::CopyPrevPosToMem () 00453 { 00454 if ( m_cudaPrevPosList ) { 00455 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00456 int p = 0; 00457 int P = *m_pIdxNext; 00458 while ( node != m_pNodeList->end() ) { 00459 m_cudaPrevPosList[p++] = node->Centerline[P].GetPosition()[0]; 00460 m_cudaPrevPosList[p++] = node->Centerline[P].GetPosition()[1]; 00461 m_cudaPrevPosList[p++] = node->Centerline[P].GetPosition()[2]; 00462 //if ( p != 0 ) { 00463 m_cudaPrevPosList[p++] = 1; 00464 //} 00465 //else { 00466 // m_cudaPrevPosList[p++] = 0; 00467 //} 00468 ++node; 00469 } 00470 } 00471 } 00472 00473 00474 template <typename T> 00475 void ElasticRod_CompByCUDA<T>::CopyOriToMem () 00476 { 00477 if ( m_cudaOriList ) { 00478 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00479 int p = 0; 00480 int C = *m_pIdxCurr; 00481 while ( node != m_pNodeList->end() ) { 00482 m_cudaOriList[p++] = node->Orientation[C].GetR(); 00483 m_cudaOriList[p++] = node->Orientation[C].GetI(); 00484 m_cudaOriList[p++] = node->Orientation[C].GetJ(); 00485 m_cudaOriList[p++] = node->Orientation[C].GetK(); 00486 ++node; 00487 } 00488 } 00489 } 00490 00491 00492 template <typename T> 00493 void ElasticRod_CompByCUDA<T>::CopyPrevOriToMem () 00494 { 00495 if ( m_cudaOriList ) { 00496 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00497 int p = 0; 00498 int P = *m_pIdxNext; 00499 while ( node != m_pNodeList->end() ) { 00500 m_cudaPrevOriList[p++] = node->Orientation[P].GetR(); 00501 m_cudaPrevOriList[p++] = node->Orientation[P].GetI(); 00502 m_cudaPrevOriList[p++] = node->Orientation[P].GetJ(); 00503 m_cudaPrevOriList[p++] = node->Orientation[P].GetK(); 00504 ++node; 00505 } 00506 } 00507 } 00508 00509 00510 template <typename T> 00511 void ElasticRod_CompByCUDA<T>::CopyIntForceToMem () 00512 { 00513 if ( m_cudaIntForceList ) { 00514 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00515 int p = 0; 00516 int C = *m_pIdxCurr; 00517 while ( node != m_pNodeList->end() ) { 00518 m_cudaIntForceList[p++] = node->Centerline[C].GetForce()[0]; 00519 m_cudaIntForceList[p++] = node->Centerline[C].GetForce()[1]; 00520 m_cudaIntForceList[p++] = node->Centerline[C].GetForce()[2]; 00521 m_cudaIntForceList[p++] = 1; 00522 ++node; 00523 } 00524 } 00525 } 00526 00527 00528 template <typename T> 00529 void ElasticRod_CompByCUDA<T>::CopyExtForceToMem () 00530 { 00531 if ( m_cudaExtForceList ) { 00532 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00533 int p = 0; 00534 int C = *m_pIdxCurr; 00535 while ( node != m_pNodeList->end() ) { 00536 m_cudaExtForceList[p++] = node->ExternalForce[0]; 00537 m_cudaExtForceList[p++] = node->ExternalForce[1]; 00538 m_cudaExtForceList[p++] = node->ExternalForce[2]; 00539 m_cudaExtForceList[p++] = 1; 00540 ++node; 00541 } 00542 } 00543 } 00544 00545 00546 template <typename T> 00547 void ElasticRod_CompByCUDA<T>::CopyMemToPos () 00548 { 00549 if ( m_cudaPosList ) { 00550 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00551 int p = 0; 00552 int C = *m_pIdxCurr; 00553 while ( node != m_pNodeList->end() ) { 00554 node->Centerline[C].SetPosition( m_cudaPosList[p], m_cudaPosList[p+1], m_cudaPosList[p+2] ); 00555 p += 4; 00556 ++node; 00557 } 00558 } 00559 } 00560 00561 00562 template <typename T> 00563 void ElasticRod_CompByCUDA<T>::CopyMemToPrevPos () 00564 { 00565 if ( m_cudaPrevPosList ) { 00566 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00567 int p = 0; 00568 int P = *m_pIdxNext; 00569 while ( node != m_pNodeList->end() ) { 00570 node->Centerline[P].SetPosition( m_cudaPrevPosList[p], m_cudaPrevPosList[p+1], m_cudaPrevPosList[p+2] ); 00571 p += 4; 00572 ++node; 00573 } 00574 } 00575 } 00576 00577 00578 template <typename T> 00579 void ElasticRod_CompByCUDA<T>::CopyMemToOri () 00580 { 00581 if ( m_cudaOriList ) { 00582 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00583 int p = 0; 00584 int C = *m_pIdxCurr; 00585 while ( node != m_pNodeList->end() ) { 00586 node->Orientation[C].SetR( m_cudaOriList[p++] ); 00587 node->Orientation[C].SetI( m_cudaOriList[p++] ); 00588 node->Orientation[C].SetJ( m_cudaOriList[p++] ); 00589 node->Orientation[C].SetK( m_cudaOriList[p++] ); 00590 ++node; 00591 } 00592 } 00593 } 00594 00595 00596 template <typename T> 00597 void ElasticRod_CompByCUDA<T>::CopyMemToPrevOri () 00598 { 00599 if ( m_cudaOriList ) { 00600 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00601 int p = 0; 00602 int P = *m_pIdxNext; 00603 while ( node != m_pNodeList->end() ) { 00604 node->Orientation[P].SetR( m_cudaPrevOriList[p++] ); 00605 node->Orientation[P].SetI( m_cudaPrevOriList[p++] ); 00606 node->Orientation[P].SetJ( m_cudaPrevOriList[p++] ); 00607 node->Orientation[P].SetK( m_cudaPrevOriList[p++] ); 00608 ++node; 00609 } 00610 } 00611 } 00612 00613 00614 template <typename T> 00615 void ElasticRod_CompByCUDA<T>::CopyMemToExtForce () 00616 { 00617 if ( m_cudaExtForceList ) { 00618 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00619 int p = 0; 00620 int C = *m_pIdxCurr; 00621 while ( node != m_pNodeList->end() ) { 00622 node->ExternalForce[0] = m_cudaExtForceList[p++]; 00623 node->ExternalForce[1] = m_cudaExtForceList[p++]; 00624 node->ExternalForce[2] = m_cudaExtForceList[p++]; 00625 ++p; 00626 ++node; 00627 } 00628 } 00629 } 00630 00631 00632 template <typename T> 00633 void ElasticRod_CompByCUDA<T>::CopyMemToIntForce () 00634 { 00635 if ( m_cudaExtForceList ) { 00636 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00637 int p = 0; 00638 int C = *m_pIdxCurr; 00639 while ( node != m_pNodeList->end() ) { 00640 node->Centerline[C].GetForce[0] = m_cudaIntForceList[p++]; 00641 node->Centerline[C].GetForce[1] = m_cudaIntForceList[p++]; 00642 node->Centerline[C].GetForce[2] = m_cudaIntForceList[p++]; 00643 ++p; 00644 ++node; 00645 } 00646 } 00647 } 00648 00649 00650 // Initialization function for utilizing CUDA's Page-Locked Host Memory 00651 template <typename T> 00652 void ElasticRod_CompByCUDA<T>::InitPLHM () throw (...) 00653 { 00654 CleanPLHM(); 00655 00656 // All of these pointers will be set to point to the allocated memory on host by cudaHostAlloc function calls 00657 // called by TAPs::CUDA::InitailizeDataForPLHMElasticRodModel function 00658 // m_cudaPosList, //!< list of centerlines' position 00659 // m_cudaPrevPosList, //!< list of centerlines' previous position 00660 // m_cudaOriList, //!< list of orientaions 00661 // m_cudaPrevOriList, //!< list of previous orientations 00662 // m_cudaIntForceList, //!< list of internal forces 00663 // m_cudaExtForceList //!< list of external forces 00664 TAPs::CUDA::InitailizeDataForPLHMElasticRodModel( 00665 m_cudaID, 00666 m_pParameters->NumOfNodes, 00667 m_cudaPosList, 00668 m_cudaPrevPosList, 00669 m_cudaOriList, 00670 m_cudaPrevOriList, 00671 m_cudaIntForceList, 00672 m_cudaExtForceList 00673 ); 00674 00675 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00676 int p = 0; 00677 int C = *m_pIdxCurr; 00678 int P = *m_pIdxNext; 00679 while ( node != m_pNodeList->end() ) { 00680 // x 00681 m_cudaPosList[p] = node->Centerline[C].GetPosition()[0]; 00682 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[0]; 00683 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[0]; 00684 m_cudaExtForceList[p] = node->ExternalForce[0]; 00685 m_cudaOriList[p] = node->Orientation[C].GetR(); 00686 m_cudaPrevOriList[p++] = node->Orientation[P].GetR(); 00687 // y 00688 m_cudaPosList[p] = node->Centerline[C].GetPosition()[1]; 00689 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[1]; 00690 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[1]; 00691 m_cudaExtForceList[p] = node->ExternalForce[1]; 00692 m_cudaOriList[p] = node->Orientation[C].GetI(); 00693 m_cudaPrevOriList[p++] = node->Orientation[P].GetI(); 00694 // z 00695 m_cudaPosList[p] = node->Centerline[C].GetPosition()[2]; 00696 m_cudaPrevPosList[p] = node->Centerline[P].GetPosition()[2]; 00697 m_cudaIntForceList[p] = node->Centerline[C].GetForce()[2]; 00698 m_cudaExtForceList[p] = node->ExternalForce[2]; 00699 m_cudaOriList[p] = node->Orientation[C].GetJ(); 00700 m_cudaPrevOriList[p++] = node->Orientation[P].GetJ(); 00701 // w 00702 m_cudaPosList[p] = 1; 00703 m_cudaPrevPosList[p] = 1; 00704 m_cudaIntForceList[p] = 1; 00705 m_cudaExtForceList[p] = 1; 00706 m_cudaOriList[p] = node->Orientation[C].GetK(); 00707 m_cudaPrevOriList[p++] = node->Orientation[P].GetK(); 00708 ++node; 00709 } 00710 } 00711 00712 00713 // Clean function for utilizing CUDA's Page-Locked Host Memory 00714 template <typename T> 00715 void ElasticRod_CompByCUDA<T>::CleanPLHM () 00716 { 00717 if ( m_cudaID != 0 ) { 00718 TAPs::CUDA::ClearDataForPLHMElasticRodModel( m_cudaID ); 00719 } 00720 } 00721 00722 00723 template <typename T> 00724 void ElasticRod_CompByCUDA<T>::CopyMemPLHMToPos () 00725 { 00726 if ( m_cudaPrevPosList ) { 00727 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00728 int p = 0; 00729 int P = *m_pIdxNext; 00730 while ( node != m_pNodeList->end() ) { 00731 node->Centerline[P].SetPosition( m_cudaPosList[p], m_cudaPosList[p+1], m_cudaPosList[p+2] ); 00732 p += 4; 00733 ++node; 00734 } 00735 } 00736 } 00737 00738 00739 template <typename T> 00740 void ElasticRod_CompByCUDA<T>::CopyMemPLHMToOri () 00741 { 00742 if ( m_cudaOriList ) { 00743 SetOfElasticRodNodes::iterator node = m_pNodeList->begin(); 00744 int p = 0; 00745 int P = *m_pIdxNext; 00746 while ( node != m_pNodeList->end() ) { 00747 node->Orientation[P].SetR( m_cudaOriList[p++] ); 00748 node->Orientation[P].SetI( m_cudaOriList[p++] ); 00749 node->Orientation[P].SetJ( m_cudaOriList[p++] ); 00750 node->Orientation[P].SetK( m_cudaOriList[p++] ); 00751 ++node; 00752 } 00753 } 00754 } 00755 00756 00757 template <typename T> 00758 void ElasticRod_CompByCUDA<T>::SwapBuffers () 00759 { 00760 float * tmpPtr = m_cudaPosList; 00761 m_cudaPosList = m_cudaPrevPosList; 00762 m_cudaPrevPosList = tmpPtr; 00763 00764 tmpPtr = m_cudaOriList; 00765 m_cudaOriList = m_cudaPrevOriList; 00766 m_cudaPrevOriList = tmpPtr; 00767 } 00768 00769 //============================================================================= 00770 END_NAMESPACE_TAPs 00771 //----------------------------------------------------------------------------- 00772 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00773 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----