sospin is hosted by Hepforge, IPPP Durham
SOSpin  1.0.0
son.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // SOSpin Library
3 // Copyright (C) 2015 SOSpin Project
4 //
5 // Authors:
6 //
7 // Nuno Cardoso (nuno.cardoso@tecnico.ulisboa.pt)
8 // David Emmanuel-Costa (david.costa@tecnico.ulisboa.pt)
9 // Nuno Gonçalves (nunogon@deec.uc.pt)
10 // Catarina Simoes (csimoes@ulg.ac.be)
11 //
12 // ----------------------------------------------------------------------------
13 // This file is part of SOSpin Library.
14 //
15 // SOSpin Library is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or any
18 // later version.
19 //
20 // SOSpin Library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with SOSpin Library. If not, see <http://www.gnu.org/licenses/>.
27 // ----------------------------------------------------------------------------
28 
29 // son.cpp created on 27/02/2015
30 //
31 // This file contains the functions necessary to do things
32 // in the SOSpin Library.
33 //
34 // Revision 1.1 28/02/2015 23:19:29 david
35 // License updated
36 //
37 
44 #include <iostream>
45 #include <string>
46 #include <cstring>
47 #include <sstream>
48 #include <cstdlib>
49 
50 #include "son.h"
51 
52 using namespace std;
53 
54 namespace sospin {
55 
56 static int GroupDim = 10; //default value
57 static Verbosity verbose_flag = SUMMARIZE;
58 
59 void setDim(int n){
60  GroupDim = n;
61  cout << "Setting group dimension to "<< GroupDim << endl;
62 }
63 
64 int getDim(){
65  return GroupDim;
66 }
67 
68 bool GroupEven(){
69  bool a = false;
70  if(GroupDim % 2 == 0) a = true;
71  return a;
72 }
73 
74 
76  form.clear();
77  tabids.clear();
78 }
79 
81  verbose_flag = verb;
82 }
83 
85  return verbose_flag;
86 }
87 
99 Braket Bop(string startid){
100  Braket b0, b1;
101  b0 = bb(startid +"1");
102  b0 -= bbt(startid +"1");
103  string constpart0 = "*e_(" + startid +"1";
104  string constpart;
105  if( (getDim()/2) % 2 == 0) constpart = "1/";
106  else constpart = "i_/";
107  int factorn = 1;
108  for(int i = 2; i <= getDim()/2; i++){
109  string idx = startid + ToString<int>(i);
110  b1 = bb(idx);
111  b1 -= bbt(idx);
112  b0 = b0 * b1;
113  constpart0 += ",";
114  constpart0 += idx;
115  factorn *= i;
116  }
117  constpart += ToString<int>(factorn) + constpart0 + ")";
118  if( (getDim()/2) % 4 == 0 || (getDim()/2) % 4 == 3) b0 = b0 * constpart;
119  else b0 = -b0 * constpart;
120  return b0;
121 }
122 
123 
135  Braket b0, b1;
136  b0 = bb(1);
137  b0 -= bbt(1);
138  for(int i = 2; i <= getDim()/2; i++){
139  b1 = bb(i);
140  b1 -= bbt(i);
141  b0 = b0 * b1;
142  }
143  string constpart;
144  if( (getDim()/2) % 2 == 0) constpart = "1";
145  else constpart = "i_";
146  if( (getDim()/2) % 4 == 0 || (getDim()/2) % 4 == 3) b0 = b0 * constpart;
147  else b0 = -b0 * constpart;
148  return b0;
149 }
150 
151 ostream& operator<<(ostream& out,const Verbosity &a){
152  switch( a ) {
153  case SILENT:
154  out<<"SILENT";
155  break;
156  case SUMMARIZE:
157  out<<"SUMMARIZE";
158  break;
159  case VERBOSE:
160  out<<"VERBOSE";
161  break;
162  case DEBUG_VERBOSE:
163  out<<"DEBUG_VERBOSE";
164  break;
165  default:
166  out<<"Not defined";
167  break;
168  }
169  return out;
170 }
171 
172 
173 
174 
175 }
176 
177 #include <unistd.h>
178 #include <ios>
179 #include <iostream>
180 #include <fstream>
181 #include <string>
182 
183 
184 namespace sospin {
185 
186 //FROM: http://nadeausoftware.com/articles/2012/07/c_c_tip_how_get_process_resident_set_size_physical_memory_use
187 
188 /*
189  * Author: David Robert Nadeau
190  * Site: http://NadeauSoftware.com/
191  * License: Creative Commons Attribution 3.0 Unported License
192  * http://creativecommons.org/licenses/by/3.0/deed.en_US
193  */
194 
195 #if defined(_WIN32)
196 #include <windows.h>
197 #include <psapi.h>
198 
199 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
200 #include <unistd.h>
201 #include <sys/resource.h>
202 
203 #if defined(__APPLE__) && defined(__MACH__)
204 #include <mach/mach.h>
205 
206 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
207 #include <fcntl.h>
208 #include <procfs.h>
209 
210 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
211 #include <stdio.h>
212 
213 #endif
214 
215 #else
216 #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
217 #endif
218 
219 
220 
221 
222 
228 size_t getPeakRSS( )
229 {
230 #if defined(_WIN32)
231  /* Windows -------------------------------------------------- */
232  PROCESS_MEMORY_COUNTERS info;
233  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
234  return (size_t)info.PeakWorkingSetSize;
235 
236 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
237  /* AIX and Solaris ------------------------------------------ */
238  struct psinfo psinfo;
239  int fd = -1;
240  if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )
241  return (size_t)0L; /* Can't open? */
242  if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
243  {
244  close( fd );
245  return (size_t)0L; /* Can't read? */
246  }
247  close( fd );
248  return (size_t)(psinfo.pr_rssize * 1024L);
249 
250 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
251  /* BSD, Linux, and OSX -------------------------------------- */
252  struct rusage rusage;
253  getrusage( RUSAGE_SELF, &rusage );
254 #if defined(__APPLE__) && defined(__MACH__)
255  return (size_t)rusage.ru_maxrss;
256 #else
257  return (size_t)(rusage.ru_maxrss * 1024L);
258 #endif
259 
260 #else
261  /* Unknown OS ----------------------------------------------- */
262  return (size_t)0L; /* Unsupported. */
263 #endif
264 }
265 
266 
267 
268 
269 
274 size_t getCurrentRSS( )
275 {
276 #if defined(_WIN32)
277  /* Windows -------------------------------------------------- */
278  PROCESS_MEMORY_COUNTERS info;
279  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
280  return (size_t)info.WorkingSetSize;
281 
282 #elif defined(__APPLE__) && defined(__MACH__)
283  /* OSX ------------------------------------------------------ */
284  struct mach_task_basic_info info;
285  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
286  if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
287  (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
288  return (size_t)0L; /* Can't access? */
289  return (size_t)info.resident_size;
290 
291 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
292  /* Linux ---------------------------------------------------- */
293  long rss = 0L;
294  FILE* fp = NULL;
295  if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
296  return (size_t)0L; /* Can't open? */
297  if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
298  {
299  fclose( fp );
300  return (size_t)0L; /* Can't read? */
301  }
302  fclose( fp );
303  return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
304 
305 #else
306  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
307  return (size_t)0L; /* Unsupported. */
308 #endif
309 }
310 
311 
312 
317  cout << "===============================================================" << endl;
318  cout << "RSS: " << getCurrentRSS( )/(1024.*1024.) << " (MB); RSS_Peak: " << getPeakRSS()/(1024.*1024.) << " (MB)" << endl;
319  cout << "===============================================================" << endl;
320 }
321 
322 }
ostream & operator<<(ostream &out, const Verbosity &a)
Return current ostream verbosity level.
Definition: son.cpp:151
int getDim()
Get the group dimension.
Definition: son.cpp:64
Verbosity getVerbosity()
Return current verbosity level.
Definition: son.cpp:84
Definition: enum.h:64
void CleanGlobalDecl()
Clean all indexes and function declarations.
Definition: son.cpp:75
template string ToString< int >(int number)
Store expression...
Definition: braket.h:198
Definition: enum.h:65
size_t getCurrentRSS()
Gets current resident set size (RSS) - physical memory use.
Definition: son.cpp:274
void setVerbosity(Verbosity verb)
Set verbosity level.
Definition: son.cpp:80
ToForm form
Definition: form.cpp:56
Definition: enum.h:66
bool GroupEven()
Return Group parity.
Definition: son.cpp:68
#define bbt(a)
C++ Macro for DList b\dagger operator.
Definition: son.h:81
Main Sospin header file. Includes C++ macros, to simplify expression writing, B operator, Verbosity level and memory usage.
vector< string > tabids
Vector container of the indexes.
Definition: index.cpp:59
Braket BopIdnum()
Operator B, charge conjugation matrix for SO(2N) spinor representations.
Definition: son.cpp:134
void setDim(int n)
Set the group dimension.
Definition: son.cpp:59
void clear()
Definition: form.cpp:72
Braket Bop(string startid)
Operator B, "charge conjugation" matrix for SO(2N) spinor representations.
Definition: son.cpp:99
enum Verbosity_s Verbosity
size_t getPeakRSS()
Gets highest (peak) value so far for resident set size (RSS) - physical memory use.
Definition: son.cpp:228
void print_process_mem_usage()
Prints memory stats (current and peak resident set size) in MB.
Definition: son.cpp:316
#define bb(a)
C++ Macro for DList b operator.
Definition: son.h:75