Skip to content

Commit

Permalink
[add] Fully support compiling on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
endyul committed Jan 6, 2015
1 parent e08a94b commit f7b47d0
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 63 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ project ("LTP - Language Technology Platform")
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

if (APPLE)
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing")
add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wno-c++11-narrowing")
endif(APPLE)

if (MINGW)
Expand Down
1 change: 0 additions & 1 deletion doc/ltp-document-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ Linux、Mac OSX(*)和Cygwin的用户,可以直接在项目根目录下使用
./configure
make

(注:Mac OSX如果要编译example下的示例程序,请加入-std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing选项)

进行编译。

Expand Down
9 changes: 4 additions & 5 deletions src/utils/cfgparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ class ConfigParser {
bool _valid;

#if defined(_MSC_VER)
typedef stdext::hash_map<std::string, std::string> internal_entries_t;
typedef stdext::hash_map<std::string, internal_entries_t> internal_sections_t;
typedef std::unordered_map<std::string, std::string> internal_entries_t;
typedef std::unordered_map<std::string, internal_entries_t> internal_sections_t;
#else
typedef std::tr1::unordered_map<std::string, std::string,
typedef std::unordered_map<std::string, std::string,
__Default_String_HashFunction> internal_entries_t;
typedef std::tr1::unordered_map<std::string, internal_entries_t,
typedef std::unordered_map<std::string, internal_entries_t,
__Default_String_HashFunction> internal_sections_t;
#endif // end for _WIN32

internal_sections_t sec;

public:
Expand Down
4 changes: 2 additions & 2 deletions src/utils/chartypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ class __chartype_collections {

private:
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, int,
typedef std::unordered_map<const char *, int,
utility::__Default_CharArray_HashFunction> internal_collection_t;
#else
typedef std::tr1::unordered_map<const char *, int,
typedef std::unordered_map<const char *, int,
utility::__Default_CharArray_HashFunction,
utility::__Default_CharArray_EqualFunction> internal_collection_t;
#endif // end for _WIN32
Expand Down
5 changes: 3 additions & 2 deletions src/utils/math/sparsevec.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ namespace math {

class SparseVec {
public:

#if defined(_MSC_VER)
typedef stdext::hash_map<int,double> internal_sparsevec_t;
typedef std::unordered_map<int,double> internal_sparsevec_t;
#else
typedef std::tr1::unordered_map<int, double> internal_sparsevec_t;
typedef std::unordered_map<int, double> internal_sparsevec_t;
// typedef __gnu_cxx::hash_map<int, double> internal_sparsevec_t;
#endif // end for _WIN32
typedef internal_sparsevec_t::iterator iterator;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/stringmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace utility {
template <class T>
class StringMap {
public:
/*
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, T,
__Default_CharArray_HashFunction> internal_map_t;
Expand All @@ -29,7 +30,8 @@ class StringMap {
__Default_CharArray_HashFunction,
__Default_CharArray_EqualFunction> internal_map_t;
#endif // end for _WIN32

*/
typedef std::unordered_map<const char *, T> internal_map_t;
typedef typename internal_map_t::iterator iterator;
typedef typename internal_map_t::const_iterator const_iterator;

Expand Down
5 changes: 2 additions & 3 deletions src/utils/template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ template <typename T>
class __Template_Token_Cache {
private:
#if defined(_MSC_VER)
typedef stdext::hash_map<const char *, int,
typedef std::unordered_map<const char *, int,
__Default_CharArray_HashFunction> indexing_t;
#else
typedef std::tr1::unordered_map<const char *, int,
typedef std::unordered_map<const char *, int,
__Default_CharArray_HashFunction,
__Default_CharArray_EqualFunction> indexing_t;
#endif // end for _WIN32

public:
/**
* The entry function for the singleton. It's a typical get_instance
Expand Down
96 changes: 95 additions & 1 deletion src/utils/unordered_map.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,105 @@
// Portable STL hashmap include file.
#ifndef __UTILS_UNORDERED_MAP_HPP__
#define __UTILS_UNORDERED_MAP_HPP__
// Portable header for std::unordered_map<K,V> template. Welcome to C++. Enjoy!
// - rlyeh / BOOST licensed

/*
* Just in case somebody else defined `unordered_map` before us.
*/

#ifdef unordered_map
#undef unordered_map
#endif

/* Headers (in order)
* - std >= C++11: GCC <4.7.X defines __cplusplus as 1, use __GXX_EXPERIMENTAL_CXX0X__ instead
* - ICC
* - G++ >= 4.3.X
* - G++ >= 3.X.X
* - MSVC++ >= 9.0
* - OTHERS
*/

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#include <unordered_map>
#elif defined(__INTEL_COMPILER)
#include <ext/hash_map>
#elif defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#include <tr1/unordered_map>
#elif defined(__GNUC__) && __GNUC__ >= 3
#include <ext/hash_map>
#elif defined(_MSC_VER) && ( ( _MSC_VER >= 1500 && _HAS_TR1 ) || ( _MSC_VER >= 1600 ) )
#include <unordered_map>
#else
#include <hash_map>
#endif

/* Namespace and type (in order)
* - C++11, C++0X (std::unordered_map)
* - STLPORT (std::hash_map)
* - MSVC++ 2010 (std::unordered_map)
* - MSVC++ 9.0 (std::tr1::unordered_map)
* - MSVC++ 7.0 (stdext::hash_map)
* - G++ 4.3.X (std::tr1::unordered_map)
* - G++ 3.X.X, ICC (__gnu_cxx::hash_map)
* - OTHERS (std::hash_map)
*/

#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
// ok

#elif defined(_STLPORT_VERSION)
#define unordered_map hash_map
namespace std { using std::hash_map; }

#elif defined(_MSC_VER) && _MSC_VER >= 1600
// ok

#elif defined(_MSC_VER) && _MSC_VER >= 1500 && _HAS_TR1
namespace std { using std::tr1::unordered_map; }

#elif defined(_MSC_VER) && _MSC_VER >= 1300
#define unordered_map hash_map
namespace std { using stdext::hash_map; }

#elif defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
namespace std { using std::tr1::unordered_map; }

#elif (defined(__GNUC__) && __GNUC__ >= 3) || defined(__INTEL_COMPILER)
#include <string>
#define unordered_map hash_map
namespace std { using __gnu_cxx::hash_map; }

namespace __gnu_cxx {
template<> struct hash<unsigned long long> {
size_t operator()(const unsigned long long &__x) const {
return (size_t)__x;
}
};
template<typename T> struct hash<T *> {
size_t operator()(T * const &__x) const {
return (size_t)__x;
}
};
template<> struct hash<std::string> {
size_t operator()(const std::string &__x) const {
return hash<const char *>()(__x.c_str());
}
};
};

#else
#define unordered_map hash_map
namespace std { using std::hash_map; }

#endif

/*
#if defined(_MSC_VER)
#include <hash_map>
#else
#include <tr1/unordered_map>
#endif

*/
#endif // end for __UTILS_UNORDERED_MAP_HPP__
38 changes: 19 additions & 19 deletions test/multi_cws_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "tinythread.h"
#include "segment_dll.h"

using namespace std;
using namespace tthread;
//using namespace std;
//using namespace tthread;

const int MAX_LEN = 1024;

Expand All @@ -35,20 +35,20 @@ class Dispatcher {
_model = model;
}

int next(string &sentence) {
int next(std::string &sentence) {
sentence = "";
lock_guard<mutex> guard(_mutex);
if (!getline(cin, sentence, '\n')) {
tthread::lock_guard<tthread::mutex> guard(_mutex);
if (!getline(std::cin, sentence, '\n')) {
return -1;
}
return 0;
}

void output(const vector<string> &result) {
lock_guard<mutex> guard(_mutex);
void output(const std::vector<std::string> &result) {
tthread::lock_guard<tthread::mutex> guard(_mutex);
for (int i = 0; i < result.size(); ++ i) {
cout << result[i];
cout << (i == result.size() - 1 ? '\n' : '\t');
std::cout << result[i];
std::cout << (i == result.size() - 1 ? '\n' : '\t');
}
return;
}
Expand All @@ -58,14 +58,14 @@ class Dispatcher {
}

private:
mutex _mutex;
tthread::mutex _mutex;
void * _model;
string _sentence;
std::string _sentence;
};

void multithreaded_segment( void * args) {
string sentence;
vector<string> result;
std::string sentence;
std::vector<std::string> result;

Dispatcher * dispatcher = (Dispatcher *)args;
void * model = dispatcher->model();
Expand Down Expand Up @@ -108,8 +108,8 @@ int main(int argc, char ** argv) {
return -1;
}

if(num_threads < 0 || num_threads > thread::hardware_concurrency()) {
num_threads = thread::hardware_concurrency();
if(num_threads < 0 || num_threads > tthread::thread::hardware_concurrency()) {
num_threads = tthread::thread::hardware_concurrency();
}

std::cerr << "TRACE: Model is loaded" << std::endl;
Expand All @@ -118,15 +118,15 @@ int main(int argc, char ** argv) {
Dispatcher* dispatcher = new Dispatcher( engine );

double tm = ltp::utility::get_time();
list<thread *> thread_list;
std::list<tthread::thread *> thread_list;
for (int i = 0; i < num_threads; ++ i) {
thread* t = new thread( multithreaded_segment, (void *)dispatcher );
tthread::thread* t = new tthread::thread( multithreaded_segment, (void *)dispatcher );
thread_list.push_back( t );
}

for (list<thread *>::iterator i = thread_list.begin();
for (std::list<tthread::thread *>::iterator i = thread_list.begin();
i != thread_list.end(); ++ i) {
thread * t = *i;
tthread::thread * t = *i;
t->join();
delete t;
}
Expand Down
32 changes: 16 additions & 16 deletions test/multi_ltp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#include "time.hpp"
#include "Ltp.h"

using namespace std;
using namespace tthread;
//using namespace std;
//using namespace tthread;

string type;
std::string type;

class Dispatcher {
public:
Expand All @@ -32,17 +32,17 @@ class Dispatcher {
_idx(0),
_ifs(ifs) {}

int next(string &sentence) {
int next(std::string &sentence) {
sentence = "";
lock_guard<mutex> guard(_mutex);
tthread::lock_guard<tthread::mutex> guard(_mutex);
if (!getline(_ifs, sentence, '\n')) {
return -1;
}
return _max_idx ++;
}

void output(int idx, const string &result) {
lock_guard<mutex> guard(_mutex);
void output(int idx, const std::string &result) {
tthread::lock_guard<tthread::mutex> guard(_mutex);

if (idx > _idx) {
_back[idx] = result;
Expand All @@ -69,7 +69,7 @@ class Dispatcher {
}

private:
mutex _mutex;
tthread::mutex _mutex;
LTP * _engine;
int _max_idx;
int _idx;
Expand All @@ -79,7 +79,7 @@ class Dispatcher {
};

void multithreaded_ltp( void * args) {
string sentence;
std::string sentence;

Dispatcher * dispatcher = (Dispatcher *)args;
LTP * engine = dispatcher->get_engine();
Expand Down Expand Up @@ -109,7 +109,7 @@ void multithreaded_ltp( void * args) {
engine->srl(xml4nlp);
}

string result;
std::string result;
// for segmentation only

if (type == "sp")
Expand Down Expand Up @@ -148,7 +148,7 @@ void multithreaded_ltp( void * args) {

int main(int argc, char ** argv) {
if (argc != 4) {
cerr << "Usage: ./ltp_test <config_file> <type> <test_file>" << endl;
std::cerr << "Usage: ./ltp_test <config_file> <type> <test_file>" << endl;
exit(1);
}

Expand All @@ -163,20 +163,20 @@ int main(int argc, char ** argv) {
type = _type;
Dispatcher * dispatcher = new Dispatcher( &engine, ifs);

int num_threads = thread::hardware_concurrency();
int num_threads = tthread::thread::hardware_concurrency();
std::cerr << "TRACE: LTP is built" << std::endl;
std::cerr << "TRACE: Running " << num_threads << " thread(s)" << std::endl;

double tm = ltp::utility::get_time();
list<thread *> thread_list;
std::list<tthread::thread *> thread_list;
for (int i = 0; i < num_threads; ++ i) {
thread * t = new thread(multithreaded_ltp, (void *)dispatcher );
tthread::thread * t = new tthread::thread(multithreaded_ltp, (void *)dispatcher );
thread_list.push_back( t );
}

for (list<thread *>::iterator i = thread_list.begin();
for (std::list<tthread::thread *>::iterator i = thread_list.begin();
i != thread_list.end(); ++ i) {
thread * t = *i;
tthread::thread * t = *i;
t->join();
delete t;
}
Expand Down
Loading

0 comments on commit f7b47d0

Please sign in to comment.