ewNode.h 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

25 26 27 28
#ifndef EW_NODE_H
#define EW_NODE_H

#include <stdlib.h>
29 30
#include <string.h>
#include "easywave.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44

#define CHKRET( x ) if( (x) == NULL ) return 1;

typedef float Float[MAX_VARS_PER_NODE];

class CNode {

public:
	virtual ~CNode() {};
	virtual float& operator()( const int idx1, const int idx2 ) = 0;
	virtual int mallocMem() = 0;
	virtual int copyToGPU() = 0;
	virtual int copyFromGPU() = 0;
	virtual int copyIntermediate() = 0;
45
	virtual int copyPOIs() = 0;
46 47
	virtual int freeMem() = 0;
	virtual int run() = 0;
48 49

	virtual void initMemory( int index, int val ) = 0;
50 51 52 53 54 55 56 57 58 59 60 61 62
};

class CStructNode : public CNode {

public:
	Float *node;

public:
	inline float& operator()( const int idx1, const int idx2 ) {

		return node[idx1][idx2];
	}

63 64 65 66 67 68 69 70 71 72 73
	void initMemory( int index, int val ) {

		int m;
		for( int i=1; i<=NLon; i++ ) {
		  for( int j=1; j<=NLat; j++ ) {
				m = idx(j,i);
				this->operator ()(m, index) = val;
		  }
		}
	}

74 75 76 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 108 109 110
	int mallocMem() {

		CHKRET( this->node = (Float*) malloc( sizeof(Float) * NLon * NLat) );

		/* FIXME: remove global variables */
		CHKRET( R6 = (float*) malloc( sizeof(float) * (NLat+1) ) );
		CHKRET( C1 = (float*) malloc( sizeof(float) * (NLon+1) ) );
		CHKRET( C3 = (float*) malloc( sizeof(float) * (NLon+1) ) );
		CHKRET( C2 = (float*) malloc( sizeof(float) * (NLat+1) ) );
		CHKRET( C4 = (float*) malloc( sizeof(float) * (NLat+1) ) );

		return 0;
	}

	int freeMem() {

		free( this->node );
		free( R6 );
		free( C1 );
		free( C2 );
		free( C3 );
		free( C4 );

		return 0;
	}

	int run() {

		if( Par.coriolis )
		  return ewStepCor();

		return ewStep();
	}

	int copyToGPU() { return 0; }
	int copyFromGPU() {	return 0; }
	int copyIntermediate() { return 0; }
111
	int copyPOIs() { return 0; }
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
};

#pragma pack(push, 1)
class CArrayNode : public CNode {

protected:
	float *d;
	float *h;
	float *hMax;
	float *fM;
	float *fN;
	float *cR1;
	float *cR2;
	float *cR3;
	float *cR4;
	float *cR5;
	float *tArr;
	float *topo;

public:
	virtual float& operator()( const int idx1, const int idx2 ) {

		return ((float**)&d)[idx2][idx1];
	}

138 139 140 141 142 143 144 145
	void *getBuf( int idx ) { return ((float**)&d)[idx]; }

	virtual void initMemory( int index, int val ) {

		memset( getBuf(index), 0, NLat * NLon * sizeof(float) );

	}

146 147 148 149 150 151 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	virtual int mallocMem() {

		CHKRET( this->d = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->h = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->hMax = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->fM = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->fN = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->cR1 = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->cR2 = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->cR3 = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->cR4 = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->cR5 = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->tArr = (float*) malloc( sizeof(float) * NLon * NLat ) );
		CHKRET( this->topo = (float*) malloc( sizeof(float) * NLon * NLat ) );

		/* FIXME: remove global variables */
		CHKRET( R6 = (float*) malloc( sizeof(float) * (NLat+1) ) );
		CHKRET( C1 = (float*) malloc( sizeof(float) * (NLon+1) ) );
		CHKRET( C3 = (float*) malloc( sizeof(float) * (NLon+1) ) );
		CHKRET( C2 = (float*) malloc( sizeof(float) * (NLat+1) ) );
		CHKRET( C4 = (float*) malloc( sizeof(float) * (NLat+1) ) );

		return 0;
	}

	virtual int freeMem() {

		free( this->d );
		free( this->h );
		free( this->hMax );
		free( this->fM );
		free( this->fN );
		free( this->cR1 );
		free( this->cR2 );
		free( this->cR3 );
		free( this->cR4 );
		free( this->cR5 );
		free( this->tArr );
		free( this->topo );

		free( R6 );
		free( C1 );
		free( C2 );
		free( C3 );
		free( C4 );

		return 0;
	}

	virtual int run() {

		if( Par.coriolis )
		  return ewStepCor();

		return ewStep();
	}

	virtual int copyToGPU() { return 0; }
	virtual int copyFromGPU() {	return 0; }
	virtual int copyIntermediate() { return 0; }
206
	virtual int copyPOIs() { return 0; }
207 208 209 210 211

};
#pragma pack(pop)

#endif /* EW_NODE_H */