From 4ab6c1e766b1bf2fc3e27056c44d39dbcf41ea6f Mon Sep 17 00:00:00 2001 From: Frank Seide Date: Tue, 4 Nov 2014 14:14:45 -0800 Subject: [PATCH] minor GCC break fixes in cn.cpp; changed wsprintf() in ComputationNode.h to sprintf() --- Common/Include/basetypes.h | 4 +++- Common/fileutil.cpp | 4 ---- MachineLearning/cn/ComputationNode.h | 29 +++++++++++------------ MachineLearning/cn/SGD.h | 35 ++++++++++++++-------------- MachineLearning/cn/SimpleEvaluator.h | 4 ++++ MachineLearning/cn/cn.cpp | 22 +++++++++-------- 6 files changed, 50 insertions(+), 48 deletions(-) diff --git a/Common/Include/basetypes.h b/Common/Include/basetypes.h index f19624829..590505b30 100644 --- a/Common/Include/basetypes.h +++ b/Common/Include/basetypes.h @@ -548,7 +548,9 @@ static inline const char *strerror_(int e) if (msgs.find(e) == msgs.end()) { char msg[1024]; strerror_s (msg, e); msgs[e] = msg; } return msgs[e].c_str(); } - +#endif +#ifdef __unix__ +extern int fileno(FILE*); // somehow got deprecated in C++11 #endif // ---------------------------------------------------------------------------- diff --git a/Common/fileutil.cpp b/Common/fileutil.cpp index d43d2146c..35ae62a1a 100644 --- a/Common/fileutil.cpp +++ b/Common/fileutil.cpp @@ -160,10 +160,6 @@ FILE * fopenOrDie (const wstring & pathname, const wchar_t * mode) // set mode to binary or text (pass 'b' or 't') // ---------------------------------------------------------------------------- -#ifdef __unix__ -extern int fileno(FILE*); // somehow got deprecated in C++11 -#endif - void fsetmode(FILE * f, char type) { if (type != 'b' && type != 't') diff --git a/MachineLearning/cn/ComputationNode.h b/MachineLearning/cn/ComputationNode.h index 7b44d21c6..3a9497196 100644 --- a/MachineLearning/cn/ComputationNode.h +++ b/MachineLearning/cn/ComputationNode.h @@ -176,9 +176,7 @@ namespace Microsoft { namespace MSR { namespace CNTK { virtual void DumpNodeInfo(const bool /*printValues*/, File& fstream) const { - wchar_t str[4096]; - wsprintf(str, L"\n%ws=%ws", NodeName().c_str(), OperationName().c_str()); - fstream << wstring(str); + fstream << NodeName() + L"=" + OperationName(); if (!IsLeaf()) { @@ -187,8 +185,7 @@ namespace Microsoft { namespace MSR { namespace CNTK { { if (i > 0) fstream << wstring(L","); - wsprintf(str, L"%ws", Inputs(i)?Inputs(i)->NodeName().c_str():L"NULL"); - fstream << wstring(str); + fstream << (Inputs(i) ? Inputs(i)->NodeName().c_str() : L"NULL"); } fstream << wstring(L")"); } @@ -960,11 +957,11 @@ protected: \ { ComputationNode::DumpNodeInfo(printValues, fstream); - wchar_t str[4096]; - wsprintf(str, L"[%lu,%lu] ", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); - fstream << wstring(str); - wsprintf(str, L"NeedGradient=%ws", NeedGradient()? L"true" : L"false"); - fstream << wstring(str); + char str[4096]; + sprintf(str, "[%lu,%lu] ", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); + fstream << string(str); + sprintf(str, "NeedGradient=%s", NeedGradient()? "true" : "false"); + fstream << string(str); PrintNodeValuesToFile(printValues, fstream); } @@ -1123,9 +1120,9 @@ protected: \ { ComputationNode::DumpNodeInfo(printValues, fstream); - wchar_t str[4096]; - wsprintf(str, L"[%lu,%lu]", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); - fstream << wstring(str); + char str[4096]; + sprintf(str, "[%lu,%lu]", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); + fstream << string(str); } // copy constructor @@ -1250,9 +1247,9 @@ protected: \ { ComputationNode::DumpNodeInfo(printValues, fstream); - wchar_t str[4096]; - wsprintf(str, L"[%lu,%lu]", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); - fstream << wstring(str); + char str[4096]; + sprintf(str, "[%lu,%lu]", FunctionValues().GetNumRows(), FunctionValues().GetNumCols()); + fstream << string(str); } // copy constructor diff --git a/MachineLearning/cn/SGD.h b/MachineLearning/cn/SGD.h index 2169f65b1..8597c63bd 100644 --- a/MachineLearning/cn/SGD.h +++ b/MachineLearning/cn/SGD.h @@ -7,6 +7,7 @@ #include "ComputationNetwork.h" #include "ComputationNetworkHelper.h" +#include "SimpleEvaluator.h" #include "DataReader.h" #include #include @@ -555,21 +556,21 @@ namespace Microsoft { namespace MSR { namespace CNTK { auto t_end_epoch = clock(); ElemType epochTime = ElemType(1.0)*(t_end_epoch-t_start_epoch)/(CLOCKS_PER_SEC); - fprintf(stderr,"Finished Epoch[%lu]: [Training Set] Train Loss Per Sample = %.8g ", i+1, epochCriterion); - if (epochEvalErrors.size()==1) - { - fprintf(stderr,"EvalErr Per Sample = %.8g Ave Learn Rate Per Sample = %.10g Epoch Time=%.8g\n", epochEvalErrors[0], learnRatePerSample, epochTime); - } - else - { - fprintf(stderr,"EvalErr Per Sample "); - for (size_t j=0; jNodeName().c_str() ,epochCriterion); - for (size_t j=0; jNodeName().c_str(), epochCriterion); + for (size_t j = 0; j < epochEvalErrors.size(); j++) + fprintf(stderr, "Finished Epoch[%lu]: Evaluation Node [%ws] Per Sample = %.8g\n", i + 1, evalNodeNames[j].c_str(), epochEvalErrors[j]); + } if (validationSetDataReader != trainSetDataReader && validationSetDataReader != nullptr) { @@ -579,8 +580,8 @@ namespace Microsoft { namespace MSR { namespace CNTK { cvSetTrainAndEvalNodes.push_back(evaluationNodes[0]->NodeName()); vector vScore = evalforvalidation.Evaluate(*validationSetDataReader, cvSetTrainAndEvalNodes, m_mbSize[i]); - fprintf(stderr,"Finished Epoch[%lu]: [Validation Set] Train Loss Per Sample = %.8g EvalErr Per Sample = %.8g\n", - i+1, vScore[0], vScore[1]); + fprintf(stderr, "Finished Epoch[%lu]: [Validation Set] Train Loss Per Sample = %.8g EvalErr Per Sample = %.8g\n", + i + 1, vScore[0], vScore[1]); epochCriterion = vScore[0]; //the first one is the training criterion. } diff --git a/MachineLearning/cn/SimpleEvaluator.h b/MachineLearning/cn/SimpleEvaluator.h index 6c6b3cd68..eef29222e 100644 --- a/MachineLearning/cn/SimpleEvaluator.h +++ b/MachineLearning/cn/SimpleEvaluator.h @@ -237,7 +237,11 @@ namespace Microsoft { namespace MSR { namespace CNTK { ofstream outputStream; if (output) { +#ifdef _MSC_VER outputStream.open(output); +#else + outputStream.open(charpath(output)); // GCC does not implement wide-char pathnames here +#endif } size_t numMBsRun = 0; diff --git a/MachineLearning/cn/cn.cpp b/MachineLearning/cn/cn.cpp index 66b3e0f3c..8b6a51924 100644 --- a/MachineLearning/cn/cn.cpp +++ b/MachineLearning/cn/cn.cpp @@ -6,6 +6,8 @@ // cn.cpp : Defines the entry point for the console application. // +#define _CRT_NONSTDC_NO_DEPRECATE // make VS accept POSIX functions without _ + #include "stdafx.h" #include "ComputationNetwork.h" #include "ComputationNode.h" @@ -42,11 +44,19 @@ void RedirectStdErr(wstring logpath) fprintf (stderr, "Redirecting stderr to file %S\n", logpath.c_str()); msra::files::make_intermediate_dirs (logpath); auto_file_ptr f (logpath.c_str(), "wb"); - if (_dup2 (_fileno (f), 2) == -1) + if (dup2 (fileno (f), 2) == -1) RuntimeError ("unexpected failure to redirect stderr to log file"); setvbuf (stderr, NULL, _IONBF, 16384); // unbuffer it } +std::string WCharToString(const wchar_t* wst) +{ + std::wstring ws(wst); + std::string s(ws.begin(), ws.end()); + s.assign(ws.begin(), ws.end()); + return s; +} + template void DumpNodeInfo(const ConfigParameters& config) { @@ -549,14 +559,6 @@ std::string TimeDateStamp() return s; } -std::string WCharToString(const wchar_t* wst) -{ - std::wstring ws(wst); - std::string s(ws.begin(), ws.end()); - s.assign(ws.begin(), ws.end()); - return s; -} - int wmain(int argc, wchar_t* argv[]) { try @@ -573,7 +575,7 @@ int wmain(int argc, wchar_t* argv[]) for (int i=0; i < command.size(); i++) { logpath += L"_"; - logpath += command[i]; + logpath += (wstring)command[i]; } logpath += L".log"; RedirectStdErr(logpath);