PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Examples/LBM/main_tang.cpp

https://github.com/rrnewton/PochoirMods
C++ | 245 lines | 196 code | 37 blank | 12 comment | 38 complexity | a569b590d2c3f02fe6d11d25796957d7 MD5 | raw file
  1. /* $Id: main.c,v 1.4 2004/04/21 04:23:43 pohlt Exp $ */
  2. /*############################################################################*/
  3. #include <pochoir.hpp>
  4. #include "lbm_tang.h"
  5. #include "main_tang.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #if defined(SPEC_CPU)
  9. # include <time.h>
  10. #else
  11. # include <sys/times.h>
  12. # include <unistd.h>
  13. #endif
  14. #include <sys/stat.h>
  15. int SIZE_X, SIZE_Y, SIZE_Z;
  16. /*############################################################################*/
  17. int main( int nArgs, char* arg[] ) {
  18. MAIN_Param param;
  19. #if !defined(SPEC_CPU)
  20. MAIN_Time time;
  21. #endif
  22. int t;
  23. MAIN_parseCommandLine( nArgs, arg, &param );
  24. MAIN_printInfo( &param );
  25. Pochoir_Shape_3D lbm_shape[] = {{0,0,0,0},
  26. {-1,0,0,0}, {-1,0,1,0}, {-1,0,-1,0},
  27. {-1,1,0,0}, {-1,-1,0,0}, {-1,0,0,1},
  28. {-1,0,0,-1}, {-1,1,1,0}, {-1,-1,1,0},
  29. {-1,1,-1,0}, {-1,-1,-1,0}, {-1,0,1,1},
  30. {-1,0,1,-1}, {-1,0,-1,1}, {-1,0,-1,-1},
  31. {-1,1,0,1}, {-1,1,0,-1}, {-1,-1,0,1},
  32. {-1,-1,0,-1}};
  33. Pochoir_3D lbm(lbm_shape);
  34. /* z ranges from -2 to SIZE_Z+2 */
  35. Pochoir_Array_3D(PoCellEntry) pa(SIZE_Z+2*MARGIN_Z, SIZE_Y, SIZE_X);
  36. Pochoir_Domain X(0, SIZE_X), Y(0, SIZE_Y), Z(0+MARGIN_Z, SIZE_Z+MARGIN_Z);
  37. lbm.Register_Array(pa);
  38. lbm.Register_Domain(Z, Y, X);
  39. #if 1
  40. Pochoir_Kernel_3D(lbm_kernel_channel, t, z, y, x)
  41. LBM_handleInOutFlow_macro( pa, t, z, y, x );
  42. LBM_performStreamCollide_macro( pa, t, z, y, x );
  43. Pochoir_Kernel_End
  44. Pochoir_Kernel_3D(lbm_kernel_ldc, t, z, y, x)
  45. LBM_performStreamCollide_macro( pa, t, z, y, x );
  46. Pochoir_Kernel_End
  47. #endif
  48. MAIN_initialize( &param, pa );
  49. #if !defined(SPEC_CPU)
  50. MAIN_startClock( &time );
  51. #endif
  52. #if 1
  53. if (param.simType == CHANNEL) {
  54. printf("CHANNEL\n");
  55. for( t = 1; t <= param.nTimeSteps; ++t ) {
  56. cilk_for (int z = 0 + MARGIN_Z; z < SIZE_Z + MARGIN_Z; ++z) {
  57. for (int y = 0; y < SIZE_Y; ++y) {
  58. for (int x = 0; x < SIZE_X; ++x) {
  59. LBM_handleInOutFlow( pa, t, z, y, x );
  60. LBM_performStreamCollide( pa, t, z, y, x );
  61. }
  62. }
  63. }
  64. if( (t & 63) == 0 ) {
  65. printf( "timestep: %i\n", t );
  66. LBM_showGridStatistics( pa, t );
  67. }
  68. }
  69. } else {
  70. printf("LDC\n");
  71. for( t = 1; t <= param.nTimeSteps; ++t ) {
  72. cilk_for (int z = 0 + MARGIN_Z; z < SIZE_Z + MARGIN_Z; ++z) {
  73. for (int y = 0; y < SIZE_Y; ++y) {
  74. for (int x = 0; x < SIZE_X; ++x) {
  75. LBM_performStreamCollide( pa, t, z, y, x );
  76. }
  77. }
  78. }
  79. if( (t & 63) == 0 ) {
  80. printf( "timestep: %i\n", t );
  81. LBM_showGridStatistics( pa, t );
  82. }
  83. }
  84. }
  85. #else
  86. t = param.nTimeSteps;
  87. if (param.simType == CHANNEL) {
  88. lbm.Run(t, lbm_kernel_channel);
  89. } else {
  90. lbm.Run(t, lbm_kernel_ldc);
  91. }
  92. #endif
  93. #if !defined(SPEC_CPU)
  94. MAIN_stopClock( &time, &param );
  95. #endif
  96. MAIN_finalize( &param, pa, t );
  97. return 0;
  98. }
  99. /*############################################################################*/
  100. void MAIN_parseCommandLine( int nArgs, char* arg[], MAIN_Param* param ) {
  101. struct stat fileStat;
  102. if( nArgs < 8 ) {
  103. printf("nArgs = %d\n", nArgs);
  104. printf( "syntax: lbm <time steps> <SIZE_X> <SIZE_Y> <SIZE_Z> <result file> <0: nil, 1: cmp, 2: str> <0: ldc, 1: channel flow> [<obstacle file>]\n" );
  105. exit( 1 );
  106. }
  107. param->nTimeSteps = atoi( arg[1] );
  108. SIZE_X = atoi( arg[2] );
  109. SIZE_Y = atoi( arg[3] );
  110. SIZE_Z = atoi( arg[4] );
  111. param->resultFilename = arg[5];
  112. param->action = (MAIN_Action) atoi( arg[6] );
  113. param->simType = (MAIN_SimType) atoi( arg[7] );
  114. if( nArgs == 9 ) {
  115. param->obstacleFilename = arg[8];
  116. if( stat( param->obstacleFilename, &fileStat ) != 0 ) {
  117. printf( "MAIN_parseCommandLine: cannot stat obstacle file '%s'\n",
  118. param->obstacleFilename );
  119. exit( 1 );
  120. }
  121. if( fileStat.st_size != SIZE_X*SIZE_Y*SIZE_Z+(SIZE_Y+1)*SIZE_Z ) {
  122. printf( "MAIN_parseCommandLine:\n"
  123. "\tsize of file '%s' is %i bytes\n"
  124. "\texpected size is %i bytes\n",
  125. param->obstacleFilename, (int) fileStat.st_size,
  126. SIZE_X*SIZE_Y*SIZE_Z+(SIZE_Y+1)*SIZE_Z );
  127. exit( 1 );
  128. }
  129. }
  130. else param->obstacleFilename = NULL;
  131. if( param->action == COMPARE &&
  132. stat( param->resultFilename, &fileStat ) != 0 ) {
  133. printf( "MAIN_parseCommandLine: cannot stat result file '%s'\n",
  134. param->resultFilename );
  135. exit( 1 );
  136. }
  137. }
  138. /*############################################################################*/
  139. void MAIN_printInfo( const MAIN_Param* param ) {
  140. const char actionString[3][32] = {"nothing", "compare", "store"};
  141. const char simTypeString[3][32] = {"lid-driven cavity", "channel flow"};
  142. printf( "MAIN_printInfo:\n"
  143. "\tgrid size : %i x %i x %i = %.2f * 10^6 Cells\n"
  144. "\tnTimeSteps : %i\n"
  145. "\tresult file : %s\n"
  146. "\taction : %s\n"
  147. "\tsimulation type: %s\n"
  148. "\tobstacle file : %s\n\n",
  149. SIZE_X, SIZE_Y, SIZE_Z, 1e-6*SIZE_X*SIZE_Y*SIZE_Z,
  150. param->nTimeSteps, param->resultFilename,
  151. actionString[param->action], simTypeString[param->simType],
  152. (param->obstacleFilename == NULL) ? "<none>" :
  153. param->obstacleFilename );
  154. }
  155. /*############################################################################*/
  156. void MAIN_initialize( const MAIN_Param* param, Pochoir_Array_3D(PoCellEntry) & pa ) {
  157. // LBM_allocateGrid( (MY_TYPE**) &srcGrid );
  158. // LBM_allocateGrid( (MY_TYPE**) &dstGrid );
  159. LBM_initializeGrid( pa, 0 );
  160. LBM_initializeGrid( pa, 1 );
  161. if( param->obstacleFilename != NULL ) {
  162. LBM_loadObstacleFile( pa, 0, param->obstacleFilename );
  163. LBM_loadObstacleFile( pa, 1, param->obstacleFilename );
  164. } else {
  165. LBM_loadRandomObstacle(pa, 0);
  166. LBM_loadRandomObstacle(pa, 1);
  167. }
  168. if( param->simType == CHANNEL ) {
  169. LBM_initializeSpecialCellsForChannel( pa, 0 );
  170. LBM_initializeSpecialCellsForChannel( pa, 1 );
  171. }
  172. else {
  173. LBM_initializeSpecialCellsForLDC( pa, 0 );
  174. LBM_initializeSpecialCellsForLDC( pa, 1 );
  175. }
  176. LBM_showGridStatistics( pa, 0 );
  177. }
  178. /*############################################################################*/
  179. void MAIN_finalize( const MAIN_Param* param, Pochoir_Array_3D(PoCellEntry) & pa, const int t ) {
  180. printf("MAIN_finalize: srcGrid:\n");
  181. LBM_showGridStatistics( pa, t-1 );
  182. printf("MAIN_finalize: dstGrid:\n");
  183. LBM_showGridStatistics( pa, t );
  184. if( param->action == COMPARE )
  185. LBM_compareVelocityField( pa, t-1, param->resultFilename, TRUE );
  186. if( param->action == STORE )
  187. LBM_storeVelocityField( pa, t-1, param->resultFilename, TRUE );
  188. }
  189. #if !defined(SPEC_CPU)
  190. /*############################################################################*/
  191. void MAIN_startClock( MAIN_Time* time ) {
  192. time->timeScale = 1.0 / sysconf( _SC_CLK_TCK );
  193. time->tickStart = times( &(time->timeStart) );
  194. }
  195. /*############################################################################*/
  196. void MAIN_stopClock( MAIN_Time* time, const MAIN_Param* param ) {
  197. time->tickStop = times( &(time->timeStop) );
  198. printf( "MAIN_stopClock:\n"
  199. "\tusr: %7.2f sys: %7.2f tot: %7.2f wct: %7.2f MLUPS: %5.2f\n\n",
  200. (time->timeStop.tms_utime - time->timeStart.tms_utime) * time->timeScale,
  201. (time->timeStop.tms_stime - time->timeStart.tms_stime) * time->timeScale,
  202. (time->timeStop.tms_utime - time->timeStart.tms_utime +
  203. time->timeStop.tms_stime - time->timeStart.tms_stime) * time->timeScale,
  204. (time->tickStop - time->tickStart ) * time->timeScale,
  205. 1.0e-6 * SIZE_X * SIZE_Y * SIZE_Z * param->nTimeSteps /
  206. (time->tickStop - time->tickStart ) / time->timeScale );
  207. }
  208. #endif