Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype for Windows Support #62

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
# xlearn output
.bin
.out
.model
.model

# Visual Studio
.vs/
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#
# $> make install
#
project ("xLearn")

cmake_minimum_required(VERSION 3.0)

Expand All @@ -30,8 +29,14 @@ cmake_minimum_required(VERSION 3.0)
# Using c++11;
# Optimization on architecture
#-------------------------------------------------------------------------------
add_definitions("-Wall -Wno-sign-compare -O3 -std=c++11
-march=native -Wno-strict-aliasing -Wno-comment")
if (UNIX)
add_definitions("-Wall -O3 -std=c++11 -march=native -Wno-sign-compare -Wno-strict-aliasing -Wno-comment")
elseif (WIN32)
#TODO: Supress warnings in WIN32
add_definitions("-Wall -O3 -std=c++11 -march=native")

endif()


#-------------------------------------------------------------------------------
# Declare where our project will be installed.
Expand Down
86 changes: 86 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"configurations": [
{
"name": "x86-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x86-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"inheritEnvironments": [ "msvc_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "Linux-Debug",
"generator": "Unix Makefiles",
"remoteMachineName": "${defaultRemoteMachineName}",
"configurationType": "Debug",
"remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}",
"cmakeExecutable": "/usr/bin/cmake",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}",
"remoteInstallRoot": "/var/tmp/build/${workspaceHash}/install/${name}",
"remoteCopySources": true,
"remoteCopySourcesOutputVerbosity": "Normal",
"remoteCopySourcesConcurrentCopies": "10",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux-x64" ]
},
{
"name": "Linux-Release",
"generator": "Unix Makefiles",
"remoteMachineName": "${defaultRemoteMachineName}",
"configurationType": "Release",
"remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}",
"cmakeExecutable": "/usr/bin/cmake",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}",
"remoteInstallRoot": "/var/tmp/build/${workspaceHash}/install/${name}",
"remoteCopySources": true,
"remoteCopySourcesOutputVerbosity": "Normal",
"remoteCopySourcesConcurrentCopies": "10",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux-x64" ]
}
]
}
4 changes: 2 additions & 2 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/test/base)

# Build static library
add_library(base STATIC logging.cc stringprintf.cc split_string.cc
levenshtein_distance.cc timer.cc)
levenshtein_distance.cc timer.cc uname.cc mman.c)

# Build shared library
add_library(base_shared SHARED logging.cc stringprintf.cc split_string.cc
levenshtein_distance.cc timer.cc)
levenshtein_distance.cc timer.cc uname.cc mman.c)

# Set properties
set_target_properties(base_shared PROPERTIES OUTPUT_NAME "base")
Expand Down
20 changes: 10 additions & 10 deletions src/base/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ programming convenient.
#ifndef _MSC_VER
#include <stdint.h> // Linux, MacOSX and Cygwin has this standard header.
#else
#include "base/stdint_msvc.h" // Visual C++ use this header.
//#include "base/stdint_msvc.h" // Visual C++ use this header.
#endif

#include <limits>
Expand All @@ -53,68 +53,68 @@ programming convenient.
//------------------------------------------------------------------------------

#define CHECK(a) if (!(a)) { \
LOG(ERROR) << "CHECK failed " \
LOG(LogSeverity::ERR) << "CHECK failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n"; \
abort(); \
} \

#define CHECK_NOTNULL(a) if ((a) == NULL) { \
LOG(ERROR) << "CHECK failed " \
LOG(LogSeverity::ERR) << "CHECK failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " == NULL \n"; \
abort(); \
} \

#define CHECK_NULL(a) if ((a) != NULL) { \
LOG(ERROR) << "CHECK failed " \
LOG(LogSeverity::ERR) << "CHECK failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n"; \
abort(); \
} \

#define CHECK_EQ(a, b) if (!((a) == (b))) { \
LOG(ERROR) << "CHECK_EQ failed " \
LOG(LogSeverity::ERR) << "CHECK_EQ failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
abort(); \
} \

#define CHECK_NE(a, b) if (!((a) != (b))) { \
LOG(ERROR) << "CHECK_NE failed " \
LOG(LogSeverity::ERR) << "CHECK_NE failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
abort(); \
} \

