void omxGlobal::reportProgress(const char *context, FitContext *fc) { if (omx_absolute_thread_num() != 0) { mxLog("omxGlobal::reportProgress called in a thread context (report this bug to developers)"); return; } R_CheckUserInterrupt(); time_t now = time(0); if (Global->maxSeconds > 0 && now > Global->startTime + Global->maxSeconds && !Global->timedOut) { Global->timedOut = true; Rf_warning("Time limit of %d minutes %d seconds exceeded", Global->maxSeconds/60, Global->maxSeconds % 60); } if (silent || now - lastProgressReport < 1 || fc->getGlobalComputeCount() == previousComputeCount) return; lastProgressReport = now; std::string str; if (previousReportFit == 0.0 || previousReportFit == fc->fit) { str = string_snprintf("%s %d %.6g", context, fc->getGlobalComputeCount(), fc->fit); } else { str = string_snprintf("%s %d %.6g %.4g", context, fc->getGlobalComputeCount(), fc->fit, fc->fit - previousReportFit); } reportProgressStr(str.c_str()); previousReportLength = str.size(); previousReportFit = fc->fit; previousComputeCount = fc->getGlobalComputeCount(); }
void omxCompleteExpectation(omxExpectation *ox) { if(ox->isComplete) return; ox->isComplete = TRUE; ox->loadFromR(); ox->init(); if (OMX_DEBUG) { omxData *od = ox->data; omxState *state = ox->currentState; std::string msg = string_snprintf("Expectation '%s' of type '%s' has" " %d definition variables:\n", ox->name, ox->expType, int(od->defVars.size())); for (int dx=0; dx < int(od->defVars.size()); ++dx) { omxDefinitionVar &dv = od->defVars[dx]; msg += string_snprintf("[%d] column '%s' ->", dx, omxDataColumnName(od, dv.column)); msg += string_snprintf(" %s[%d,%d]", state->matrixToName(~dv.matrix), dv.row, dv.col); msg += "\n dirty:"; for (int mx=0; mx < dv.numDeps; ++mx) { msg += string_snprintf(" %s", state->matrixToName(dv.deps[mx])); } msg += "\n"; } mxLogBig(msg); } }
void CScriptSystem::RaiseError(lua_State* L, const char* sErr, ...) { va_list arglist; char error[2048]; va_start(arglist, sErr); vsprintf(error, sErr, arglist); va_end(arglist); behaviac::string errorMessage; errorMessage.reserve(1024); errorMessage = "A script error has occurred: "; errorMessage += error; errorMessage += "\nStack traceback:\n"; int callDepth = 1; lua_Debug ar; while (lua_getstack(L, callDepth++, &ar)) { errorMessage += "\t"; if (lua_getinfo(L, "nSl", &ar)) { char stackLine[256]; string_snprintf(stackLine, 256, "\t%s (%s line %d)\n", ar.name, ar.source, ar.currentline); errorMessage += stackLine; } else { errorMessage += "\tNo call information"; } } BEHAVIAC_ASSERT(0, errorMessage.c_str()); }
void LogManager::Output(const behaviac::Agent* pAgent, const char* msg) { if (Config::IsLogging()) { FILE* fp = this->GetFile(pAgent); char szTime[64]; time_t tTime = time(NULL); tm* ptmCurrent = localtime(&tTime); string_snprintf(szTime, sizeof(szTime) - 1, "%.2d:%.2d:%.2d", ptmCurrent->tm_hour, ptmCurrent->tm_min, ptmCurrent->tm_sec); //behaviac::string buffer = FormatString("[%s]%s\n", szTime, msg); behaviac::string buffer = FormatString("[%s]%s", szTime, msg); //printf(buffer.c_str()); if (fp) { behaviac::Mutex cs; behaviac::ScopedLock lock(cs); fwrite(buffer.c_str(), 1, buffer.size(), fp); if (this->m_bFlush) { fflush(fp); } } } }
void mxLogBig(const std::string &str) // thread-safe { ssize_t len = ssize_t(str.size()); if (len == 0) Rf_error("Attempt to log 0 characters with mxLogBig"); std::string fullstr; if (mxLogCurrentRow == -1) { fullstr = string_snprintf("[%d] ", omx_absolute_thread_num()); } else { fullstr = string_snprintf("[%d@%d] ", omx_absolute_thread_num(), mxLogCurrentRow); } fullstr += str; len = ssize_t(fullstr.size()); const char *outBuf = fullstr.c_str(); ssize_t wrote = mxLogWriteSynchronous(outBuf, len); if (wrote != len) Rf_error("mxLogBig only wrote %d/%d, errno %d", wrote, len, errno); }
void omxExpectation::loadThresholds() { bool debug = false; CheckAST(thresholdsMat, 0); numOrdinal = 0; //omxPrint(thresholdsMat, "loadThr"); auto dc = base::getDataColumns(); thresholds.reserve(dc.size()); std::vector<bool> found(thresholdsMat->cols, false); for(int dx = 0; dx < int(dc.size()); dx++) { int index = dc[dx]; omxThresholdColumn col; col.dColumn = index; const char *colname = data->columnName(index); int tc = thresholdsMat->lookupColumnByName(colname); if (tc < 0 || (data->rawCols.size() && !omxDataColumnIsFactor(data, index))) { // Continuous variable if(debug || OMX_DEBUG) { mxLog("%s: column[%d] '%s' is continuous (tc=%d isFactor=%d)", name, index, colname, tc, omxDataColumnIsFactor(data, index)); } thresholds.push_back(col); } else { found[tc] = true; col.column = tc; if (data->rawCols.size()) { col.numThresholds = omxDataGetNumFactorLevels(data, index) - 1; } else { // See omxData::permute } thresholds.push_back(col); if(debug || OMX_DEBUG) { mxLog("%s: column[%d] '%s' is ordinal with %d thresholds in threshold column %d.", name, index, colname, col.numThresholds, tc); } numOrdinal++; } } if (numOrdinal != thresholdsMat->cols) { std::string buf; for (int cx=0; cx < thresholdsMat->cols; ++cx) { if (found[cx]) continue; buf += string_snprintf(" %d", 1+cx); } omxRaiseErrorf("%s: cannot find data for threshold columns:%s\n(Do appropriate threshold column names match data column names?)", name, buf.c_str()); } if(debug || OMX_DEBUG) { mxLog("%d threshold columns processed.", numOrdinal); } }
int CFunctionHandler::EndFunction(EntityId entityId) { // LUA does not support 64 bit numbers // convert the number to behaviac::string char str[1024]; string_snprintf(str, 1024 - 1, "%llu", entityId.GetUniqueID()); str[1023] = 0; // buffer overrun ensure lua_pushstring(m_pLS, str); return 1; }
inline void append_printf_formatted_string(std::string& out, const char* format, TArgs&&... args) { // First try to write string with the max_size, if that doesn't // work snprintf will tell us how much space it needs. We // reserve that much space and try again. So this will always // work, even if the output is larger than the given max_size. // // Unfortunately this trick doesn't work on Windows, because // the _snprintf() function there only returns the length it // needs if max_size==0 and the buffer pointer is the null // pointer. So we have to take this into account. #ifndef _MSC_VER static const size_t max_size = 100; #else static const size_t max_size = 0; #endif size_t old_size = out.size(); int len = string_snprintf(out, old_size, max_size, format, std::forward<TArgs>(args)...); assert(len > 0); if (size_t(len) >= max_size) { #ifndef NDEBUG int len2 = #endif string_snprintf(out, old_size, size_t(len) + 1, format, std::forward<TArgs>(args)...); assert(len2 == len); } out.resize(old_size + size_t(len)); }
void ConsoleOut::Dump(uint32_t Filter, const void* pUserData, size_t UserSize) { TestInit(); if ((pUserData) && (UserSize > 0)) { char Str[80]; size_t i; uint8_t* pData; size_t Size; size_t StartingOfs; size_t EndingOfs; pData = BEHAVIAC_ALIGN_PTR(uint8_t*, pUserData, 16); StartingOfs = (size_t)BEHAVIAC_DIFF_PTR(pUserData, pData); EndingOfs = StartingOfs + UserSize; Size = BEHAVIAC_ROUND(EndingOfs, 16); for (i = 0; i < (Size / 16); i++) { memset(Str, ' ', sizeof(Str)); string_snprintf(Str, sizeof(Str), "%p", pData + i * 16); Str[8] = ':'; for (size_t j = 0; j < 16; j++) { size_t Ofs = i * 16 + j; size_t Index = (j * 3) + ((j > 7) ? 11 : 10); if ((Ofs >= StartingOfs) && (Ofs < EndingOfs)) { string_snprintf(Str + Index, sizeof(Str) - Index, "%02X", pData[Ofs]); Str[Index + 2] = ' '; //Str[60+j] = Str::IsPrintable(pData[Ofs]) ? pData[Ofs] : '.'; Str[60 + j] = pData[Ofs]; } } Str[76] = '\n'; Str[77] = 0; Output(Filter, Str); } // for(i = 0; i < (Size / 16) ; i++) } // if((pData) && (Size > 0))
void pia(const int *ar, int rows, int cols) { if (rows == 0 || cols == 0) return; std::string buf; for (int rx=0; rx < rows; rx++) { // column major order for (int cx=0; cx < cols; cx++) { buf += string_snprintf("%d, ", ar[cx * rows + rx]); } buf += "\n"; } mxLogBig(buf); }
void CLogger::OutputDecoratedLine(unsigned int LogFilter, const char* pStr) { behaviac::THREAD_ID_TYPE threadId = behaviac::GetTID(); time_t tTime = time(NULL); tm* ptmCurrent = localtime(&tTime); const char* LogFilterStr = GetLogFilterString(LogFilter); char szTime[64]; string_snprintf(szTime, sizeof(szTime) - 1, "%.2d:%.2d:%.2d", ptmCurrent->tm_hour, ptmCurrent->tm_min, ptmCurrent->tm_sec); static int s_index = 0; int index = s_index++; const int kMaxStringLength = 2048; char temp[kMaxStringLength]; string_snprintf(temp, kMaxStringLength, "[behaviac][%05d][thread %04d][%s][%s]%s", index, (int)threadId, szTime, LogFilterStr, pStr); temp[kMaxStringLength - 1] = '\0'; OutputLine(temp); }
void CScriptSystem::PushFuncParam(uint64_t value) { if (m_nTempArg == -1) { return; } // LUA does not support 64 bit numbers // convert the number to behaviac::string char str[1024]; string_snprintf(str, 1024 - 1, "%llu", value); str[1023] = 0; // buffer overrun ensure lua_pushstring(m_pLS, str); m_nTempArg++; }
void FreeVarGroup::log(omxState *os) { size_t numMats = os->matrixList.size(); size_t numAlgs = os->algebraList.size(); std::string str; str += string_snprintf("FreeVarGroup(id=%d", id[0]); for (size_t ix=1; ix < id.size(); ++ix) { str += string_snprintf(",%d", id[ix]); } str += string_snprintf(") with %d variables:", (int) vars.size()); for (size_t vx=0; vx < vars.size(); ++vx) { str += " "; str += vars[vx]->name; } if (vars.size()) str += "\nwill dirty:"; for(size_t i = 0; i < numMats; i++) { if (dependencies[i]) { int offset = ~(i - numMats); str += " "; str += os->matrixList[offset]->name(); } } for(size_t i = 0; i < numAlgs; i++) { if (dependencies[i + numMats]) { str += " "; str += os->algebraList[i]->name(); } } str += "\n"; mxLogBig(str); }
FILE* LogManager::GetFile(const behaviac::Agent* pAgent) { if (Config::IsLogging()) { BEHAVIAC_UNUSED_VAR(pAgent); FILE* fp = 0; //int agentId = pAgent->GetId(); int agentId = -1; Logs_t::iterator it = this->m_logs.find(agentId); if (it == this->m_logs.end()) { const char* pLogFile = 0; char buffer[64]; if (m_logFilePath == 0) { if (agentId == -1) { string_snprintf(buffer, 64, "_behaviac_$_.log"); } else { string_sprintf(buffer, "Agent_$_%03d.log", agentId); } pLogFile = buffer; } else { pLogFile = m_logFilePath; } fp = fopen(pLogFile, "wt"); this->m_logs[agentId] = fp; } else { fp = it->second; } return fp; } return 0; }
void omxGlobal::unpackConfidenceIntervals(omxState *currentState) { if (unpackedConfidenceIntervals) return; unpackedConfidenceIntervals = true; // take care to preserve order std::vector<ConfidenceInterval*> tmp; std::swap(tmp, intervalList); std::set<ConfidenceInterval*, ciCmp> uniqueCIs; for (int ix=0; ix < (int) tmp.size(); ++ix) { ConfidenceInterval *ci = tmp[ix]; if (!ci->isWholeAlgebra()) { auto iter = uniqueCIs.find(ci); if (iter == uniqueCIs.end()) { uniqueCIs.insert(ci); intervalList.push_back(ci); } else if (ci->cmpBoundAndType(**iter)) { Rf_warning("Different confidence intervals '%s' and '%s' refer to the same thing", ci->name.c_str(), (*iter)->name.c_str()); } continue; } omxMatrix *mat = ci->getMatrix(currentState); for (int cx=0; cx < mat->cols; ++cx) { for (int rx=0; rx < mat->rows; ++rx) { ConfidenceInterval *cell = new ConfidenceInterval(*ci); cell->name = string_snprintf("%s[%d,%d]", ci->name.c_str(), 1+rx, 1+cx); cell->row = rx; cell->col = cx; auto iter = uniqueCIs.find(cell); if (iter == uniqueCIs.end()) { uniqueCIs.insert(cell); intervalList.push_back(cell); } else { if (cell->cmpBoundAndType(**iter)) { Rf_warning("Different confidence intervals '%s' and '%s' refer to the same thing", cell->name.c_str(), (*iter)->name.c_str()); } delete cell; } } } delete ci; } }
const char *omxGlobal::getBads() { if (bads.size() == 0) return NULL; std::string str; for (size_t mx=0; mx < bads.size(); ++mx) { if (bads.size() > 1) str += string_snprintf("%d:", (int)mx+1); str += bads[mx]; if (str.size() > (1<<14)) break; if (mx < bads.size() - 1) str += "\n"; } size_t sz = str.size(); char *mem = R_alloc(sz+1, 1); // use R's memory memcpy(mem, str.c_str(), sz); mem[sz] = 0; return mem; }
void omxCompleteExpectation(omxExpectation *ox) { if(ox->isComplete) return; if (ox->rObj) { omxState *os = ox->currentState; SEXP rObj = ox->rObj; SEXP slot; {ScopedProtect(slot, R_do_slot(rObj, Rf_install("container"))); if (Rf_length(slot) == 1) { int ex = INTEGER(slot)[0]; ox->container = os->expectationList.at(ex); } } {ScopedProtect(slot, R_do_slot(rObj, Rf_install("submodels"))); if (Rf_length(slot)) { int numSubmodels = Rf_length(slot); int *submodel = INTEGER(slot); for (int ex=0; ex < numSubmodels; ex++) { int sx = submodel[ex]; ox->submodels.push_back(omxExpectationFromIndex(sx, os)); } } } } omxExpectationProcessDataStructures(ox, ox->rObj); int numSubmodels = (int) ox->submodels.size(); for (int ex=0; ex < numSubmodels; ex++) { omxCompleteExpectation(ox->submodels[ex]); } ox->initFun(ox); if(ox->computeFun == NULL) { if (isErrorRaised()) { Rf_error("Failed to initialize '%s' of type %s: %s", ox->name, ox->expType, Global->getBads()); } else { Rf_error("Failed to initialize '%s' of type %s", ox->name, ox->expType); } } if (OMX_DEBUG) { omxData *od = ox->data; omxState *state = ox->currentState; std::string msg = string_snprintf("Expectation '%s' of type '%s' has" " %d definition variables:\n", ox->name, ox->expType, int(od->defVars.size())); for (int dx=0; dx < int(od->defVars.size()); ++dx) { omxDefinitionVar &dv = od->defVars[dx]; msg += string_snprintf("[%d] column '%s' ->", dx, omxDataColumnName(od, dv.column)); for (int lx=0; lx < dv.numLocations; ++lx) { msg += string_snprintf(" %s[%d,%d]", state->matrixToName(~dv.matrices[lx]), dv.rows[lx], dv.cols[lx]); } msg += "\n dirty:"; for (int mx=0; mx < dv.numDeps; ++mx) { msg += string_snprintf(" %s", state->matrixToName(dv.deps[mx])); } msg += "\n"; } mxLogBig(msg); } ox->isComplete = TRUE; }
void ba81NormalQuad::layer::setStructure(Eigen::ArrayBase<T1> ¶m, Eigen::MatrixBase<T2> &gmean, Eigen::MatrixBase<T3> &gcov) { abilitiesMap.clear(); std::string str = string_snprintf("layer:"); for (int ax=0; ax < gmean.rows(); ++ax) { if (!abilitiesMask[ax]) continue; abilitiesMap.push_back(ax); str += " "; str += quad->ig.factorNames[ax]; } str += ":"; itemsMap.clear(); glItemsMap.resize(param.cols(), -1); for (int ix=0, lx=0; ix < param.cols(); ++ix) { if (!itemsMask[ix]) continue; itemsMap.push_back(ix); glItemsMap[ix] = lx++; str += string_snprintf(" %d", ix); } str += "\n"; //mxLogBig(str); dataColumns.clear(); dataColumns.reserve(numItems()); totalOutcomes = 0; for (int ix=0; ix < numItems(); ++ix) { int outcomes = quad->ig.itemOutcomes[ itemsMap[ix] ]; itemOutcomes.push_back(outcomes); cumItemOutcomes.push_back(totalOutcomes); totalOutcomes += outcomes; dataColumns.push_back(quad->ig.dataColumns[ itemsMap[ix] ]); } Eigen::VectorXd mean; Eigen::MatrixXd cov; globalToLocalDist(gmean, gcov, mean, cov); if (mean.size() == 0) { numSpecific = 0; primaryDims = 0; maxDims = 1; totalQuadPoints = 1; totalPrimaryPoints = 1; weightTableSize = 1; return; } numSpecific = 0; if (quad->ig.twotier) detectTwoTier(param, mean, cov); if (numSpecific) quad->hasBifactorStructure = true; primaryDims = cov.cols() - numSpecific; maxDims = primaryDims + (numSpecific? 1 : 0); totalQuadPoints = 1; for (int dx=0; dx < maxDims; dx++) { totalQuadPoints *= quad->gridSize; } totalPrimaryPoints = totalQuadPoints; weightTableSize = totalQuadPoints; if (numSpecific) { totalPrimaryPoints /= quad->gridSize; weightTableSize *= numSpecific; } }