fix windows run

This commit is contained in:
Tianqi Chen 2014-11-23 22:12:55 -08:00
parent 77ffd0465b
commit fde580b08e
5 changed files with 43 additions and 18 deletions

View File

@ -235,7 +235,7 @@ class SyncManager {
if (i != parent_index) { if (i != parent_index) {
reducer(links[i].buffer_head + start, reducer(links[i].buffer_head + start,
sendrecvbuf + size_up_reduce, sendrecvbuf + size_up_reduce,
nread / type_nbytes, static_cast<int>(nread / type_nbytes),
MPI::Datatype(type_nbytes)); MPI::Datatype(type_nbytes));
} }
} }
@ -522,7 +522,7 @@ ReduceHandle::ReduceHandle(void) : handle(NULL), htype(NULL) {
ReduceHandle::~ReduceHandle(void) {} ReduceHandle::~ReduceHandle(void) {}
int ReduceHandle::TypeSize(const MPI::Datatype &dtype) { int ReduceHandle::TypeSize(const MPI::Datatype &dtype) {
return dtype.type_size; return static_cast<int>(dtype.type_size);
} }
void ReduceHandle::Init(ReduceFunction redfunc, size_t type_n4bytes, bool commute) { void ReduceHandle::Init(ReduceFunction redfunc, size_t type_n4bytes, bool commute) {
utils::Assert(handle == NULL, "cannot initialize reduce handle twice"); utils::Assert(handle == NULL, "cannot initialize reduce handle twice");

View File

@ -7,6 +7,7 @@
*/ */
#if defined(_WIN32) #if defined(_WIN32)
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h>
#else #else
#include <fcntl.h> #include <fcntl.h>
#include <netdb.h> #include <netdb.h>
@ -25,8 +26,10 @@ namespace xgboost {
namespace utils { namespace utils {
#if defined(_WIN32) #if defined(_WIN32)
typedef int ssize_t; typedef int ssize_t;
typedef int sock_size_t;
#else #else
typedef int SOCKET; typedef int SOCKET;
typedef size_t sock_size_t;
const int INVALID_SOCKET = -1; const int INVALID_SOCKET = -1;
#endif #endif
@ -63,7 +66,11 @@ struct SockAddr {
/*! \return a string representation of the address */ /*! \return a string representation of the address */
inline std::string AddrStr(void) const { inline std::string AddrStr(void) const {
std::string buf; buf.resize(256); std::string buf; buf.resize(256);
const char *s = inet_ntop(AF_INET, &addr.sin_addr, &buf[0], buf.length()); #ifdef _WIN32
const char *s = inet_ntop(AF_INET, (PVOID)&addr.sin_addr, &buf[0], buf.length());
#else
const char *s = inet_ntop(AF_INET, &addr.sin_addr, &buf[0], buf.length());
#endif
Assert(s != NULL, "cannot decode address"); Assert(s != NULL, "cannot decode address");
return std::string(s); return std::string(s);
} }
@ -103,11 +110,22 @@ class TCPSocket {
* call this before using the sockets * call this before using the sockets
*/ */
inline static void Startup(void) { inline static void Startup(void) {
#ifdef _WIN32
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != -1) {
SockError("Startup");
}
if (LOBYTE(wsa_data.wVersion) != 2 || HIBYTE(wsa_data.wVersion) != 2) {
WSACleanup();
utils::Error("Could not find a usable version of Winsock.dll\n");
}
#endif
} }
/*! /*!
* \brief shutdown the socket module after use, all sockets need to be closed * \brief shutdown the socket module after use, all sockets need to be closed
*/ */
inline static void Finalize(void) { inline static void Finalize(void) {
WSACleanup();
} }
/*! /*!
* \brief set this socket to use non-blocking mode * \brief set this socket to use non-blocking mode
@ -207,9 +225,10 @@ class TCPSocket {
* \param flags extra flags * \param flags extra flags
* \return size of data actually sent * \return size of data actually sent
*/ */
inline size_t Send(const void *buf, size_t len, int flag = 0) { inline size_t Send(const void *buf_, size_t len, int flag = 0) {
const char *buf = reinterpret_cast<const char*>(buf_);
if (len == 0) return 0; if (len == 0) return 0;
ssize_t ret = send(sockfd, buf, len, flag); ssize_t ret = send(sockfd, buf, static_cast<sock_size_t>(len), flag);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return 0; if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
SockError("Send"); SockError("Send");
@ -218,14 +237,15 @@ class TCPSocket {
} }
/*! /*!
* \brief receive data using the socket * \brief receive data using the socket
* \param buf the pointer to the buffer * \param buf_ the pointer to the buffer
* \param len the size of the buffer * \param len the size of the buffer
* \param flags extra flags * \param flags extra flags
* \return size of data actually received * \return size of data actually received
*/ */
inline size_t Recv(void *buf, size_t len, int flags = 0) { inline size_t Recv(void *buf_, size_t len, int flags = 0) {
char *buf = reinterpret_cast<char*>(buf_);
if (len == 0) return 0; if (len == 0) return 0;
ssize_t ret = recv(sockfd, buf, len, flags); ssize_t ret = recv(sockfd, buf, static_cast<sock_size_t>(len), flags);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return 0; if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
SockError("Recv"); SockError("Recv");
@ -264,7 +284,7 @@ class TCPSocket {
char *buf = reinterpret_cast<char*>(buf_); char *buf = reinterpret_cast<char*>(buf_);
size_t ndone = 0; size_t ndone = 0;
while (ndone < len) { while (ndone < len) {
ssize_t ret = recv(sockfd, buf, len - ndone, MSG_WAITALL); ssize_t ret = recv(sockfd, buf, static_cast<sock_size_t>(len - ndone), MSG_WAITALL);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone;
SockError("Recv"); SockError("Recv");
@ -310,14 +330,14 @@ struct SelectHelper {
* \param fd file descriptor to check status * \param fd file descriptor to check status
*/ */
inline bool CheckRead(SOCKET fd) const { inline bool CheckRead(SOCKET fd) const {
return FD_ISSET(fd, &read_set); return FD_ISSET(fd, &read_set) != 0;
} }
/*! /*!
* \brief Check if the descriptor is ready for write * \brief Check if the descriptor is ready for write
* \param fd file descriptor to check status * \param fd file descriptor to check status
*/ */
inline bool CheckWrite(SOCKET fd) const { inline bool CheckWrite(SOCKET fd) const {
return FD_ISSET(fd, &write_set); return FD_ISSET(fd, &write_set) != 0;
} }
/*! /*!
* \brief clear all the monitored descriptors * \brief clear all the monitored descriptors
@ -343,12 +363,12 @@ struct SelectHelper {
} }
int ret; int ret;
if (timeout == 0) { if (timeout == 0) {
ret = select(maxfd + 1, &read_set, &write_set, NULL, NULL); ret = select(static_cast<int>(maxfd + 1), &read_set, &write_set, NULL, NULL);
} else { } else {
timeval tm; timeval tm;
tm.tv_usec = (timeout % 1000) * 1000; tm.tv_usec = (timeout % 1000) * 1000;
tm.tv_sec = timeout / 1000; tm.tv_sec = timeout / 1000;
ret = select(maxfd + 1, &read_set, &write_set, NULL, &tm); ret = select(static_cast<int>(maxfd + 1), &read_set, &write_set, NULL, &tm);
} }
if (ret == -1) { if (ret == -1) {
int errsv = errno; int errsv = errno;
@ -358,9 +378,9 @@ struct SelectHelper {
} }
private: private:
int maxfd; SOCKET maxfd;
fd_set read_set, write_set; fd_set read_set, write_set;
std::vector<int> read_fds, write_fds; std::vector<SOCKET> read_fds, write_fds;
}; };
} }
} }

View File

@ -112,6 +112,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -21,6 +21,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\gbm\gbm.cpp" /> <ClCompile Include="..\..\src\gbm\gbm.cpp" />
<ClCompile Include="..\..\src\io\io.cpp" /> <ClCompile Include="..\..\src\io\io.cpp" />
<ClCompile Include="..\..\src\sync\sync_tcp.cpp" />
<ClCompile Include="..\..\src\tree\updater.cpp" /> <ClCompile Include="..\..\src\tree\updater.cpp" />
<ClCompile Include="..\..\wrapper\xgboost_wrapper.cpp" /> <ClCompile Include="..\..\wrapper\xgboost_wrapper.cpp" />
</ItemGroup> </ItemGroup>
@ -112,6 +113,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -8,7 +8,9 @@
#include <algorithm> #include <algorithm>
// include all std functions // include all std functions
using namespace std; using namespace std;
#ifdef _MSC_VER
#define isnan(x) (_isnan(x) != 0)
#endif
#include "./xgboost_wrapper.h" #include "./xgboost_wrapper.h"
#include "../src/data.h" #include "../src/data.h"
#include "../src/learner/learner-inl.hpp" #include "../src/learner/learner-inl.hpp"
@ -149,7 +151,7 @@ extern "C"{
bst_ulong nrow, bst_ulong nrow,
bst_ulong ncol, bst_ulong ncol,
float missing) { float missing) {
bool nan_missing = std::isnan(missing); bool nan_missing = isnan(missing);
DMatrixSimple *p_mat = new DMatrixSimple(); DMatrixSimple *p_mat = new DMatrixSimple();
DMatrixSimple &mat = *p_mat; DMatrixSimple &mat = *p_mat;
mat.info.info.num_row = nrow; mat.info.info.num_row = nrow;
@ -157,7 +159,7 @@ extern "C"{
for (bst_ulong i = 0; i < nrow; ++i, data += ncol) { for (bst_ulong i = 0; i < nrow; ++i, data += ncol) {
bst_ulong nelem = 0; bst_ulong nelem = 0;
for (bst_ulong j = 0; j < ncol; ++j) { for (bst_ulong j = 0; j < ncol; ++j) {
if (std::isnan(data[j])) { if (isnan(data[j])) {
utils::Check(nan_missing, "There are NAN in the matrix, however, you did not set missing=NAN"); utils::Check(nan_missing, "There are NAN in the matrix, however, you did not set missing=NAN");
} else { } else {
if (nan_missing || data[j] != missing) { if (nan_missing || data[j] != missing) {