#define CHECK_GT(a, b) if (!((a) > (b))) { \
LOG(ERROR) << "CHECK_GT failed " \
LOG(LogSeverity::ERR) << "CHECK_GT failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
abort(); \
} \

#define CHECK_LT(a, b) if (!((a) < (b))) { \
LOG(ERROR) << "CHECK_LT failed " \
LOG(LogSeverity::ERR) << "CHECK_LT failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
abort(); \
} \

#define CHECK_GE(a, b) if (!((a) >= (b))) { \
LOG(ERROR) << "CHECK_GE failed " \
LOG(LogSeverity::ERR) << "CHECK_GE failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
abort(); \
} \

#define CHECK_LE(a, b) if (!((a) <= (b))) { \
LOG(ERROR) << "CHECK_LE failed " \
LOG(LogSeverity::ERR) << "CHECK_LE failed " \
<< __FILE__ << ":" << __LINE__ << "\n" \
<< #a << " = " << (a) << "\n" \
<< #b << " = " << (b) << "\n"; \
Expand Down
24 changes: 12 additions & 12 deletions src/base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ This file contains facilitlies to control the file.
#ifndef XLEARN_BASE_FILE_UTIL_H_
#define XLEARN_BASE_FILE_UTIL_H_

#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#include "src/base/unistd.h"
#include "src/base/common.h"
#include "src/base/stringprintf.h"

