/** * This code assumes that the server replies the exact text the client sent it (as opposed to the practical session example) */ int main (int argc, char *argv[]) { if (argc < 3) { std::cerr << "Usage: " << argv[0] << " host port" << std::endl << std::endl; return -1; } std::string host = argv[1]; short port = atoi(argv[2]); ConnectionHandler connectionHandler(host, port); if (!connectionHandler.connect()) { std::cerr << "Cannot connect to " << host << ":" << port << std::endl; return 1; } UserInhandler usr(connectionHandler); SocketHandler sh(connectionHandler); boost::thread th1(&UserInhandler::run, &usr); boost::thread th2(&SocketHandler::run, &sh); th1.join(); th2.join(); return 1; }
/** Free the users array. */ void free_users(gboolean reset) { #ifdef DEBUG printf("free_users\n"); #endif gint i; if(users == NULL) { if(reset) users = g_array_new(FALSE, FALSE, sizeof(User)); return; } for(i=0; i<users->len; i++) free_user(&usr(i)); free_g_array(&users); if(reset) users = g_array_new(FALSE, FALSE, sizeof(User)); }
WinAbsSD::WinAbsSD(const ustring& name, const ustring& group, bool prot): m_owner(nullptr), m_group(nullptr), m_dacl(nullptr), m_sacl(nullptr) { m_sd = alloc(SECURITY_DESCRIPTOR_MIN_LENGTH); CheckApi(::InitializeSecurityDescriptor(m_sd, SECURITY_DESCRIPTOR_REVISION)); m_dacl = WinDacl::create(64); if (!name.empty()) { try { Sid usr(name.c_str()); DWORD ownerSize = SECURITY_MAX_SID_SIZE; m_owner = (PSID)::LocalAlloc(LPTR, ownerSize); usr.copy_to(m_owner, ownerSize); } catch (...) { } } if (!group.empty()) { try { DWORD groupSize = SECURITY_MAX_SID_SIZE; m_group = (PSID)::LocalAlloc(LPTR, groupSize); Sid grp(group.c_str()); grp.copy_to(m_group, groupSize); } catch (...) { } } set_owner(m_sd, m_owner); set_group(m_sd, m_group); set_dacl(m_sd, m_dacl); CheckApi(::IsValidSecurityDescriptor(m_sd)); set_protect(prot); }
Array lookupDefinedConstants(bool categorize /*= false */) { assert(s_stringDataMap); Array usr(RDS::s_constants()); Array sys; for (StringDataMap::const_iterator it = s_stringDataMap->begin(); it != s_stringDataMap->end(); ++it) { if (it->second.bound()) { Array *tbl = (categorize && RDS::isPersistentHandle(it->second.handle())) ? &sys : &usr; auto& tv = *it->second; if (tv.m_type != KindOfUninit) { StrNR key(const_cast<StringData*>(to_sdata(it->first))); tbl->set(key, tvAsVariant(&tv), true); } else if (tv.m_data.pref) { StrNR key(const_cast<StringData*>(to_sdata(it->first))); ClassInfo::ConstantInfo* ci = (ClassInfo::ConstantInfo*)(void*)tv.m_data.pref; auto cns = ci->getDeferredValue(); if (cns.isInitialized()) { tbl->set(key, cns, true); } } } } if (categorize) { Array ret; ret.set(s_user, usr); ret.set(s_Core, sys); return ret; } else { return usr; } }
bool userfile::write (const perform & /* a_perf */ ) { std::ofstream file(m_name.c_str(), std::ios::out | std::ios::trunc); if (! file.is_open()) { fprintf(stderr, "? error opening [%s] for writing\n", m_name.c_str()); return false; } dump_setting_summary(); /* * Header commentary. Write out comments about the nature of this file. */ if (rc().legacy_format()) { file << "# Sequencer64 user configuration file (legacy Seq24 0.9.2 format)\n"; } else file << "# Sequencer64 0.9.20 (and above) user configuration file\n"; file << "#\n" "# Created by reading the following file and writing it out via the\n" "# Sequencer64 application:\n" "#\n" "# https://raw.githubusercontent.com/vext01/" "seq24/master/seq24usr.example\n" ; file << "#\n" "# This is an example for a sequencer64.usr file. Edit and place in\n" "# your home directory. It allows you to give an alias to each\n" "# MIDI bus, MIDI channel, and MIDI control codes per channel.\n" ; file << "#\n" "# 1. Define your instruments and their control-code names,\n" "# if they have them.\n" "# 2. Define a MIDI bus, its name, and what instruments are\n" "# on which channel.\n" ; file << "#\n" "# In the following MIDI buss definitions, channels are counted\n" "# from 0 to 15, not 1 to 16. Instruments unspecified are set to\n" "# -1 (SEQ64_GM_INSTRUMENT_FLAG) and are GM (General MIDI).\n" ; /* * [user-midi-bus-definitions] */ file << "\n" << "[user-midi-bus-definitions]\n" << "\n" << usr().bus_count() << " # number of user-defined MIDI busses\n" ; if (usr().bus_count() == 0) file << "\n"; /* * [user-midi-bus-x] */ for (int buss = 0; buss < usr().bus_count(); ++buss) { file << "\n[user-midi-bus-" << buss << "]\n\n"; const user_midi_bus & umb = usr().bus(buss); if (umb.is_valid()) { file << "# Device name for this buss:\n" << "\n" << umb.name() << "\n" << "\n" << "# Number of channels:\n" << "\n" << umb.channel_count() << "\n" << "\n" << "# channel and instrument (or program) number\n" << "\n" ; for (int channel = 0; channel < umb.channel_max(); ++channel) { if (umb.instrument(channel) != SEQ64_GM_INSTRUMENT_FLAG) file << channel << " " << umb.instrument(channel) << "\n"; #if defined PLATFORM_DEBUG && defined SHOW_IGNORED_ITEMS else { fprintf ( stderr, "bus %d, channel %d (%d) ignored\n", buss, channel, umb.instrument(channel) ); } #endif } } else file << "? This buss specification is invalid\n"; file << "\n# End of buss definition " << buss << "\n"; } file << "# In the following MIDI instrument definitions, active controller\n" "# numbers (i.e. supported by the instrument) are paired with\n" "# the (optional) name of the controller supported.\n" ; /* * [user-instrument-definitions] */ file << "\n" << "[user-instrument-definitions]\n" << "\n" << usr().instrument_count() << " # instrument list count\n" ; if (usr().instrument_count() == 0) file << "\n"; /* * [user-instrument-x] */ for (int inst = 0; inst < usr().instrument_count(); ++inst) { file << "\n[user-instrument-" << inst << "]\n" << "\n" ; const user_instrument & uin = usr().instrument(inst); if (uin.is_valid()) { file << "# Name of instrument:\n" << "\n" << uin.name() << "\n" << "\n" << "# Number of MIDI controller values:\n" << "\n" << uin.controller_count() << "\n" << "\n" << "# controller number and (optional) name:\n" << "\n" ; for (int ctlr = 0; ctlr < uin.controller_max(); ++ctlr) { if (uin.controller_active(ctlr)) file << ctlr << " " << uin.controller_name(ctlr) << "\n"; #if defined PLATFORM_DEBUG && defined SHOW_IGNORED_ITEMS else { fprintf ( stderr, "instrument %d, controller %d (%s) ignored\n", inst, ctlr, uin.controller_name(ctlr).c_str() ); } #endif } } else { file << "? This instrument specification is invalid\n"; } file << "\n# End of instrument/controllers definition " << inst << "\n\n" ; } /* * [user-interface settings] * * These are new items stored in the user file. The settings are obtained * from member functions of the user_settings class. Not all members are * saved to the "user" configuration file. */ if (! rc().legacy_format()) { file << "# ======== Sequencer64-Specific Variables Section ========\n" "\n" "[user-interface-settings]\n" "\n" "# These settings specify the soon-to-be-modifiable configuration\n" "# of some of the Sequencer64 user-interface elements.\n" ; file << "\n" "# Specifies the style of the main-window grid of patterns.\n" "#\n" "# 0 = Normal style, matches the GTK theme, has brackets.\n" "# 1 = White grid boxes that have brackets.\n" "# 2 = Black grid boxes (no brackets).\n" "\n" << usr().grid_style() << " # grid_style\n" ; file << "\n" "# Specifies box style of an empty slot in the main-window grid.\n" "#\n" "# 0 = Draw a one-pixel box outline around the pattern slot.\n" "# 1 = Draw brackets on the sides of the pattern slot.\n" "# 2 to 30 = Make the brackets thicker and thicker.\n" "# -1 = Same as 0, draw a box outline one-pixel thick.\n" "# -2 to -30 = Draw a box outline, thicker and thicker.\n" "\n" << usr().grid_brackets() << " # grid_brackets\n" ; file << "\n" "# Specifies the number of rows in the main window.\n" "# At present, only a value of 4 is supportable.\n" "# In the future, we hope to support an alternate value of 8.\n" "\n" << usr().mainwnd_rows() << " # mainwnd_rows\n" ; file << "\n" "# Specifies the number of columns in the main window.\n" "# At present, only a value of 8 is supportable.\n" "\n" << usr().mainwnd_cols() << " # mainwnd_cols\n" ; file << "\n" "# Specifies the maximum number of sets, which defaults to 1024.\n" "# It is currently never necessary to change this value.\n" "\n" << usr().max_sets() << " # max_sets\n" ; file << "\n" "# Specifies the border width in the main window.\n" "\n" << usr().mainwid_border() << " # mainwid_border\n" ; file << "\n" "# Specifies the border spacing in the main window.\n" "\n" << usr().mainwid_spacing() << " # mainwid_spacing\n" ; file << "\n" "# Specifies some quantity, it is not known what it means.\n" "\n" << usr().control_height() << " # control_height\n" ; file << "\n" "# Specifies the initial zoom for the piano rolls. Ranges from 1.\n" "# to 512 (the legacy maximum was 32), and defaults to 2 unless\n" "# changed here. Note that large PPQN values will require larger\n" "# zoom values in order to look good in the sequence editor.\n" "# Sequencer64 adapts the zoom to the PPQN, if zoom is set to 0.\n" "# \n" "\n" << usr().zoom() << " # default zoom (0 = auto-adjust to PPQN)\n" ; /* * This boolean affects the behavior of the scale, key, and background * sequence features. */ file << "\n" "# Specifies if the key, scale, and background sequence are to be\n" "# applied to all sequences, or to individual sequences. The\n" "# behavior of Seq24 was to apply them to all sequences. But\n" "# Sequencer64 takes it further by applying it immediately, and\n" "# by saving to the end of the MIDI file. Note that these three\n" "# values are stored in the MIDI file, not this configuration file.\n" "# Also note that reading MIDI files not created with this feature\n" "# will pick up this feature if active, and the file gets saved.\n" "# It is contagious.\n" ; file << "#\n" "# 0 = Allow each sequence to have its own key/scale/background.\n" "# Settings are saved with each sequence.\n" "# 1 = Apply these settings globally (similar to seq24).\n" "# Settings are saved in the global final section of the file.\n" "\n" << (usr().global_seq_feature() ? "1" : "0") << " # global_seq_feature\n" ; /* * The usage of the old versus new font is now a run-time option. */ file << "\n" "# Specifies if the old, console-style font, or the new anti-\n" "# aliased font, is to be used as the font throughout the GUI.\n" "# In legacy mode, the old font is the default.\n" "#\n" "# 0 = Use the old-style font.\n" "# 1 = Use the new-style font.\n" "\n" << (usr().use_new_font() ? "1" : "0") << " # use_new_font\n" ; file << "\n" "# Specifies if the user-interface will support two song editor\n" "# windows being shown at the same time. This makes it easier to\n" "# edit songs with a large number of sequences.\n" "#\n" "# 0 = Allow only one song editor (performance editor).\n" "# 1 = Allow two song editors.\n" "\n" << (usr().allow_two_perfedits() ? "1" : "0") << " # allow_two_perfedits\n" ; file << "\n" "# Specifies the number of 4-measure blocks for horizontal page\n" "# scrolling in the song editor. The old default, 1, is a bit\n" "# small. The new default is 4. The legal range is 1 to 6, where\n" "# 6 is the width of the whole performance piano roll view.\n" "\n" << usr().perf_h_page_increment() << " # perf_h_page_increment\n" ; file << "\n" "# Specifies the number of 1-track blocks for vertical page\n" "# scrolling in the song editor. The old default, 1, is a bit\n" "# small. The new default is 8. The legal range is 1 to 18, where\n" "# 18 is about the height of the whole performance piano roll view.\n" "\n" << usr().perf_v_page_increment() << " # perf_v_page_increment\n" ; file << "\n" "# Specifies if the progress bar is colored black, or a different\n" "# color. The following integer color values are supported:\n" "# \n" "# 0 = black\n" "# 1 = dark red\n" "# 2 = dark green\n" "# 3 = dark orange\n" "# 4 = dark blue\n" "# 5 = dark magenta\n" "# 6 = dark cyan\n" "\n" << usr().progress_bar_colored() // (usr().progress_bar_colored() ? "1" : "0") << " # progress_bar_colored\n" ; file << "\n" "# Specifies if the progress bar is thicker. The default is 1\n" "# pixel. The 'thick' value is 2 pixels. (More than that is not\n" "# useful. Set this value to 1 to enable the feature, 0 to disable\n" "# it.\n" "\n" << (usr().progress_bar_thick() ? "1" : "0") << " # progress_bar_thick\n" ; file << "\n" "# Specifies using an alternate (darker) color palette. The\n" "# default is the normal palette. Not all items in the user\n" "# interface are altered by this setting, and it's not perfect.\n" "# Set this value to 1 to enable the feature, 0 to disable it.\n" "\n" << (usr().inverse_colors() ? "1" : "0") << " # inverse_colors\n" ; file << "\n" "# Specifies the window redraw rate for all windows that support\n" "# that concept. The default is 40 ms. Some windows used 25 ms.\n" "\n" << usr().window_redraw_rate() << " # window_redraw_rate\n" ; file << "\n" "# Specifies using icons for some of the user-interface buttons\n" "# instead of text buttons. This is purely a preference setting.\n" "# If 0, text is used in some buttons (the main window buttons).\n" "# Otherwise, icons are used. One will have to experiment :-).\n" "\n" << usr().use_more_icons() << " # use_more_icons (currently affects only main window)\n" ; } /* * [user-midi-settings] */ if (! rc().legacy_format()) { file << "\n" "[user-midi-settings]\n" "\n" "# These settings specify MIDI-specific value that might be\n" "# better off as variables, rather than constants.\n" "\n" "# Specifies parts-per-quarter note to use, if the MIDI file.\n" "# does not override it. Default is 192, but we'd like to go\n" "# higher than that. BEWARE: STILL GETTING IT TO WORK!\n" "\n" << usr().midi_ppqn() << " # midi_ppqn\n" ; file << "\n" "# Specifies the default beats per measure, or beats per bar.\n" "# The default value is 4.\n" "\n" << usr().midi_beats_per_bar() << " # midi_beats_per_measure/bar\n" ; file << "\n" "# Specifies the default beats per minute. The default value\n" "# is 120, and the legal range is 20 to 500.\n" "\n" << usr().midi_beats_per_minute() << " # midi_beats_per_minute\n" ; file << "\n" "# Specifies the default beat width. The default value is 4.\n" "\n" << usr().midi_beat_width() << " # midi_beat_width\n" ; file << "\n" "# Specifies the buss-number override, the same as the --bus\n" "# command-line option. The default value is -1, which\n" "# means that there is no buss override. If a value\n" "# from 0 to 31 is given, then that buss value overrides all\n" "# buss values specified in all sequences/patterns.\n" "# Change this value from -1 only to use a single\n" "# output buss, either for testing or convenience. And don't\n" "# save the MIDI file afterwards, unless you really want to change\n" "# all of its buss values!\n" "\n" ; int bo = int(usr().midi_buss_override()); /* writing char no good */ if (SEQ64_NO_BUSS_OVERRIDE(bo)) file << "-1" << " # midi_buss_override (disabled)\n"; else file << bo << " # midi_buss_override (enabled, careful!)\n"; } /* * End of file. */ file << "\n" << "# End of " << m_name << "\n#\n" << "# vim: sw=4 ts=4 wm=4 et ft=sh\n" /* ft=sh for nice colors */ ; file.close(); return true; }
bool userfile::parse (perform & /* a_perf */) { std::ifstream file(m_name.c_str(), std::ios::in | std::ios::ate); if (! file.is_open()) { fprintf(stderr, "? error opening [%s]\n", m_name.c_str()); return false; } file.seekg(0, std::ios::beg); /* seek to start */ /* * Header commentary is skipped during parsing. * * \change ca 2016-04-04 * Next, if we're using the manual or auto ALSA port options * specified in the RC file, we do want to override the ports that * the queries of the ALSA system find. Otherwise, we might want to * reveal the names obtained by port detection for ALSA, and do not * want to override the names of the ports that are found. */ if (! rc().reveal_alsa_ports()) { /* * [user-midi-bus-definitions] */ line_after(file, "[user-midi-bus-definitions]"); /* find the tag */ int buses = 0; sscanf(m_line, "%d", &buses); /* atavistic! */ /* * [user-midi-bus-x] */ for (int bus = 0; bus < buses; ++bus) { std::string label = make_section_name("user-midi-bus", bus); line_after(file, label); if (usr().add_bus(m_line)) { (void) next_data_line(file); int instruments = 0; int instrument; int channel; sscanf(m_line, "%d", &instruments); for (int j = 0; j < instruments; ++j) { (void) next_data_line(file); sscanf(m_line, "%d %d", &channel, &instrument); usr().set_bus_instrument(bus, channel, instrument); } } else { fprintf ( stderr, "? error adding %s (line = '%s')\n", label.c_str(), m_line ); } } } /* * [user-instrument-definitions] */ line_after(file, "[user-instrument-definitions]"); int instruments = 0; sscanf(m_line, "%d", &instruments); /* * [user-instrument-x] */ for (int i = 0; i < instruments; ++i) { std::string label = make_section_name("user-instrument", i); line_after(file, label); if (usr().add_instrument(m_line)) { char ccname[SEQ64_LINE_MAX]; int ccs = 0; (void) next_data_line(file); sscanf(m_line, "%d", &ccs); for (int j = 0; j < ccs; ++j) { int c = 0; (void) next_data_line(file); ccname[0] = 0; // clear the buffer sscanf(m_line, "%d %[^\n]", &c, ccname); if (c >= 0 && c < SEQ64_MIDI_CONTROLLER_MAX) // 128 { usr().set_instrument_controllers ( i, c, std::string(ccname), true ); } else { fprintf ( stderr, "? illegal controller value %d for '%s'\n", c, label.c_str() ); } } } else { fprintf ( stderr, "? error adding %s (line = '%s')\n", label.c_str(), m_line ); } } /* * [user-interface-settings] * * These are new items stored in the user file. Only variables whose * effects we can be completely sure of are read from this section, and * used, at this time. More to come. */ if (! rc().legacy_format()) { int scratch = 0; line_after(file, "[user-interface-settings]"); sscanf(m_line, "%d", &scratch); usr().grid_style(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().grid_brackets(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().mainwnd_rows(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().mainwnd_cols(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().max_sets(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().mainwid_border(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().mainwid_spacing(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().control_height(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().zoom(scratch); /* * This boolean affects the behavior of the scale, key, and background * sequence features, but their actual values are stored in the MIDI * file, not in the "user" configuration file. */ (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().global_seq_feature(scratch != 0); /* * The user-interface font is now selectable at run time. Old versus * new font. */ (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().use_new_font(scratch != 0); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().allow_two_perfedits(scratch != 0); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().perf_h_page_increment(scratch); (void) next_data_line(file); sscanf(m_line, "%d", &scratch); usr().perf_v_page_increment(scratch); /* * Here, we start checking the lines, on the theory that these * very new (2016-02-14) items might mess up people who already have * older Sequencer64 "user" configuration files. */ if (next_data_line(file)) { sscanf(m_line, "%d", &scratch); /* now an int */ usr().progress_bar_colored(scratch); /* pick a color */ if (next_data_line(file)) { sscanf(m_line, "%d", &scratch); usr().progress_bar_thick(scratch != 0); } if (next_data_line(file)) { sscanf(m_line, "%d", &scratch); if (scratch <= 1) /* boolean? */ { usr().inverse_colors(scratch != 0); if (next_data_line(file)) sscanf(m_line, "%d", &scratch); /* get redraw */ } if (scratch < SEQ64_MINIMUM_REDRAW) scratch = SEQ64_MINIMUM_REDRAW; else if (scratch > SEQ64_MAXIMUM_REDRAW) scratch = SEQ64_MAXIMUM_REDRAW; usr().window_redraw_rate(scratch); } if (next_data_line(file)) { sscanf(m_line, "%d", &scratch); if (scratch <= 1) /* boolean? */ usr().use_more_icons(scratch != 0); } } usr().normalize(); /* calculate derived values */ } else { /* * Legacy values. Compare to user_settings::set_defaults(). */ usr().grid_style(0); usr().grid_brackets(1); usr().mainwnd_rows(4); usr().mainwnd_cols(8); usr().max_sets(32); usr().mainwid_border(0); usr().mainwid_spacing(2); usr().control_height(0); usr().zoom(SEQ64_DEFAULT_ZOOM); usr().global_seq_feature(false); usr().use_new_font(false); usr().allow_two_perfedits(false); usr().perf_h_page_increment(1); usr().perf_v_page_increment(1); usr().progress_bar_colored(0); usr().progress_bar_thick(false); usr().inverse_colors(false); usr().window_redraw_rate(c_redraw_ms); } /* * [user-midi-settings] */ if (! rc().legacy_format()) { line_after(file, "[user-midi-settings]"); int scratch = 0; sscanf(m_line, "%d", &scratch); usr().midi_ppqn(scratch); next_data_line(file); sscanf(m_line, "%d", &scratch); usr().midi_beats_per_bar(scratch); next_data_line(file); sscanf(m_line, "%d", &scratch); usr().midi_beats_per_minute(scratch); next_data_line(file); sscanf(m_line, "%d", &scratch); usr().midi_beat_width(scratch); next_data_line(file); sscanf(m_line, "%d", &scratch); usr().midi_buss_override(char(scratch)); } /* * We have all of the data. Close the file. */ dump_setting_summary(); file.close(); /* End Of File, EOF, done! */ return true; }
void userfile::dump_setting_summary () { usr().dump_summary(); }
/** * Vertex update function. */ void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) { if (gcontext.iteration == 0) { if (is_user(vertex.id()) && vertex.num_outedges() > 0) { vertex_data& user = latent_factors_inmem[vertex.id()]; user.pvec = zeros(D*3); for(int e=0; e < vertex.num_outedges(); e++) { rbm_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; float observation = vertex.edge(e)->get_data(); int r = (int)(observation/rbm_scaling); assert(r < rbm_bins); mov.bi[r]++; } } return; } else if (gcontext.iteration == 1) { if (vertex.num_inedges() > 0) { rbm_movie mov = latent_factors_inmem[vertex.id()]; setRand2(mov.w, D*rbm_bins, 0.001); for(int r = 0; r < rbm_bins; ++r) { mov.bi[r] /= (double)vertex.num_inedges(); mov.bi[r] = log(1E-9 + mov.bi[r]); if (mov.bi[r] > 1000) { assert(false); Rcpp::Rcerr<<"Numerical overflow" <<std::endl; } } } return; //done with initialization } //go over all user nodes if (is_user(vertex.id()) && vertex.num_outedges()) { vertex_data & user = latent_factors_inmem[vertex.id()]; user.pvec = zeros(3*D); rbm_user usr(user); vec v1 = zeros(vertex.num_outedges()); //go over all ratings for(int e=0; e < vertex.num_outedges(); e++) { float observation = vertex.edge(e)->get_data(); rbm_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; int r = (int)(observation / rbm_scaling); assert(r < rbm_bins); for(int k=0; k < D; k++) { usr.h[k] += mov.w[D*r + k]; assert(!std::isnan(usr.h[k])); } } for(int k=0; k < D; k++) { usr.h[k] = sigmoid(usr.h[k]); if (drand48() < usr.h[k]) usr.h0[k] = 1; else usr.h0[k] = 0; } int i = 0; double prediction; for(int e=0; e < vertex.num_outedges(); e++) { rbm_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; float observation = vertex.edge(e)->get_data(); predict1(usr, mov, observation, prediction); int vi = (int)(prediction / rbm_scaling); v1[i] = vi; i++; } i = 0; for(int e=0; e < vertex.num_outedges(); e++) { rbm_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; int r = (int)v1[i]; for (int k=0; k< D; k++) { usr.h1[k] += mov.w[r*D+k]; } i++; } for (int k=0; k < D; k++) { usr.h1[k] = sigmoid(usr.h1[k]); if (drand48() < usr.h1[k]) usr.h1[k] = 1; else usr.h1[k] = 0; } i = 0; for(int e=0; e < vertex.num_outedges(); e++) { rbm_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; float observation = vertex.edge(e)->get_data(); double prediction; rbm_predict(user, mov, observation, prediction, NULL); double pui = prediction / rbm_scaling; double rui = observation / rbm_scaling; rmse_vec[omp_get_thread_num()] += (pui - rui) * (pui - rui); //nn += 1.0; int vi0 = (int)(rui); int vi1 = (int)v1[i]; for (int k = 0; k < D; k++) { mov.w[D*vi0+k] += rbm_alpha * (usr.h0[k] - rbm_beta * mov.w[vi0*D+k]); assert(!std::isnan(mov.w[D*vi0+k])); mov.w[D*vi1+k] -= rbm_alpha * (usr.h1[k] + rbm_beta * mov.w[vi1*D+k]); assert(!std::isnan(mov.w[D*vi1+k])); } i++; } } }
af::Msg* threadProcessMsgCase( ThreadArgs * i_args, af::Msg * i_msg) { //i_msg->stdOut(); //printf("IM=%d LM=%d MM=%d\n", i_msg->getMagicNumber(), af::Msg::Magic, af::Environment::getMagicMode()); af::Msg * o_msg_response = NULL; switch( i_msg->type()) { case af::Msg::TVersionMismatch: { AFCommon::QueueLogError( i_msg->v_generateInfoString( false)); o_msg_response = new af::Msg( af::Msg::TVersionMismatch, 1); break; } case af::Msg::TMagicMismatch: { std::string err = "Magick number mismatch: recieved "; err += af::itos( i_msg->getMagicNumber()) + " != " + af::itos( af::Msg::Magic) += " local."; AFCommon::QueueLogError( err); o_msg_response = new af::Msg( af::Msg::TMagicMismatch, 1); break; } case af::Msg::TMagicNumber: { std::string msg = "Magick Number " + af::itos( af::Msg::Magic) + " changed to " + af::itos( i_msg->int32()); AFCommon::QueueLog( msg); o_msg_response = new af::Msg(); o_msg_response->setString( msg); af::Msg::Magic = i_msg->int32(); break; } case af::Msg::TInvalid: { AFCommon::QueueLogError( std::string("Invalid message recieved: ") + i_msg->v_generateInfoString( false)); break; } case af::Msg::TNULL: case af::Msg::TDATA: case af::Msg::TTESTDATA: case af::Msg::TStringList: { i_msg->stdOutData(); break; } case af::Msg::THTTP: case af::Msg::TJSON: { return threadProcessJSON( i_args, i_msg); } case af::Msg::TString: { std::string str = i_msg->getString(); if( str.empty()) break; AFCommon::QueueLog( str); AfContainerLock mLock( i_args->monitors, AfContainerLock::WRITELOCK); i_args->monitors->sendMessage( str); break; } case af::Msg::TStatRequest: { o_msg_response = new af::Msg; af::statwrite( o_msg_response); break; } case af::Msg::TConfirm: { printf("Thread process message: Msg::TConfirm: %d\n", i_msg->int32()); i_args->msgQueue->pushMsg( new af::Msg( af::Msg::TConfirm, 1)); o_msg_response = new af::Msg( af::Msg::TConfirm, 1 - i_msg->int32()); break; } case af::Msg::TConfigLoad: { AfContainerLock jlock( i_args->jobs, AfContainerLock::WRITELOCK); AfContainerLock rlock( i_args->renders, AfContainerLock::WRITELOCK); AfContainerLock tlock( i_args->talks, AfContainerLock::WRITELOCK); AfContainerLock ulock( i_args->users, AfContainerLock::WRITELOCK); printf("\n ========= RELOADING CONFIG =========\n\n"); std::string message; if( af::Environment::reload()) { message = "Reloaded successfully."; printf("\n ========= CONFIG RELOADED SUCCESSFULLY =========\n\n"); } else { message = "Failed, see server logs fo details."; printf("\n ========= CONFIG RELOADING FAILED =========\n\n"); } o_msg_response = new af::Msg(); o_msg_response->setString( message); break; } case af::Msg::TFarmLoad: { AfContainerLock mLock( i_args->monitors, AfContainerLock::WRITELOCK); AfContainerLock rlock( i_args->renders, AfContainerLock::WRITELOCK); printf("\n ========= RELOADING FARM =========\n\n"); std::string message; if( af::loadFarm( true)) { RenderContainerIt rendersIt( i_args->renders); for( RenderAf *render = rendersIt.render(); render != NULL; rendersIt.next(), render = rendersIt.render()) { render->getFarmHost(); i_args->monitors->addEvent( af::Msg::TMonitorRendersChanged, render->getId()); } message = "Reloaded successfully."; printf("\n ========= FARM RELOADED SUCCESSFULLY =========\n\n"); } else { message = "Failed, see server logs fo details. Check farm with \"afcmd fcheck\" at first."; printf("\n ========= FARM RELOADING FAILED =========\n\n"); } o_msg_response = new af::Msg(); o_msg_response->setString( message); break; } // ---------------------------------- Monitor ---------------------------------// case af::Msg::TMonitorRegister: { AfContainerLock lock( i_args->monitors, AfContainerLock::WRITELOCK); MonitorAf * newMonitor = new MonitorAf( i_msg); newMonitor->setAddressIP( i_msg->getAddress()); o_msg_response = i_args->monitors->addMonitor( newMonitor); break; } case af::Msg::TMonitorUpdateId: { AfContainerLock lock( i_args->monitors, AfContainerLock::READLOCK); if( i_args->monitors->updateId( i_msg->int32())) { o_msg_response = new af::Msg( af::Msg::TMonitorId, i_msg->int32()); } else { o_msg_response = new af::Msg( af::Msg::TMonitorId, 0); } break; } case af::Msg::TMonitorsListRequest: { AfContainerLock lock( i_args->monitors, AfContainerLock::READLOCK); o_msg_response = i_args->monitors->generateList( af::Msg::TMonitorsList); break; } case af::Msg::TMonitorsListRequestIds: { AfContainerLock lock( i_args->monitors, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->monitors->generateList( af::Msg::TMonitorsList, ids); break; } case af::Msg::TMonitorLogRequestId: { AfContainerLock lock( i_args->monitors, AfContainerLock::READLOCK); MonitorContainerIt it( i_args->monitors); MonitorAf* node = it.getMonitor( i_msg->int32()); if( node == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); o_msg_response->setStringList( node->getLog()); break; } // ---------------------------------- Talk ---------------------------------// case af::Msg::TTalkRegister: { AfContainerLock mlock( i_args->monitors, AfContainerLock::WRITELOCK); AfContainerLock tlock( i_args->talks, AfContainerLock::WRITELOCK); TalkAf * newTalk = new TalkAf( i_msg); newTalk->setAddressIP( i_msg->getAddress()); o_msg_response = i_args->talks->addTalk( newTalk, i_args->monitors); break; } case af::Msg::TTalksListRequest: { AfContainerLock lock( i_args->talks, AfContainerLock::READLOCK); o_msg_response = i_args->talks->generateList( af::Msg::TTalksList); break; } case af::Msg::TTalksListRequestIds: { AfContainerLock lock( i_args->talks, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->talks->generateList( af::Msg::TTalksList, ids); break; } case af::Msg::TTalkUpdateId: { AfContainerLock lock( i_args->talks, AfContainerLock::READLOCK); if( i_args->talks->updateId( i_msg->int32())) { o_msg_response = i_args->talks->generateList( af::Msg::TTalksList); } else { o_msg_response = new af::Msg( af::Msg::TTalkId, 0); } break; } case af::Msg::TTalkDistributeData: { AfContainerLock lock( i_args->talks, AfContainerLock::READLOCK); i_args->talks->distributeData( i_msg); break; } // ---------------------------------- Render -------------------------------// case af::Msg::TRenderRegister: { //printf("case af::Msg::TRenderRegister:\n"); AfContainerLock mLock( i_args->monitors, AfContainerLock::WRITELOCK); AfContainerLock rLock( i_args->renders, AfContainerLock::WRITELOCK); RenderAf * newRender = new RenderAf( i_msg); newRender->setAddressIP( i_msg->getAddress()); o_msg_response = i_args->renders->addRender( newRender, i_args->monitors); break; } case af::Msg::TRenderUpdate: { //printf("case af::Msg::TRenderUpdate:\n"); AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); af::Render render_up( i_msg); //printf("Msg::TRenderUpdate: %s - %s\n", render_up.getName().toUtf8().data(), time2Qstr( time(NULL)).toUtf8().data()); RenderContainerIt rendersIt( i_args->renders); RenderAf* render = rendersIt.getRender( render_up.getId()); int id = 0; // If there is not such render, a zero id will be send. // It is a signal for client to register again (may be server was restarted). if((render != NULL) && ( render->update( &render_up))) { id = render->getId(); } o_msg_response = new af::Msg( af::Msg::TRenderId, id); break; } case af::Msg::TRendersListRequest: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); o_msg_response = i_args->renders->generateList( af::Msg::TRendersList); break; } case af::Msg::TRendersListRequestIds: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->renders->generateList( af::Msg::TRendersList, ids); break; } case af::Msg::TRendersResourcesRequestIds: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->renders->generateList( af::Msg::TRendersResources, ids); break; } case af::Msg::TRenderLogRequestId: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); RenderContainerIt rendersIt( i_args->renders); RenderAf* render = rendersIt.getRender( i_msg->int32()); if( render == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg; o_msg_response->setStringList( render->getLog()); break; } case af::Msg::TRenderTasksLogRequestId: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); RenderContainerIt rendersIt( i_args->renders); RenderAf* render = rendersIt.getRender( i_msg->int32()); if( render == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg; if( render->getTasksLog().empty()) { o_msg_response->setString("No tasks execution log."); } else { o_msg_response->setStringList( render->getTasksLog()); } break; } case af::Msg::TRenderInfoRequestId: { AfContainerLock lock( i_args->renders, AfContainerLock::READLOCK); RenderContainerIt rendersIt( i_args->renders); RenderAf* render = rendersIt.getRender( i_msg->int32()); if( render == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = render->writeFullInfo(); break; } // ---------------------------------- Users -------------------------------// case af::Msg::TUserIdRequest: { AfContainerLock lock( i_args->users, AfContainerLock::READLOCK); af::MsgClassUserHost usr( i_msg); std::string name = usr.getUserName(); int id = 0; UserContainerIt usersIt( i_args->users); for( af::User *user = usersIt.user(); user != NULL; usersIt.next(), user = usersIt.user()) { if( user->getName() == name) { id = user->getId(); } } o_msg_response = new af::Msg( af::Msg::TUserId, id); break; } case af::Msg::TUsersListRequest: { AfContainerLock lock( i_args->users, AfContainerLock::READLOCK); o_msg_response = i_args->users->generateList( af::Msg::TUsersList); break; } case af::Msg::TUsersListRequestIds: { AfContainerLock lock( i_args->users, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->users->generateList( af::Msg::TUsersList, ids); break; } case af::Msg::TUserLogRequestId: { AfContainerLock lock( i_args->users, AfContainerLock::READLOCK); UserContainerIt usersIt( i_args->users); UserAf* user = usersIt.getUser( i_msg->int32()); if( user == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); o_msg_response->setStringList( user->getLog()); break; } case af::Msg::TUserJobsOrderRequestId: { AfContainerLock lock( i_args->users, AfContainerLock::READLOCK); UserContainerIt usersIt( i_args->users); UserAf* user = usersIt.getUser( i_msg->int32()); if( user == NULL ) { // FIXME: Better to return some message in any case. break; } af::MCGeneral ids; ids.setId( user->getId()); ids.setList( user->generateJobsIds()); o_msg_response = new af::Msg( af::Msg::TUserJobsOrder, &ids); break; } // ------------------------------------- Job -------------------------------// case af::Msg::TJobRequestId: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( i_msg->int32()); if( job == NULL ) { o_msg_response = new af::Msg( af::Msg::TJobRequestId, 0); break; } o_msg_response = new af::Msg( af::Msg::TJob, job); break; } case af::Msg::TJobLogRequestId: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( i_msg->int32()); if( job == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); o_msg_response->setStringList( job->getLog()); break; } case af::Msg::TJobErrorHostsRequestId: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( i_msg->int32()); if( job == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); o_msg_response->setString( job->v_getErrorHostsListString()); break; } case af::Msg::TJobProgressRequestId: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( i_msg->int32()); if( job == NULL ) { // FIXME: Send back the same message on error - is it good? o_msg_response = new af::Msg( af::Msg::TJobProgressRequestId, 0); break; } o_msg_response = new af::Msg; job->writeProgress( *o_msg_response); break; } case af::Msg::TJobsListRequest: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); o_msg_response = i_args->jobs->generateList( af::Msg::TJobsList); break; } case af::Msg::TJobsListRequestIds: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); af::MCGeneral ids( i_msg); o_msg_response = i_args->jobs->generateList( af::Msg::TJobsList, ids); break; } case af::Msg::TJobsListRequestUserId: { AfContainerLock jLock( i_args->jobs, AfContainerLock::READLOCK); AfContainerLock uLock( i_args->users, AfContainerLock::READLOCK); o_msg_response = i_args->users->generateJobsList( i_msg->int32()); if( o_msg_response == NULL ) { o_msg_response = new af::Msg( af::Msg::TUserId, 0); } break; } case af::Msg::TJobsListRequestUsersIds: { AfContainerLock jLock( i_args->jobs, AfContainerLock::READLOCK); AfContainerLock uLock( i_args->users, AfContainerLock::READLOCK); af::MCGeneral mcids( i_msg); std::string type_name; o_msg_response = i_args->users->generateJobsList( mcids.getList(), type_name); break; } case af::Msg::TTaskRequest: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); af::MCTaskPos mctaskpos( i_msg); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( mctaskpos.getJobId()); if( job == NULL ) { o_msg_response = new af::Msg(); std::ostringstream stream; stream << "Msg::TTaskRequest: No job with id=" << mctaskpos.getJobId(); o_msg_response->setString( stream.str()); break; } af::TaskExec * task = job->generateTask( mctaskpos.getNumBlock(), mctaskpos.getNumTask()); if( task ) { o_msg_response = new af::Msg( af::Msg::TTask, task); delete task; } else { o_msg_response = new af::Msg(); std::ostringstream stream; stream << "Msg::TTaskRequest: No such task[" << mctaskpos.getJobId() << "][" << mctaskpos.getNumBlock() << "][" << mctaskpos.getNumTask() << "]"; o_msg_response->setString( stream.str()); } break; } case af::Msg::TTaskLogRequest: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); af::MCTaskPos mctaskpos( i_msg); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( mctaskpos.getJobId()); if( job == NULL ) { o_msg_response = new af::Msg(); std::ostringstream stream; stream << "Msg::TTaskLogRequest: No job with id=" << mctaskpos.getJobId(); o_msg_response->setString( stream.str()); break; } const std::list<std::string> * list = &(job->getTaskLog( mctaskpos.getNumBlock(), mctaskpos.getNumTask())); if( list == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); if( list->size() == 0) { std::list<std::string> list; list.push_back("Task log is empty."); o_msg_response->setStringList( list); } else { o_msg_response->setStringList( *list); } break; } case af::Msg::TTaskErrorHostsRequest: { AfContainerLock lock( i_args->jobs, AfContainerLock::READLOCK); af::MCTaskPos mctaskpos( i_msg); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( mctaskpos.getJobId()); if( job == NULL ) { // FIXME: Better to return some message in any case. break; } o_msg_response = new af::Msg(); o_msg_response->setString( job->v_getErrorHostsListString( mctaskpos.getNumBlock(), mctaskpos.getNumTask())); break; } case af::Msg::TTaskOutputRequest: { af::Msg * msg_request_render = NULL; std::string filename, error; af::MCTaskPos tp( i_msg); //printf("ThreadReadMsg::msgCase: case af::Msg::TJobTaskOutputRequest: job=%d, block=%d, task=%d, number=%d\n", tp.getJobId(), tp.getNumBlock(), tp.getNumTask(), tp.getNumber()); { AfContainerLock jLock( i_args->jobs, AfContainerLock::READLOCK); AfContainerLock rLock( i_args->renders, AfContainerLock::READLOCK); JobContainerIt jobsIt( i_args->jobs); JobAf* job = jobsIt.getJob( tp.getJobId()); if( job == NULL ) { o_msg_response = af::msgString("Error: Job is NULL."); AFCommon::QueueLogError("Jobs is NULL"); break; } // Trying to set message to request output from running remote host. msg_request_render = job->v_getTaskStdOut( tp.getNumBlock(), tp.getNumTask(), tp.getNumber(), i_args->renders, filename, error); if( error.size()) { if( msg_request_render ) delete msg_request_render; o_msg_response = af::msgString( error); AFCommon::QueueLogError( error); break; } } if( filename.size()) { // // Retrieving output from file // int readsize = -1; char * data = af::fileRead( filename, readsize, af::Msg::SizeDataMax, &error); if( data ) { o_msg_response = new af::Msg(); o_msg_response->setData( readsize, data); delete [] data; } else if( error.size()) { error = std::string("Getting task output: ") + error; AFCommon::QueueLogError( error); o_msg_response = af::msgString( error); } } else if( msg_request_render) { // // Retrieving output from render // msg_request_render->setReceiving(); bool ok; o_msg_response = af::msgsend( msg_request_render, ok, af::VerboseOn); if( o_msg_response == NULL ) { error = "Retrieving output from render failed. See server logs for details."; o_msg_response = af::msgString( error); AFCommon::QueueLogError( error); } delete msg_request_render; } else { if( error.size()) { o_msg_response = af::msgString( error); AFCommon::QueueLogError("TTaskOutputRequest: Neiter message nor filename\n" + error); } else AFCommon::QueueLogError("TTaskOutputRequest: Neiter message nor filename."); } break; } case af::Msg::TJobsWeightRequest: { AfContainerLock jLock( i_args->jobs, AfContainerLock::READLOCK); af::MCJobsWeight jobsWeight; i_args->jobs->getWeight( jobsWeight); o_msg_response = new af::Msg( af::Msg::TJobsWeight, &jobsWeight); break; } // Cases for run cycle thread: case af::Msg::TTaskUpdateState: { af::MCTaskUp taskup( i_msg); af::MCTaskPos taskpos( taskup.getNumJob(), taskup.getNumBlock(), taskup.getNumTask(), taskup.getNumber()); o_msg_response = new af::Msg( af::Msg::TRenderCloseTask, &taskpos); } case af::Msg::TTaskUpdatePercent: case af::Msg::TTaskListenOutput: case af::Msg::TRenderDeregister: case af::Msg::TTalkDeregister: /* { // Check magic number mismatch mode: // All message types above are not allowed in "GetOnly" mode. if( i_msg->isMagicInvalid() && ( af::Environment::getMagicMode() <= af::MMM_GetOnly )) { std::string err = "Magic Mismatch Mode: \""; err += af::Environment::getMagicModeName(); err += "\""; err += "\nMessage type not allowed: \""; err += af::Msg::TNAMES[i_msg->type()]; err += "\""; AFCommon::QueueLogError( err); delete i_msg; return o_msg_response; } // Only Monitor message types are allowed in "GetOnly" mode. }*/ case af::Msg::TMonitorSubscribe: case af::Msg::TMonitorUnsubscribe: case af::Msg::TMonitorDeregister: case af::Msg::TMonitorUsersJobs: case af::Msg::TMonitorJobsIdsAdd: case af::Msg::TMonitorJobsIdsSet: case af::Msg::TMonitorJobsIdsDel: case af::Msg::TMonitorMessage: { // Push message for run cycle thread. i_args->msgQueue->pushMsg( i_msg); // Need to return here to not to delete input message (i_msg) later. return o_msg_response; // ( o_msg_response is NULL in all cases except Msg::TTaskUpdateState, // in that case render should recieve an answer to close task // and finish sending any updates for the task ) } // -------------------------------------------------------------------------// default: { AFCommon::QueueLogError( std::string("Unknown message recieved: ") + i_msg->v_generateInfoString( false)); break; } } // Deleting input message as it not needed any more. delete i_msg; // Returning an answer return o_msg_response; }
void xml_loadsave_users_write(const gchar *prefix) { #ifdef DEBUG printf("xml_loadsave_users_write\n"); #endif gint i, j, k; gchar buf[SMALL]; FILE *fil = NULL; for(i=0;i<users->len;i++) { sprintf(buf, "%s___user_%02d_options", prefix, i); file_save_opt_file(buf, &usr(i).options); sprintf(buf, "%s___user_%02d_live_game.xml", prefix, i); xml_loadsave_live_game_write(buf, &usr(i).live_game); } sprintf(buf, "%s___users.xml", prefix); file_my_fopen(buf, "w", &fil, TRUE); fprintf(fil, "<_%d>\n", TAG_USERS); for(i=0;i<users->len;i++) { fprintf(fil, "<_%d>\n", TAG_USER); xml_write_string(fil, usr(i).name, TAG_NAME, I1); xml_write_int(fil, usr(i).team_id, TAG_TEAM_ID, I1); xml_write_int(fil, usr(i).money, TAG_USER_MONEY, I1); xml_write_int(fil, usr(i).debt, TAG_USER_DEBT, I1); xml_write_float(fil, usr(i).debt_interest, TAG_USER_DEBT_INTEREST, I1); xml_write_int(fil, usr(i).alr_start_week, TAG_USER_ALR_START_WEEK, I1); xml_write_int(fil, usr(i).alr_weekly_installment, TAG_USER_ALR_WEEKLY_INSTALLMENT, I1); xml_write_int(fil, usr(i).scout, TAG_USER_SCOUT, I1); xml_write_int(fil, usr(i).physio, TAG_USER_PHYSIO, I1); strcpy(buf, usr(i).sponsor.name->str); misc_string_replace_token(buf, "&", "AND"); xml_write_string(fil, buf, TAG_USER_SPONSOR_NAME, I1); xml_write_int(fil, usr(i).sponsor.contract, TAG_USER_SPONSOR_CONTRACT, I1); xml_write_int(fil, usr(i).sponsor.benefit, TAG_USER_SPONSOR_BENEFIT, I1); xml_user_write_history(fil, usr(i).history); xml_user_write_events(fil, usr(i).events); xml_user_write_bets(fil, usr(i).bets); for(j=0;j<COUNT_USER_END;j++) xml_write_int(fil, usr(i).counters[j], TAG_USER_COUNTER, I1); for(j=0;j<2;j++) { fprintf(fil, "%s<_%d>\n", I1, TAG_USER_MONEY_INS); for(k=0;k<MON_IN_END;k++) xml_write_int(fil, usr(i).money_in[j][k], TAG_USER_MONEY_IN, I2); fprintf(fil, "%s</_%d>\n", I1, TAG_USER_MONEY_INS); } for(j=0;j<2;j++) { fprintf(fil, "%s<_%d>\n", I1, TAG_USER_MONEY_OUTS); for(k=0;k<MON_OUT_END;k++) xml_write_int(fil, usr(i).money_out[j][k], TAG_USER_MONEY_OUT, I2); fprintf(fil, "%s</_%d>\n", I1, TAG_USER_MONEY_OUTS); } xml_write_int(fil, usr(i).youth_academy.coach, TAG_USER_YA_COACH, I1); xml_write_int(fil, usr(i).youth_academy.percentage, TAG_USER_YA_PERCENTAGE, I1); xml_write_float(fil, usr(i).youth_academy.av_coach, TAG_USER_YA_AV_COACH, I1); xml_write_float(fil, usr(i).youth_academy.av_percentage, TAG_USER_YA_AV_PERCENTAGE, I1); xml_write_float(fil, usr(i).youth_academy.counter_youth, TAG_USER_YA_COUNTER, I1); xml_write_int(fil, usr(i).youth_academy.pos_pref, TAG_USER_YA_PREFERENCE, I1); for (j=0; j < usr(i).default_team->len; j++){ xml_write_int(fil, g_array_index(usr(i).default_team, gint, j), TAG_USER_DEFAULT_TEAM, I1); } xml_write_int(fil, usr(i).default_structure, TAG_USER_DEFAULT_STRUCTURE, I1); xml_write_int(fil, usr(i).default_style, TAG_USER_DEFAULT_STYLE, I1); xml_write_int(fil, usr(i).default_boost, TAG_USER_DEFAULT_BOOST, I1); xml_loadsave_players_write(fil, usr(i).youth_academy.players); fprintf(fil, "</_%d>\n", TAG_USER); } fprintf(fil, "</_%d>\n", TAG_USERS); fclose(fil); }
int main() { usr(); return 0; }
/** Manage the bets made by the users. */ void bet_update_user_bets(void) { #ifdef DEBUG printf("bet_update_user_bets\n"); #endif gint i, j, outcome; const BetMatch *bet = NULL; const Fixture *fix = NULL; for(i=0;i<users->len;i++) { g_array_free(usr(i).bets[0], TRUE); usr(i).bets[0] = g_array_new(FALSE, FALSE, sizeof(BetUser)); for(j=0;j<usr(i).bets[1]->len;j++) { bet = bet_from_fixture(g_array_index(usr(i).bets[1], BetUser, j).fix_id); fix = fixture_from_id(g_array_index(usr(i).bets[1], BetUser, j).fix_id, TRUE); if(fix->result[0][0] < fix->result[1][0]) outcome = 2; else outcome = (fix->result[0][0] == fix->result[1][0]); if(outcome == g_array_index(usr(i).bets[1], BetUser, j).outcome) { usr(i).money += (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager * bet->odds[outcome]); usr(i).money_in[1][MON_IN_BETS] += (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager * bet->odds[outcome]); g_array_index(usr(i).bets[1], BetUser, j).wager = (gint)rint((gfloat)g_array_index(usr(i).bets[1], BetUser, j).wager * bet->odds[outcome]); } else { usr(i).money -= g_array_index(usr(i).bets[1], BetUser, j).wager; usr(i).money_out[1][MON_OUT_BETS] -= g_array_index(usr(i).bets[1], BetUser, j).wager; g_array_index(usr(i).bets[1], BetUser, j).wager *= -1; } g_array_append_val(usr(i).bets[0], g_array_index(usr(i).bets[1], BetUser, j)); } g_array_free(usr(i).bets[1], TRUE); usr(i).bets[1] = g_array_new(FALSE, FALSE, sizeof(BetUser)); } }
int main() { usr(); getchar(); return 1; }
/* * Vertex update function - computes the least square step */ void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) { //go over all user nodes if (is_user(vertex.id())){ vertex_data & user = latent_factors_inmem[vertex.id()]; time_svdpp_usr usr(user); unsigned int userRatings = vertex.num_outedges(); double rRuNum = 1/sqrt(userRatings+10); int dim = D; double sumY = 0.0; //go over all ratings for(int e=0; e < vertex.num_outedges(); e++) { uint pos = vertex.edge(e)->vertex_id(); assert(pos >= M && pos < M+N); vertex_data & data = latent_factors_inmem[pos]; time_svdpp_movie movie(data); Map<vec> y(movie.y, D); sumY += sum((const vec&)y); //y } for( int k=0; k<dim; ++k) { usr.ptemp[k] = usr.pu[k] + rRuNum * sumY; // pTemp = pu + rRuNum*sumY } vec sum = zeros(dim); for(int e=0; e < vertex.num_edges(); e++) { //edge_data & edge = scope.edge_data(oedgeid); //float rui = edge.weight; float rui = vertex.edge(e)->get_data().weight; uint t = (uint)(vertex.edge(e)->get_data().time - 1); // we assume time bins start from 1 assert(t < M+N+K); vertex_data & data = latent_factors_inmem[vertex.edge(e)->vertex_id()]; time_svdpp_movie mov(data); time_svdpp_time time(latent_factors_inmem[t]); double pui = 0; time_svdpp_predict(usr, mov, time, rui, pui); double eui = rui - pui; *usr.bu += tsp.lrate*(eui - tsp.beta* *usr.bu); *mov.bi += tsp.lrate * (eui - tsp.beta* *mov.bi); for (int k = 0; k < dim; k++) { double oldValue = mov.q[k]; double userValue = usr.ptemp[k] + usr.pu[k] * time.pt[k]; sum[k] += eui * mov.q[k]; mov.q[k] += tsp.lrate * (eui * userValue - tsp.gamma*mov.q[k]); usr.ptemp[k] += tsp.lrate * ( eui * oldValue - tsp.gamma * usr.ptemp[k]); usr.p[k] += tsp.lrate * ( eui * oldValue - tsp.gamma*usr.p[k] ); usr.pu[k] += tsp.lrate * (eui * oldValue * time.pt[k] - tsp.gamma * usr.pu[k]); time.pt[k] += tsp.lrate * (eui * oldValue * usr.pu[k] - tsp.gamma * time.pt[k]); double xOldValue = usr.x[k]; double zOldValue = time.z[k]; usr.x[k] += tsp.lrate * (eui * zOldValue - tsp.gamma * xOldValue); time.z[k] += tsp.lrate * (eui * xOldValue - tsp.gamma * zOldValue); } rmse_vec[omp_get_thread_num()] += eui*eui; } for(int e=0; e < vertex.num_edges(); e++) { time_svdpp_movie mov = latent_factors_inmem[vertex.edge(e)->vertex_id()]; for(int k=0;k<dim;k++){ mov.y[k] += tsp.lrate * (rRuNum * sum[k]- tsp.gamma*mov.y[k]); } } } };