summaryrefslogtreecommitdiff
path: root/openEMS/FDTD/operator_mpi.cpp
blob: 432f5d7e4fb61253972ea15c64f98d9df79deafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
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
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
206
207
208
209
/*
*	Copyright (C) 2011 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
*	This program is free software: you can redistribute it and/or modify
*	it under the terms of the GNU 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 General Public License for more details.
*
*	You should have received a copy of the GNU General Public License
*	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "operator_mpi.h"
#include "operator_sse_compressed.h"
#include "engine_sse_compressed.h"
#include "engine_mpi.h"
#include "extensions/operator_extension.h"
#include "tools/array_ops.h"
#include "tools/useful.h"
#include "mpi.h"

Operator_MPI* Operator_MPI::New()
{
	cout << "Create FDTD operator (compressed SSE + MPI)" << endl;
	Operator_MPI* op = new Operator_MPI();
	op->Init();
	return op;
}

Operator_MPI::Operator_MPI() : Operator_SSE_Compressed()
{
	m_NumProc = MPI::COMM_WORLD.Get_size();

	//enabled only if more than one process is active
	m_MPI_Enabled = m_NumProc>1;
}

Operator_MPI::~Operator_MPI()
{
	Delete();
}

double Operator_MPI::CalcTimestep()
{
	double ret = Operator::CalcTimestep();

	if (!m_MPI_Enabled)
		return ret;

	double local_dT = dT;
	//find the smallest time-step requestes by all processings
	MPI_Reduce(&local_dT, &dT, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
	//send the smallest time-step to all
	MPI_Bcast(&dT, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);

	return ret;
}

void Operator_MPI::SetBoundaryCondition(int* BCs)
{
	if (!m_MPI_Enabled)
		return Operator_SSE_Compressed::SetBoundaryCondition(BCs);

	//set boundary conditions on MPI interfaces to PEC, ApplyElectricBC will handle proper interface handling...
	for (int n=0;n<3;++n)
	{
		if (m_NeighborUp[n]>=0)
			BCs[2*n+1] = 0;
		if (m_NeighborDown[n]>=0)
			BCs[2*n] = 0;
	}
	Operator_SSE_Compressed::SetBoundaryCondition(BCs);
}

Engine* Operator_MPI::CreateEngine()
{
	if (m_MPI_Enabled)
		m_Engine = Engine_MPI::New(this);
	else
		m_Engine = Engine_SSE_Compressed::New(this);
	return m_Engine;
}

void Operator_MPI::SetNeighborUp(int ny, int id)
{
	if ((ny<0) || (ny>2))
		return;
	m_NeighborUp[ny]=id;
}

void Operator_MPI::SetNeighborDown(int ny, int id)
{
	if ((ny<0) || (ny>2))
		return;
	m_NeighborDown[ny]=id;
}

void Operator_MPI::Init()
{
	Operator_SSE_Compressed::Init();

	m_MyTag = 0;

	for (int i=0;i<3;++i)
	{
		m_NeighborUp[i]=-1;
		m_NeighborDown[i]=-1;
		m_OrigDiscLines[i]=NULL;
	}

	m_ProcTable = NULL;
	m_SplitNumber[0]=0;
	m_SplitNumber[1]=0;
	m_SplitNumber[2]=0;

	int  namelen;
	m_NumProc = MPI::COMM_WORLD.Get_size();
	m_MyID = MPI::COMM_WORLD.Get_rank();

	m_Processor_Name = new char[MPI_MAX_PROCESSOR_NAME];
	MPI::Get_processor_name(m_Processor_Name,namelen);

	if ((m_MPI_Enabled) && (g_settings.GetVerboseLevel()>0))
		cerr << "Operator_MPI::Init(): Running on " << m_Processor_Name << endl;
}

void Operator_MPI::Delete()
{
	delete[] m_Processor_Name;
	m_Processor_Name = NULL;
	for (int i=0;i<3;++i)
	{
		delete[] m_OrigDiscLines[i];
		m_OrigDiscLines[i] = NULL;
	}
	Delete3DArray(m_ProcTable,m_SplitNumber);
	m_ProcTable=NULL;
}

void Operator_MPI::Reset()
{
	Delete();
	Operator_SSE_Compressed::Reset();
}

void Operator_MPI::SetOriginalMesh(CSRectGrid* orig_Mesh)
{
	for (int n=0;n<3;++n)
	{
		delete[] m_OrigDiscLines[n];
		m_OrigDiscLines[n] = orig_Mesh->GetLines(n,NULL,m_OrigNumLines[n]);
	}
}

unsigned int Operator_MPI::GetNumberOfLines(int ny, bool fullMesh) const
{
	if (fullMesh)
		return Operator_SSE_Compressed::GetNumberOfLines(ny,fullMesh);

	if ((!m_MPI_Enabled) || (m_NeighborUp[ny]<0))
		return Operator_SSE_Compressed::GetNumberOfLines(ny,fullMesh);

	return Operator_SSE_Compressed::GetNumberOfLines(ny)-1;
}

void Operator_MPI::AddExtension(Operator_Extension* op_ext)
{
	if (m_MPI_Enabled==false)
		return Operator_SSE_Compressed::AddExtension(op_ext);

	if (op_ext->IsMPISave())
		Operator_SSE_Compressed::AddExtension(op_ext);
	else
	{
		cerr << "Operator_MPI::AddExtension: Warning: Operator extension \"" << op_ext->GetExtensionName() << "\" is not compatible with MPI!! skipping...!" << endl;
		delete op_ext;
	}
}


string Operator_MPI::PrependRank(string name)
{
	stringstream out_name;
	if (m_MPI_Enabled)
		out_name << "ID" << m_MyID << "_" << name;
	else
		out_name << name;
	return out_name.str();
}

void Operator_MPI::DumpOperator2File(string filename)
{
	Operator_SSE_Compressed::DumpOperator2File(PrependRank(filename));
}

void Operator_MPI::DumpMaterial2File(string filename)
{
	Operator_SSE_Compressed::DumpMaterial2File(PrependRank(filename));
}

void Operator_MPI::DumpPEC2File(string filename , unsigned int *range)
{
	Operator_SSE_Compressed::DumpPEC2File(PrependRank(filename), range);
}