IPCC  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KNMatrixDebug.cpp
Go to the documentation of this file.
1 
7 #include "stdafx.h"
8 #include "KNMatrixDebug.h"
9 #include <algorithm>
10 #include "KNMPIManager.h"
11 
12 #ifdef _WIN32
13 #include <direct.h>
14 #else
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #endif
18 
19 #define DUMY_STRING_FOR_COMPLEX_SHOWING "\t\t\t"
20 
21 using namespace std;
22 bool CKNMatrixDebug::m_bShowMsg = false;
23 
25 {
26 }
27 
29 {
30 }
31 
35 void CKNMatrixDebug::ShowMsg(char *pszBuffer)
36 {
37  if (false == m_bShowMsg)
38  return;
39 #ifdef _WIN32
40 #if _MFC_VER == 0x0C00
41  OutputDebugStringA(pszBuffer);
42 #else// _MFC_VER == 0x0C00
43  printf("%s", pszBuffer);
44 #endif //_MFC_VER == 0x0C00
45 #else
46  printf("%s", pszBuffer);
47 #endif
48 }
49 
60 void CKNMatrixDebug::ShowDoubleMatrix( double *pMatrix, int m, int n, int startm, int endm, int startn, int endn, char *pszDebugMsg)
61 {
62  ShowMsg(pszDebugMsg);
63  ShowMsg("\n");
64  ShowMsg("\n");
65 
66  for (int i = startm; i < endm; i++)
67  {
68  char szRow[1024] = "";
69  for (int j = startn; j < endn; j++)
70  {
71  char szCol[1024];
72 
73  sprintf(szCol, "%20.20f ", pMatrix[i*m + j]);
74  strcat(szRow, szCol);
75  }
76  strcat(szRow, "\n");
77  ShowMsg(szRow);
78  }
79 }
90 void CKNMatrixDebug::ShowDenseMatrix(CKNMatrixOperation::CKNDMatrix *pMatrix, int startm, int endm, int startn, int endn, char *pszDebugMsg, bool bLineFeed, bool bShowImaginary)
91 {
92  ShowMsg(pszDebugMsg);
93  ShowMsg("\n");
94  ShowMsg("\n");
95 
96  for (int i = startm; i <= endm; i++)
97  {
98  char szRow[1024] = "";
99  for (int j = startn; j <= endn; j++)
100  {
101  char szCol[1024];
102  CKNComplex temppNumber = pMatrix->GetElement(i, j);
103 
104  sprintf(szCol, "%10.10f ", temppNumber.GetRealNumber());
105  strcat(szRow, szCol);
106  if (bShowImaginary)
107  {
108  sprintf(szCol, "+ %10.10f i ", temppNumber.GetImaginaryNumber());
109  strcat(szRow, szCol);
110  }
111  strcat(szRow, "\t");
112  }
113  if (bLineFeed)
114  strcat(szRow, "\n");
115  strcat(szRow, "\n");
116  ShowMsg(szRow);
117  }
118 }
119 
128 void CKNMatrixDebug::ShowCSR(CKNMatrixOperation::CKNCSR *pCSR, int startm, int endm, int startn, int endn, char *pszDebugMsg)
129 {
130  bool bExisted;
131 
132  ShowMsg(pszDebugMsg);
133  ShowMsg("\n");
134  ShowMsg("\n");
135 
136  for (unsigned int i = startm; i <= (unsigned int)endm; i++)
137  {
138  char szRow[1024] = "";
139  for (unsigned int j = startn; j <= (unsigned int)endn; j++)
140  {
141  char szCol[1024];
142  CKNComplex element = pCSR->GetElement(i, j, bExisted);
143 
144  if (bExisted)
145  sprintf(szCol, "%3.3f:%3.3f |\t\t", element.GetRealNumber(), element.GetImaginaryNumber());
146  else
147  sprintf(szCol, "%s |\t\t", DUMY_STRING_FOR_COMPLEX_SHOWING);
148  strcat(szRow, szCol);
149  }
150  strcat(szRow, "\n");
151  ShowMsg(szRow);
152  }
153 }
154 
160 {
161  bool bExisted;
162  unsigned int iMax, jMax;
163 
164 #ifdef _WIN32
165  iMax = min(10, pCSR->GetRowCount());
166  jMax = min(10, pCSR->GetColumnCount());
167 #else
168  iMax = std::min(10, (int)pCSR->GetRowCount());
169  jMax = std::min(10, (int)pCSR->GetColumnCount());
170 #endif
171 
172  ShowMsg(pszDebugMsg);
173  ShowMsg("\n");
174  ShowMsg("\n");
175 
176  for (unsigned int i = 0; i < iMax; i++)
177  {
178  char szRow[1024] = "";
179  for (unsigned int j = 0; j < jMax; j++)
180  {
181  char szCol[1024];
182  CKNComplex element = pCSR->GetElement(i, j, bExisted);
183 
184  if (bExisted)
185  sprintf(szCol, "%3.3f:%3.3f |\t\t", element.GetRealNumber(), element.GetImaginaryNumber());
186  else
187  sprintf(szCol, "%s |\t\t", DUMY_STRING_FOR_COMPLEX_SHOWING);
188  strcat(szRow, szCol);
189  }
190  strcat(szRow, "\n");
191  ShowMsg(szRow);
192  }
193 }
194 
200 void CKNMatrixDebug::ShowDoubleArray(double *vector, char *pszDebugMsg, int due)
201 {
202  char szDebug[1024] = "";
203  int i;
204 
205  ShowMsg(pszDebugMsg);
206  ShowMsg("\n");
207 
208  for (i = 0; i < due; i++)
209  {
210  sprintf(szDebug, "%5d: %20.20f\n", i, vector[i]);
211  ShowMsg(szDebug);
212  }
213 }
214 
220 void CKNMatrixDebug::ShowDoubleVector(CKNMatrixOperation::CKNVector vector, char *pszDebugMsg, int due)
221 {
222  char szDebug[1024] = "";
223  int i, nSize;
224 
225  if (-1 == due)
226  nSize = vector.GetSize();
227  else
228  nSize = due;
229 
230  ShowMsg(pszDebugMsg);
231  ShowMsg("\n");
232 
233  for (i = 0; i < nSize; i++)
234  {
235  sprintf(szDebug, "%5d: %20.20f + %20.20f i\n", i, vector.GetAt(i).GetRealNumber(), vector.GetAt(i).GetImaginaryNumber());
236  ShowMsg(szDebug);
237  }
238 }
239 
244 void CKNMatrixDebug::ShowComplex(CKNComplex number, char *pszDebugMsg)
245 {
246  char szDebug[1024];
247 
248  sprintf(szDebug, " %20.20f + %20.20f i\n", number.GetRealNumber(), number.GetImaginaryNumber());
249 
250  ShowMsg(pszDebugMsg);
251  ShowMsg("\n");
252  ShowMsg(szDebug);
253 }
254 
259 void CKNMatrixDebug::ShowDouble(double number, char *pszDebugMsg)
260 {
261  char szDebug[1024];
262 
263  sprintf(szDebug, " %20.20f \n", number);
264 
265  ShowMsg(pszDebugMsg);
266  ShowMsg("\n");
267  ShowMsg(szDebug);
268 }
269 
275 {
276  char szBuffer[1024];
277  FILE *fpResult;
278  unsigned int i;
279 
280  sprintf(szBuffer, "lanczosresult_%d.txt", nIndex);
281  if (NULL != (fpResult = fopen(szBuffer, "wt")))
282  {
283  for (i = 0; i < lpResult->nEigenValueCount; i++)
284  {
285  sprintf(szBuffer, "%f\n", lpResult->pEigenValues[i]);
286  fputs(szBuffer, fpResult);
287  }
288  fclose(fpResult);
289  }
290 }
291 
292 void CKNMatrixDebug::DumpCSRBinary(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
293 {
294  unsigned int i, j, nSize;
295  FILE *out;
296 
297 #ifdef DISABLE_MPI_ROUTINE
298  out = fopen(pszFileName, "wb");
299 #else //DISABLE_MPI_ROUTINE
300  MPI_Request req;
301  double fTemp = 1;
302 
304  out = fopen(pszFileName, "wb");
305  else
306  {
308  out = fopen(pszFileName, "ab");
309  }
310 #endif //DISABLE_MPI_ROUTINE
311 
312  if (NULL == out)
313  return;
314 
315  nSize = pCSR->GetRowCount();
316  for (i = 0; i < nSize; ++i)
317  {
318  unsigned int nSubStart = pCSR->m_vectRow[i], nSubEnd = pCSR->m_vectRow[i + 1];
319 
320  for (j = nSubStart; j < nSubEnd; ++j)
321  {
322  double fTemp;
323  int nTemp = i + (int)(fAtomIDStartIndex * 10);
324  fwrite(&nTemp, sizeof(int), 1, out);
325  fwrite(&pCSR->m_vectColumn[j], sizeof(int), 1, out);
326  fTemp = pCSR->m_vectValueBuffer[j].GetRealNumber();
327  fwrite(&fTemp, sizeof(double), 1, out);
328  fTemp = pCSR->m_vectValueBuffer[j].GetImaginaryNumber();
329  fwrite(&fTemp, sizeof(double), 1, out);
330  }
331  }
332  fclose(out);
333 
334 #ifndef DISABLE_MPI_ROUTINE
337 #endif //DISABLE_MPI_ROUTINE
338 }
339 
340 void CKNMatrixDebug::DumpCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
341 {
342  unsigned int i, j, nSize;
343  FILE *out;
344  char szMsg[1024];
345  char szFileName[1024];
346 
347 #ifdef _WIN32
348  _mkdir("result");
349  sprintf(szFileName, "result\\%s", pszFileName);
350 #else
351  mkdir("result", 0777);
352  sprintf(szFileName, "result/%s", pszFileName);
353 #endif
354 
355 #ifdef DISABLE_MPI_ROUTINE
356  out = fopen(szFileName, "wt");
357 #else //DISABLE_MPI_ROUTINE
358  MPI_Request req;
359  double fTemp = 1;
360 
362  out = fopen(szFileName, "wt");
363  else
364  {
366  out = fopen(szFileName, "at");
367  }
368 #endif //DISABLE_MPI_ROUTINE
369 
370  if( NULL == out )
371  return;
372 
373  nSize = pCSR->GetRowCount();
374  for(i = 0; i < nSize ; ++ i)
375  {
376  unsigned int nSubStart = pCSR->m_vectRow[i], nSubEnd = pCSR->m_vectRow[i + 1];
377 
378  for (j = nSubStart; j < nSubEnd; ++ j)
379  {
380  sprintf(szMsg, "%05d\t%05d\t%40.30f\t%40.30f\n", i + (int)(fAtomIDStartIndex*10) + 1,
381  pCSR->m_vectColumn[j] + 1,
382  pCSR->m_vectValueBuffer[j].GetRealNumber(),
383  pCSR->m_vectValueBuffer[j].GetImaginaryNumber());
384  fputs(szMsg, out);
385  }
386  }
387  fclose(out);
388 
389 #ifndef DISABLE_MPI_ROUTINE
392 #endif //DISABLE_MPI_ROUTINE
393 }
double GetImaginaryNumber() const
Get imaginary part.
Definition: KNComplex.h:27
Data and operation representation of Matrix.
static bool m_bShowMsg
Definition: KNMatrixDebug.h:38
double GetRealNumber() const
Get real part.
Definition: KNComplex.h:26
unsigned int GetColumnCount()
Getting row size of matrix.
Data and operation representation of CSR(Compressed Sparse Row)
static void DumpCSRBinary(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
Dump CSR to binary file.
static void SaveResult(CKNLanczosMethod::LPEIGENVALUE_RESULT lpResult, int nIndex)
Show lanczos method result.
static int GetTotalNodeCount()
Definition: KNMPIManager.h:44
static void ShowDenseMatrix(CKNMatrixOperation::CKNDMatrix *pMatrix, int startm, int endm, int startn, int endn, char *pszDebugMsg, bool bLineFeed=false, bool bShowImaginary=false)
Show matrix partialy.
uint_vector_t m_vectColumn
A member variable for saving column information.
static void ShowDoubleVector(CKNMatrixOperation::CKNVector vector, char *pszDebugMsg, int due=-1)
CKNComplex GetAt(unsigned int nIndex)
Get element value from specific index.
Structure for engienvalue computing.
static void ShowDoubleArray(double *vector, char *pszDebugMsg, int due)
Show double array.
static void ShowMsg(char *pszBuffer)
Show message.
This class includes functions for matrix debugging.
static int GetCurrentRank()
Definition: KNMPIManager.h:42
CKNComplex GetElement(unsigned int nRowIndex, unsigned int nColumnIndex)
Get matrix element with row, column index.
static void ShowDouble(double number, char *pszDebugMsg)
Show one double data.
static void ShowCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszDebugMsg)
Set show message or not.
unsigned int GetSize()
Return Vector elements size.
CKNComplex GetElement(unsigned int nRow, unsigned int nColumn, bool &bResult)
Get Element by index.
This class for complex operation and saving value.
Definition: KNComplex.h:18
MPI Mangement class.
static void DumpCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
Dump CSR to text file.
static void ShowComplex(CKNComplex number, char *pszDebugMsg)
Show vector class.
static bool IsRootRank()
Get Total node count.
#define DUMY_STRING_FOR_COMPLEX_SHOWING
uint_vector_t m_vectRow
A member variable for saving row information.
static void SendDoubleBufferSync(int nTargetRank, double *pBuffer, int nSize, MPI_Request *req, MPI_Comm commWorld=MPI_COMM_NULL)
Sending buffer for double data array with sync.
static void ShowDoubleMatrix(double *pMatrix, int m, int n, int startm, int endm, int startn, int endn, char *pszDebugMsg)
Show matrix in output debugging windows - Visual studio and Windows DebugView only.
This class for describing vector for Lanczos method.
static void ReceiveDoubleBufferSync(int nSourceRank, double *pBuffer, int nSize, MPI_Request *req, MPI_Comm commWorld=MPI_COMM_NULL)
Receivinging buffer for double data array with sync.