IPCC  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KNIPCCUtility.cpp
Go to the documentation of this file.
1 
7 #include "stdafx.h"
8 #include "KNIPCCUtility.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 CKNIPCCUtility::m_bShowMsg = false;
23 
25 {
26 }
27 
29 {
30 }
31 
35 void CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 CKNIPCCUtility::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 
297 void CKNIPCCUtility::DumpCSRBinary(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
298 {
299  unsigned int i, j, nSize;
300  FILE *out;
301 
302 #ifdef DISABLE_MPI_ROUTINE
303  out = fopen(pszFileName, "wb");
304 #else //DISABLE_MPI_ROUTINE
305  MPI_Request req;
306  double fTemp = 1;
307 
309  out = fopen(pszFileName, "wb");
310  else
311  {
313  out = fopen(pszFileName, "ab");
314  }
315 #endif //DISABLE_MPI_ROUTINE
316 
317  if (NULL == out)
318  return;
319 
320  nSize = pCSR->GetRowCount();
321  for (i = 0; i < nSize; ++i)
322  {
323  unsigned int nSubStart = pCSR->m_vectRow[i], nSubEnd = pCSR->m_vectRow[i + 1];
324 
325  for (j = nSubStart; j < nSubEnd; ++j)
326  {
327  double fTemp;
328  int nTemp = i + (int)(fAtomIDStartIndex * 10);
329  fwrite(&nTemp, sizeof(int), 1, out);
330  fwrite(&pCSR->m_vectColumn[j], sizeof(int), 1, out);
331  fTemp = pCSR->m_vectValueRealBuffer[j];
332  fwrite(&fTemp, sizeof(double), 1, out);
333  fTemp = pCSR->m_vectValueImaginaryBuffer[j];
334  fwrite(&fTemp, sizeof(double), 1, out);
335  }
336  }
337  fclose(out);
338 
339 #ifndef DISABLE_MPI_ROUTINE
342 #endif //DISABLE_MPI_ROUTINE
343 }
344 
350 void CKNIPCCUtility::DumpCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
351 {
352  unsigned int i, j, nSize;
353  FILE *out;
354  char szMsg[1024];
355  char szFileName[1024];
356 
357  if( false == CKNMPIManager::IsDeflationRoot() )
358  return;
359 
360 #ifdef _WIN32
361  _mkdir("result");
362  sprintf(szFileName, "result\\%s", pszFileName);
363 #else
364  mkdir("result", 0777);
365  sprintf(szFileName, "result/%s", pszFileName);
366 #endif
367 
368 #ifdef DISABLE_MPI_ROUTINE
369  out = fopen(szFileName, "wt");
370 #else //DISABLE_MPI_ROUTINE
371  MPI_Request req;
372  double fTemp = 1;
373 
375  out = fopen(szFileName, "wt");
376  else
377  {
379  out = fopen(szFileName, "at");
380  }
381 #endif //DISABLE_MPI_ROUTINE
382 
383  if( NULL == out )
384  return;
385 
386  nSize = pCSR->GetRowCount();
387  for(i = 0; i < nSize ; ++ i)
388  {
389  unsigned int nSubStart = pCSR->m_vectRow[i], nSubEnd = pCSR->m_vectRow[i + 1];
390 
391  for (j = nSubStart; j < nSubEnd; ++ j)
392  {
393  sprintf(szMsg, "%05d\t%05d\t%40.30f\t%40.30f\n", i + (int)(fAtomIDStartIndex*10) + 1,
394  pCSR->m_vectColumn[j] + 1,
395  pCSR->m_vectValueRealBuffer[j],
396  pCSR->m_vectValueImaginaryBuffer[j]);
397  fputs(szMsg, out);
398  }
399  }
400  fclose(out);
401 
402 #ifndef DISABLE_MPI_ROUTINE
405 #endif //DISABLE_MPI_ROUTINE
406 }
407 
411 void CKNIPCCUtility::ConvertLower(char *pszBuffer)
412 {
413  int i, nLen;
414 
415  nLen = strlen(pszBuffer);
416  for (i = 0; i < nLen; ++i)
417  {
418  if (pszBuffer[i] < 'Z')
419  pszBuffer[i] += 32;
420  }
421 }
double GetImaginaryNumber() const
Get imaginary part.
Definition: KNComplex.h:27
static void ShowMsg(char *pszBuffer)
Show message.
Data and operation representation of Matrix.
double_vector_t m_vectValueRealBuffer
A member variable for saving none zero elements.
double GetRealNumber() const
Get real part.
Definition: KNComplex.h:26
static void ShowComplex(CKNComplex number, char *pszDebugMsg)
Show vector class.
unsigned int GetColumnCount()
Getting row size of matrix.
Data and operation representation of CSR(Compressed Sparse Row)
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.
double_vector_t m_vectValueImaginaryBuffer
A member variable for saving none zero elements.
static int GetTotalNodeCount()
Definition: KNMPIManager.h:44
static void SaveResult(CKNLanczosMethod::LPEIGENVALUE_RESULT lpResult, int nIndex)
Show lanczos method result.
uint_vector_t m_vectColumn
A member variable for saving column information.
static void ShowDouble(double number, char *pszDebugMsg)
Show one double data.
static void ConvertLower(char *pszBuffer)
Convert string to lower case.
static bool m_bShowMsg
Flag for display message.
Definition: KNIPCCUtility.h:39
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.
static void ShowCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszDebugMsg)
Set show message or not.
CKNComplex GetAt(unsigned int nIndex)
Get element value from specific index.
static void DumpCSR(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
Dump CSR to text file.
Structure for engienvalue computing.
#define DUMY_STRING_FOR_COMPLEX_SHOWING
This class includes functions for matrix debugging.
static int GetCurrentRank()
Definition: KNMPIManager.h:42
static void ShowDoubleArray(double *vector, char *pszDebugMsg, int due)
Show double array.
CKNComplex GetElement(unsigned int nRowIndex, unsigned int nColumnIndex)
Get matrix element with row, column index.
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 DumpCSRBinary(CKNMatrixOperation::CKNCSR *pCSR, char *pszFileName, double fAtomIDStartIndex)
Dump CSR to binary file.
static bool IsRootRank()
Get Total node count.
static void ShowDoubleVector(CKNMatrixOperation::CKNVector vector, char *pszDebugMsg, int due=-1)
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.
This class for describing vector for Lanczos method.
static bool IsDeflationRoot()
Checking is root rank of Lanczos computation.
Definition: KNMPIManager.h:72
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.