EasyWave.cu 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * EasyWave - A realtime tsunami simulation program with GPU support.
 * Copyright (C) 2014  Andrey Babeyko, Johannes Spazier
 * GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
 *
 * Parts of this program (especially the GPU extension) were developed
 * within the context of the following publicly funded project:
 * - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
 *   (http://www.tridec-online.eu)
 *
Johannes Spazier's avatar
Johannes Spazier committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence"),
 * complemented with the following provision: For the scientific transparency
 * and verification of results obtained and communicated to the public after
 * using a modified version of the work, You (as the recipient of the source
 * code and author of this modified version, used to produce the published
 * results in scientific communications) commit to make this modified source
 * code available in a repository that is easily and freely accessible for a
 * duration of five years after the communication of the obtained results.
 * 
 * You may not use this work except in compliance with the Licence.
 * 
 * You may obtain a copy of the Licence at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
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
 */

#define HEADER "\neasyWave ver.2013-04-11\n"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <utilits.h>
#include "easywave.h"

#include "ewGpuNode.cuh"

CNode *gNode;

double diff(timespec start, timespec end) {

	timespec temp;
	if ((end.tv_nsec-start.tv_nsec)<0) {
		temp.tv_sec = end.tv_sec-start.tv_sec-1;
		temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
	} else {
		temp.tv_sec = end.tv_sec-start.tv_sec;
		temp.tv_nsec = end.tv_nsec-start.tv_nsec;
	}

	return (double)((double)temp.tv_nsec / 1000000000.0 + (double)temp.tv_sec);
}

int commandLineHelp( void );

int main( int argc, char **argv )
{
  char buf[1024];
  int ierr,argn,elapsed;
  int lastProgress,lastPropagation,lastDump;
  int loop;

  printf(HEADER);
  Err.setchannel(MSG_OUTFILE);

  // Read parameters from command line and use default
  ierr = ewParam( argc, argv ); if(ierr) return commandLineHelp();

  // Log command line
  /* FIXME: buffer overflow */
Johannes Spazier's avatar
Johannes Spazier committed
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
/*
 * EasyWave - A realtime tsunami simulation program with GPU support.
 * Copyright (C) 2014  Andrey Babeyko, Johannes Spazier
 * GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
 *
 * Parts of this program (especially the GPU extension) were developed
 * within the context of the following publicly funded project:
 * - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
 *   (http://www.tridec-online.eu)
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence"),
 * complemented with the following provision: For the scientific transparency
 * and verification of results obtained and communicated to the public after
 * using a modified version of the work, You (as the recipient of the source
 * code and author of this modified version, used to produce the published
 * results in scientific communications) commit to make this modified source
 * code available in a repository that is easily and freely accessible for a
 * duration of five years after the communication of the obtained results.
 * 
 * You may not use this work except in compliance with the Licence.
 * 
 * You may obtain a copy of the Licence at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */
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
  sprintf( buf, "Command line: " );
  for( argn=1; argn<argc; argn++ ) {
    strcat( buf, " " );
    strcat( buf, argv[argn] );
  }
  Log.print( "%s", buf );

  if( Par.gpu ) {
	  gNode = new CGpuNode();
  } else {
	  gNode = new CStructNode();
	  //gNode = new CArrayNode();
  }

  CNode& Node = *gNode;

  // Read bathymetry
  ierr = ewLoadBathymetry(); if(ierr) return ierr;

  // Read points of interest
  ierr = ewLoadPOIs(); if(ierr) return ierr;

  // Init tsunami with faults or uplift-grid
  ierr = ewSource(); if(ierr) return ierr;
  Log.print( "Read source from %s", Par.fileSource );

  // Write model parameters into the log
  ewLogParams();

  if( Par.outPropagation ) ewStart2DOutput();

  Node.copyToGPU();

  // Main loop
  Log.print("Starting main loop...");
  printf("Starting main loop %ld msec\n", clock()/(CLOCKS_PER_SEC/1000) );

  timespec start, end;
  clock_gettime(CLOCK_MONOTONIC, &start);

  for( Par.time=0,loop=1,lastProgress=Par.outProgress,lastPropagation=Par.outPropagation,lastDump=0;
    Par.time<=Par.timeMax; loop++,Par.time+=Par.dt,lastProgress+=Par.dt,lastPropagation+=Par.dt ) {

	/* FIXME: check if Par.poiDt can be used for those purposes */
Johannes Spazier's avatar
Johannes Spazier committed
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
/*
 * EasyWave - A realtime tsunami simulation program with GPU support.
 * Copyright (C) 2014  Andrey Babeyko, Johannes Spazier
 * GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
 *
 * Parts of this program (especially the GPU extension) were developed
 * within the context of the following publicly funded project:
 * - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
 *   (http://www.tridec-online.eu)
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence"),
 * complemented with the following provision: For the scientific transparency
 * and verification of results obtained and communicated to the public after
 * using a modified version of the work, You (as the recipient of the source
 * code and author of this modified version, used to produce the published
 * results in scientific communications) commit to make this modified source
 * code available in a repository that is easily and freely accessible for a
 * duration of five years after the communication of the obtained results.
 * 
 * You may not use this work except in compliance with the Licence.
 * 
 * You may obtain a copy of the Licence at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */
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
    if( Par.filePOIs && Par.poiDt && ((Par.time/Par.poiDt)*Par.poiDt == Par.time) ) {
    	Node.copyPOIs();
    	ewSavePOIs();
    }

    Node.run();

    elapsed = ((int)clock())/CLOCKS_PER_SEC;

    if( Par.outProgress ) {
      if( lastProgress >= Par.outProgress ) {
        printf( "Model time = %s,   elapsed: %ld msec\n", utlTimeSplitString(Par.time), clock()/(CLOCKS_PER_SEC/1000) );
        Log.print( "Model time = %s,   elapsed: %ld msec", utlTimeSplitString(Par.time), clock()/(CLOCKS_PER_SEC/1000) );
        lastProgress = 0;
      }
    }

    if( Par.outPropagation ) {
      if( lastPropagation >= Par.outPropagation ) {
    	Node.copyIntermediate();
        ewOut2D();
        lastPropagation = 0;
      }
    }

    fflush(stdout); 

    if( Par.outDump ) {
      if( (elapsed-lastDump) >= Par.outDump ) {
    	Node.copyIntermediate();
    	/* FIXME: needs tArr as well */
Johannes Spazier's avatar
Johannes Spazier committed
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
/*
 * EasyWave - A realtime tsunami simulation program with GPU support.
 * Copyright (C) 2014  Andrey Babeyko, Johannes Spazier
 * GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
 *
 * Parts of this program (especially the GPU extension) were developed
 * within the context of the following publicly funded project:
 * - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
 *   (http://www.tridec-online.eu)
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence"),
 * complemented with the following provision: For the scientific transparency
 * and verification of results obtained and communicated to the public after
 * using a modified version of the work, You (as the recipient of the source
 * code and author of this modified version, used to produce the published
 * results in scientific communications) commit to make this modified source
 * code available in a repository that is easily and freely accessible for a
 * duration of five years after the communication of the obtained results.
 * 
 * You may not use this work except in compliance with the Licence.
 * 
 * You may obtain a copy of the Licence at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */
245 246 247 248 249 250 251 252 253 254 255
        ewDumpPOIs();
        ewDump2D();
        lastDump = elapsed;
      }
    }

  } // main loop
  clock_gettime(CLOCK_MONOTONIC, &end);
  Log.print("Finishing main loop");

  /* TODO: check if theses calls can be combined */
Johannes Spazier's avatar
Johannes Spazier committed
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
/*
 * EasyWave - A realtime tsunami simulation program with GPU support.
 * Copyright (C) 2014  Andrey Babeyko, Johannes Spazier
 * GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
 *
 * Parts of this program (especially the GPU extension) were developed
 * within the context of the following publicly funded project:
 * - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
 *   (http://www.tridec-online.eu)
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence"),
 * complemented with the following provision: For the scientific transparency
 * and verification of results obtained and communicated to the public after
 * using a modified version of the work, You (as the recipient of the source
 * code and author of this modified version, used to produce the published
 * results in scientific communications) commit to make this modified source
 * code available in a repository that is easily and freely accessible for a
 * duration of five years after the communication of the obtained results.
 * 
 * You may not use this work except in compliance with the Licence.
 * 
 * You may obtain a copy of the Licence at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  Node.copyIntermediate();
  Node.copyFromGPU();

  // Final output
  Log.print("Final dump...");
  ewDumpPOIs();
  ewDump2D();

  Node.freeMem();

  printf_v("Runtime: %.3lf\n", diff(start, end) * 1000.0);

  delete gNode;

  return 0;
}


//========================================================================
int commandLineHelp( void )
{
  printf( "Usage: easyWave  -grid ...  -source ...  -time ... [optional parameters]\n" );
  printf( "-grid ...         bathymetry in GoldenSoftware(C) GRD format (text or binary)\n" );
  printf( "-source ...       input wave either als GRD-file or file with Okada faults\n" );
  printf( "-time ...         simulation time in [min]\n" );
  printf( "Optional parameters:\n" );
  printf( "-step ...         simulation time step, default- estimated from bathymetry\n" );
  printf( "-coriolis         use Coriolis fource, default- no\n" );
  printf( "-poi ...          POIs file\n" );
  printf( "-label ...        model name, default- 'eWave'\n" );
  printf( "-progress ...     show simulation progress each ... minutes, default- 10\n" );
  printf( "-propagation ...  write wave propagation grid each ... minutes, default- 5\n" );
  printf( "-dump ...         make solution dump each ... physical seconds, default- 0\n" );
  printf( "-nolog            deactivate logging\n" );
  printf( "-poi_dt_out ...   output time step for mariograms in [sec], default- 30\n" );
  printf( "-poi_search_dist ...  in [km], default- 10\n" );
  printf( "-poi_min_depth ...    in [m], default- 1\n" );
  printf( "-poi_max_depth ...    in [m], default- 10 000\n" );
  printf( "-poi_report       enable POIs loading report, default- disabled\n" );
  printf( "-ssh0_rel ...     relative threshold for initial wave, default- 0.01\n" );
  printf( "-ssh0_abs ...     absolute threshold for initial wave in [m], default- 0\n" );
  printf( "-ssh_arrival ...  threshold for arrival times in [m], default- 0.001\n" );
  printf( "                  negative value considered as relative threshold\n" );
  printf( "-gpu              start GPU version of EasyWave (requires a CUDA capable device)\n" );
  printf( "-verbose          generate verbose output on stdout\n" );
  printf( "\nExample:\n" );
  printf( "\t easyWave -grid gebcoIndonesia.grd  -source fault.inp  -time 120\n\n" );

  return -1;
}