/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/Init.cc Copyright (C) 2015 Author: Azusa Yamaguchi Author: Peter Boyle Author: Peter Boyle Author: paboyle 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. See the full license in the file "LICENSE" in the top level distribution directory *************************************************************************************/ /* END LEGAL */ /****************************************************************************/ /* pab: Signal magic. Processor state dump is x86-64 specific */ /****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __APPLE__ static int feenableexcept (unsigned int excepts) { static fenv_t fenv; unsigned int new_excepts = excepts & FE_ALL_EXCEPT; unsigned int old_excepts; // previous masks int iold_excepts; // previous masks if ( fegetenv (&fenv) ) return -1; old_excepts = fenv.__control & FE_ALL_EXCEPT; // unmask fenv.__control &= ~new_excepts; fenv.__mxcsr &= ~(new_excepts << 7); iold_excepts = (int) old_excepts; return ( fesetenv (&fenv) ? -1 : iold_excepts ); } #endif NAMESPACE_BEGIN(Grid); ////////////////////////////////////////////////////// // Convenience functions to access stadard command line arg // driven parallelism controls ////////////////////////////////////////////////////// static Coordinate Grid_default_latt; static Coordinate Grid_default_mpi; int GridThread::_threads =1; int GridThread::_hyperthreads=1; int GridThread::_cores=1; const Coordinate &GridDefaultLatt(void) {return Grid_default_latt;}; const Coordinate &GridDefaultMpi(void) {return Grid_default_mpi;}; const Coordinate GridDefaultSimd(int dims,int nsimd) { Coordinate layout(dims); int nn=nsimd; for(int d=dims-1;d>=0;d--){ if ( nn>=2) { layout[d]=2; nn/=2; } else { layout[d]=1; } } assert(nn==1); return layout; } //////////////////////////////////////////////////////////// // Command line parsing assist for stock controls //////////////////////////////////////////////////////////// std::string GridCmdOptionPayload(char ** begin, char ** end, const std::string & option) { char ** itr = std::find(begin, end, option); if (itr != end && ++itr != end) { std::string payload(*itr); return payload; } return std::string(""); } bool GridCmdOptionExists(char** begin, char** end, const std::string& option) { return std::find(begin, end, option) != end; } // Comma separated list void GridCmdOptionCSL(std::string str,std::vector & vec) { size_t pos = 0; std::string token; std::string delimiter(","); vec.resize(0); while ((pos = str.find(delimiter)) != std::string::npos) { token = str.substr(0, pos); vec.push_back(token); str.erase(0, pos + delimiter.length()); } token = str; vec.push_back(token); return; } template void GridCmdOptionIntVector(std::string &str,VectorInt & vec) { vec.resize(0); std::stringstream ss(str); int i; while (ss >> i){ vec.push_back(i); if(std::ispunct(ss.peek())) ss.ignore(); } return; } void GridCmdOptionInt(std::string &str,int & val) { std::stringstream ss(str); ss>>val; return; } void GridParseLayout(char **argv,int argc, Coordinate &latt_c, Coordinate &mpi_c) { auto mpi =std::vector({1,1,1,1}); auto latt=std::vector({8,8,8,8}); GridThread::SetMaxThreads(); std::string arg; if( GridCmdOptionExists(argv,argv+argc,"--mpi") ){ arg = GridCmdOptionPayload(argv,argv+argc,"--mpi"); GridCmdOptionIntVector(arg,mpi); } if( GridCmdOptionExists(argv,argv+argc,"--grid") ){ arg= GridCmdOptionPayload(argv,argv+argc,"--grid"); GridCmdOptionIntVector(arg,latt); } if( GridCmdOptionExists(argv,argv+argc,"--threads") ){ std::vector ompthreads(0); #ifndef GRID_OMP std::cout << GridLogWarning << "'--threads' option used but Grid was" << " not compiled with thread support" << std::endl; #endif arg= GridCmdOptionPayload(argv,argv+argc,"--threads"); GridCmdOptionIntVector(arg,ompthreads); assert(ompthreads.size()==1); GridThread::SetThreads(ompthreads[0]); } if( GridCmdOptionExists(argv,argv+argc,"--cores") ){ int cores; arg= GridCmdOptionPayload(argv,argv+argc,"--cores"); GridCmdOptionInt(arg,cores); GridThread::SetCores(cores); } // Copy back into coordinate format int nd = mpi.size(); assert(latt.size()==nd); latt_c.resize(nd); mpi_c.resize(nd); for(int d=0;d std::string GridCmdVectorIntToString(const VectorInt & vec_in){ int sz = vec_in.size(); std::vector vec(sz); for(int s=0;s(oss, " ")); return oss.str(); } ///////////////////////////////////////////////////////// // Reinit guard ///////////////////////////////////////////////////////// static MemoryStats dbgMemStats; static int Grid_is_initialised; ///////////////////////////////////////////////////////// // Reinit guard ///////////////////////////////////////////////////////// #ifdef GRID_NVCC void GridGpuInit(void) { int nDevices; cudaGetDeviceCount(&nDevices); for (int i = 0; i < nDevices; i++) { cudaDeviceProp prop; cudaGetDeviceProperties(&prop, i); printf("Device Number: %d\n", i); printf(" Device name: %s\n", prop.name); printf(" Memory Clock Rate (KHz): %d\n", prop.memoryClockRate); printf(" Memory Bus Width (bits): %d\n", prop.memoryBusWidth); printf(" Peak Memory Bandwidth (GB/s): %f\n\n", 2.0*prop.memoryClockRate*(prop.memoryBusWidth/8)/1.0e6); #define GPU_PROP_FMT(canMapHostMemory,FMT) printf(" " #canMapHostMemory ": " FMT" \n",prop.canMapHostMemory); #define GPU_PROP(canMapHostMemory) printf(" " #canMapHostMemory ": %d \n",prop.canMapHostMemory); GPU_PROP(canMapHostMemory); GPU_PROP(canUseHostPointerForRegisteredMem); GPU_PROP(globalL1CacheSupported); GPU_PROP(isMultiGpuBoard); GPU_PROP(kernelExecTimeoutEnabled); GPU_PROP(l2CacheSize); GPU_PROP(managedMemory); GPU_PROP(pageableMemoryAccess); GPU_PROP(regsPerMultiprocessor); GPU_PROP_FMT(sharedMemPerBlock,"%lx"); GPU_PROP_FMT(sharedMemPerMultiprocessor,"%lx"); GPU_PROP(singleToDoublePrecisionPerfRatio); GPU_PROP(unifiedAddressing); GPU_PROP(warpSize); } } #endif void Grid_init(int *argc,char ***argv) { assert(Grid_is_initialised == 0); GridLogger::GlobalStopWatch.Start(); std::string arg; //////////////////////////////////// // Shared memory block size //////////////////////////////////// if( GridCmdOptionExists(*argv,*argv+*argc,"--shm") ){ int MB; arg= GridCmdOptionPayload(*argv,*argv+*argc,"--shm"); GridCmdOptionInt(arg,MB); uint64_t MB64 = MB; GlobalSharedMemory::MAX_MPI_SHM_BYTES = MB64*1024LL*1024LL; } if( GridCmdOptionExists(*argv,*argv+*argc,"--shm-hugepages") ){ GlobalSharedMemory::Hugepages = 1; } if( GridCmdOptionExists(*argv,*argv+*argc,"--debug-signals") ){ Grid_debug_handler_init(); } CartesianCommunicator::Init(argc,argv); if( !GridCmdOptionExists(*argv,*argv+*argc,"--debug-stdout") ){ Grid_quiesce_nodes(); } else { FILE *fp; std::ostringstream fname; fname<<"Grid.stdout."; fname< logstreams; std::string defaultLog("Error,Warning,Message,Performance"); GridCmdOptionCSL(defaultLog,logstreams); GridLogConfigure(logstreams); if( GridCmdOptionExists(*argv,*argv+*argc,"--log") ){ arg = GridCmdOptionPayload(*argv,*argv+*argc,"--log"); GridCmdOptionCSL(arg,logstreams); GridLogConfigure(logstreams); } //////////////////////////////////// // Help message //////////////////////////////////// if( GridCmdOptionExists(*argv,*argv+*argc,"--help") ){ std::cout<si_signo); fprintf(stderr," mem address %llx\n",(unsigned long long)si->si_addr); fprintf(stderr," code %d\n",si->si_code); // Linux/Posix #ifdef __linux__ // And x86 64bit #ifdef __x86_64__ ucontext_t * uc= (ucontext_t *)ptr; struct sigcontext *sc = (struct sigcontext *)&uc->uc_mcontext; fprintf(stderr," instruction %llx\n",(unsigned long long)sc->rip); #define REG(A) printf(" %s %lx\n",#A,sc-> A); REG(rdi); REG(rsi); REG(rbp); REG(rbx); REG(rdx); REG(rax); REG(rcx); REG(rsp); REG(rip); REG(r8); REG(r9); REG(r10); REG(r11); REG(r12); REG(r13); REG(r14); REG(r15); #endif #endif fflush(stderr); BACKTRACEFP(stderr); fprintf(stderr,"Called backtrace\n"); fflush(stdout); fflush(stderr); exit(0); return; }; void Grid_debug_handler_init(void) { struct sigaction sa; sigemptyset (&sa.sa_mask); sa.sa_sigaction= Grid_sa_signal_handler; sa.sa_flags = SA_SIGINFO; sigaction(SIGSEGV,&sa,NULL); sigaction(SIGTRAP,&sa,NULL); sigaction(SIGBUS,&sa,NULL); feenableexcept( FE_INVALID|FE_OVERFLOW|FE_DIVBYZERO); sigaction(SIGFPE,&sa,NULL); sigaction(SIGKILL,&sa,NULL); sigaction(SIGILL,&sa,NULL); } NAMESPACE_END(Grid);