double NV_EM_log_likelihood::operator()( const Matrix& data, const ProbTable& theta, const Distribution& pY, const CondProbTable& pXY ) const { double result = 0.0; unsigned N = utility::nrows(data); for ( unsigned i = 0; i < N; ++i ) { const std::vector<int>& X = data[i]; unsigned K = pY.size(), P = X.size(); for ( unsigned y = 0; y < K; ++y ) { double llh_y = m_log(pY[y]); for ( int p = 0; p < P; ++p ) { int x = X[p]; if (pXY[y][p][x]) { llh_y += m_log( pXY[y][p][x] ); } } llh_y *= theta[i][y]; result += llh_y; } } // printf("result: %f\n", result ); return result; }
/* Free resources used by a VM */ void vm_free(vm_instance_t *vm) { if (vm != NULL) { /* Free hardware resources */ vm_hardware_shutdown(vm); m_log("VM","VM %s destroyed.\n",vm->name); /* Close log file */ vm_close_log(vm); /* Remove the lock file */ vm_release_lock(vm,TRUE); /* Free all chunks */ vm_chunk_free_all(vm); /* Free various elements */ free(vm->rommon_vars.filename); free(vm->ghost_ram_filename); free(vm->sym_filename); free(vm->ios_image); free(vm->ios_startup_config); free(vm->ios_private_config); free(vm->rom_filename); free(vm->name); free(vm); } }
/* Load a new ghost image */ static vm_ghost_image_t *vm_ghost_image_load(char *filename) { vm_ghost_image_t *img; if (!(img = calloc(1,sizeof(*img)))) return NULL; img->fd = -1; if (!(img->filename = strdup(filename))) { vm_ghost_image_free(img); return NULL; } img->fd = memzone_open_file_ro(img->filename,&img->area_ptr,&img->file_size); if (img->fd == -1) { vm_ghost_image_free(img); return NULL; } m_log("GHOST","loaded ghost image %s (fd=%d) at addr=%p (size=0x%llx)\n", img->filename,img->fd,img->area_ptr,(long long)img->file_size); return img; }
/* Get a ghost image */ int vm_ghost_image_get(char *filename,u_char **ptr,int *fd) { vm_ghost_image_t *img; VM_GLOCK(); /* Do we already have this image in the pool ? */ if ((img = vm_ghost_image_find(filename)) != NULL) { img->ref_count++; *ptr = img->area_ptr; *fd = img->fd; VM_GUNLOCK(); return(0); } /* Load the ghost file and add it into the pool */ if (!(img = vm_ghost_image_load(filename))) { VM_GUNLOCK(); fprintf(stderr,"Unable to load ghost image %s\n",filename); return(-1); } img->ref_count = 1; *ptr = img->area_ptr; *fd = img->fd; img->next = vm_ghost_pool; vm_ghost_pool = img; VM_GUNLOCK(); m_log("GHOST","loaded image %s successfully.\n",filename); return(0); }
/* Release a ghost image */ int vm_ghost_image_release(int fd) { vm_ghost_image_t **img,*next; VM_GLOCK(); for(img=&vm_ghost_pool;*img;img=&(*img)->next) { if ((*img)->fd == fd) { assert((*img)->ref_count > 0); (*img)->ref_count--; if ((*img)->ref_count == 0) { m_log("GHOST","unloaded ghost image %s (fd=%d) at " "addr=%p (size=0x%llx)\n", (*img)->filename,(*img)->fd,(*img)->area_ptr, (long long)(*img)->file_size); next = (*img)->next; vm_ghost_image_free(*img); *img = next; } VM_GUNLOCK(); return(0); } } VM_GUNLOCK(); return(-1); }
// the constructor just launches some amount of workers threadpool::Pool::Pool(size_t threads, Logger & l) : stop(false), m_log(l) { m_log(LOG_HDR + "ThreadPool creation with " + std::to_string(threads) + " threads."); for(size_t i = 0; i<threads; ++i) { workers.push_back(std::thread(Worker(*this, i, m_log))); } }
// add new work item to the pool //void threadpool::Pool::enqueue(threadpool::ARunnable & instance, void (threadpool::ARunnable::*f)(bool&)) void threadpool::Pool::enqueue(threadpool::ARunnable & instance) { { m_log(std::string(LOG_HDR) + " threadPool::enqueue - " + instance.getName()); // acquire lock std::unique_lock<std::mutex> lock(queue_mutex); m_log(std::string(LOG_HDR) + "enqueue $> locked"); //std::function<void(bool&) > nt = std::bind(f, instance, std::placeholders::_1); //m_log(std::string(LOG_HDR) + "enqueue $> binded"); // add the task tasks.push_back(&instance); m_log(std::string(LOG_HDR) + "enqueue $> task pushed"); } // release lock // wake up one thread condition.notify_one(); m_log(std::string(LOG_HDR) + "enqueue $> notified"); }
void threadpool::Worker::operator()() { threadpool::ARunnable* task; int loop_ctr = 0; while(true) { loop_ctr++; { // acquire lock std::unique_lock<std::mutex> lock(m_pool.queue_mutex); // look for a work item while(!m_pool.stop && m_pool.tasks.empty()) { // if there are none wait for notification m_log( LOG_HDR + " Worker [" + std::to_string(m_id) + "] is waiting for task on loop :" + std::to_string(loop_ctr)); m_pool.condition.wait(lock); m_log( LOG_HDR + " Worker [" + std::to_string(m_id) + "] Waiting done."); } if(m_pool.stop && !m_pool.tasks.size()) // exit if the pool is stopped { return; } m_log(LOG_HDR + std::to_string(m_id) + " - It's time to work."); // get the task from the queue task = m_pool.tasks.front(); m_pool.tasks.pop_front(); } // release lock // execute the task bool task_err = false; m_log( LOG_HDR + " Worker [" + std::to_string(m_id) + "] run."); m_log( LOG_HDR + std::to_string(m_id) + " - Task run now !"); threadpool::ARunnable::t_run torun = &threadpool::ARunnable::run; (task->*torun)(task_err); if (task_err) { m_log.error(LOG_HDR + " worker task fail."); } delete task; } }
inline double log_likelihood_wo( const Node& latentNode, std::vector<int>& obs ) { double result = 0.0; size_t N = obs.size();// / latentNode.cardinality(); Graph& graph = *latentNode.graph; double rs = 0.0; auto cvars = latentNode.get_children_global_indexes(); for (int i = 0; i < N; ++i) { int y = obs[i]; rs += m_log(latentNode.compute_prob(y)); for (int j = 0; j < latentNode.nbr_children(); ++j) { Node& X = graph[cvars[j]]; assert(X.dataVec->size() == obs.size()); int x = X.dataVec->at(i); rs += m_log( latentNode.compute_cond_prob(X,x,y) ); } } return rs; }
/* Set working directory */ static int cmd_set_working_dir(hypervisor_conn_t *conn,int argc,char *argv[]) { if (chdir(argv[0]) == -1) { hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1, "chdir: %s",strerror(errno)); } else { m_log("GENERAL","working_dir=%s\n",argv[0]); hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); } return(0); }
double NV_EM_log_likelihood::likelihood( const Matrix& data, const unsigned i, const ProbTable& theta, const Distribution& pY, const CondProbTable& pXY) const { double llh = 0.0; const std::vector<int>& X = data[i]; unsigned K = pY.size(), P = X.size(); for ( unsigned y = 0; y < K; ++y ) { double llh_y = m_log(pY[y]); for ( int p = 0; p < P; ++p ) { int x = X[p]; llh_y += m_log( pXY[y][p][x] ); } llh_y *= theta[i][y]; } return llh; }
inline double log_likelihood( const Node& latentNode ) { double result = 0.0; size_t N = latentNode.cndObsDist->size() / latentNode.cardinality(); Graph& graph = *latentNode.graph; auto childrenIndexes = latentNode.get_children_global_indexes(); for (int i = 0; i < N; ++i) { for (int y = 0; y < latentNode.cardinality(); ++y) { double llh = m_log(latentNode.compute_prob(y)); for ( auto x_idx: childrenIndexes ) { Node& X = graph[x_idx]; int x = X.dataVec->at(i); llh += m_log(latentNode.compute_cond_prob(X,x,y)); } llh *= latentNode.compute_cond_prob_obs(y, i); result += llh; } } return result; }
// the destructor joins all threads threadpool::Pool::~Pool() { // stop all threads stop = true; condition.notify_all(); // join them for(size_t i = 0; i<workers.size(); ++i) { m_log(LOG_HDR + " - dtor - join : " + std::to_string(i) + " thread."); workers[i].join(); } }
/* * Shutdown hardware resources used by a VM. * The CPU must have been stopped. */ int vm_hardware_shutdown(vm_instance_t *vm) { int i; if ((vm->status == VM_STATUS_HALTED) || !vm->cpu_group) { vm_log(vm,"VM","trying to shutdown an inactive VM.\n"); return(-1); } vm_log(vm,"VM","shutdown procedure engaged.\n"); /* Mark the VM as halted */ vm->status = VM_STATUS_HALTED; /* Free the object list */ vm_object_free_list(vm); /* Free resources used by PCI busses */ vm_log(vm,"VM","removing PCI busses.\n"); pci_io_data_remove(vm,vm->pci_io_space); pci_bus_remove(vm->pci_bus[0]); pci_bus_remove(vm->pci_bus[1]); vm->pci_bus[0] = vm->pci_bus[1] = NULL; /* Free the PCI bus pool */ for(i=0;i<VM_PCI_POOL_SIZE;i++) { if (vm->pci_bus_pool[i] != NULL) { pci_bus_remove(vm->pci_bus_pool[i]); vm->pci_bus_pool[i] = NULL; } } /* Remove the IRQ routing vectors */ vm->set_irq = NULL; vm->clear_irq = NULL; /* Delete the VTTY for Console and AUX ports */ vm_log(vm,"VM","deleting VTTY.\n"); vm_delete_vtty(vm); /* Delete system CPU group */ vm_log(vm,"VM","deleting system CPUs.\n"); cpu_group_delete(vm->cpu_group); vm->cpu_group = NULL; vm->boot_cpu = NULL; vm_log(vm,"VM","shutdown procedure completed.\n"); m_log("VM","VM %s shutdown.\n",vm->name); return(0); }
ScriptState ScriptDriver::load(const m::String& filename, const m::String& moduleName) { (*m_moduleCompiled)[moduleName] = false; //_context->Unprepare(); MUON_ASSERT(!filename.empty(), "Failed to open file: no filename given!"); if (filename.empty()) { m_log(m::LOG_ERROR) << "Failed to open script file: no filename given!"; return SCRIPT_FAILED_OPEN; } m::String completePath; if (filename[0] != m::PATH_SEPARATOR) { completePath = Engine::getProgramPath() + filename; } else { completePath = filename; } ScriptState sls = _load(completePath, moduleName); switch (sls) { case SCRIPT_FAILED_OPEN: m_log(m::LOG_ERROR) << "Script file \"" << completePath << "\" not found" << m::endl; break; case SCRIPT_FAILED_LOAD: m_log(m::LOG_ERROR) << "Script file \"" << completePath << "\" couldn't be loaded" << m::endl; break; case SCRIPT_FAILED_SECTION: m_log(m::LOG_ERROR) << "Script file \"" << completePath << "\" couldn't be added in Script Engine correctly!" << m::endl; break; case SCRIPT_FAILED_BUILD: m_log(m::LOG_ERROR) << "Script file \"" << completePath << "\" couldn't be build." << m::endl; m_log(m::LOG_ERROR) << "\t>Please check syntax errors or use of unbinded classes" << m::endl; break; case SCRIPT_SUCCESS: { m::String moduleNameLog = (moduleName.empty() ? "" : " (Module: " + moduleName + ") "); m_log(m::LOG_INFO) << "Script \"" << filename << "\"" << moduleNameLog << " is correctly loaded!" << m::endl; } break; } return sls; }
void Log::m_hexdump(const void *addr, size_t len, const char *desc, ...) { size_t i; unsigned char buff[17]; unsigned char *pc = (unsigned char*) addr; // Output description if given. if (desc != nullptr && strlen(desc) > 0) { va_list va_alist; va_start(va_alist, desc); m_write(desc, va_alist); va_end(va_alist); m_log(":\n"); } // Process every byte in the data. for (i = 0; i < len; i++) { // Multiple of 16 means new line (with line offset). if ((i % 16) == 0) { // Just don't print ASCII for the zeroth line. if (i != 0) m_log(" %s\n", buff); // Output the offset. m_log(" %04x ", i); } // Now the hex code for the specific character. m_log(" %02x", pc[i]); // And store a printable ASCII character for later. if (isprint(pc[i])) buff[i % 16] = pc[i]; else buff[i % 16] = '.'; buff[(i % 16) + 1] = '\0'; } // Pad out last line if not exactly 16 characters. while ((i % 16) != 0) { m_log(" "); i++; } // And print the final ASCII bit. m_log(" %s\n", buff); }
/* Delete all objects */ void dynamips_reset(void) { printf("Shutdown in progress...\n"); /* Delete all virtual router instances */ vm_delete_all_instances(); /* Delete ATM and Frame-Relay switches + bridges */ netio_bridge_delete_all(); atmsw_delete_all(); atm_bridge_delete_all(); frsw_delete_all(); ethsw_delete_all(); /* Delete all NIO descriptors */ netio_delete_all(); m_log("GENERAL","reset done.\n"); printf("Shutdown completed.\n"); }
void event_device::debug_input_event() const { std::string typestr(boost::lexical_cast<std::string>(m_iev.type)); std::string codestr(boost::lexical_cast<std::string>(m_iev.code)); const char *type = typestr.c_str(), *code = codestr.c_str(); switch (m_iev.type) { case EV_SYN: type = "SYN"; break; case EV_KEY: type = "KEY"; break; case EV_REL: type = "REL"; break; case EV_ABS: type = "ABS"; break; case EV_MSC: type = "MSC"; break; case EV_SW: type = "SW"; break; case EV_LED: type = "LED"; break; case EV_SND: type = "SND"; break; case EV_REP: type = "REP"; break; case EV_FF: type = "FF"; break; case EV_PWR: type = "PWR"; break; case EV_FF_STATUS: type = "FF_STATUS"; break; } if (m_iev.type == EV_KEY) { switch (m_iev.code) { case KEY_RESERVED: code = "RESERVED"; break; case KEY_ESC: code = "ESC"; break; case KEY_1: code = "1"; break; case KEY_2: code = "2"; break; case KEY_3: code = "3"; break; case KEY_4: code = "4"; break; case KEY_5: code = "5"; break; case KEY_6: code = "6"; break; case KEY_7: code = "7"; break; case KEY_8: code = "8"; break; case KEY_9: code = "9"; break; case KEY_0: code = "0"; break; case KEY_MINUS: code = "MINUS"; break; case KEY_EQUAL: code = "EQUAL"; break; case KEY_BACKSPACE: code = "BACKSPACE"; break; case KEY_TAB: code = "TAB"; break; case KEY_Q: code = "Q"; break; case KEY_W: code = "W"; break; case KEY_E: code = "E"; break; case KEY_R: code = "R"; break; case KEY_T: code = "T"; break; case KEY_Y: code = "Y"; break; case KEY_U: code = "U"; break; case KEY_I: code = "I"; break; case KEY_O: code = "O"; break; case KEY_P: code = "P"; break; case KEY_LEFTBRACE: code = "LEFTBRACE"; break; case KEY_RIGHTBRACE: code = "RIGHTBRACE"; break; case KEY_ENTER: code = "ENTER"; break; case KEY_LEFTCTRL: code = "LEFTCTRL"; break; case KEY_A: code = "A"; break; case KEY_S: code = "S"; break; case KEY_D: code = "D"; break; case KEY_F: code = "F"; break; case KEY_G: code = "G"; break; case KEY_H: code = "H"; break; case KEY_J: code = "J"; break; case KEY_K: code = "K"; break; case KEY_L: code = "L"; break; case KEY_SEMICOLON: code = "SEMICOLON"; break; case KEY_APOSTROPHE: code = "APOSTROPHE"; break; case KEY_GRAVE: code = "GRAVE"; break; case KEY_LEFTSHIFT: code = "LEFTSHIFT"; break; case KEY_BACKSLASH: code = "BACKSLASH"; break; case KEY_Z: code = "Z"; break; case KEY_X: code = "X"; break; case KEY_C: code = "C"; break; case KEY_V: code = "V"; break; case KEY_B: code = "B"; break; case KEY_N: code = "N"; break; case KEY_M: code = "M"; break; case KEY_COMMA: code = "COMMA"; break; case KEY_DOT: code = "DOT"; break; case KEY_SLASH: code = "SLASH"; break; case KEY_RIGHTSHIFT: code = "RIGHTSHIFT"; break; case KEY_KPASTERISK: code = "KPASTERISK"; break; case KEY_LEFTALT: code = "LEFTALT"; break; case KEY_SPACE: code = "SPACE"; break; case KEY_CAPSLOCK: code = "CAPSLOCK"; break; case KEY_F1: code = "F1"; break; case KEY_F2: code = "F2"; break; case KEY_F3: code = "F3"; break; case KEY_F4: code = "F4"; break; case KEY_F5: code = "F5"; break; case KEY_F6: code = "F6"; break; case KEY_F7: code = "F7"; break; case KEY_F8: code = "F8"; break; case KEY_F9: code = "F9"; break; case KEY_F10: code = "F10"; break; case KEY_NUMLOCK: code = "NUMLOCK"; break; case KEY_SCROLLLOCK: code = "SCROLLLOCK"; break; case KEY_KP7: code = "KP7"; break; case KEY_KP8: code = "KP8"; break; case KEY_KP9: code = "KP9"; break; case KEY_KPMINUS: code = "KPMINUS"; break; case KEY_KP4: code = "KP4"; break; case KEY_KP5: code = "KP5"; break; case KEY_KP6: code = "KP6"; break; case KEY_KPPLUS: code = "KPPLUS"; break; case KEY_KP1: code = "KP1"; break; case KEY_KP2: code = "KP2"; break; case KEY_KP3: code = "KP3"; break; case KEY_KP0: code = "KP0"; break; case KEY_KPDOT: code = "KPDOT"; break; case KEY_ZENKAKUHANKAKU: code = "ZENKAKUHANKAKU"; break; case KEY_102ND: code = "102ND"; break; case KEY_F11: code = "F11"; break; case KEY_F12: code = "F12"; break; case KEY_RO: code = "RO"; break; case KEY_KATAKANA: code = "KATAKANA"; break; case KEY_HIRAGANA: code = "HIRAGANA"; break; case KEY_HENKAN: code = "HENKAN"; break; case KEY_KATAKANAHIRAGANA: code = "KATAKANAHIRAGANA"; break; case KEY_MUHENKAN: code = "MUHENKAN"; break; case KEY_KPJPCOMMA: code = "KPJPCOMMA"; break; case KEY_KPENTER: code = "KPENTER"; break; case KEY_RIGHTCTRL: code = "RIGHTCTRL"; break; case KEY_KPSLASH: code = "KPSLASH"; break; case KEY_SYSRQ: code = "SYSRQ"; break; case KEY_RIGHTALT: code = "RIGHTALT"; break; case KEY_LINEFEED: code = "LINEFEED"; break; case KEY_HOME: code = "HOME"; break; case KEY_UP: code = "UP"; break; case KEY_PAGEUP: code = "PAGEUP"; break; case KEY_LEFT: code = "LEFT"; break; case KEY_RIGHT: code = "RIGHT"; break; case KEY_END: code = "END"; break; case KEY_DOWN: code = "DOWN"; break; case KEY_PAGEDOWN: code = "PAGEDOWN"; break; case KEY_INSERT: code = "INSERT"; break; case KEY_DELETE: code = "DELETE"; break; case KEY_MACRO: code = "MACRO"; break; case KEY_MUTE: code = "MUTE"; break; case KEY_VOLUMEDOWN: code = "VOLUMEDOWN"; break; case KEY_VOLUMEUP: code = "VOLUMEUP"; break; case KEY_POWER: code = "POWER"; break; case KEY_KPEQUAL: code = "KPEQUAL"; break; case KEY_KPPLUSMINUS: code = "KPPLUSMINUS"; break; case KEY_PAUSE: code = "PAUSE"; break; case KEY_SCALE: code = "SCALE"; break; case KEY_KPCOMMA: code = "KPCOMMA"; break; case KEY_HANGEUL: code = "HANGEUL"; break; case KEY_HANJA: code = "HANJA"; break; case KEY_YEN: code = "YEN"; break; case KEY_LEFTMETA: code = "LEFTMETA"; break; case KEY_RIGHTMETA: code = "RIGHTMETA"; break; case KEY_COMPOSE: code = "COMPOSE"; break; case KEY_STOP: code = "STOP"; break; case KEY_AGAIN: code = "AGAIN"; break; case KEY_PROPS: code = "PROPS"; break; case KEY_UNDO: code = "UNDO"; break; case KEY_FRONT: code = "FRONT"; break; case KEY_COPY: code = "COPY"; break; case KEY_OPEN: code = "OPEN"; break; case KEY_PASTE: code = "PASTE"; break; case KEY_FIND: code = "FIND"; break; case KEY_CUT: code = "CUT"; break; case KEY_HELP: code = "HELP"; break; case KEY_MENU: code = "MENU"; break; case KEY_CALC: code = "CALC"; break; case KEY_SETUP: code = "SETUP"; break; case KEY_SLEEP: code = "SLEEP"; break; case KEY_WAKEUP: code = "WAKEUP"; break; case KEY_FILE: code = "FILE"; break; case KEY_SENDFILE: code = "SENDFILE"; break; case KEY_DELETEFILE: code = "DELETEFILE"; break; case KEY_XFER: code = "XFER"; break; case KEY_PROG1: code = "PROG1"; break; case KEY_PROG2: code = "PROG2"; break; case KEY_WWW: code = "WWW"; break; case KEY_MSDOS: code = "MSDOS"; break; case KEY_COFFEE: code = "COFFEE"; break; case KEY_DIRECTION: code = "DIRECTION"; break; case KEY_CYCLEWINDOWS: code = "CYCLEWINDOWS"; break; case KEY_MAIL: code = "MAIL"; break; case KEY_BOOKMARKS: code = "BOOKMARKS"; break; case KEY_COMPUTER: code = "COMPUTER"; break; case KEY_BACK: code = "BACK"; break; case KEY_FORWARD: code = "FORWARD"; break; case KEY_CLOSECD: code = "CLOSECD"; break; case KEY_EJECTCD: code = "EJECTCD"; break; case KEY_EJECTCLOSECD: code = "EJECTCLOSECD"; break; case KEY_NEXTSONG: code = "NEXTSONG"; break; case KEY_PLAYPAUSE: code = "PLAYPAUSE"; break; case KEY_PREVIOUSSONG: code = "PREVIOUSSONG"; break; case KEY_STOPCD: code = "STOPCD"; break; case KEY_RECORD: code = "RECORD"; break; case KEY_REWIND: code = "REWIND"; break; case KEY_PHONE: code = "PHONE"; break; case KEY_ISO: code = "ISO"; break; case KEY_CONFIG: code = "CONFIG"; break; case KEY_HOMEPAGE: code = "HOMEPAGE"; break; case KEY_REFRESH: code = "REFRESH"; break; case KEY_EXIT: code = "EXIT"; break; case KEY_MOVE: code = "MOVE"; break; case KEY_EDIT: code = "EDIT"; break; case KEY_SCROLLUP: code = "SCROLLUP"; break; case KEY_SCROLLDOWN: code = "SCROLLDOWN"; break; case KEY_KPLEFTPAREN: code = "KPLEFTPAREN"; break; case KEY_KPRIGHTPAREN: code = "KPRIGHTPAREN"; break; case KEY_NEW: code = "NEW"; break; case KEY_REDO: code = "REDO"; break; case KEY_F13: code = "F13"; break; case KEY_F14: code = "F14"; break; case KEY_F15: code = "F15"; break; case KEY_F16: code = "F16"; break; case KEY_F17: code = "F17"; break; case KEY_F18: code = "F18"; break; case KEY_F19: code = "F19"; break; case KEY_F20: code = "F20"; break; case KEY_F21: code = "F21"; break; case KEY_F22: code = "F22"; break; case KEY_F23: code = "F23"; break; case KEY_F24: code = "F24"; break; case KEY_PLAYCD: code = "PLAYCD"; break; case KEY_PAUSECD: code = "PAUSECD"; break; case KEY_PROG3: code = "PROG3"; break; case KEY_PROG4: code = "PROG4"; break; case KEY_DASHBOARD: code = "DASHBOARD"; break; case KEY_SUSPEND: code = "SUSPEND"; break; case KEY_CLOSE: code = "CLOSE"; break; case KEY_PLAY: code = "PLAY"; break; case KEY_FASTFORWARD: code = "FASTFORWARD"; break; case KEY_BASSBOOST: code = "BASSBOOST"; break; case KEY_PRINT: code = "PRINT"; break; case KEY_HP: code = "HP"; break; case KEY_CAMERA: code = "CAMERA"; break; case KEY_SOUND: code = "SOUND"; break; case KEY_QUESTION: code = "QUESTION"; break; case KEY_EMAIL: code = "EMAIL"; break; case KEY_CHAT: code = "CHAT"; break; case KEY_SEARCH: code = "SEARCH"; break; case KEY_CONNECT: code = "CONNECT"; break; case KEY_FINANCE: code = "FINANCE"; break; case KEY_SPORT: code = "SPORT"; break; case KEY_SHOP: code = "SHOP"; break; case KEY_ALTERASE: code = "ALTERASE"; break; case KEY_CANCEL: code = "CANCEL"; break; case KEY_BRIGHTNESSDOWN: code = "BRIGHTNESSDOWN"; break; case KEY_BRIGHTNESSUP: code = "BRIGHTNESSUP"; break; case KEY_MEDIA: code = "MEDIA"; break; case KEY_SWITCHVIDEOMODE: code = "SWITCHVIDEOMODE"; break; case KEY_KBDILLUMTOGGLE: code = "KBDILLUMTOGGLE"; break; case KEY_KBDILLUMDOWN: code = "KBDILLUMDOWN"; break; case KEY_KBDILLUMUP: code = "KBDILLUMUP"; break; case KEY_SEND: code = "SEND"; break; case KEY_REPLY: code = "REPLY"; break; case KEY_FORWARDMAIL: code = "FORWARDMAIL"; break; case KEY_SAVE: code = "SAVE"; break; case KEY_DOCUMENTS: code = "DOCUMENTS"; break; case KEY_BATTERY: code = "BATTERY"; break; case KEY_BLUETOOTH: code = "BLUETOOTH"; break; case KEY_WLAN: code = "WLAN"; break; case KEY_UWB: code = "UWB"; break; case KEY_UNKNOWN: code = "UNKNOWN"; break; case KEY_VIDEO_NEXT: code = "VIDEO_NEXT"; break; case KEY_VIDEO_PREV: code = "VIDEO_PREV"; break; case KEY_BRIGHTNESS_CYCLE: code = "BRIGHTNESS_CYCLE"; break; case KEY_BRIGHTNESS_ZERO: code = "BRIGHTNESS_ZERO"; break; case KEY_DISPLAY_OFF: code = "DISPLAY_OFF"; break; case KEY_WIMAX: code = "WIMAX"; break; case KEY_RFKILL: code = "RFKILL"; break; case KEY_MICMUTE: code = "MICMUTE"; break; case BTN_0: code = "0"; break; case BTN_1: code = "1"; break; case BTN_2: code = "2"; break; case BTN_3: code = "3"; break; case BTN_4: code = "4"; break; case BTN_5: code = "5"; break; case BTN_6: code = "6"; break; case BTN_7: code = "7"; break; case BTN_8: code = "8"; break; case BTN_9: code = "9"; break; case BTN_LEFT: code = "LEFT"; break; case BTN_RIGHT: code = "RIGHT"; break; case BTN_MIDDLE: code = "MIDDLE"; break; case BTN_SIDE: code = "SIDE"; break; case BTN_EXTRA: code = "EXTRA"; break; case BTN_FORWARD: code = "FORWARD"; break; case BTN_BACK: code = "BACK"; break; case BTN_TASK: code = "TASK"; break; case BTN_TRIGGER: code = "TRIGGER"; break; case BTN_THUMB: code = "THUMB"; break; case BTN_THUMB2: code = "THUMB2"; break; case BTN_TOP: code = "TOP"; break; case BTN_TOP2: code = "TOP2"; break; case BTN_PINKIE: code = "PINKIE"; break; case BTN_BASE: code = "BASE"; break; case BTN_BASE2: code = "BASE2"; break; case BTN_BASE3: code = "BASE3"; break; case BTN_BASE4: code = "BASE4"; break; case BTN_BASE5: code = "BASE5"; break; case BTN_BASE6: code = "BASE6"; break; case BTN_DEAD: code = "DEAD"; break; case BTN_A: code = "A"; break; case BTN_B: code = "B"; break; case BTN_C: code = "C"; break; case BTN_X: code = "X"; break; case BTN_Y: code = "Y"; break; case BTN_Z: code = "Z"; break; case BTN_TL: code = "TL"; break; case BTN_TR: code = "TR"; break; case BTN_TL2: code = "TL2"; break; case BTN_TR2: code = "TR2"; break; case BTN_SELECT: code = "SELECT"; break; case BTN_START: code = "START"; break; case BTN_MODE: code = "MODE"; break; case BTN_THUMBL: code = "THUMBL"; break; case BTN_THUMBR: code = "THUMBR"; break; case BTN_TOOL_PEN: code = "TOOL_PEN"; break; case BTN_TOOL_RUBBER: code = "TOOL_RUBBER"; break; case BTN_TOOL_BRUSH: code = "TOOL_BRUSH"; break; case BTN_TOOL_PENCIL: code = "TOOL_PENCIL"; break; case BTN_TOOL_AIRBRUSH: code = "TOOL_AIRBRUSH"; break; case BTN_TOOL_FINGER: code = "TOOL_FINGER"; break; case BTN_TOOL_MOUSE: code = "TOOL_MOUSE"; break; case BTN_TOOL_LENS: code = "TOOL_LENS"; break; case BTN_TOOL_QUINTTAP: code = "TOOL_QUINTTAP"; break; case BTN_TOUCH: code = "TOUCH"; break; case BTN_STYLUS: code = "STYLUS"; break; case BTN_STYLUS2: code = "STYLUS2"; break; case BTN_TOOL_DOUBLETAP: code = "TOOL_DOUBLETAP"; break; case BTN_TOOL_TRIPLETAP: code = "TOOL_TRIPLETAP"; break; case BTN_TOOL_QUADTAP: code = "TOOL_QUADTAP"; break; case BTN_GEAR_DOWN: code = "GEAR_DOWN"; break; case BTN_GEAR_UP: code = "GEAR_UP"; break; case KEY_OK: code = "OK"; break; case KEY_SELECT: code = "SELECT"; break; case KEY_GOTO: code = "GOTO"; break; case KEY_CLEAR: code = "CLEAR"; break; case KEY_POWER2: code = "POWER2"; break; case KEY_OPTION: code = "OPTION"; break; case KEY_INFO: code = "INFO"; break; case KEY_TIME: code = "TIME"; break; case KEY_VENDOR: code = "VENDOR"; break; case KEY_ARCHIVE: code = "ARCHIVE"; break; case KEY_PROGRAM: code = "PROGRAM"; break; case KEY_CHANNEL: code = "CHANNEL"; break; case KEY_FAVORITES: code = "FAVORITES"; break; case KEY_EPG: code = "EPG"; break; case KEY_PVR: code = "PVR"; break; case KEY_MHP: code = "MHP"; break; case KEY_LANGUAGE: code = "LANGUAGE"; break; case KEY_TITLE: code = "TITLE"; break; case KEY_SUBTITLE: code = "SUBTITLE"; break; case KEY_ANGLE: code = "ANGLE"; break; case KEY_ZOOM: code = "ZOOM"; break; case KEY_MODE: code = "MODE"; break; case KEY_KEYBOARD: code = "KEYBOARD"; break; case KEY_SCREEN: code = "SCREEN"; break; case KEY_PC: code = "PC"; break; case KEY_TV: code = "TV"; break; case KEY_TV2: code = "TV2"; break; case KEY_VCR: code = "VCR"; break; case KEY_VCR2: code = "VCR2"; break; case KEY_SAT: code = "SAT"; break; case KEY_SAT2: code = "SAT2"; break; case KEY_CD: code = "CD"; break; case KEY_TAPE: code = "TAPE"; break; case KEY_RADIO: code = "RADIO"; break; case KEY_TUNER: code = "TUNER"; break; case KEY_PLAYER: code = "PLAYER"; break; case KEY_TEXT: code = "TEXT"; break; case KEY_DVD: code = "DVD"; break; case KEY_AUX: code = "AUX"; break; case KEY_MP3: code = "MP3"; break; case KEY_AUDIO: code = "AUDIO"; break; case KEY_VIDEO: code = "VIDEO"; break; case KEY_DIRECTORY: code = "DIRECTORY"; break; case KEY_LIST: code = "LIST"; break; case KEY_MEMO: code = "MEMO"; break; case KEY_CALENDAR: code = "CALENDAR"; break; case KEY_RED: code = "RED"; break; case KEY_GREEN: code = "GREEN"; break; case KEY_YELLOW: code = "YELLOW"; break; case KEY_BLUE: code = "BLUE"; break; case KEY_CHANNELUP: code = "CHANNELUP"; break; case KEY_CHANNELDOWN: code = "CHANNELDOWN"; break; case KEY_FIRST: code = "FIRST"; break; case KEY_LAST: code = "LAST"; break; case KEY_AB: code = "AB"; break; case KEY_NEXT: code = "NEXT"; break; case KEY_RESTART: code = "RESTART"; break; case KEY_SLOW: code = "SLOW"; break; case KEY_SHUFFLE: code = "SHUFFLE"; break; case KEY_BREAK: code = "BREAK"; break; case KEY_PREVIOUS: code = "PREVIOUS"; break; case KEY_DIGITS: code = "DIGITS"; break; case KEY_TEEN: code = "TEEN"; break; case KEY_TWEN: code = "TWEN"; break; case KEY_VIDEOPHONE: code = "VIDEOPHONE"; break; case KEY_GAMES: code = "GAMES"; break; case KEY_ZOOMIN: code = "ZOOMIN"; break; case KEY_ZOOMOUT: code = "ZOOMOUT"; break; case KEY_ZOOMRESET: code = "ZOOMRESET"; break; case KEY_WORDPROCESSOR: code = "WORDPROCESSOR"; break; case KEY_EDITOR: code = "EDITOR"; break; case KEY_SPREADSHEET: code = "SPREADSHEET"; break; case KEY_GRAPHICSEDITOR: code = "GRAPHICSEDITOR"; break; case KEY_PRESENTATION: code = "PRESENTATION"; break; case KEY_DATABASE: code = "DATABASE"; break; case KEY_NEWS: code = "NEWS"; break; case KEY_VOICEMAIL: code = "VOICEMAIL"; break; case KEY_ADDRESSBOOK: code = "ADDRESSBOOK"; break; case KEY_MESSENGER: code = "MESSENGER"; break; case KEY_DISPLAYTOGGLE: code = "DISPLAYTOGGLE"; break; case KEY_SPELLCHECK: code = "SPELLCHECK"; break; case KEY_LOGOFF: code = "LOGOFF"; break; case KEY_DOLLAR: code = "DOLLAR"; break; case KEY_EURO: code = "EURO"; break; case KEY_FRAMEBACK: code = "FRAMEBACK"; break; case KEY_FRAMEFORWARD: code = "FRAMEFORWARD"; break; case KEY_CONTEXT_MENU: code = "CONTEXT_MENU"; break; case KEY_MEDIA_REPEAT: code = "MEDIA_REPEAT"; break; case KEY_10CHANNELSUP: code = "10CHANNELSUP"; break; case KEY_10CHANNELSDOWN: code = "10CHANNELSDOWN"; break; case KEY_IMAGES: code = "IMAGES"; break; case KEY_DEL_EOL: code = "DEL_EOL"; break; case KEY_DEL_EOS: code = "DEL_EOS"; break; case KEY_INS_LINE: code = "INS_LINE"; break; case KEY_DEL_LINE: code = "DEL_LINE"; break; case KEY_FN: code = "FN"; break; case KEY_FN_ESC: code = "FN_ESC"; break; case KEY_FN_F1: code = "FN_F1"; break; case KEY_FN_F2: code = "FN_F2"; break; case KEY_FN_F3: code = "FN_F3"; break; case KEY_FN_F4: code = "FN_F4"; break; case KEY_FN_F5: code = "FN_F5"; break; case KEY_FN_F6: code = "FN_F6"; break; case KEY_FN_F7: code = "FN_F7"; break; case KEY_FN_F8: code = "FN_F8"; break; case KEY_FN_F9: code = "FN_F9"; break; case KEY_FN_F10: code = "FN_F10"; break; case KEY_FN_F11: code = "FN_F11"; break; case KEY_FN_F12: code = "FN_F12"; break; case KEY_FN_1: code = "FN_1"; break; case KEY_FN_2: code = "FN_2"; break; case KEY_FN_D: code = "FN_D"; break; case KEY_FN_E: code = "FN_E"; break; case KEY_FN_F: code = "FN_F"; break; case KEY_FN_S: code = "FN_S"; break; case KEY_FN_B: code = "FN_B"; break; case KEY_BRL_DOT1: code = "BRL_DOT1"; break; case KEY_BRL_DOT2: code = "BRL_DOT2"; break; case KEY_BRL_DOT3: code = "BRL_DOT3"; break; case KEY_BRL_DOT4: code = "BRL_DOT4"; break; case KEY_BRL_DOT5: code = "BRL_DOT5"; break; case KEY_BRL_DOT6: code = "BRL_DOT6"; break; case KEY_BRL_DOT7: code = "BRL_DOT7"; break; case KEY_BRL_DOT8: code = "BRL_DOT8"; break; case KEY_BRL_DOT9: code = "BRL_DOT9"; break; case KEY_BRL_DOT10: code = "BRL_DOT10"; break; case KEY_NUMERIC_0: code = "NUMERIC_0"; break; case KEY_NUMERIC_1: code = "NUMERIC_1"; break; case KEY_NUMERIC_2: code = "NUMERIC_2"; break; case KEY_NUMERIC_3: code = "NUMERIC_3"; break; case KEY_NUMERIC_4: code = "NUMERIC_4"; break; case KEY_NUMERIC_5: code = "NUMERIC_5"; break; case KEY_NUMERIC_6: code = "NUMERIC_6"; break; case KEY_NUMERIC_7: code = "NUMERIC_7"; break; case KEY_NUMERIC_8: code = "NUMERIC_8"; break; case KEY_NUMERIC_9: code = "NUMERIC_9"; break; case KEY_NUMERIC_STAR: code = "NUMERIC_STAR"; break; case KEY_NUMERIC_POUND: code = "NUMERIC_POUND"; break; case KEY_CAMERA_FOCUS: code = "CAMERA_FOCUS"; break; case KEY_WPS_BUTTON: code = "WPS_BUTTON"; break; case KEY_TOUCHPAD_TOGGLE: code = "TOUCHPAD_TOGGLE"; break; case KEY_TOUCHPAD_ON: code = "TOUCHPAD_ON"; break; case KEY_TOUCHPAD_OFF: code = "TOUCHPAD_OFF"; break; case KEY_CAMERA_ZOOMIN: code = "CAMERA_ZOOMIN"; break; case KEY_CAMERA_ZOOMOUT: code = "CAMERA_ZOOMOUT"; break; case KEY_CAMERA_UP: code = "CAMERA_UP"; break; case KEY_CAMERA_DOWN: code = "CAMERA_DOWN"; break; case KEY_CAMERA_LEFT: code = "CAMERA_LEFT"; break; case KEY_CAMERA_RIGHT: code = "CAMERA_RIGHT"; break; default: code = codestr.c_str(); break; } } char buffer[256]; sprintf(buffer, "%d.%06d type=%s code=%s -> %d", int(m_iev.time.tv_sec), int(m_iev.time.tv_usec), type, code, m_iev.value); m_log(log_level::DEBUG, std::string(buffer)); }
void event_device::handle_input_event(const boost::system::error_code& e, size_t /*bytes_read*/) { if (e) { if (m_stopped && e == boost::asio::error::operation_aborted) { LINFO(m_log, "stopped"); } else { LERROR(m_log, "async read failed: " << e.message()); } return; } #ifndef NDEBUG if (m_log(log_level::DEBUG)) { debug_input_event(); } #endif switch (m_iev.type) { case EV_KEY: { event_code::type code = event_code::KEYBOARD_ACTIVITY; switch (m_iev.code) { case KEY_LEFTCTRL: setmod(M_CTRL, m_iev.value > 0); break; case KEY_LEFTSHIFT: setmod(M_SHIFT, m_iev.value > 0); break; case KEY_LEFTALT: setmod(M_ALT, m_iev.value > 0); break; case KEY_LEFTMETA: setmod(M_META, m_iev.value > 0); break; case KEY_BRIGHTNESSUP: code = m_mod == M_CTRL ? event_code::DISPLAY_BRIGHTNESS_UP_SLOW : event_code::DISPLAY_BRIGHTNESS_UP; break; case KEY_BRIGHTNESSDOWN: code = m_mod == M_CTRL ? event_code::DISPLAY_BRIGHTNESS_DOWN_SLOW : event_code::DISPLAY_BRIGHTNESS_DOWN; break; case KEY_KBDILLUMUP: code = m_mod == M_CTRL ? event_code::KEYBOARD_BRIGHTNESS_UP_SLOW : event_code::KEYBOARD_BRIGHTNESS_UP; break; case KEY_KBDILLUMDOWN: code = m_mod == M_CTRL ? event_code::KEYBOARD_BRIGHTNESS_DOWN_SLOW : event_code::KEYBOARD_BRIGHTNESS_DOWN; break; } m_handler->handle_event(code); } case EV_SW: switch (m_iev.code) { case SW_LID: m_handler->handle_event(m_iev.value > 0 ? event_code::LID_CLOSED : event_code::LID_OPENED); break; } default: break; } read_next_event(); }
sim_results simrun( const sim_parameters par, const string dir_init ) { // ----- PREPARE SIMULATION ----- // assume something went wrong until we are sure it didn't sim_results res; res.success = false; // make a folder to work in stringstream tmp; tmp << setfill( '0' ); tmp << "./" << dir_init << '/'; const string dir = tmp.str(); tmp.str( "" ); // folder for images of the model if ( par.take_images != 0 ) { tmp << dir << "images/" ; const string image_dir = tmp.str(); tmp.str( "" ); if ( system( ( "mkdir " + image_dir ).c_str() ) != 0 ) { cout << "ERROR while making the image directory " << dir << endl; return res; } } // start a logfile ofstream run_log( ( dir + "run.log" ).c_str() ); if ( !run_log.is_open() ) { cout << "ERROR while opening the run log file in " << dir << endl; return res; } run_log.precision( numeric_limits<double>::digits10 + 1 ); // write the simulations parameters to the logfile run_log << "Simulation running in " << dir << endl << endl << "--- PARAMETERS ---" << endl << "system = " << par.system_type << endl << "N = " << par.N << endl << "periodic = " << par.periodic << endl << "init = " << par.init << endl << "drysweeps = " << par.drysweeps << endl << "bins = " << par.bins << endl << "binwidth = " << par.binwidth << endl << "intersweeps = " << par.intersweeps << endl << "smode_perbin = " << par.smode_perbin << endl << "smode_permcs = " << par.smode_permcs << endl << "J = " << par.J << endl << "g = " << par.g << endl << "B = " << par.B << endl << "T = " << par.T << endl << endl; run_log.flush(); // ----- RUN SIMULATION ----- time_t rawtime; time( &rawtime ); run_log << ctime( &rawtime ) << "-> creating the system\n\n"; run_log.flush(); // create a new model SystemModel* model; if ( par.system_type == 1 ) { model = new IsingModel1d( par.N, par.periodic, par.J, par.B, par.T, par.use_fsize_correction, dir ); } else if ( par.system_type == 2 ) { model = new IsingModel2d( par.N, par.periodic, par.J, par.B, par.T, par.use_fsize_correction, dir ); } else if ( par.system_type == 3 ) { model = new IsingModel2dWolff( par.N, par.periodic, par.J, par.T, par.use_fsize_correction, dir ); } else if ( par.system_type == 4 ) { model = new IsingModel2dDipole( par.N, par.periodic, par.J, par.g, par.B, par.T, dir ); } else if ( par.system_type == 5 ) { model = new Ising2dDipSqr( par.N, par.periodic, par.J, par.g, par.B, par.T, par.use_fsize_correction, dir ); } else if ( par.system_type == 6 ) { model = new Ising2dDipHC( par.N, par.J, par.g, par.B, par.T, par.use_fsize_correction, dir ); // ___ ADD CUSTOM SYSTEM MODELS HERE ___ } else { cout << "ERROR creating the model system in " << dir << endl; return res; } if ( model->prepare( par.init ) == false ) { cout << "ERROR preparing the models spins in " << dir << endl; delete model; return res; } unsigned int spin_count = model->spin_count(); // open measurement logfiles ofstream h_log( ( dir + "h.log" ).c_str() ); ofstream m_log( ( dir + "m.log" ).c_str() ); if ( !( h_log.is_open() && m_log.is_open() ) ) { cout << "ERROR while opening measurement log files in " << dir << endl; delete model; return res; } // initialize binning array vector <bin_results> binres; // initialize the magnetization data log (needed for autocorr calc only) vector <float> m_memlog; // initialize the spin-spin correlation vector <double> ss_corr; try { // try to allocate enough memory ... binres.reserve( par.bins ); unsigned int ss_corr_size = model->ss_corr().size(); ss_corr.reserve( ss_corr_size ); for ( unsigned int i = 0; i < ss_corr_size; i++ ) { ss_corr.push_back( 0 ); } if ( par.calc_autocorr ) { m_memlog.reserve( par.bins * par.binwidth ); } } catch ( bad_alloc ) { cout << "ERROR while allocating memory in " << dir << endl; delete model; return res; } run_log << 1 + ( ( sizeof( m_memlog ) + m_memlog.capacity() * sizeof( float ) + sizeof( binres ) + binres.capacity() * sizeof( bin_results ) ) / 1024 ) << "KiB of memory reserved\n\n"; time( &rawtime ); run_log << ctime( &rawtime ) << "-> relaxing the system\n\n"; run_log.flush(); // perform dry runs to reach thermal equilibrium model->mcstep_dry( par.drysweeps ); time( &rawtime ); run_log << ctime( &rawtime ) << "-> simulation started\n\n"; run_log.flush(); // binning loop for ( unsigned int bin = 0; bin < par.bins; bin++ ) { //double startTime = current_time(); // initialize variables to measure the systems properties double h = 0, h2 = 0; double m = 0, m2 = 0; // sample loop for ( unsigned int sample = 0; sample < par.binwidth; sample++ ) { double thissample_h = model->h(); double thissample_m = model->m(); // write this sample's properties to the logfile h_log << model->t() << ' ' << thissample_h << endl; m_log << model->t() << ' ' << thissample_m << endl; if ( par.calc_autocorr ) { m_memlog.push_back( float( thissample_m ) ); } // remember the sample's properties to calculate their mean value h += thissample_h; h2 += thissample_h * thissample_h; m += thissample_m; m2 += thissample_m * thissample_m; // make an image of the system if ( ( par.take_images != 0 ) && ( ( bin * par.binwidth + sample ) % par.take_images == 0 ) ) { tmp << dir << "images/" << setw( 9 ) << model->t() << ".png"; const string image_file = tmp.str(); tmp.str( "" ); model->get_image().write( image_file ); } // flip the spins! for ( unsigned int step = 0; step < par.intersweeps; step++ ) { model->special_permcs( par.smode_permcs ); model->mcstep(); } } if ( par.calc_sscorr ) { // spin-spin correlation calculation vector <double> ss_corr_thisbin = model->ss_corr(); for ( unsigned int i = 0; i < par.N; i++ ) { ss_corr[i] += ss_corr_thisbin[i]; } } // invoke the systems special function model->special_perbin( par.smode_perbin ); // calculate mean h = h / par.binwidth; h2 = h2 / par.binwidth; m = m / par.binwidth; m2 = m2 / par.binwidth; // write the bin's results to binres bin_results this_binres; this_binres.h = h; this_binres.h2 = h2; this_binres.m = m; this_binres.m2 = m2; binres.push_back( this_binres ); //cout << "Bin: " << current_time() - startTime << "ms" << endl; } // all measurements done ... let's tidy things up delete model; h_log.close(); m_log.close(); // calculate simulation results from the individual bins double h = 0, sigma3_h = 0; double h2 = 0, sigma3_h2 = 0; double m = 0, sigma3_m = 0; double m2 = 0, sigma3_m2 = 0; // average values for ( unsigned int bin = 0; bin < par.bins; bin++ ) { h += binres[bin].h / par.bins; h2 += binres[bin].h2 / par.bins; m += binres[bin].m / par.bins; m2 += binres[bin].m2 / par.bins; } if ( par.calc_sscorr ) { for ( unsigned int i = 0; i < par.N; i++ ) { ss_corr[i] /= par.bins; } } // calculate susceptibilities double c = spin_count / par.T / par.T * ( h2 - h * h ); double x = spin_count / par.T * ( m2 - m * m ); // calculate variance of the results from the bins first for ( unsigned int bin = 0; bin < par.bins; bin++ ) { sigma3_h += pow( ( binres[bin].h - h ), 2 ) / par.bins; sigma3_h2 += pow( ( binres[bin].h2 - h2 ), 2 ) / par.bins; sigma3_m += pow( ( binres[bin].m - m ), 2 ) / par.bins; sigma3_m2 += pow( ( binres[bin].m2 - m2 ), 2 ) / par.bins; } // use variances to calculate the error of the average sigma3_h = 3 * sqrt( sigma3_h / par.bins ); sigma3_h2 = 3 * sqrt( sigma3_h2 / par.bins ); sigma3_m = 3 * sqrt( sigma3_m / par.bins ); sigma3_m2 = 3 * sqrt( sigma3_m2 / par.bins ); // calculate errors of the susceptibilities (bootstrapping) double sigma3_c = 0, sigma3_x = 0; gsl_rng* rng; // make a new random number generator ... rng = gsl_rng_alloc( gsl_rng_mt19937 ); gsl_rng_set( rng, rand() ); for ( unsigned int bsset = 0; bsset < par.bins; bsset++ ) { double bsset_h = 0, bsset_h2 = 0, bsset_m = 0, bsset_m2 = 0; for ( unsigned int bssample = 0; bssample < par.bins; bssample++ ) { unsigned int bssample_this = gsl_rng_uniform_int( rng, par.bins ); bsset_h += binres[bssample_this].h; bsset_h2 += binres[bssample_this].h2; bsset_m += binres[bssample_this].m; bsset_m2 += binres[bssample_this].m2; } bsset_h /= par.bins; bsset_h2 /= par.bins; bsset_m /= par.bins; bsset_m2 /= par.bins; // calculate the c and x for the selected set ... double bsset_c = spin_count / par.T / par.T * ( bsset_h2 - bsset_h * bsset_h ); double bsset_x = spin_count / par.T * ( bsset_m2 - bsset_m * bsset_m ); sigma3_c += pow( ( bsset_c - c ), 2 ) / par.bins; sigma3_x += pow( ( bsset_x - x ), 2 ) / par.bins; } sigma3_c = 3 * sqrt( sigma3_c ); sigma3_x = 3 * sqrt( sigma3_x ); gsl_rng_free( rng ); time( &rawtime ); run_log << ctime( &rawtime ) << "-> simulation finished"; if ( par.calc_autocorr ) { run_log << " ... starting autocorrelation calculation"; } run_log << "\n\n"; run_log.flush(); // ----- AUTOCORRELATION CALCULATION ----- double tau = 0; if ( par.calc_autocorr ) { // open output file ofstream acout_log( ( dir + "ac.log" ).c_str() ); if ( !acout_log.is_open() ) { cout << "ERROR while opening the acout_log file in " << dir << endl; return res; } // loop over different delta_t double norm = 1; for ( unsigned int d = 0; d < par.binwidth; d++ ) { double product = 0; unsigned int acsamples = m_memlog.size() - d; for ( unsigned int i = 0; i < acsamples; i++ ) { unsigned int sample = i; product += m_memlog[sample] * m_memlog[sample + d]; } product = product / acsamples; if ( d == 0 ) { norm = ( product - m * m ); } tau += ( product - m * m ) / norm * par.intersweeps; acout_log << d* par.intersweeps << ' ' << ( product - m * m ) / norm << endl; } acout_log.close(); // estimate if bins are correlated if ( tau > ( par.binwidth * par.intersweeps ) / 4 ) { cout << "WARNING: bins correlated in " << dir << endl; run_log << "!!!!! WARNING !!!!!\n" << "Bins correlated: Errors and autocorr time are underestimated!" << endl << endl; run_log.flush(); } } // ----- WRITE SPIN-SPIN CORRELATIONS TO DISK ----- if ( par.calc_sscorr ) { // correction for wrap-around errors with periodic boundaries if ( par.periodic ) { for ( unsigned int i = 1; i < ss_corr.size(); i++ ) { ss_corr[i] = ( ss_corr[i] + ss_corr[ss_corr.size() - 1] ) / 2; ss_corr.pop_back(); } } // open output file ofstream sscorr_log( ( dir + "sscorr.log" ).c_str() ); if ( !sscorr_log.is_open() ) { cout << "ERROR while opening the sscorr_log file in " << dir << endl; return res; } double norm = ss_corr[0] - m * m; for ( unsigned int i = 0; i < ss_corr.size(); i++ ) { sscorr_log << i << ' ' << ( ss_corr[i] - m * m ) / norm << ' ' << ss_corr[i] / ss_corr[0] << endl; } sscorr_log.close(); } // ---- RESULT OUTPUT ----- // write simulation results to the output struct res.h = h; res.sigma3_h = sigma3_h; res.c = c; res.sigma3_c = sigma3_c; res.m = m; res.sigma3_m = sigma3_m; res.x = x; res.sigma3_x = sigma3_x; res.tau = tau; // write simulation results to the logfile run_log.precision( numeric_limits<float>::digits10 + 1 ); run_log.setf( ios::scientific ); run_log.setf( ios::showpos ); run_log << "--- RESULTS ---\n" << "h = " << h << " +- " << sigma3_h << endl << "c = " << c << " +- " << sigma3_c << endl << "m = " << m << " +- " << sigma3_m << endl << "chi = " << x << " +- " << sigma3_x << endl << "tau = " << tau << "\n\n"; run_log.flush(); // write results to a pyxplot readable output file ofstream results_file( ( dir + "results.dat" ).c_str() ); if ( !results_file.is_open() ) { cout << "FATAL ERROR: unable to create results.dat in " << dir << endl; return res; } results_file << setiosflags( ios::scientific ); results_file.setf( ios::showpos ); results_file.precision( numeric_limits<float>::digits10 + 1 ); results_file << par.N << ' ' << par.J << ' ' << par.g << ' ' << par.B << ' ' << par.T << ' ' << res.h << ' ' << res.sigma3_h << ' ' << res.c << ' ' << res.sigma3_c << ' ' << res.m << ' ' << res.sigma3_m << ' ' << res.x << ' ' << res.sigma3_x << ' ' << res.tau << endl; results_file.close(); // ----- PLOTTING ----- if ( par.run_plot ) { time( &rawtime ); run_log << ctime( &rawtime ) << "-> starting to plot the results\n\n"; run_log.flush(); } // run_plot ofstream run_plot( ( dir + "run_plot.gnu" ).c_str() ); if ( !run_plot.is_open() ) { cout << "ERROR while opening run_plot.gnu in " << dir << endl; return res; } tmp << "system_type = " << par.system_type << ", N = " << par.N << ", periodic = " << par.periodic << ", init = " << par.init << ", drysweeps = " << par.drysweeps << ", bins = " << par.bins << ", binwidth = " << par.binwidth << ", intersweeps = " << par.intersweeps << ", J = " << par.J << ", g = " << par.g << ", B = " << par.B << ", T = " << par.T; int plot_size = ( par.bins * par.binwidth < 1600 ) ? 1600 : min( ( uint )10000, par.bins * par.binwidth ); run_plot << " set terminal pngcairo size " << plot_size << ",600 \n\ set output 'run_plot.png' \n\n\ set multiplot layout 2,1 title '" << tmp.str() << "'\n\ set grid x y \n\ set mxtics 10 \n\ set key top left \n\ set arrow from graph 0,first " << m << " to graph 1,first " << m << " ls -1 lw 2 nohead" << endl; for ( unsigned int bin = 0; bin < par.bins; bin++ ) { double sep = double( bin ) / double( par.bins ); run_plot <<"\ set arrow from graph " << sep << ",0 to graph " << sep << ",1 nohead\n\ set arrow from graph " << sep << ", first " << binres[bin].m << "\ to graph " << sep + 1 / double( par.bins ) << ", first " << binres[bin].m << " ls -1 lw 1 nohead\n"; } run_plot << "\ plot 'm.log' with lines ls 2 title 'magnetization (" << m << " +- " << sigma3_m << ")' \n\ unset arrow\n\ set arrow from graph 0,first " << h << " to graph 1,first " << h << " ls -1 lw 2 nohead" << endl; for ( unsigned int bin = 0; bin < par.bins; bin++ ) { double sep = double( bin ) / double( par.bins ); run_plot << "\ set arrow from graph " << sep << ",0 to graph " << sep << ",1 nohead\n\ set arrow from graph " << sep << ", first " << binres[bin].h << "\ to graph " << sep + 1 / double( par.bins ) << ", first " << binres[bin].h << " ls -1 lw 1 nohead\n"; } run_plot << "\ plot 'h.log' with lines ls 1 title 'energy per spin (" << h << " +- " << sigma3_h << ")' \n"; tmp.str( "" ); run_plot.close(); if ( par.run_plot && par.bins * par.binwidth <= 1e5 ) { if ( system( ( "cd " + dir + " ; gnuplot run_plot.gnu" ).c_str() ) != 0 ) { cout << "ERROR while running gnuplot run_plot.gnu in " << dir << endl; return res; } } // magnetization and energy histogram plots ofstream histo_plot( ( dir + "histo_plot.pyx" ).c_str() ); if ( !histo_plot.is_open() ) { cout << "ERROR while opening histo_plot.gnu in " << dir << endl; return res; } histo_plot << " set terminal pdf\n\ set output 'mhisto_plot.pdf'\n\ set title 'magnetization histogram'\n\ histogram m() 'm.log' using $2 binorigin 0.005 binwidth 0.01\n\ plot m(x) with boxes notitle \n\ set output 'hhisto_plot.pdf'\n\ set title 'energy histogram'\n\ histogram h() 'h.log' using $2 binorigin 0.005 binwidth 0.01\n\ plot h(x) with boxes notitle"; histo_plot.close(); if ( par.run_plot ) { if ( system( ( "cd " + dir + " ; pyxplot histo_plot.pyx" ).c_str() ) != 0 ) { cout << "ERROR running pyxplot histo_plot.pyx in " << dir << endl; return res; } } // ac_plot if ( par.calc_autocorr ) { ofstream ac_plot( ( dir + "ac_plot.gnu" ).c_str() ); if ( !ac_plot.is_open() ) { cout << "ERROR while opening ac_plot.gnu in " << dir << endl; return res; } ac_plot << " set terminal pngcairo size 1000,600\n\ set output 'ac_plot.png'\n\ set grid x y\n\ set arrow from graph 0, first 0 to graph 1, first 0 nohead lw 2\n\ set title 'magnetization autocorrelation'\n\ set arrow from first " << tau << ", graph 0 to first " << tau << ", graph 1 nohead\n\ plot 'ac.log' with linespoints notitle"; ac_plot.close(); if ( par.run_plot ) { if ( system( ( "cd " + dir + " ; gnuplot ac_plot.gnu" ).c_str() ) != 0 ) { cout << "ERROR while running gnuplot ac_plot.gnu in " << dir << endl; return res; } } } // sscorr_plot if ( par.calc_sscorr ) { ofstream sscorr_plot( ( dir + "sscorr_plot.gnu" ).c_str() ); if ( !sscorr_plot.is_open() ) { cout << "ERROR while opening sscorr_plot.gnu in " << dir << endl; return res; } sscorr_plot << " set terminal pngcairo size 1000,600\n\ set output 'sscorr_plot.png'\n\ set grid x y\n\ set arrow from graph 0, first 0 to graph 1, first 0 nohead lw 2\n\ set title 'spin-spin correlation'\n\ plot 'sscorr.log' u 1:2 with linespoints title '<s1s2> - <m>^2', \\\n\ 'sscorr.log' u 1:3 with linespoints title '<s1s2> '"; sscorr_plot.close(); if ( par.run_plot ) { if ( system( ( "cd " + dir + " ; gnuplot sscorr_plot.gnu" ).c_str() ) != 0 ) { cout << "ERROR running gnuplot sscorr_plot.gnu in " << dir << endl; return res; } } }
static void m_log(const First& f, const Rest& ... r) { m_tlog<First>(f); m_log(r...); }
/* Create a new VM instance */ static vm_instance_t *vm_create(char *name,int instance_id, vm_platform_t *platform) { vm_instance_t *vm; if (!(vm = malloc(sizeof(*vm)))) { fprintf(stderr,"VM %s: unable to create new instance!\n",name); return NULL; } memset(vm,0,sizeof(*vm)); if (!(vm->name = strdup(name))) { fprintf(stderr,"VM %s: unable to store instance name!\n",name); goto err_name; } vm->instance_id = instance_id; vm->platform = platform; vm->status = VM_STATUS_HALTED; vm->jit_use = JIT_SUPPORT; vm->exec_blk_direct_jump = TRUE; vm->vtty_con_type = VTTY_TYPE_TERM; vm->vtty_aux_type = VTTY_TYPE_NONE; vm->timer_irq_check_itv = VM_TIMER_IRQ_CHECK_ITV; vm->log_file_enabled = TRUE; vm->rommon_vars.filename = vm_build_filename(vm,"rommon_vars"); if (!vm->rommon_vars.filename) goto err_rommon; /* XXX */ rommon_load_file(&vm->rommon_vars); /* create lock file */ if (vm_get_lock(vm) == -1) goto err_lock; /* create log file */ if (vm_create_log(vm) == -1) goto err_log; if (registry_add(vm->name,OBJ_TYPE_VM,vm) == -1) { fprintf(stderr,"VM: Unable to store instance '%s' in registry!\n", vm->name); goto err_reg_add; } m_log("VM","VM %s created.\n",vm->name); return vm; err_reg_add: vm_close_log(vm); err_log: free(vm->lock_file); err_lock: free(vm->rommon_vars.filename); err_rommon: free(vm->name); err_name: free(vm); return NULL; }
/* Hypervisor TCP server */ int hypervisor_tcp_server(char *ip_addr,int tcp_port) { int fd_array[HYPERVISOR_MAX_FD]; struct sockaddr_storage remote_addr; socklen_t remote_len; int i,res,clnt,fd_count,fd_max; struct timeval tv; fd_set fds; /* Initialize all hypervisor modules */ hypervisor_init(); hypervisor_nio_init(); hypervisor_nio_bridge_init(); hypervisor_frsw_init(); hypervisor_atmsw_init(); hypervisor_atm_bridge_init(); hypervisor_ethsw_init(); hypervisor_vm_init(); hypervisor_vm_debug_init(); hypervisor_store_init(); signal(SIGPIPE,sigpipe_handler); if (!tcp_port) tcp_port = HYPERVISOR_TCP_PORT; fd_count = ip_listen(ip_addr,tcp_port,SOCK_STREAM, HYPERVISOR_MAX_FD,fd_array); if (fd_count <= 0) { fprintf(stderr,"Hypervisor: unable to create TCP sockets.\n"); return(-1); } /* Start accepting connections */ m_log("HYPERVISOR","Release %s/%s (tag %s)\n", sw_version,os_name,sw_version_tag); if (ip_addr != NULL) { binding_addr = ip_addr; m_log("HYPERVISOR","Started on IP = %s, TCP port = %d.\n", ip_addr, tcp_port); printf("Hypervisor TCP control server started (IP %s port %d).\n", ip_addr, tcp_port); } else { m_log("HYPERVISOR","Started on TCP port = %d.\n",tcp_port); printf("Hypervisor TCP control server started (port %d).\n",tcp_port); } hypervisor_running = TRUE; while(hypervisor_running) { FD_ZERO(&fds); fd_max = -1; for(i=0;i<fd_count;i++) if (fd_array[i] != -1) { FD_SET(fd_array[i],&fds); if (fd_array[i] > fd_max) fd_max = fd_array[i]; } /* Wait for incoming connections */ tv.tv_sec = 0; tv.tv_usec = 500 * 1000; /* 500 ms */ res = select(fd_max+1,&fds,NULL,NULL,&tv); if (res == -1) { if (errno == EINTR) continue; else perror("hypervisor_tcp_server: select"); } /* Accept connections on signaled sockets */ for(i=0;i<fd_count;i++) { if (fd_array[i] == -1) continue; if (!FD_ISSET(fd_array[i],&fds)) continue; remote_len = sizeof(remote_addr); clnt = accept(fd_array[i],(struct sockaddr *)&remote_addr, &remote_len); if (clnt < 0) { perror("hypervisor_tcp_server: accept"); continue; } /* create a new connection and start a thread to handle it */ if (!hypervisor_create_conn(clnt)) { fprintf(stderr,"hypervisor_tcp_server: unable to create new " "connection for FD %d\n",clnt); close(clnt); } } /* Walk through the connection list to eliminate dead connections */ hypervisor_close_conn_list(TRUE); } /* Close all control sockets */ printf("Hypervisor: closing control sockets.\n"); for(i=0;i<fd_count;i++) { if (fd_array[i] != -1) { shutdown(fd_array[i],2); close(fd_array[i]); } } /* Close all remote client connections */ printf("Hypervisor: closing remote client connections.\n"); hypervisor_close_conn_list(FALSE); m_log("HYPERVISOR","Stopped.\n"); return(0); }
/* Thread for servicing connections */ static void *hypervisor_thread(void *arg) { hypervisor_conn_t *conn = arg; char buffer[512],**tokens; parser_context_t ctx; int res; tokens = NULL; parser_context_init(&ctx); while(conn->active) { if (!fgets(buffer,sizeof(buffer),conn->in)) break; if (!*buffer) continue; /* Tokenize command line */ res = parser_scan_buffer(&ctx,buffer,strlen(buffer)); if (res != 0) { tokens = NULL; if (ctx.error != 0) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"Parse error: %s", parser_strerror(&ctx)); goto free_tokens; } if (ctx.tok_count < 2) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1, "At least a module and a command " "must be specified"); goto free_tokens; } /* Map token list to an array */ tokens = parser_map_array(&ctx); if (!tokens) { hypervisor_send_reply(conn,HSC_ERR_PARSING,1,"No memory"); goto free_tokens; } /* Execute command */ m_log("HYPERVISOR","exec_cmd: "); m_flog_str_array(log_file,ctx.tok_count,tokens); hypervisor_exec_cmd(conn,tokens[0],tokens[1],ctx. tok_count-2,&tokens[2]); free_tokens: free(tokens); tokens = NULL; parser_context_free(&ctx); } } free(tokens); parser_context_free(&ctx); return NULL; }
static void log(const T& ... t) { m_log(t...); }
sa_results sarun( const sa_parameters par, const string dir_init ) { // ----- PREPARE SIMULATED ANNEALING ----- // assume something went wrong until we are sure it didn't sa_results res; res.success = false; // make a folder to work in stringstream tmp; tmp << setfill( '0' ); tmp << "./" << dir_init << '/'; const string dir = tmp.str(); tmp.str( "" ); // folder for images of the model if ( par.take_images != 0 ) { tmp << dir << "images/" ; const string image_dir = tmp.str(); tmp.str( "" ); if ( system( ( "mkdir " + image_dir ).c_str() ) != 0 ) { cout << "ERROR while making the image directory " << dir << endl; return res; } } // start a logfile ofstream run_log( ( dir + "run.log" ).c_str() ); if ( !run_log.is_open() ) { cout << "ERROR while opening the run log file in " << dir << endl; return res; } run_log.precision( numeric_limits<double>::digits10 + 1 ); // write the simulations parameters to the logfile run_log << "Simulated annealing running in " << dir << endl << endl << "--- PARAMETERS ---" << endl << "system = " << par.system_type << endl << "N = " << par.N << endl << "periodic = " << par.periodic << endl << "init = " << par.init << endl << "T = " << par.T_start << "->" << par.T_end << endl << "t_end = " << par.t_end << endl << "coolingschedule = " << par.cooling_schedule << endl << "J = " << par.J << endl << "g = " << par.g << endl << "B = " << par.B << endl << endl; run_log.flush(); // ----- RUN SIMULATED ANNEALING ----- time_t rawtime; time( &rawtime ); run_log << ctime( &rawtime ) << "-> creating the system\n\n"; run_log.flush(); // create a new model SystemModel* model; if ( par.system_type == 1 ) { model = new IsingModel1d( par.N, par.periodic, par.J, par.B, par.T_start, 0, dir ); } else if ( par.system_type == 2 ) { model = new IsingModel2d( par.N, par.periodic, par.J, par.B, par.T_start, 0, dir ); } else if ( par.system_type == 3 ) { model = new IsingModel2dWolff( par.N, par.periodic, par.J, par.T_start, 0, dir ); } else if ( par.system_type == 4 ) { model = new IsingModel2dDipole( par.N, par.periodic, par.J, par.g, par.B, par.T_start, dir ); } else if ( par.system_type == 5 ) { model = new Ising2dDipSqr( par.N, par.periodic, par.J, par.g, par.B, par.T_start, 0, dir ); } else if ( par.system_type == 6 ) { model = new Ising2dDipHC( par.N, par.J, par.g, par.B, par.T_start, 0, dir ); // ___ ADD CUSTOM SYSTEM MODELS HERE ___ } else { cout << "ERROR creating the model system in " << dir << endl; return res; } if ( model->prepare( par.init ) == false ) { cout << "ERROR preparing the models spins in " << dir << endl; delete model; return res; } double ( *Toft )( double const&, double const&, unsigned long int const&, unsigned long int const& ); // select a cooling schedule if ( par.cooling_schedule == 'l' ) { Toft = &linear_cooling; } else if ( par.cooling_schedule == 'p' ) { Toft = ¶bolic_cooling; } else { cout << "ERROR selecting the cooling schedule in " << dir << endl; delete model; return res; } // open measurement logfiles ofstream h_log( ( dir + "h.log" ).c_str() ); ofstream m_log( ( dir + "m.log" ).c_str() ); if ( !( h_log.is_open() && m_log.is_open() ) ) { cout << "ERROR while opening measurement log files in " << dir << endl; delete model; return res; } time( &rawtime ); run_log << ctime( &rawtime ) << "-> simulation started\n\n"; run_log.flush(); // sample loop while ( model->t() <= par.t_end ) { double T_now = Toft( par.T_start, par.T_end, par.t_end, model->t() ); model->set_T( T_now ); // write this sample's properties to the logfile h_log << model->t() << ' ' << T_now << ' ' << model->h() << endl; m_log << model->t() << ' ' << T_now << ' ' << model->m() << endl; // make an image of the system if ( ( par.take_images != 0 ) && ( model->t() % par.take_images == 0 ) ) { tmp << dir << "images/" << setw( 9 ) << model->t() << ".png"; const string image_file = tmp.str(); tmp.str( "" ); model->get_image().write( image_file ); } // do t_boost monte carlo steps for ( unsigned int i = 0; i < par.t_boost; ++i ) { model->mcstep(); } } // all measurements done ... let's tidy things up delete model; h_log.close(); m_log.close(); time( &rawtime ); run_log << ctime( &rawtime ) << "-> simulated annealing finished"; run_log.flush(); // ----- PLOTTING ----- if ( par.run_plot ) { time( &rawtime ); run_log << ctime( &rawtime ) << "-> starting to plot the results\n\n"; run_log.flush(); } // run_plot ofstream run_plot( ( dir + "run_plot.gnu" ).c_str() ); if ( !run_plot.is_open() ) { cout << "ERROR while opening run_plot.gnu in " << dir << endl; return res; } tmp << "system_type = " << par.system_type << ", N = " << par.N << ", periodic = " << par.periodic << ", init = " << par.init << ", T = " << par.T_start << "->" << par.T_end << ", t_end = " << par.t_end << "cooling_schedule = " << par.cooling_schedule << ", J = " << par.J << ", g = " << par.g << ", B = " << par.B; int plot_size = ( par.t_end < 1600 ) ? 1600 : min( ( unsigned long int )10000, par.t_end ); run_plot << " set terminal pngcairo size " << plot_size << ",600 \n\ set output 'run_plot.png' \n\n\ set multiplot layout 2,1 title '" << tmp.str() << "'\n\ set grid x y \n\ set mxtics 10 \n\ set key top left \n\ plot 'm.log' using 1:3 with lines ls 2 title 'magnetization per spin' \n\ plot 'h.log' using 1:3 with lines ls 1 title 'energy per spin' \n"; tmp.str( "" ); run_plot.close(); if ( par.run_plot && par.t_end <= 1e5 ) { if ( system( ( "cd " + dir + " ; gnuplot run_plot.gnu" ).c_str() ) != 0 ) { cout << "ERROR while running gnuplot run_plot.gnu in " << dir << endl; return res; } } // everything is fine ... return the results! time( &rawtime ); run_log << ctime( &rawtime ) << "-> everything finished!\n\n"; run_log.close(); res.success = true; return res; }
m::system::Log& getLog() { return m_log(); }
m::system::Log& getLog(m::LogLevel level) { return m_log(level); }