Expand Down Expand Up @@ -130,7 +130,7 @@ inline bool FileExist(const char* filename) {
inline FILE* OpenFileOrDie(const char* filename, const char* mode) {
FILE* input_stream = fopen(filename, mode);
if (input_stream == nullptr) {
LOG(FATAL) << "Cannot open file: " << filename
LOG(LogSeverity::FATAL) << "Cannot open file: " << filename
<< " with mode: " << mode;
}
return input_stream;
Expand All @@ -139,18 +139,18 @@ inline FILE* OpenFileOrDie(const char* filename, const char* mode) {
// Close file using fclose().
inline void Close(FILE *file) {
if (fclose(file) == -1) {
LOG(FATAL) << "Error invoke fclose().";
LOG(LogSeverity::FATAL) << "Error invoke fclose().";
}
}

// Return the size (byte) of a target file.
inline uint64 GetFileSize(FILE* file) {
if (fseek(file, 0L, SEEK_END) != 0) {
LOG(FATAL) << "Error: invoke fseek().";
LOG(LogSeverity::FATAL) << "Error: invoke fseek().";
}
uint64 total_size = ftell(file);
if (total_size == -1) {
LOG(FATAL) << "Error: invoke ftell().";
LOG(LogSeverity::FATAL) << "Error: invoke ftell().";
}
rewind(file); /* Return to the head */
return total_size;
Expand All @@ -164,7 +164,7 @@ inline void GetLine(FILE* file, std::string& str_line) {
CHECK_NOTNULL(ret);
int read_len = strlen(line);
if (line[read_len - 1] != '\n') {
LOG(FATAL) << "Encountered a too-long line. \
LOG(LogSeverity::FATAL) << "Encountered a too-long line. \
Please check the data.";
} else {
line[read_len - 1] = '\0';
Expand All @@ -184,7 +184,7 @@ inline size_t WriteDataToDisk(FILE* file, const char* buf, size_t len) {
CHECK_GE(len, 0);
size_t write_len = fwrite(buf, 1, len, file);
if (write_len != len) {
LOG(FATAL) << "Error: invoke fwrite().";
LOG(LogSeverity::FATAL) << "Error: invoke fwrite().";
}
return write_len;
}
Expand All @@ -202,7 +202,7 @@ inline size_t ReadDataFromDisk(FILE* file, char* buf, size_t len) {
}
size_t ret = fread(buf, 1, len, file);
if (ret > len) {
LOG(FATAL) << "Error: invoke fread().";
LOG(LogSeverity::FATAL) << "Error: invoke fread().";
}
return ret;
}
Expand All @@ -211,7 +211,7 @@ inline size_t ReadDataFromDisk(FILE* file, char* buf, size_t len) {
inline void RemoveFile(const char* filename) {
CHECK_NOTNULL(filename);
if (remove(filename) == -1) {
LOG(FATAL) << "Error: invoke remove().";
LOG(LogSeverity::FATAL) << "Error: invoke remove().";
}
}

Expand Down Expand Up @@ -296,7 +296,7 @@ inline uint64_t HashFile(const std::string& filename, bool one_block=false) {

uint64_t magic = 90359;
for(long pos = 0; pos < end; ) {
long next_pos = std::min(pos + kChunkSize, end);
long next_pos = std::min(static_cast<long>(pos + kChunkSize), end);
long size = next_pos - pos;
std::vector<char> buffer(kChunkSize);
f.read(buffer.data(), size);
Expand All @@ -323,12 +323,12 @@ inline uint64_t HashFile(const std::string& filename, bool one_block=false) {
// Return size (byte) of current file.
inline uint64 ReadFileToMemory(const std::string& filename, char **buf) {
CHECK(!filename.empty());
FILE* file = OpenFileOrDie(filename.c_str(), "r");
FILE* file = OpenFileOrDie(filename.c_str(), "rb");
uint64 len = GetFileSize(file);
try {
*buf = new char[len];
} catch (std::bad_alloc&) {
LOG(FATAL) << "Cannot allocate enough memory for Reader.";
LOG(LogSeverity::FATAL) << "Cannot allocate enough memory for Reader.";
}
uint64 read_size = fread(*buf, 1, len, file);
CHECK_EQ(read_size, len);
Expand Down
2 changes: 1 addition & 1 deletion src/base/format_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ inline void print_warning(const std::string& out) {
Color::Modifier mag(Color::FG_MAGENTA);
Color::Modifier bold(Color::BOLD);
Color::Modifier reset(Color::RESET);
std::cout << mag << bold << "[ WARNING ] "
std::cout << mag << bold << "[ WARNING ] "
<< out << reset << std::endl;
}

Expand Down
25 changes: 20 additions & 5 deletions src/base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file is the implementation of the logging facilities.

#include <stdlib.h>
#include <time.h>
#include <assert.h>

//------------------------------------------------------------------------------
// Logger
Expand All @@ -33,6 +34,20 @@ std::ofstream Logger::info_log_file_;
std::ofstream Logger::warn_log_file_;
std::ofstream Logger::erro_log_file_;

const char *cross_plat_ctime_r(time_t *cur_time, char *buffer)
{
#ifdef _WIN32
size_t bufsize = 26; // The str length is always 26 after formatting
errno_t e = ctime_s(buffer, bufsize, cur_time);
//assert(e == 0 && "Huh? ctime_s returned an error");
return buffer;
#else
const char *res = ctime_r(cur_time, buffer);
//assert(res != NULL && "ctime_r failed...");
return res;
#endif
}

void InitializeLogger(const std::string& info_log_filename,
const std::string& warn_log_filename,
const std::string& erro_log_filename) {
Expand All @@ -43,11 +58,11 @@ void InitializeLogger(const std::string& info_log_filename,

/*static*/
std::ostream& Logger::GetStream(LogSeverity severity) {
if (severity == INFO) {
if (severity == LogSeverity::INFO) {
return info_log_file_.is_open() ? info_log_file_ : std::cout;
} else if (severity == WARNING) {
} else if (severity == LogSeverity::WARNING) {
return warn_log_file_.is_open() ? warn_log_file_ : std::cerr;
} else if (severity == ERROR || severity == FATAL) {
} else if (severity == LogSeverity::ERR || severity == LogSeverity::FATAL) {
return erro_log_file_.is_open() ? erro_log_file_ : std::cerr;
}
return std::cout; // Print message
Expand All @@ -61,15 +76,15 @@ std::ostream& Logger::Start(LogSeverity severity,
time_t tm;
time(&tm);
char time_string[128];
ctime_r(&tm, time_string);
cross_plat_ctime_r(&tm, time_string);
return GetStream(severity) << time_string
<< " " << file << ":" << line
<< " (" << function << ") " << std::flush;
}

Logger::~Logger() {
GetStream(severity_) << "\n" << std::flush;
if (severity_ == FATAL) {
if (severity_ == LogSeverity::FATAL) {
info_log_file_.close();
warn_log_file_.close();
erro_log_file_.close();
Expand Down
Loading