Skip to content

Commit

Permalink
rearrange the project
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed Sep 26, 2013
1 parent 546b425 commit e8f6c63
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
15 changes: 11 additions & 4 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#################################################
# Example for call LTP libraries under UNIX #
#################################################

all: cws cws_cmdline multi_cws_cmdline \
pos pos_cmdline multi_pos_cmdline \
par \
Expand All @@ -15,13 +19,16 @@ cws_cmdline: cws_cmdline.cpp
-I../thirdparty/boost/include \
-Wl,-dn -L../lib/ -lsegmentor -lboost_regex -Wl,-dy

#
# tinythread is directly invoked into the example
#
multi_cws_cmdline: multi_cws_cmdline.cpp
g++ -o multi_cws_cmdline multi_cws_cmdline.cpp \
thirdparty/tinythreadpp/tinythread.cpp \
../thirdparty/tinythreadpp/tinythread.cpp \
-I./ \
-I../include/ \
-I../thirdparty/boost/include/ \
-I./thirdparty/tinythreadpp/ \
-I../thirdparty/tinythreadpp/ \
-Wl,-dn -L../lib/ -lsegmentor -lboost_regex -Wl,-dy -lpthread

pos: pos.cpp
Expand All @@ -36,10 +43,10 @@ pos_cmdline: pos_cmdline.cpp

multi_pos_cmdline: multi_pos_cmdline.cpp
g++ -o multi_pos_cmdline multi_pos_cmdline.cpp \
thirdparty/tinythreadpp/tinythread.cpp \
../thirdparty/tinythreadpp/tinythread.cpp \
-I./ \
-I../include/ \
-I./thirdparty/tinythreadpp/ \
-I../thirdparty/tinythreadpp/ \
-Wl,-dn -L../lib/ -lpostagger -Wl,-dy -lpthread

ner: ner.cpp
Expand Down
10 changes: 6 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ include_directories (./
${SOURCE_DIR}/_ner
${SOURCE_DIR}/_srl
${SOURCE_DIR}/__ltp_dll
${PROJECT_SOURCE_DIR}/examples/thirdparty/tinythreadpp
${THIRDPARTY_DIR}/tinythreadpp
${THIRDPARTY_DIR}/crfpp
${THIRDPARTY_DIR}/maxent
${THIRDPARTY_DIR}/tinyxml)

set (ltp_test_SRC ltp_test.cpp)
set (multi_ltp_test_SRC multi_ltp_test.cpp
${THIRDPARTY_DIR}/tinythreadpp/tinythread.cpp)
#set (ltp_test_xml_SRC ltp_test_xml.cpp)
#set (ltp_test2_SRC ltp_test2.cpp)

link_directories ( ${LIBRARY_OUTPUT_PATH} )

add_executable (ltp_test ${ltp_test_SRC})
add_executable (multi_ltp_test multi_ltp_test.cpp ../examples/thirdparty/tinythreadpp/tinythread.cpp)
add_executable (ltp_test ${ltp_test_SRC})
add_executable (multi_ltp_test ${multi_ltp_test_SRC})
#add_executable (ltp_test_xml ${ltp_test_xml_SRC})
#add_executable (ltp_test2 ${ltp_test2_SRC})

Expand Down Expand Up @@ -76,4 +78,4 @@ target_link_libraries (multi_ltp_test ${ltp_test_LIBS} pthread)
#add_test (NAME ltp_test
# COMMAND "${CMAKE_COMMAND}"
# -DTEST_PROG=${EXECUTABLE_TEST_OUTPUT}
# -P ${CMAKE_MODULE_PATH}/LtpDiff.cmake)
# -P ${CMAKE_MODULE_PATH}/LtpDiff.cmake)
64 changes: 48 additions & 16 deletions test/multi_ltp_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/*
* this is the multi-threaded test suite for LTP.
*
* @author: NIU, Guochen <[email protected]>
* HAN, Bin <[email protected]>
* LIU, Yijia <[email protected]>
*
* @data: 2013-09-26
*/
#include <iostream>
#include <cstring>
#include <ctime>
#include <vector>
#include <list>
#include <map>
#include <sys/time.h>
#include <sys/types.h>

Expand All @@ -25,9 +35,10 @@ double get_time(void) {

class Dispatcher {
public:
Dispatcher( LTP * engine) {
_engine = engine;
}
Dispatcher( LTP * engine) :
_engine(engine),
_max_idx(0),
_idx(0) {}

int next(string &sentence) {
sentence = "";
Expand All @@ -38,33 +49,53 @@ class Dispatcher {
return 0;
}

void output(const string &result) {
void output(int idx, const string &result) {
lock_guard<fast_mutex> guard(_mutex);
cout << result;

if (idx > _idx) {
_back[idx] = result;
} else if (idx == _idx) {
std::cout << result;
++ _idx;

std::map<int, std::string>::iterator itx;
itx = _back.find(_idx);

while (itx != _back.end()) {
std::cout << itx->second;

_back.erase(itx);
++ _idx;
itx = _back.find(_idx);
}
}
return;
}

LTP* getEngine() {
LTP* get_engine() {
return _engine;
}

private:
fast_mutex _mutex;
LTP* _engine;
fast_mutex _mutex;
LTP * _engine;
int _max_idx;
int _idx;
std::map<int, std::string> _back;
};

void multithreaded_ltp( void * args) {
string sentence;

Dispatcher * dispatcher = (Dispatcher *)args;
LTP * engine = dispatcher->getEngine();
LTP * engine = dispatcher->get_engine();

while (true) {
int ret = dispatcher->next(sentence);

if (ret < 0)
break;

XML4NLP xml4nlp;
xml4nlp.CreateDOMFromString(sentence);

Expand All @@ -84,21 +115,21 @@ void multithreaded_ltp( void * args) {

string result;
xml4nlp.SaveDOM(result);
dispatcher->output(result);
dispatcher->output(ret, result);
xml4nlp.ClearDOM();

}

return;
}

int main(int argc, char ** argv) {

if (argc != 3) {
cerr << "Usage: ./ltp_test <config_file> <type>" << endl;
exit(1);
}

LTP ltp(argv[1]);
string _type(argv[2]);
type = _type;
Expand All @@ -121,12 +152,13 @@ int main(int argc, char ** argv) {
t->join();
delete t;
}

delete dispatcher;
tm = get_time() - tm;
std::cerr << "TRACE: consume "
<< tm
<< " seconds."
<< std::endl;

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e8f6c63

Please sign in to comment.